Another round of cleanup and beautification
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
using IO.Swagger.Services;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@ -17,20 +18,12 @@ namespace IO.Swagger.Security
|
||||
/// </summary>
|
||||
public class BearerAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
//TODO: Clean this up and use the service for it
|
||||
private readonly string secretKey;
|
||||
private readonly byte[] secretBytes;
|
||||
|
||||
/// <summary>
|
||||
/// scheme name for authentication handler.
|
||||
/// </summary>
|
||||
public const string SchemeName = "Bearer";
|
||||
private readonly JwtService jwtService;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public BearerAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
|
||||
public BearerAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, JwtService jwtService) : base(options, logger, encoder, clock)
|
||||
{
|
||||
secretKey = Environment.GetEnvironmentVariable("JWT_SECRET_KEY");
|
||||
secretBytes = Encoding.UTF8.GetBytes(secretKey);
|
||||
this.jwtService = jwtService ?? throw new ArgumentNullException(nameof(jwtService));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -38,53 +31,9 @@ namespace IO.Swagger.Security
|
||||
/// </summary>
|
||||
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
if (!Request.Headers.ContainsKey("Authorization"))
|
||||
{
|
||||
return AuthenticateResult.Fail("Missing Authorization Header");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
AuthenticationHeaderValue authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
|
||||
|
||||
JwtSecurityTokenHandler tokenHandler = new();
|
||||
TokenValidationParameters validationParameters = new()
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(secretBytes),
|
||||
ValidateIssuer = false,
|
||||
ValidateAudience = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(authHeader.Parameter, validationParameters, out _);
|
||||
Claim userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier);
|
||||
|
||||
if (userIdClaim != null && int.TryParse(userIdClaim.Value, out int userId))
|
||||
{
|
||||
Claim[] claims = new[] { new Claim(ClaimTypes.NameIdentifier, userId.ToString()) };
|
||||
ClaimsIdentity identity = new(claims, SchemeName);
|
||||
ClaimsPrincipal principal = new(identity);
|
||||
AuthenticationTicket ticket = new(principal, Scheme.Name);
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return AuthenticateResult.Fail("Invalid Auth Token");
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return AuthenticateResult.Fail("Invalid Authorization Header");
|
||||
}
|
||||
|
||||
return AuthenticateResult.Fail("Missing Authorization Header");
|
||||
});
|
||||
return Task.Run(() => Request.Headers.ContainsKey("Authorization")
|
||||
? jwtService.ValidateJwt(Request, Scheme.Name)
|
||||
: AuthenticateResult.Fail("Missing Authorization Header"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user