Initial Commit from Swagger-Gen
This commit is contained in:
58
src/IO.Swagger/Security/BearerAuthenticationHandler.cs
Normal file
58
src/IO.Swagger/Security/BearerAuthenticationHandler.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace IO.Swagger.Security
|
||||
{
|
||||
/// <summary>
|
||||
/// class to handle bearer authentication.
|
||||
/// </summary>
|
||||
public class BearerAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
/// <summary>
|
||||
/// scheme name for authentication handler.
|
||||
/// </summary>
|
||||
public const string SchemeName = "Bearer";
|
||||
|
||||
public BearerAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// verify that require authorization header exists.
|
||||
/// </summary>
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
if (!Request.Headers.ContainsKey("Authorization"))
|
||||
{
|
||||
return AuthenticateResult.Fail("Missing Authorization Header");
|
||||
}
|
||||
try
|
||||
{
|
||||
var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]);
|
||||
|
||||
/// TODO handle token.
|
||||
}
|
||||
catch
|
||||
{
|
||||
return AuthenticateResult.Fail("Invalid Authorization Header");
|
||||
}
|
||||
|
||||
var claims = new[] {
|
||||
new Claim(ClaimTypes.NameIdentifier, "changeme"),
|
||||
new Claim(ClaimTypes.Name, "changeme"),
|
||||
};
|
||||
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user