-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
73 additions
and
62 deletions.
There are no files selected for viewing
6 changes: 0 additions & 6 deletions
6
src/Digdir.Domain.Dialogporten.Application/Common/Authentication/Constants.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Digdir.Domain.Dialogporten.WebApi/Common/Authentication/UserTypeValidationMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Net; | ||
using Azure; | ||
using Digdir.Domain.Dialogporten.Application.Common.Extensions; | ||
using Digdir.Domain.Dialogporten.Application.Externals.Authentication; | ||
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions; | ||
using FluentValidation.Results; | ||
|
||
namespace Digdir.Domain.Dialogporten.WebApi.Common.Authentication; | ||
|
||
public class UserTypeValidationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public UserTypeValidationMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (context.User.Identity is { IsAuthenticated: true }) | ||
{ | ||
var userType = context.User.GetUserType(); | ||
if (userType == UserType.Unknown) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status403Forbidden; | ||
await context.Response.WriteAsJsonAsync(context.ResponseBuilder( | ||
context.Response.StatusCode, | ||
new List<ValidationFailure>() { new("UserType", | ||
"The request was authenticated, but we were unable to determine valid user type (person, enterprise or system user) in order to authorize the request.") } | ||
)); | ||
|
||
return; | ||
} | ||
} | ||
|
||
await _next(context); | ||
} | ||
} | ||
|
||
public static class UserTypeValidationMiddlewareExtensions | ||
{ | ||
public static IApplicationBuilder UseUserTypeValidation(this IApplicationBuilder app) | ||
=> app.UseMiddleware<UserTypeValidationMiddleware>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters