From 4aadb05cd5718c7d16bf432c96e23ae4e7db4783 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 21 Jan 2020 01:11:17 +0800 Subject: ... --- Timeline/Services/UserService.cs | 68 +++++++++++++--------------------------- 1 file changed, 22 insertions(+), 46 deletions(-) (limited to 'Timeline/Services/UserService.cs') diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 4012539f..db2350a2 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -11,47 +11,37 @@ using Timeline.Models.Validation; namespace Timeline.Services { - public class CreateTokenResult - { - public string Token { get; set; } = default!; - public UserInfo User { get; set; } = default!; - } - public interface IUserService { /// - /// Try to anthenticate with the given username and password. - /// If success, create a token and return the user info. + /// Try to verify the given username and password. /// - /// The username of the user to anthenticate. - /// The password of the user to anthenticate. - /// The expired time point. Null then use default. See for what is default. - /// An containing the created token and user info. + /// The username of the user to verify. + /// The password of the user to verify. + /// The user info. /// Thrown when or is null. /// Thrown when username is of bad format. /// Thrown when the user with given username does not exist. /// Thrown when password is wrong. - Task CreateToken(string username, string password, DateTime? expires = null); + Task VerifyCredential(string username, string password); /// - /// Verify the given token. - /// If success, return the user info. + /// Try to get a user by id. /// - /// The token to verify. - /// The user info specified by the token. - /// Thrown when is null. - /// Thrown when the token is of bad format. Thrown by . - /// Thrown when the user specified by the token does not exist. Usually it has been deleted after the token was issued. - Task VerifyToken(string token); + /// The id of the user. + /// The user info. + /// Thrown when the user with given id does not exist. + Task GetUserById(long id); /// /// Get the user info of given username. /// /// Username of the user. - /// The info of the user. Null if the user of given username does not exists. + /// The info of the user. /// Thrown when is null. /// Thrown when is of bad format. - Task GetUser(string username); + /// Thrown when the user with given username does not exist. + Task GetUserByUsername(string username); /// /// List all users. @@ -120,39 +110,24 @@ namespace Timeline.Services Task ChangeUsername(string oldUsername, string newUsername); } - internal class UserCache - { - public string Username { get; set; } = default!; - public bool Administrator { get; set; } - public long Version { get; set; } - - public UserInfo ToUserInfo() - { - return new UserInfo(Username, Administrator); - } - } - public class UserService : IUserService { private readonly ILogger _logger; - private readonly IMemoryCache _memoryCache; private readonly DatabaseContext _databaseContext; - private readonly IJwtService _jwtService; + private readonly IMemoryCache _memoryCache; + private readonly IPasswordService _passwordService; - private readonly UsernameValidator _usernameValidator; + private readonly UsernameValidator _usernameValidator = new UsernameValidator(); - public UserService(ILogger logger, IMemoryCache memoryCache, DatabaseContext databaseContext, IJwtService jwtService, IPasswordService passwordService) + public UserService(ILogger logger, IMemoryCache memoryCache, DatabaseContext databaseContext, IPasswordService passwordService) { _logger = logger; _memoryCache = memoryCache; _databaseContext = databaseContext; - _jwtService = jwtService; _passwordService = passwordService; - - _usernameValidator = new UsernameValidator(); } private static string GenerateCacheKeyByUserId(long id) => $"user:{id}"; @@ -176,12 +151,13 @@ namespace Timeline.Services } } - public async Task CreateToken(string username, string password, DateTime? expires) + public async Task CheckCredential(string username, string password) { if (username == null) throw new ArgumentNullException(nameof(username)); if (password == null) throw new ArgumentNullException(nameof(password)); + CheckUsernameFormat(username); // We need password info, so always check the database. @@ -231,12 +207,12 @@ namespace Timeline.Services } if (tokenInfo.Version != cache.Version) - throw new JwtVerifyException(new JwtBadVersionException(tokenInfo.Version, cache.Version), JwtVerifyException.ErrorCodes.OldVersion); + throw new JwtUserTokenBadFormatException(new JwtBadVersionException(tokenInfo.Version, cache.Version), JwtUserTokenBadFormatException.ErrorCodes.OldVersion); return cache.ToUserInfo(); } - public async Task GetUser(string username) + public async Task GetUserByUsername(string username) { if (username == null) throw new ArgumentNullException(nameof(username)); @@ -267,7 +243,7 @@ namespace Timeline.Services if (user == null) { - var newUser = new User + var newUser = new UserEntity { Name = username, EncryptedPassword = _passwordService.HashPassword(password), -- cgit v1.2.3