diff options
author | 杨宇千 <crupest@outlook.com> | 2019-10-31 00:56:46 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-10-31 00:56:46 +0800 |
commit | d3a1bf5f2939049f11e77f91ad9ddea30d8acd64 (patch) | |
tree | 7d034bb824b50d892136c6f1225a15e8baa30741 /Timeline/Authentication/AuthHandler.cs | |
parent | 006d799d2fe5f081c188f95a8590c4b75a93caae (diff) | |
download | timeline-d3a1bf5f2939049f11e77f91ad9ddea30d8acd64.tar.gz timeline-d3a1bf5f2939049f11e77f91ad9ddea30d8acd64.tar.bz2 timeline-d3a1bf5f2939049f11e77f91ad9ddea30d8acd64.zip |
Continue to construct feature and tests.
Diffstat (limited to 'Timeline/Authentication/AuthHandler.cs')
-rw-r--r-- | Timeline/Authentication/AuthHandler.cs | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/Timeline/Authentication/AuthHandler.cs b/Timeline/Authentication/AuthHandler.cs deleted file mode 100644 index 2b457eb1..00000000 --- a/Timeline/Authentication/AuthHandler.cs +++ /dev/null @@ -1,99 +0,0 @@ -using Microsoft.AspNetCore.Authentication;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-using Microsoft.Net.Http.Headers;
-using System;
-using System.Linq;
-using System.Security.Claims;
-using System.Text.Encodings.Web;
-using System.Threading.Tasks;
-using Timeline.Models;
-using Timeline.Services;
-using static Timeline.Resources.Authentication.AuthHandler;
-
-namespace Timeline.Authentication
-{
- static class AuthConstants
- {
- public const string Scheme = "Bearer";
- public const string DisplayName = "My Jwt Auth Scheme";
- }
-
- public class AuthOptions : AuthenticationSchemeOptions
- {
- /// <summary>
- /// The query param key to search for token. If null then query params are not searched for token. Default to <c>"token"</c>.
- /// </summary>
- public string TokenQueryParamKey { get; set; } = "token";
- }
-
- public class AuthHandler : AuthenticationHandler<AuthOptions>
- {
- private readonly ILogger<AuthHandler> _logger;
- private readonly IUserService _userService;
-
- public AuthHandler(IOptionsMonitor<AuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserService userService)
- : base(options, logger, encoder, clock)
- {
- _logger = logger.CreateLogger<AuthHandler>();
- _userService = userService;
- }
-
- // return null if no token is found
- private string? ExtractToken()
- {
- // check the authorization header
- string header = Request.Headers[HeaderNames.Authorization];
- if (!string.IsNullOrEmpty(header) && header.StartsWith("Bearer ", StringComparison.InvariantCultureIgnoreCase))
- {
- var token = header.Substring("Bearer ".Length).Trim();
- _logger.LogInformation(LogTokenFoundInHeader, token);
- return token;
- }
-
- // check the query params
- var paramQueryKey = Options.TokenQueryParamKey;
- if (!string.IsNullOrEmpty(paramQueryKey))
- {
- string token = Request.Query[paramQueryKey];
- if (!string.IsNullOrEmpty(token))
- {
- _logger.LogInformation(LogTokenFoundInQuery, paramQueryKey, token);
- return token;
- }
- }
-
- // not found anywhere then return null
- return null;
- }
-
- protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
- {
- var token = ExtractToken();
- if (string.IsNullOrEmpty(token))
- {
- _logger.LogInformation(LogTokenNotFound);
- return AuthenticateResult.NoResult();
- }
-
- try
- {
- var userInfo = await _userService.VerifyToken(token);
-
- var identity = new ClaimsIdentity(AuthConstants.Scheme);
- identity.AddClaim(new Claim(identity.NameClaimType, userInfo.Username, ClaimValueTypes.String));
- identity.AddClaims(UserRoleConvert.ToArray(userInfo.Administrator).Select(role => new Claim(identity.RoleClaimType, role, ClaimValueTypes.String)));
-
- var principal = new ClaimsPrincipal();
- principal.AddIdentity(identity);
-
- return AuthenticateResult.Success(new AuthenticationTicket(principal, AuthConstants.Scheme));
- }
- catch (Exception e) when (!(e is ArgumentException))
- {
- _logger.LogInformation(e, LogTokenValidationFail);
- return AuthenticateResult.Fail(e);
- }
- }
- }
-}
|