diff options
author | crupest <crupest@outlook.com> | 2021-04-28 16:55:37 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-28 16:55:37 +0800 |
commit | db6629940e294b44d678e776ccce769a8ac715de (patch) | |
tree | 7be5ab09f40083f3c12c6e4beb27003161cb9704 /BackEnd/Timeline | |
parent | cade46338cd3b3864948c278c5dd64e48fa4634e (diff) | |
download | timeline-db6629940e294b44d678e776ccce769a8ac715de.tar.gz timeline-db6629940e294b44d678e776ccce769a8ac715de.tar.bz2 timeline-db6629940e294b44d678e776ccce769a8ac715de.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline')
18 files changed, 256 insertions, 133 deletions
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
+ {
+ /// <summary>
+ /// Create a token for a given token info.
+ /// </summary>
+ /// <param name="tokenInfo">The info to generate token.</param>
+ /// <returns>Return the generated token.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="tokenInfo"/> is null.</exception>
+ string GenerateToken(UserTokenInfo tokenInfo);
+
+ /// <summary>
+ /// Verify a token and get the saved info. Do not validate lifetime!!!
+ /// </summary>
+ /// <param name="token">The token to verify.</param>
+ /// <returns>The saved info in token.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="token"/> is null.</exception>
+ /// <exception cref="UserTokenBadFormatException">Thrown when the token is of bad format.</exception>
+ /// <remarks>
+ /// If this method throw <see cref="UserTokenBadFormatException"/>, 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.
+ /// </remarks>
+ 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
+ {
+ /// <summary>
+ /// Try to create a token for given username and password.
+ /// </summary>
+ /// <param name="username">The username.</param>
+ /// <param name="password">The password.</param>
+ /// <param name="expireAt">The expire time of the token.</param>
+ /// <returns>The created token and the user info.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> or <paramref name="password"/> is null.</exception>
+ /// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
+ /// <exception cref="UserNotExistException">Thrown when the user with <paramref name="username"/> does not exist.</exception>
+ /// <exception cref="BadPasswordException">Thrown when <paramref name="password"/> is wrong.</exception>
+ public Task<UserTokenCreateResult> CreateToken(string username, string password, DateTime? expireAt = null);
+
+ /// <summary>
+ /// Verify a token and get the saved user info. This also check the database for existence of the user.
+ /// </summary>
+ /// <param name="token">The token.</param>
+ /// <returns>The user stored in token.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="token"/> is null.</exception>
+ /// <exception cref="UserTokenTimeExpiredException">Thrown when the token is expired.</exception>
+ /// <exception cref="UserTokenVersionExpiredException">Thrown when the token is of bad version.</exception>
+ /// <exception cref="UserTokenBadFormatException">Thrown when the token is of bad format.</exception>
+ /// <exception cref="UserTokenUserNotExistException">Thrown when the user specified by the token does not exist. Usually the user had been deleted after the token was issued.</exception>
+ public Task<UserEntity> 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);
}
}
+
+ /// <summary>
+ /// Looks up a localized string similar to A token is created for user with username={0}, id={1}..
+ /// </summary>
+ internal static string LogTokenCreate {
+ get {
+ return ResourceManager.GetString("LogTokenCreate", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A token of user with username = {0}, id = {1} is verified successfully..
+ /// </summary>
+ internal static string LogTokenVerified {
+ get {
+ return ResourceManager.GetString("LogTokenVerified", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to A token fails to be verified..
+ /// </summary>
+ 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 @@ <data name="ExceptionUserTokenVersionExpired" xml:space="preserve">
<value>The token is of bad version.</value>
</data>
+ <data name="LogTokenCreate" xml:space="preserve">
+ <value>A token is created for user with username={0}, id={1}.</value>
+ </data>
+ <data name="LogTokenVerified" xml:space="preserve">
+ <value>A token of user with username = {0}, id = {1} is verified successfully.</value>
+ </data>
+ <data name="LogTokenVerifiedFail" xml:space="preserve">
+ <value>A token fails to be verified.</value>
+ </data>
</root>
\ No newline at end of file diff --git a/BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs b/BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs index d3219ec4..1ad84311 100644 --- a/BackEnd/Timeline/Services/Token/TokenServiceColletionExtensions.cs +++ b/BackEnd/Timeline/Services/Token/TokenServicesServiceColletionExtensions.cs @@ -4,9 +4,9 @@ using Timeline.Configs; namespace Timeline.Services.Token
{
- public static class TokenServiceColletionExtensions
+ public static class TokenServicesServiceColletionExtensions
{
- public static IServiceCollection AddTokenService(this IServiceCollection services, IConfiguration configuration)
+ public static IServiceCollection AddTokenServices(this IServiceCollection services, IConfiguration configuration)
{
services.Configure<TokenOptions>(configuration.GetSection("Token"));
services.Configure<JwtOptions>(configuration.GetSection("Jwt"));
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
- {
- /// <summary>
- /// Create a token for a given token info.
- /// </summary>
- /// <param name="tokenInfo">The info to generate token.</param>
- /// <returns>Return the generated token.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="tokenInfo"/> is null.</exception>
- string GenerateToken(UserTokenInfo tokenInfo);
-
- /// <summary>
- /// Verify a token and get the saved info. Do not validate lifetime!!!
- /// </summary>
- /// <param name="token">The token to verify.</param>
- /// <returns>The saved info in token.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="token"/> is null.</exception>
- /// <exception cref="UserTokenBadFormatException">Thrown when the token is of bad format.</exception>
- /// <remarks>
- /// If this method throw <see cref="UserTokenBadFormatException"/>, it usually means the token is not created by this service.
- /// </remarks>
- 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
- {
- /// <summary>
- /// Try to create a token for given username and password.
- /// </summary>
- /// <param name="username">The username.</param>
- /// <param name="password">The password.</param>
- /// <param name="expireAt">The expire time of the token.</param>
- /// <returns>The created token and the user info.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> or <paramref name="password"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
- /// <exception cref="UserNotExistException">Thrown when the user with <paramref name="username"/> does not exist.</exception>
- /// <exception cref="BadPasswordException">Thrown when <paramref name="password"/> is wrong.</exception>
- public Task<UserTokenCreateResult> CreateToken(string username, string password, DateTime? expireAt = null);
-
- /// <summary>
- /// Verify a token and get the saved user info. This also check the database for existence of the user.
- /// </summary>
- /// <param name="token">The token.</param>
- /// <returns>The user stored in token.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="token"/> is null.</exception>
- /// <exception cref="UserTokenTimeExpiredException">Thrown when the token is expired.</exception>
- /// <exception cref="UserTokenVersionExpiredException">Thrown when the token is of bad version.</exception>
- /// <exception cref="UserTokenBadFormatException">Thrown when the token is of bad format.</exception>
- /// <exception cref="UserTokenUserNotExistException">Thrown when the user specified by the token does not exist. Usually the user had been deleted after the token was issued.</exception>
- public Task<UserEntity> VerifyToken(string token);
- }
-
public class UserTokenManager : IUserTokenManager
{
private readonly ILogger<UserTokenManager> _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);
}
}
+
+ /// <summary>
+ /// Looks up a localized string similar to An attemp to login with wrong pasword with username '{0}' failed..
+ /// </summary>
+ internal static string LogVerifyCredentialsPasswordBad {
+ get {
+ return ResourceManager.GetString("LogVerifyCredentialsPasswordBad", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to An attemp to login with wrong username '{0}' failed..
+ /// </summary>
+ 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 @@ <data name="LogUserModified" xml:space="preserve">
<value>A user is modified with username = {0}, id = {1}.</value>
</data>
+ <data name="LogVerifyCredentialsPasswordBad" xml:space="preserve">
+ <value>An attemp to login with wrong pasword with username '{0}' failed.</value>
+ </data>
+ <data name="LogVerifyCredentialsUsernameBad" xml:space="preserve">
+ <value>An attemp to login with wrong username '{0}' failed.</value>
+ </data>
</root>
\ 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<IBasicTimelineService, BasicTimelineService>();
services.AddScoped<ITimelineService, TimelineService>();
|