From da9139b7bab95f6e5ba5f4bb2d99011c2d6db03a Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 23 Mar 2022 21:30:14 +0800 Subject: … MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BackEnd/Timeline/Auth/MyAuthenticationHandler.cs | 29 +-- BackEnd/Timeline/Auth/Resource.Designer.cs | 294 ++++++++++------------- BackEnd/Timeline/Auth/Resource.resx | 17 +- 3 files changed, 147 insertions(+), 193 deletions(-) (limited to 'BackEnd/Timeline/Auth') diff --git a/BackEnd/Timeline/Auth/MyAuthenticationHandler.cs b/BackEnd/Timeline/Auth/MyAuthenticationHandler.cs index 016cc938..740f5aee 100644 --- a/BackEnd/Timeline/Auth/MyAuthenticationHandler.cs +++ b/BackEnd/Timeline/Auth/MyAuthenticationHandler.cs @@ -41,11 +41,8 @@ namespace Timeline.Auth { return e switch { - UserTokenTimeExpiredException => ErrorCodes.Common.Token.TimeExpired, - UserTokenVersionExpiredException => ErrorCodes.Common.Token.VersionExpired, - UserTokenBadFormatException => ErrorCodes.Common.Token.BadFormat, - UserTokenUserNotExistException => ErrorCodes.Common.Token.UserNotExist, - _ => ErrorCodes.Common.Token.Unknown + UserTokenExpiredException => ErrorCodes.Common.Token.TimeExpired, + _ => ErrorCodes.Common.Token.Invalid }; } @@ -53,25 +50,22 @@ namespace Timeline.Auth { return errorCode switch { - ErrorCodes.Common.Token.TimeExpired => Resource.MessageTokenTimeExpired, - ErrorCodes.Common.Token.VersionExpired => Resource.MessageTokenVersionExpired, - ErrorCodes.Common.Token.BadFormat => Resource.MessageTokenBadFormat, - ErrorCodes.Common.Token.UserNotExist => Resource.MessageTokenUserNotExist, - _ => Resource.MessageTokenUnknownError + ErrorCodes.Common.Token.TimeExpired => Resource.MessageTokenExpired, + _ => Resource.MessageTokenInvalid }; } private readonly ILogger _logger; - private readonly IUserTokenManager _userTokenManager; + private readonly IUserTokenService _userTokenService; private readonly IUserPermissionService _userPermissionService; private readonly IOptionsMonitor _jsonOptions; - public MyAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserTokenManager userTokenManager, IUserPermissionService userPermissionService, IOptionsMonitor jsonOptions) + public MyAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IUserTokenService userTokenService, IUserPermissionService userPermissionService, IOptionsMonitor jsonOptions) : base(options, logger, encoder, clock) { _logger = logger.CreateLogger(); - _userTokenManager = userTokenManager; + _userTokenService = userTokenService; _userPermissionService = userPermissionService; _jsonOptions = jsonOptions; } @@ -126,13 +120,12 @@ namespace Timeline.Auth try { - var user = await _userTokenManager.VerifyTokenAsync(token); + var userTokenInfo = await _userTokenService.ValidateTokenAsync(token); var identity = new ClaimsIdentity(AuthenticationConstants.Scheme); - identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64)); - identity.AddClaim(new Claim(identity.NameClaimType, user.Username, ClaimValueTypes.String)); + identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userTokenInfo.UserId.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64)); - var permissions = await _userPermissionService.GetPermissionsOfUserAsync(user.Id); + var permissions = await _userPermissionService.GetPermissionsOfUserAsync(userTokenInfo.UserId); identity.AddClaims(permissions.Select(permission => new Claim(AuthenticationConstants.PermissionClaimName, permission.ToString(), ClaimValueTypes.String))); var principal = new ClaimsPrincipal(); @@ -161,7 +154,7 @@ namespace Timeline.Auth if (properties.Items.TryGetValue(TokenErrorCodeKey, out var tokenErrorCode)) { if (!int.TryParse(tokenErrorCode, out var errorCode)) - errorCode = ErrorCodes.Common.Token.Unknown; + throw new Exception("A logic error: failed to parse token error code."); body = new CommonResponse(errorCode, GetTokenErrorMessageFromErrorCode(errorCode)); } else diff --git a/BackEnd/Timeline/Auth/Resource.Designer.cs b/BackEnd/Timeline/Auth/Resource.Designer.cs index 05394551..6175b266 100644 --- a/BackEnd/Timeline/Auth/Resource.Designer.cs +++ b/BackEnd/Timeline/Auth/Resource.Designer.cs @@ -1,162 +1,132 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Timeline.Auth { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resource { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resource() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Auth.Resource", typeof(Resource).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Looks up a localized string similar to User identitifier claim is of bad format.. - /// - internal static string ExceptionUserIdentifierClaimBadFormat { - get { - return ResourceManager.GetString("ExceptionUserIdentifierClaimBadFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Token is found in authorization header. Token is {0} .. - /// - internal static string LogTokenFoundInHeader { - get { - return ResourceManager.GetString("LogTokenFoundInHeader", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to Token is found in query param with key "{0}". Token is {1} .. - /// - internal static string LogTokenFoundInQuery { - get { - return ResourceManager.GetString("LogTokenFoundInQuery", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to No jwt token is found.. - /// - internal static string LogTokenNotFound { - get { - return ResourceManager.GetString("LogTokenNotFound", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A jwt token validation failed. Error reason: {0}. - /// - internal static string LogTokenValidationFail { - get { - return ResourceManager.GetString("LogTokenValidationFail", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to You must use a token to authenticate to access this resource.. - /// - internal static string MessageNoToken { - get { - return ResourceManager.GetString("MessageNoToken", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The token is of bad format. It might not be created by this server.. - /// - internal static string MessageTokenBadFormat { - get { - return ResourceManager.GetString("MessageTokenBadFormat", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The token is out of date and expired. Please create a new one.. - /// - internal static string MessageTokenTimeExpired { - get { - return ResourceManager.GetString("MessageTokenTimeExpired", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to A unknown error occured when verify token.. - /// - internal static string MessageTokenUnknownError { - get { - return ResourceManager.GetString("MessageTokenUnknownError", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The owner of the token does not exist. It might have been deleted.. - /// - internal static string MessageTokenUserNotExist { - get { - return ResourceManager.GetString("MessageTokenUserNotExist", resourceCulture); - } - } - - /// - /// Looks up a localized string similar to The token is of old version and expired. Please create a new one.. - /// - internal static string MessageTokenVersionExpired { - get { - return ResourceManager.GetString("MessageTokenVersionExpired", resourceCulture); - } - } - } -} +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Timeline.Auth { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// This class was generated by MSBuild using the GenerateResource task. + /// To add or remove a member, edit your .resx file then rerun MSBuild. + /// + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Build.Tasks.StronglyTypedResourceBuilder", "15.1.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resource { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resource() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Timeline.Auth.Resource", typeof(Resource).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to User identitifier claim is of bad format.. + /// + internal static string ExceptionUserIdentifierClaimBadFormat { + get { + return ResourceManager.GetString("ExceptionUserIdentifierClaimBadFormat", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Token is found in authorization header. Token is {0} .. + /// + internal static string LogTokenFoundInHeader { + get { + return ResourceManager.GetString("LogTokenFoundInHeader", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Token is found in query param with key "{0}". Token is {1} .. + /// + internal static string LogTokenFoundInQuery { + get { + return ResourceManager.GetString("LogTokenFoundInQuery", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to No jwt token is found.. + /// + internal static string LogTokenNotFound { + get { + return ResourceManager.GetString("LogTokenNotFound", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A jwt token validation failed. Error reason: {0}. + /// + internal static string LogTokenValidationFail { + get { + return ResourceManager.GetString("LogTokenValidationFail", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You must use a token to authenticate to access this resource.. + /// + internal static string MessageNoToken { + get { + return ResourceManager.GetString("MessageNoToken", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The token is expired. Please create a new one.. + /// + internal static string MessageTokenExpired { + get { + return ResourceManager.GetString("MessageTokenExpired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The token is invalid.. + /// + internal static string MessageTokenInvalid { + get { + return ResourceManager.GetString("MessageTokenInvalid", resourceCulture); + } + } + } +} diff --git a/BackEnd/Timeline/Auth/Resource.resx b/BackEnd/Timeline/Auth/Resource.resx index 88cdbd6b..a2183290 100644 --- a/BackEnd/Timeline/Auth/Resource.resx +++ b/BackEnd/Timeline/Auth/Resource.resx @@ -135,19 +135,10 @@ You must use a token to authenticate to access this resource. - - The token is of bad format. It might not be created by this server. + + The token is invalid. - - The token is out of date and expired. Please create a new one. - - - A unknown error occured when verify token. - - - The owner of the token does not exist. It might have been deleted. - - - The token is of old version and expired. Please create a new one. + + The token is expired. Please create a new one. \ No newline at end of file -- cgit v1.2.3