From 344d189e5860a20ebe42cec03b86974a2a3aaa95 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Apr 2021 16:55:37 +0800 Subject: refactor: ... --- .../Timeline/Services/Token/UserTokenManager.cs | 67 +++++++++------------- 1 file changed, 28 insertions(+), 39 deletions(-) (limited to 'BackEnd/Timeline/Services/Token/UserTokenManager.cs') diff --git a/BackEnd/Timeline/Services/Token/UserTokenManager.cs b/BackEnd/Timeline/Services/Token/UserTokenManager.cs index 31cc70f2..1d5348a5 100644 --- a/BackEnd/Timeline/Services/Token/UserTokenManager.cs +++ b/BackEnd/Timeline/Services/Token/UserTokenManager.cs @@ -9,40 +9,6 @@ using Timeline.Services.User; namespace Timeline.Services.Token { - public class UserTokenCreateResult - { - public string Token { get; set; } = default!; - public UserEntity User { get; set; } = default!; - } - - public interface IUserTokenManager - { - /// - /// Try to create a token for given username and password. - /// - /// The username. - /// The password. - /// The expire time of the token. - /// The created token and the user info. - /// Thrown when or is null. - /// Thrown when is of bad format. - /// Thrown when the user with does not exist. - /// Thrown when is wrong. - public Task CreateToken(string username, string password, DateTime? expireAt = null); - - /// - /// Verify a token and get the saved user info. This also check the database for existence of the user. - /// - /// The token. - /// The user stored in token. - /// Thrown when is null. - /// Thrown when the token is expired. - /// Thrown when the token is of bad version. - /// Thrown when the token is of bad format. - /// Thrown when the user specified by the token does not exist. Usually the user had been deleted after the token was issued. - public Task VerifyToken(string token); - } - public class UserTokenManager : IUserTokenManager { private readonly ILogger _logger; @@ -79,6 +45,8 @@ namespace Timeline.Services.Token ExpireAt = expireAt ?? _clock.GetCurrentTime() + TimeSpan.FromSeconds(_tokenOptionsMonitor.CurrentValue.DefaultExpireSeconds) }); + _logger.LogInformation(Resource.LogTokenCreate, user.Username, userId); + return new UserTokenCreateResult { Token = token, User = user }; } @@ -88,25 +56,46 @@ namespace Timeline.Services.Token if (token == null) throw new ArgumentNullException(nameof(token)); - var tokenInfo = _userTokenService.VerifyToken(token); + UserTokenInfo tokenInfo; + + try + { + tokenInfo = _userTokenService.VerifyToken(token); + } + catch (UserTokenBadFormatException e) + { + _logger.LogInformation(e, Resource.LogTokenVerifiedFail); + throw; + } var currentTime = _clock.GetCurrentTime(); if (tokenInfo.ExpireAt < currentTime) - throw new UserTokenTimeExpiredException(token, tokenInfo.ExpireAt, currentTime); + { + var e = new UserTokenTimeExpiredException(token, tokenInfo.ExpireAt, currentTime); + _logger.LogInformation(e, Resource.LogTokenVerifiedFail); + throw e; + } try { var user = await _userService.GetUserAsync(tokenInfo.Id); if (tokenInfo.Version < user.Version) - throw new UserTokenVersionExpiredException(token, tokenInfo.Version, user.Version); + { + var e = new UserTokenVersionExpiredException(token, tokenInfo.Version, user.Version); + _logger.LogInformation(e, Resource.LogTokenVerifiedFail); + throw e; + } - return user; + _logger.LogInformation(Resource.LogTokenVerified, user.Username, user.Id); + return user; } catch (UserNotExistException e) { - throw new UserTokenUserNotExistException(token, e); + var exception = new UserTokenUserNotExistException(token, e); + _logger.LogInformation(exception, Resource.LogTokenVerifiedFail); + throw exception; } } } -- cgit v1.2.3