diff options
author | 杨宇千 <crupest@outlook.com> | 2019-07-23 17:11:59 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-07-23 17:11:59 +0800 |
commit | 79d4c444ace7cb10fbd638936c2e47a6314d2758 (patch) | |
tree | 8fcf9bdeaccac760dca563b04691bf7f0be07453 | |
parent | 6191dbfdfa4f048e2f5a78729b49c8cf8a654ae3 (diff) | |
download | timeline-79d4c444ace7cb10fbd638936c2e47a6314d2758.tar.gz timeline-79d4c444ace7cb10fbd638936c2e47a6314d2758.tar.bz2 timeline-79d4c444ace7cb10fbd638936c2e47a6314d2758.zip |
WIP: Rewrite TokenController.
-rw-r--r-- | Timeline/Controllers/TokenController.cs | 80 | ||||
-rw-r--r-- | Timeline/Entities/Http/Common.cs | 22 | ||||
-rw-r--r-- | Timeline/Entities/Http/Token.cs | 6 | ||||
-rw-r--r-- | Timeline/Entities/Http/User.cs | 20 |
4 files changed, 72 insertions, 56 deletions
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index cb4408cd..3a364ffe 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -12,8 +12,21 @@ namespace Timeline.Controllers { private static class LoggingEventIds { - public const int LogInSucceeded = 4000; - public const int LogInFailed = 4001; + public const int LogInSucceeded = 1000; + public const int LogInFailed = 1001; + + public const int VerifySucceeded = 2000; + public const int VerifyFailed = 2001; + } + + private static class ErrorCodes + { + public const int Create_UserNotExist = 1001; + public const int Create_BadPassword = 1002; + + public const int Verify_BadToken = 2001; + public const int Verify_UserNotExist = 2002; + public const int Verify_BadVersion = 2003; } private readonly IUserService _userService; @@ -27,48 +40,63 @@ namespace Timeline.Controllers [HttpPost("create")] [AllowAnonymous] - public async Task<ActionResult<CreateTokenResponse>> Create([FromBody] CreateTokenRequest request) + public async Task<IActionResult> Create([FromBody] CreateTokenRequest request) { - var result = await _userService.CreateToken(request.Username, request.Password); - - if (result == null) + try { - _logger.LogInformation(LoggingEventIds.LogInFailed, "Attemp to login with username: {} and password: {} failed.", request.Username, request.Password); + var result = await _userService.CreateToken(request.Username, request.Password); + _logger.LogInformation(LoggingEventIds.LogInSucceeded, "Login succeeded. Username: {} .", request.Username); return Ok(new CreateTokenResponse { - Success = false + Token = result.Token, + User = result.User }); } - - _logger.LogInformation(LoggingEventIds.LogInSucceeded, "Login with username: {} succeeded.", request.Username); - - return Ok(new CreateTokenResponse + catch(UserNotExistException e) + { + var code = ErrorCodes.Create_UserNotExist; + _logger.LogInformation(LoggingEventIds.LogInFailed, e, "Attemp to login failed. Code: {} Username: {} Password: {} .", code, request.Username, request.Password); + return BadRequest(new CommonErrorResponse(code, "Bad username or password.")); + } + catch (BadPasswordException e) { - Success = true, - Token = result.Token, - UserInfo = result.User - }); + var code = ErrorCodes.Create_BadPassword; + _logger.LogInformation(LoggingEventIds.LogInFailed, e, "Attemp to login failed. Code: {} Username: {} Password: {} .", code, request.Username, request.Password); + return BadRequest(new CommonErrorResponse(code, "Bad username or password.")); + } } [HttpPost("verify")] [AllowAnonymous] - public async Task<ActionResult<VerifyTokenResponse>> Verify([FromBody] VerifyTokenRequest request) + public async Task<IActionResult> Verify([FromBody] VerifyTokenRequest request) { - var result = await _userService.VerifyToken(request.Token); - - if (result == null) + try { + var result = await _userService.VerifyToken(request.Token); + _logger.LogInformation(LoggingEventIds.VerifySucceeded, "Verify token succeeded. Username: {} Token: {} .", result.Username, request.Token); return Ok(new VerifyTokenResponse { - IsValid = false, + User = result }); } - - return Ok(new VerifyTokenResponse + catch (JwtTokenVerifyException e) { - IsValid = true, - UserInfo = result - }); + var code = ErrorCodes.Verify_BadToken; + _logger.LogInformation(LoggingEventIds.VerifyFailed, e, "Attemp to verify a bad token. Code: {} Token: {}.", code, request.Token); + return BadRequest(new CommonErrorResponse(code, "A token of bad format.")); + } + catch (UserNotExistException e) + { + var code = ErrorCodes.Verify_UserNotExist; + _logger.LogInformation(LoggingEventIds.VerifyFailed, e, "Attemp to verify a bad token. Code: {} Token: {}.", code, request.Token); + return BadRequest(new CommonErrorResponse(code, "The user does not exist. Administrator might have deleted this user.")); + } + catch (BadTokenVersionException e) + { + var code = ErrorCodes.Verify_BadToken; + _logger.LogInformation(LoggingEventIds.VerifyFailed, e, "Attemp to verify a bad token. Code: {} Token: {}.", code, request.Token); + return BadRequest(new CommonErrorResponse(code, "The token is expired. Try recreate a token.")); + } } } } diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs index 9575e6fa..7708927a 100644 --- a/Timeline/Entities/Http/Common.cs +++ b/Timeline/Entities/Http/Common.cs @@ -1,29 +1,19 @@ namespace Timeline.Entities.Http { - public class ReturnCodeMessageResponse + public class CommonErrorResponse { - public ReturnCodeMessageResponse() + public CommonErrorResponse() { } - public ReturnCodeMessageResponse(int code) + public CommonErrorResponse(int code, string message) { - ReturnCode = code; - } - - public ReturnCodeMessageResponse(string message) - { - Message = message; - } - - public ReturnCodeMessageResponse(int code, string message) - { - ReturnCode = code; + Code = code; Message = message; } - public int? ReturnCode { get; set; } = null; - public string Message { get; set; } = null; + public int Code { get; set; } + public string Message { get; set; } } } diff --git a/Timeline/Entities/Http/Token.cs b/Timeline/Entities/Http/Token.cs index 45ee0fc5..aeb9fbf2 100644 --- a/Timeline/Entities/Http/Token.cs +++ b/Timeline/Entities/Http/Token.cs @@ -8,9 +8,8 @@ public class CreateTokenResponse { - public bool Success { get; set; } public string Token { get; set; } - public UserInfo UserInfo { get; set; } + public UserInfo User { get; set; } } public class VerifyTokenRequest @@ -20,7 +19,6 @@ public class VerifyTokenResponse { - public bool IsValid { get; set; } - public UserInfo UserInfo { get; set; } + public UserInfo User { get; set; } } } diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs index db3d5071..f5d233cd 100644 --- a/Timeline/Entities/Http/User.cs +++ b/Timeline/Entities/Http/User.cs @@ -17,8 +17,8 @@ public const int CreatedCode = 0; public const int ModifiedCode = 1; - public static ReturnCodeMessageResponse Created { get; } = new ReturnCodeMessageResponse(CreatedCode, "A new user is created."); - public static ReturnCodeMessageResponse Modified { get; } = new ReturnCodeMessageResponse(ModifiedCode, "A existing user is modified."); + public static CommonErrorResponse Created { get; } = new CommonErrorResponse(CreatedCode, "A new user is created."); + public static CommonErrorResponse Modified { get; } = new CommonErrorResponse(ModifiedCode, "A existing user is modified."); } public static class UserDeleteResponse @@ -26,8 +26,8 @@ public const int DeletedCode = 0; public const int NotExistsCode = 1; - public static ReturnCodeMessageResponse Deleted { get; } = new ReturnCodeMessageResponse(DeletedCode, "A existing user is deleted."); - public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "User with given name does not exists."); + public static CommonErrorResponse Deleted { get; } = new CommonErrorResponse(DeletedCode, "A existing user is deleted."); + public static CommonErrorResponse NotExists { get; } = new CommonErrorResponse(NotExistsCode, "User with given name does not exists."); } public class ChangePasswordRequest @@ -42,9 +42,9 @@ public const int BadOldPasswordCode = 1; public const int NotExistsCode = 2; - public static ReturnCodeMessageResponse Success { get; } = new ReturnCodeMessageResponse(SuccessCode, "Success to change password."); - public static ReturnCodeMessageResponse BadOldPassword { get; } = new ReturnCodeMessageResponse(BadOldPasswordCode, "Old password is wrong."); - public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "Username does not exists, please update token."); + public static CommonErrorResponse Success { get; } = new CommonErrorResponse(SuccessCode, "Success to change password."); + public static CommonErrorResponse BadOldPassword { get; } = new CommonErrorResponse(BadOldPasswordCode, "Old password is wrong."); + public static CommonErrorResponse NotExists { get; } = new CommonErrorResponse(NotExistsCode, "Username does not exists, please update token."); } public static class PutAvatarResponse @@ -53,8 +53,8 @@ public const int ForbiddenCode = 1; public const int NotExistsCode = 2; - public static ReturnCodeMessageResponse Success { get; } = new ReturnCodeMessageResponse(SuccessCode, "Success to upload avatar."); - public static ReturnCodeMessageResponse Forbidden { get; } = new ReturnCodeMessageResponse(ForbiddenCode, "You are not allowed to upload the user's avatar."); - public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "The username does not exists. If you are a user, try update your token."); + public static CommonErrorResponse Success { get; } = new CommonErrorResponse(SuccessCode, "Success to upload avatar."); + public static CommonErrorResponse Forbidden { get; } = new CommonErrorResponse(ForbiddenCode, "You are not allowed to upload the user's avatar."); + public static CommonErrorResponse NotExists { get; } = new CommonErrorResponse(NotExistsCode, "The username does not exists. If you are a user, try update your token."); } } |