Skip to content

Commit da7c624

Browse files
authored
Merge pull request #1325 from TechnologyEnhancedLearning/Develop/Fixes/TD-5490-auth-fix-for-openapi
TD-5490 auth issues for LH api request when user is yet to login
2 parents 47084b1 + a1e3058 commit da7c624

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/BookmarkController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System.Threading.Tasks;
55
using LearningHub.Nhs.Models.Bookmark;
66
using LearningHub.Nhs.OpenApi.Services.Interface.Services;
7+
using Microsoft.AspNetCore.Authorization;
78
using Microsoft.AspNetCore.Mvc;
89

910
/// <summary>
1011
/// Learning Hub Bookmark controller.
1112
/// </summary>
1213
[Route("Bookmark")]
1314
[ApiController]
15+
[Authorize]
1416
public class BookmarkController : OpenApiControllerBase
1517
{
1618
private readonly IBookmarkService bookmarkService;

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/CatalogueController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/// </summary>
1515
[Route("Catalogue")]
1616
[Authorize]
17+
[ApiController]
1718
public class CatalogueController : OpenApiControllerBase
1819
{
1920
private readonly ICatalogueService catalogueService;

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/ResourceController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace LearningHub.NHS.OpenAPI.Controllers
2929
/// </summary>
3030
[Route("Resource")]
3131
[Authorize]
32+
[ApiController]
3233
public class ResourceController : OpenApiControllerBase
3334
{
3435
private const int MaxNumberOfReferenceIds = 1000;

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/SearchController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
using LearningHub.Nhs.OpenApi.Models.Configuration;
1212
using LearningHub.Nhs.OpenApi.Repositories.Interface.Repositories;
1313
using LearningHub.Nhs.OpenApi.Services.Interface.Services;
14+
using Microsoft.AspNetCore.Authorization;
1415
using Microsoft.AspNetCore.Mvc;
1516
using Microsoft.Extensions.Logging;
1617
using Microsoft.Extensions.Options;
1718

1819
/// <summary>
1920
/// Search operations.
2021
/// </summary>
22+
[Authorize]
2123
[Route("Search")]
2224
[ApiController]
2325
public class SearchController : OpenApiControllerBase

OpenAPI/LearningHub.Nhs.OpenApi/Startup.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,29 @@ namespace LearningHub.NHS.OpenAPI
88
using System.Collections.Generic;
99
using System.IO;
1010
using AspNetCore.Authentication.ApiKey;
11+
using LearningHub.Nhs.Api.Authentication;
1112
using LearningHub.Nhs.Caching;
1213
using LearningHub.Nhs.Models.Enums;
1314
using LearningHub.Nhs.Models.Extensions;
1415
using LearningHub.NHS.OpenAPI.Auth;
16+
using LearningHub.NHS.OpenAPI.Authentication;
1517
using LearningHub.NHS.OpenAPI.Configuration;
1618
using LearningHub.NHS.OpenAPI.Middleware;
1719
using LearningHub.Nhs.OpenApi.Repositories;
1820
using LearningHub.Nhs.OpenApi.Repositories.EntityFramework;
1921
using LearningHub.Nhs.OpenApi.Services;
2022
using Microsoft.AspNetCore.Authentication;
2123
using Microsoft.AspNetCore.Authentication.JwtBearer;
24+
using Microsoft.AspNetCore.Authorization;
2225
using Microsoft.AspNetCore.Builder;
2326
using Microsoft.AspNetCore.Hosting;
24-
using Microsoft.AspNetCore.Mvc.Authorization;
2527
using Microsoft.EntityFrameworkCore;
2628
using Microsoft.Extensions.Configuration;
2729
using Microsoft.Extensions.DependencyInjection;
2830
using Microsoft.Extensions.FileProviders;
2931
using Microsoft.Extensions.Hosting;
3032
using Microsoft.IdentityModel.Tokens;
3133
using Microsoft.OpenApi.Models;
32-
using Microsoft.AspNetCore.Authorization;
33-
using LearningHub.NHS.OpenAPI.Authentication;
34-
using LearningHub.Nhs.Api.Authentication;
3534

3635
/// <summary>
3736
/// The Startup class.
@@ -62,8 +61,12 @@ public void ConfigureServices(IServiceCollection services)
6261

6362
services.AddApiKeyAuth();
6463

65-
services.AddAuthentication()
66-
.AddJwtBearer(options =>
64+
services.AddAuthentication(options =>
65+
{
66+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
67+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
68+
})
69+
.AddJwtBearer(options =>
6770
{
6871
options.Authority = this.Configuration.GetValue<string>("LearningHUbAuthServiceConfig:Authority");
6972
options.TokenValidationParameters = new TokenValidationParameters()
@@ -76,7 +79,7 @@ public void ConfigureServices(IServiceCollection services)
7679
});
7780

7881
services.AddCustomMiddleware();
79-
services.AddSingleton<IAuthorizationHandler, ReadWriteHandler>();
82+
services.AddSingleton<IAuthorizationHandler, ReadWriteHandler>();
8083
services.AddSingleton<IAuthorizationHandler, AuthorizeOrCallFromLHHandler>();
8184

8285
services.AddRepositories(this.Configuration);
@@ -89,7 +92,6 @@ public void ConfigureServices(IServiceCollection services)
8992
services.AddControllers(options =>
9093
{
9194
options.Filters.Add(new HttpResponseExceptionFilter());
92-
options.Filters.Add(new AuthorizeFilter());
9395
});
9496

9597
services.AddMvc()

0 commit comments

Comments
 (0)