Skip to content

Commit

Permalink
Add authentication for frontend and API (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b authored Nov 20, 2023
2 parents 3987ce9 + 3001a72 commit 0ab1fcb
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 64 deletions.
2 changes: 2 additions & 0 deletions src/GeoCop.Api/Controllers/DownloadController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GeoCop.Api.Validation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Swashbuckle.AspNetCore.Annotations;
Expand All @@ -8,6 +9,7 @@ namespace GeoCop.Api.Controllers
/// <summary>
/// Controller to download log files of validation jobs.
/// </summary>
[AllowAnonymous]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class DownloadController : ControllerBase
Expand Down
2 changes: 2 additions & 0 deletions src/GeoCop.Api/Controllers/StatusController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GeoCop.Api.Validation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

Expand All @@ -7,6 +8,7 @@ namespace GeoCop.Api.Controllers
/// <summary>
/// Controller to get the status information of validation jobs.
/// </summary>
[AllowAnonymous]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class StatusController : Controller
Expand Down
2 changes: 2 additions & 0 deletions src/GeoCop.Api/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Asp.Versioning;
using GeoCop.Api.Contracts;
using GeoCop.Api.Validation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -11,6 +12,7 @@ namespace GeoCop.Api.Controllers
/// <summary>
/// Controller for uploading files.
/// </summary>
[AllowAnonymous]
[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class UploadController : ControllerBase
Expand Down
4 changes: 3 additions & 1 deletion src/GeoCop.Api/Controllers/VersionController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;

namespace GeoCop.Api.Controllers
{
/// <summary>
/// Constroller for Version information.
/// </summary>
[AllowAnonymous]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class VersionController : ControllerBase
Expand Down
3 changes: 2 additions & 1 deletion src/GeoCop.Api/GeoCop.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<PackageReference Include="Asp.Versioning.Mvc" Version="7.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.1.0" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.11">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
19 changes: 18 additions & 1 deletion src/GeoCop.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using GeoCop.Api;
using GeoCop.Api.Validation;
using GeoCop.Api.Validation.Interlis;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
Expand All @@ -13,12 +16,26 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddControllers()
.AddControllers(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});

builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.Authority = builder.Configuration["Auth:Authority"];
options.Audience = builder.Configuration["Auth:ClientId"];
});

builder.Services
.AddApiVersioning(config =>
{
Expand Down
6 changes: 5 additions & 1 deletion src/GeoCop.Api/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
{
"Auth": {
"Authority": "https://login.microsoftonline.com/16e916d3-12c9-4353-ad04-5a4319422e03/v2.0",
"ClientId": "ac09549e-6cf8-40fe-91a9-25515ec71954"
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
2 changes: 1 addition & 1 deletion src/GeoCop.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"ConnectionStrings": {
"Context": "Server=localhost;Port=5432;Database=geocop;User Id=HAPPYWALK;Password=SOMBERSPORK;"
},
Expand Down
131 changes: 123 additions & 8 deletions src/GeoCop.Frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/GeoCop.Frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"predev": "node aspnetcore-https.js"
},
"dependencies": {
"@azure/msal-browser": "^3.5.0",
"@azure/msal-react": "^2.0.7",
"bootstrap": "^5.3.2",
"dayjs": "^1.11.10",
"react": "^18.2.0",
Expand Down
14 changes: 14 additions & 0 deletions src/GeoCop.Frontend/public/client-settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
{
"oauth": {
"auth": {
"clientId": "ac09549e-6cf8-40fe-91a9-25515ec71954",
"authority": "https://login.microsoftonline.com/16e916d3-12c9-4353-ad04-5a4319422e03/",
"redirectUri": "/",
"postLogoutRedirectUri": "/",
"navigateToLoginRequestUrl": false
},
"cache": {
"cacheLocation": "sessionStorage",
"storeAuthStateInCookie": false
}
},
"authScopes": ["user.read"],
"application": {
"name": "geocop",
"logo": "/app.svg"
Expand Down
Loading

0 comments on commit 0ab1fcb

Please sign in to comment.