-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Authorization to ProductUserChange endpoint (#1008)
- Loading branch information
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
source/OptimaJet.DWKit.StarterApplication/BearerAuthenticationHandler.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,48 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using static OptimaJet.DWKit.StarterApplication.Utility.EnvironmentHelpers; | ||
|
||
namespace OptimaJet.DWKit.StarterApplication | ||
{ | ||
public class UserManagementBearerAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions> | ||
{ | ||
public const string AuthenticationScheme = "UserManagementBearerScheme"; | ||
|
||
public UserManagementBearerAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
var authHeader = Context.Request.Headers["Authorization"].FirstOrDefault(); | ||
|
||
if (string.IsNullOrEmpty(authHeader)) | ||
{ | ||
return AuthenticateResult.Fail("Authorization header is missing."); | ||
} | ||
|
||
// Validate the Bearer token as needed | ||
// You might want to perform custom validation or retrieve user information from the token. | ||
var token = GetVarOrThrow("USER_MANAGEMENT_TOKEN"); | ||
if (!authHeader.EndsWith(token)) | ||
{ | ||
return AuthenticateResult.Fail("Authorization failure."); | ||
} | ||
|
||
|
||
// For simplicity, let's assume the token is valid and create a sample identity. | ||
var claims = new[] { new Claim(ClaimTypes.Name, "UserManagement") }; | ||
var identity = new ClaimsIdentity(claims, Scheme.Name); | ||
var principal = new ClaimsPrincipal(identity); | ||
var ticket = new AuthenticationTicket(principal, Scheme.Name); | ||
|
||
return AuthenticateResult.Success(ticket); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
source/OptimaJet.DWKit.StarterApplication/Controllers/ProductUserChangeController.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