From db6629940e294b44d678e776ccce769a8ac715de Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 28 Apr 2021 16:55:37 +0800 Subject: refactor: ... --- .../Timeline/Services/Token/IUserTokenHandler.cs | 28 +++++++++ .../Timeline/Services/Token/IUserTokenManager.cs | 35 +++++++++++ .../Timeline/Services/Token/Resource.Designer.cs | 27 +++++++++ BackEnd/Timeline/Services/Token/Resource.resx | 9 +++ .../Token/TokenServiceColletionExtensions.cs | 18 ------ .../TokenServicesServiceColletionExtensions.cs | 18 ++++++ .../Services/Token/UserTokenBadFormatException.cs | 17 ++++++ .../Services/Token/UserTokenCreateResult.cs | 10 ++++ .../Timeline/Services/Token/UserTokenException.cs | 61 -------------------- .../Timeline/Services/Token/UserTokenHandler.cs | 30 ---------- BackEnd/Timeline/Services/Token/UserTokenInfo.cs | 11 ++++ .../Timeline/Services/Token/UserTokenManager.cs | 67 +++++++++------------- .../Token/UserTokenTimeExpiredException.cs | 21 +++++++ .../Token/UserTokenUserNotExistException.cs | 16 ++++++ .../Token/UserTokenVersionExpiredException.cs | 21 +++++++ .../Timeline/Services/User/Resource.Designer.cs | 18 ++++++ BackEnd/Timeline/Services/User/Resource.resx | 6 ++ BackEnd/Timeline/Services/User/UserService.cs | 6 ++ BackEnd/Timeline/Startup.cs | 2 +- 19 files changed, 272 insertions(+), 149 deletions(-) create mode 100644 BackEnd/Timeline/Services/Token/IUserTokenHandler.cs create mode 100644 BackEnd/Timeline/Services/Token/IUserTokenManager.cs delete mode 100644 BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs create mode 100644 BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenBadFormatException.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenCreateResult.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenInfo.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenTimeExpiredException.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenUserNotExistException.cs create mode 100644 BackEnd/Timeline/Services/Token/UserTokenVersionExpiredException.cs (limited to 'BackEnd') diff --git a/BackEnd/Timeline/Services/Token/IUserTokenHandler.cs b/BackEnd/Timeline/Services/Token/IUserTokenHandler.cs new file mode 100644 index 00000000..d9788909 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/IUserTokenHandler.cs @@ -0,0 +1,28 @@ +using System; + +namespace Timeline.Services.Token +{ + public interface IUserTokenHandler + { + /// + /// Create a token for a given token info. + /// + /// The info to generate token. + /// Return the generated token. + /// Thrown when is null. + string GenerateToken(UserTokenInfo tokenInfo); + + /// + /// Verify a token and get the saved info. Do not validate lifetime!!! + /// + /// The token to verify. + /// The saved info in token. + /// Thrown when is null. + /// Thrown when the token is of bad format. + /// + /// If this method throw , it usually means the token is not created by this service. + /// Do not check expire time in this method, only check whether it is present. + /// + UserTokenInfo VerifyToken(string token); + } +} diff --git a/BackEnd/Timeline/Services/Token/IUserTokenManager.cs b/BackEnd/Timeline/Services/Token/IUserTokenManager.cs new file mode 100644 index 00000000..c6eaa5b7 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/IUserTokenManager.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Services.User; + +namespace Timeline.Services.Token +{ + 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); + } +} diff --git a/BackEnd/Timeline/Services/Token/Resource.Designer.cs b/BackEnd/Timeline/Services/Token/Resource.Designer.cs index 4321c665..07b0057f 100644 --- a/BackEnd/Timeline/Services/Token/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/Token/Resource.Designer.cs @@ -158,5 +158,32 @@ namespace Timeline.Services.Token { return ResourceManager.GetString("ExceptionUserTokenVersionExpired", resourceCulture); } } + + /// + /// Looks up a localized string similar to A token is created for user with username={0}, id={1}.. + /// + internal static string LogTokenCreate { + get { + return ResourceManager.GetString("LogTokenCreate", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A token of user with username = {0}, id = {1} is verified successfully.. + /// + internal static string LogTokenVerified { + get { + return ResourceManager.GetString("LogTokenVerified", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to A token fails to be verified.. + /// + internal static string LogTokenVerifiedFail { + get { + return ResourceManager.GetString("LogTokenVerifiedFail", resourceCulture); + } + } } } diff --git a/BackEnd/Timeline/Services/Token/Resource.resx b/BackEnd/Timeline/Services/Token/Resource.resx index c42da2ca..7abf2e75 100644 --- a/BackEnd/Timeline/Services/Token/Resource.resx +++ b/BackEnd/Timeline/Services/Token/Resource.resx @@ -150,4 +150,13 @@ The token is of bad version. + + A token is created for user with username={0}, id={1}. + + + A token of user with username = {0}, id = {1} is verified successfully. + + + A token fails to be verified. + \ No newline at end of file diff --git a/BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs b/BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs deleted file mode 100644 index d3219ec4..00000000 --- a/BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Timeline.Configs; - -namespace Timeline.Services.Token -{ - public static class TokenServiceColletionExtensions - { - public static IServiceCollection AddTokenService(this IServiceCollection services, IConfiguration configuration) - { - services.Configure(configuration.GetSection("Token")); - services.Configure(configuration.GetSection("Jwt")); - services.AddScoped(); - services.AddScoped(); - return services; - } - } -} diff --git a/BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs b/BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs new file mode 100644 index 00000000..1ad84311 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Timeline.Configs; + +namespace Timeline.Services.Token +{ + public static class TokenServicesServiceColletionExtensions + { + public static IServiceCollection AddTokenServices(this IServiceCollection services, IConfiguration configuration) + { + services.Configure(configuration.GetSection("Token")); + services.Configure(configuration.GetSection("Jwt")); + services.AddScoped(); + services.AddScoped(); + return services; + } + } +} diff --git a/BackEnd/Timeline/Services/Token/UserTokenBadFormatException.cs b/BackEnd/Timeline/Services/Token/UserTokenBadFormatException.cs new file mode 100644 index 00000000..39ed1be4 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenBadFormatException.cs @@ -0,0 +1,17 @@ +using System; + +namespace Timeline.Services.Token +{ + [Serializable] + public class UserTokenBadFormatException : UserTokenException + { + public UserTokenBadFormatException() : base(Resource.ExceptionUserTokenBadFormat) { } + public UserTokenBadFormatException(string token) : base(token, Resource.ExceptionUserTokenBadFormat) { } + public UserTokenBadFormatException(string token, string message) : base(token, message) { } + public UserTokenBadFormatException(string token, Exception inner) : base(token, Resource.ExceptionUserTokenBadFormat, inner) { } + public UserTokenBadFormatException(string token, string message, Exception inner) : base(token, message, inner) { } + protected UserTokenBadFormatException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/BackEnd/Timeline/Services/Token/UserTokenCreateResult.cs b/BackEnd/Timeline/Services/Token/UserTokenCreateResult.cs new file mode 100644 index 00000000..94542057 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenCreateResult.cs @@ -0,0 +1,10 @@ +using Timeline.Entities; + +namespace Timeline.Services.Token +{ + public class UserTokenCreateResult + { + public string Token { get; set; } = default!; + public UserEntity User { get; set; } = default!; + } +} diff --git a/BackEnd/Timeline/Services/Token/UserTokenException.cs b/BackEnd/Timeline/Services/Token/UserTokenException.cs index a781eb05..357ca2aa 100644 --- a/BackEnd/Timeline/Services/Token/UserTokenException.cs +++ b/BackEnd/Timeline/Services/Token/UserTokenException.cs @@ -16,65 +16,4 @@ namespace Timeline.Services.Token public string Token { get; private set; } = ""; } - - - [Serializable] - public class UserTokenTimeExpiredException : UserTokenException - { - public UserTokenTimeExpiredException() : base(Resource.ExceptionUserTokenTimeExpired) { } - public UserTokenTimeExpiredException(string message) : base(message) { } - public UserTokenTimeExpiredException(string message, Exception inner) : base(message, inner) { } - public UserTokenTimeExpiredException(string token, DateTime expireTime, DateTime verifyTime) : base(token, Resource.ExceptionUserTokenTimeExpired) { ExpireTime = expireTime; VerifyTime = verifyTime; } - public UserTokenTimeExpiredException(string token, DateTime expireTime, DateTime verifyTime, Exception inner) : base(token, Resource.ExceptionUserTokenTimeExpired, inner) { ExpireTime = expireTime; VerifyTime = verifyTime; } - protected UserTokenTimeExpiredException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - public DateTime ExpireTime { get; private set; } - - public DateTime VerifyTime { get; private set; } - } - - [Serializable] - public class UserTokenVersionExpiredException : UserTokenException - { - public UserTokenVersionExpiredException() : base(Resource.ExceptionUserTokenVersionExpired) { } - public UserTokenVersionExpiredException(string message) : base(message) { } - public UserTokenVersionExpiredException(string message, Exception inner) : base(message, inner) { } - public UserTokenVersionExpiredException(string token, long tokenVersion, long requiredVersion) : base(token, Resource.ExceptionUserTokenVersionExpired) { TokenVersion = tokenVersion; RequiredVersion = requiredVersion; } - public UserTokenVersionExpiredException(string token, long tokenVersion, long requiredVersion, Exception inner) : base(token, Resource.ExceptionUserTokenVersionExpired, inner) { TokenVersion = tokenVersion; RequiredVersion = requiredVersion; } - protected UserTokenVersionExpiredException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - - public long TokenVersion { get; set; } - - public long RequiredVersion { get; set; } - } - - - [Serializable] - public class UserTokenUserNotExistException : UserTokenException - { - public UserTokenUserNotExistException() : base(Resource.ExceptionUserTokenUserNotExist) { } - public UserTokenUserNotExistException(string token) : base(token, Resource.ExceptionUserTokenUserNotExist) { } - public UserTokenUserNotExistException(string token, Exception inner) : base(token, Resource.ExceptionUserTokenUserNotExist, inner) { } - - protected UserTokenUserNotExistException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } - - [Serializable] - public class UserTokenBadFormatException : UserTokenException - { - public UserTokenBadFormatException() : base(Resource.ExceptionUserTokenBadFormat) { } - public UserTokenBadFormatException(string token) : base(token, Resource.ExceptionUserTokenBadFormat) { } - public UserTokenBadFormatException(string token, string message) : base(token, message) { } - public UserTokenBadFormatException(string token, Exception inner) : base(token, Resource.ExceptionUserTokenBadFormat, inner) { } - public UserTokenBadFormatException(string token, string message, Exception inner) : base(token, message, inner) { } - protected UserTokenBadFormatException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } } diff --git a/BackEnd/Timeline/Services/Token/UserTokenHandler.cs b/BackEnd/Timeline/Services/Token/UserTokenHandler.cs index 2eaea57e..7b57a06c 100644 --- a/BackEnd/Timeline/Services/Token/UserTokenHandler.cs +++ b/BackEnd/Timeline/Services/Token/UserTokenHandler.cs @@ -10,36 +10,6 @@ using Timeline.Entities; namespace Timeline.Services.Token { - public class UserTokenInfo - { - public long Id { get; set; } - public long Version { get; set; } - public DateTime ExpireAt { get; set; } - } - - public interface IUserTokenHandler - { - /// - /// Create a token for a given token info. - /// - /// The info to generate token. - /// Return the generated token. - /// Thrown when is null. - string GenerateToken(UserTokenInfo tokenInfo); - - /// - /// Verify a token and get the saved info. Do not validate lifetime!!! - /// - /// The token to verify. - /// The saved info in token. - /// Thrown when is null. - /// Thrown when the token is of bad format. - /// - /// If this method throw , it usually means the token is not created by this service. - /// - UserTokenInfo VerifyToken(string token); - } - public class JwtUserTokenHandler : IUserTokenHandler { private const string VersionClaimType = "timeline_version"; diff --git a/BackEnd/Timeline/Services/Token/UserTokenInfo.cs b/BackEnd/Timeline/Services/Token/UserTokenInfo.cs new file mode 100644 index 00000000..547f5ba6 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenInfo.cs @@ -0,0 +1,11 @@ +using System; + +namespace Timeline.Services.Token +{ + public class UserTokenInfo + { + public long Id { get; set; } + public long Version { get; set; } + public DateTime ExpireAt { get; set; } + } +} 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; } } } diff --git a/BackEnd/Timeline/Services/Token/UserTokenTimeExpiredException.cs b/BackEnd/Timeline/Services/Token/UserTokenTimeExpiredException.cs new file mode 100644 index 00000000..6e33ab4d --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenTimeExpiredException.cs @@ -0,0 +1,21 @@ +using System; + +namespace Timeline.Services.Token +{ + [Serializable] + public class UserTokenTimeExpiredException : UserTokenException + { + public UserTokenTimeExpiredException() : base(Resource.ExceptionUserTokenTimeExpired) { } + public UserTokenTimeExpiredException(string message) : base(message) { } + public UserTokenTimeExpiredException(string message, Exception inner) : base(message, inner) { } + public UserTokenTimeExpiredException(string token, DateTime expireTime, DateTime verifyTime) : base(token, Resource.ExceptionUserTokenTimeExpired) { ExpireTime = expireTime; VerifyTime = verifyTime; } + public UserTokenTimeExpiredException(string token, DateTime expireTime, DateTime verifyTime, Exception inner) : base(token, Resource.ExceptionUserTokenTimeExpired, inner) { ExpireTime = expireTime; VerifyTime = verifyTime; } + protected UserTokenTimeExpiredException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public DateTime ExpireTime { get; private set; } + + public DateTime VerifyTime { get; private set; } + } +} diff --git a/BackEnd/Timeline/Services/Token/UserTokenUserNotExistException.cs b/BackEnd/Timeline/Services/Token/UserTokenUserNotExistException.cs new file mode 100644 index 00000000..28f56938 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenUserNotExistException.cs @@ -0,0 +1,16 @@ +using System; + +namespace Timeline.Services.Token +{ + [Serializable] + public class UserTokenUserNotExistException : UserTokenException + { + public UserTokenUserNotExistException() : base(Resource.ExceptionUserTokenUserNotExist) { } + public UserTokenUserNotExistException(string token) : base(token, Resource.ExceptionUserTokenUserNotExist) { } + public UserTokenUserNotExistException(string token, Exception inner) : base(token, Resource.ExceptionUserTokenUserNotExist, inner) { } + + protected UserTokenUserNotExistException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/BackEnd/Timeline/Services/Token/UserTokenVersionExpiredException.cs b/BackEnd/Timeline/Services/Token/UserTokenVersionExpiredException.cs new file mode 100644 index 00000000..db6b4669 --- /dev/null +++ b/BackEnd/Timeline/Services/Token/UserTokenVersionExpiredException.cs @@ -0,0 +1,21 @@ +using System; + +namespace Timeline.Services.Token +{ + [Serializable] + public class UserTokenVersionExpiredException : UserTokenException + { + public UserTokenVersionExpiredException() : base(Resource.ExceptionUserTokenVersionExpired) { } + public UserTokenVersionExpiredException(string message) : base(message) { } + public UserTokenVersionExpiredException(string message, Exception inner) : base(message, inner) { } + public UserTokenVersionExpiredException(string token, long tokenVersion, long requiredVersion) : base(token, Resource.ExceptionUserTokenVersionExpired) { TokenVersion = tokenVersion; RequiredVersion = requiredVersion; } + public UserTokenVersionExpiredException(string token, long tokenVersion, long requiredVersion, Exception inner) : base(token, Resource.ExceptionUserTokenVersionExpired, inner) { TokenVersion = tokenVersion; RequiredVersion = requiredVersion; } + protected UserTokenVersionExpiredException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + + public long TokenVersion { get; set; } + + public long RequiredVersion { get; set; } + } +} diff --git a/BackEnd/Timeline/Services/User/Resource.Designer.cs b/BackEnd/Timeline/Services/User/Resource.Designer.cs index 908e2732..b5fb81bc 100644 --- a/BackEnd/Timeline/Services/User/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/User/Resource.Designer.cs @@ -257,5 +257,23 @@ namespace Timeline.Services.User { return ResourceManager.GetString("LogUserModified", resourceCulture); } } + + /// + /// Looks up a localized string similar to An attemp to login with wrong pasword with username '{0}' failed.. + /// + internal static string LogVerifyCredentialsPasswordBad { + get { + return ResourceManager.GetString("LogVerifyCredentialsPasswordBad", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An attemp to login with wrong username '{0}' failed.. + /// + internal static string LogVerifyCredentialsUsernameBad { + get { + return ResourceManager.GetString("LogVerifyCredentialsUsernameBad", resourceCulture); + } + } } } diff --git a/BackEnd/Timeline/Services/User/Resource.resx b/BackEnd/Timeline/Services/User/Resource.resx index a734bd70..0865bf8b 100644 --- a/BackEnd/Timeline/Services/User/Resource.resx +++ b/BackEnd/Timeline/Services/User/Resource.resx @@ -183,4 +183,10 @@ A user is modified with username = {0}, id = {1}. + + An attemp to login with wrong pasword with username '{0}' failed. + + + An attemp to login with wrong username '{0}' failed. + \ No newline at end of file diff --git a/BackEnd/Timeline/Services/User/UserService.cs b/BackEnd/Timeline/Services/User/UserService.cs index 6496b55b..443afb90 100644 --- a/BackEnd/Timeline/Services/User/UserService.cs +++ b/BackEnd/Timeline/Services/User/UserService.cs @@ -178,10 +178,16 @@ namespace Timeline.Services.User var entity = await _databaseContext.Users.Where(u => u.Username == username).Select(u => new { u.Id, u.Password }).SingleOrDefaultAsync(); if (entity is null) + { + _logger.LogInformation(Resource.LogVerifyCredentialsUsernameBad, username); throw new UserNotExistException(username); + } if (!_passwordService.VerifyPassword(entity.Password, password)) + { + _logger.LogInformation(Resource.LogVerifyCredentialsPasswordBad, username); throw new BadPasswordException(password); + } return entity.Id; } diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 32208d53..274b15e1 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -100,7 +100,7 @@ namespace Timeline services.AddDataServices(); services.AddImageServices(); services.AddUserServices(); - services.AddTokenService(Configuration); + services.AddTokenServices(Configuration); services.AddScoped(); services.AddScoped(); -- cgit v1.2.3