Skip to content

Commit 2654357

Browse files
committed
#164 Added auth to WebAPI backend with Auth0
1 parent 0f23b59 commit 2654357

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

src/AdminAssistant.Blazor/Server/AdminAssistant.Blazor.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<ItemGroup>
3535
<PackageReference Include="FluentValidation.AspNetCore" Version="9.5.3" />
3636
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="4.3.0" />
37+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.4" />
3738
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.4" />
3839
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.4" />
3940
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">

src/AdminAssistant.Blazor/Server/Startup.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AutoMapper;
44
using FluentValidation.AspNetCore;
55
using MicroElements.Swashbuckle.FluentValidation;
6+
using Microsoft.AspNetCore.Authentication.JwtBearer;
67
using Microsoft.AspNetCore.Builder;
78
using Microsoft.AspNetCore.Hosting;
89
using Microsoft.AspNetCore.Mvc;
@@ -35,6 +36,18 @@ public Startup(IHostEnvironment env, IConfiguration configuration)
3536
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
3637
public void ConfigureServices(IServiceCollection services)
3738
{
39+
var configSettings = _configuration.GetSection(nameof(ConfigurationSettings)).Get<ConfigurationSettings>();
40+
41+
services.AddAuthentication(options =>
42+
{
43+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
44+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
45+
}).AddJwtBearer(options =>
46+
{
47+
options.Authority = $"https://{configSettings.Auth0Authority}/";
48+
options.Audience = configSettings.Auth0ApiIdentifier;
49+
});
50+
3851
services.AddMvc(opts =>
3952
{
4053
// Define MediaType limits ...
@@ -75,7 +88,7 @@ public void ConfigureServices(IServiceCollection services)
7588

7689
services.AddAdminAssistantServerSideProviders();
7790
services.AddAdminAssistantServerSideDomainModel();
78-
services.AddAdminAssistantServerSideInfra(_configuration.GetSection(nameof(ConfigurationSettings)).Get<ConfigurationSettings>());
91+
services.AddAdminAssistantServerSideInfra(configSettings);
7992
}
8093

8194
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -105,9 +118,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
105118
app.UseHttpsRedirection();
106119
app.UseBlazorFrameworkFiles();
107120
app.UseStaticFiles();
108-
109121
app.UseRouting();
110-
122+
app.UseAuthentication();
123+
app.UseAuthorization();
111124
app.UseEndpoints(endpoints =>
112125
{
113126
endpoints.MapRazorPages();

src/AdminAssistant.Blazor/Server/WebAPI/WebAPIControllerBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using AdminAssistant.Infra.Providers;
22
using AutoMapper;
33
using MediatR;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56

67
namespace AdminAssistant.WebAPI
78
{
9+
[Authorize]
810
public abstract class WebAPIControllerBase : ControllerBase
911
{
12+
// TODO: expand basic Authorize with policy to differentiate a user from an admin.
1013
public WebAPIControllerBase(IMapper mapper, IMediator mediator, ILoggingProvider loggingProvider)
1114
{
1215
Mapper = mapper;

src/AdminAssistant/DomainModel/Shared/ConfigurationSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ public record ConfigurationSettings
44
{
55
public string DatabaseProvider { get; set; } = string.Empty;
66
public string ConnectionString { get; set; } = string.Empty;
7+
public string Auth0Authority { get; set; } = string.Empty;
8+
public string Auth0ApiIdentifier { get; set; } = string.Empty;
79
}
810
}

0 commit comments

Comments
 (0)