diff options
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/TokenController.cs | 38 | ||||
-rw-r--r-- | Timeline/Controllers/UserController.cs | 31 | ||||
-rw-r--r-- | Timeline/Controllers/UserDetailController.cs | 96 |
3 files changed, 36 insertions, 129 deletions
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index cf32a562..4e32d26f 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -9,6 +9,7 @@ using Timeline.Services; using Timeline.Helpers;
using Microsoft.Extensions.Localization;
using System.Globalization;
+using static Timeline.Resources.Controllers.TokenController;
namespace Timeline
{
@@ -60,7 +61,7 @@ namespace Timeline.Controllers {
void LogFailure(string reason, Exception? e = null)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogCreateFailure"],
+ _logger.LogInformation(e, Log.Format(LogCreateFailure,
("Reason", reason),
("Username", request.Username),
("Password", request.Password),
@@ -76,7 +77,7 @@ namespace Timeline.Controllers var result = await _userService.CreateToken(request.Username, request.Password, expireTime);
- _logger.LogInformation(Log.Format(_localizer["LogCreateSuccess"],
+ _logger.LogInformation(Log.Format(LogCreateSuccess,
("Username", request.Username),
("Expire At", expireTime?.ToString(CultureInfo.CurrentUICulture.DateTimeFormat) ?? "default")
));
@@ -88,13 +89,13 @@ namespace Timeline.Controllers }
catch (UserNotExistException e)
{
- LogFailure(_localizer["LogUserNotExist"], e);
+ LogFailure(LogUserNotExist, e);
return BadRequest(new CommonResponse(ErrorCodes.Http.Token.Create.BadCredential,
_localizer["ErrorBadCredential"]));
}
catch (BadPasswordException e)
{
- LogFailure(_localizer["LogBadPassword"], e);
+ LogFailure(LogBadPassword, e);
return BadRequest(new CommonResponse(ErrorCodes.Http.Token.Create.BadCredential,
_localizer["ErrorBadCredential"]));
}
@@ -110,49 +111,50 @@ namespace Timeline.Controllers properties[0] = ("Reason", reason);
properties[1] = ("Token", request.Token);
otherProperties.CopyTo(properties, 2);
- _logger.LogInformation(e, Log.Format(_localizer["LogVerifyFailure"], properties));
+ _logger.LogInformation(e, Log.Format(LogVerifyFailure, properties));
}
try
{
var result = await _userService.VerifyToken(request.Token);
- _logger.LogInformation(Log.Format(_localizer["LogVerifySuccess"],
+ _logger.LogInformation(Log.Format(LogVerifySuccess,
("Username", result.Username), ("Token", request.Token)));
return Ok(new VerifyTokenResponse
{
User = result
});
}
- catch (JwtTokenVerifyException e)
+ catch (JwtVerifyException e)
{
- if (e.ErrorCode == JwtTokenVerifyException.ErrorCodes.Expired)
+ if (e.ErrorCode == JwtVerifyException.ErrorCodes.Expired)
{
var innerException = e.InnerException as SecurityTokenExpiredException;
- LogFailure(_localizer["LogVerifyExpire"], e, ("Expires", innerException?.Expires),
+ LogFailure(LogVerifyExpire, e, ("Expires", innerException?.Expires),
("Current Time", _clock.GetCurrentTime()));
return BadRequest(new CommonResponse(
ErrorCodes.Http.Token.Verify.Expired, _localizer["ErrorVerifyExpire"]));
}
+ else if (e.ErrorCode == JwtVerifyException.ErrorCodes.OldVersion)
+ {
+ var innerException = e.InnerException as JwtBadVersionException;
+ LogFailure(LogVerifyOldVersion, e,
+ ("Token Version", innerException?.TokenVersion), ("Required Version", innerException?.RequiredVersion));
+ return BadRequest(new CommonResponse(
+ ErrorCodes.Http.Token.Verify.OldVersion, _localizer["ErrorVerifyOldVersion"]));
+ }
else
{
- LogFailure(_localizer["LogVerifyBadFormat"], e);
+ LogFailure(LogVerifyBadFormat, e);
return BadRequest(new CommonResponse(
ErrorCodes.Http.Token.Verify.BadFormat, _localizer["ErrorVerifyBadFormat"]));
}
}
catch (UserNotExistException e)
{
- LogFailure(_localizer["LogVerifyUserNotExist"], e);
+ LogFailure(LogVerifyUserNotExist, e);
return BadRequest(new CommonResponse(
ErrorCodes.Http.Token.Verify.UserNotExist, _localizer["ErrorVerifyUserNotExist"]));
}
- catch (BadTokenVersionException e)
- {
- LogFailure(_localizer["LogVerifyOldVersion"], e,
- ("Token Version", e.TokenVersion), ("Required Version", e.RequiredVersion));
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Verify.OldVersion, _localizer["ErrorVerifyOldVersion"]));
- }
}
}
}
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 6afc890c..b8d1d659 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -8,6 +8,7 @@ using Timeline.Helpers; using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Services;
+using static Timeline.Resources.Controllers.UserController;
namespace Timeline
{
@@ -82,7 +83,7 @@ namespace Timeline.Controllers var user = await _userService.GetUser(username);
if (user == null)
{
- _logger.LogInformation(Log.Format(_localizer["LogGetUserNotExist"], ("Username", username)));
+ _logger.LogInformation(Log.Format(LogGetUserNotExist, ("Username", username)));
return NotFound(new CommonResponse(ErrorCodes.Http.User.Get.NotExist, _localizer["ErrorGetUserNotExist"]));
}
return Ok(user);
@@ -96,11 +97,11 @@ namespace Timeline.Controllers var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value);
switch (result)
{
- case PutResult.Created:
- _logger.LogInformation(Log.Format(_localizer["LogPutCreate"], ("Username", username)));
+ case PutResult.Create:
+ _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username)));
return CreatedAtAction("Get", new { username }, CommonPutResponse.Create(_localizerFactory));
- case PutResult.Modified:
- _logger.LogInformation(Log.Format(_localizer["LogPutModify"], ("Username", username)));
+ case PutResult.Modify:
+ _logger.LogInformation(Log.Format(LogPutModify, ("Username", username)));
return Ok(CommonPutResponse.Modify(_localizerFactory));
default:
throw new InvalidBranchException();
@@ -108,7 +109,7 @@ namespace Timeline.Controllers }
catch (UsernameBadFormatException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogPutBadUsername"], ("Username", username)));
+ _logger.LogInformation(e, Log.Format(LogPutBadUsername, ("Username", username)));
return BadRequest(new CommonResponse(ErrorCodes.Http.User.Put.BadUsername, _localizer["ErrorPutBadUsername"]));
}
}
@@ -123,7 +124,7 @@ namespace Timeline.Controllers }
catch (UserNotExistException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogPatchUserNotExist"], ("Username", username)));
+ _logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username)));
return NotFound(new CommonResponse(ErrorCodes.Http.User.Patch.NotExist, _localizer["ErrorPatchUserNotExist"]));
}
}
@@ -134,12 +135,12 @@ namespace Timeline.Controllers try
{
await _userService.DeleteUser(username);
- _logger.LogInformation(Log.Format(_localizer["LogDeleteDelete"], ("Username", username)));
+ _logger.LogInformation(Log.Format(LogDeleteDelete, ("Username", username)));
return Ok(CommonDeleteResponse.Delete(_localizerFactory));
}
catch (UserNotExistException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogDeleteUserNotExist"], ("Username", username)));
+ _logger.LogInformation(e, Log.Format(LogDeleteNotExist, ("Username", username)));
return Ok(CommonDeleteResponse.NotExist(_localizerFactory));
}
}
@@ -150,19 +151,19 @@ namespace Timeline.Controllers try
{
await _userService.ChangeUsername(request.OldUsername, request.NewUsername);
- _logger.LogInformation(Log.Format(_localizer["LogChangeUsernameSuccess"],
+ _logger.LogInformation(Log.Format(LogChangeUsernameSuccess,
("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
return Ok();
}
catch (UserNotExistException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogChangeUsernameNotExist"],
+ _logger.LogInformation(e, Log.Format(LogChangeUsernameNotExist,
("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.NotExist, _localizer["ErrorChangeUsernameNotExist", request.OldUsername]));
}
- catch (UserAlreadyExistException e)
+ catch (UsernameConfictException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogChangeUsernameAlreadyExist"],
+ _logger.LogInformation(e, Log.Format(LogChangeUsernameAlreadyExist,
("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.AlreadyExist, _localizer["ErrorChangeUsernameAlreadyExist"]));
}
@@ -175,12 +176,12 @@ namespace Timeline.Controllers try
{
await _userService.ChangePassword(User.Identity.Name!, request.OldPassword, request.NewPassword);
- _logger.LogInformation(Log.Format(_localizer["LogChangePasswordSuccess"], ("Username", User.Identity.Name)));
+ _logger.LogInformation(Log.Format(LogChangePasswordSuccess, ("Username", User.Identity.Name)));
return Ok();
}
catch (BadPasswordException e)
{
- _logger.LogInformation(e, Log.Format(_localizer["LogChangePasswordBadPassword"],
+ _logger.LogInformation(e, Log.Format(LogChangePasswordBadPassword,
("Username", User.Identity.Name), ("Old Password", request.OldPassword)));
return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangePassword.BadOldPassword,
_localizer["ErrorChangePasswordBadPassword"]));
diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs deleted file mode 100644 index 5e1183c1..00000000 --- a/Timeline/Controllers/UserDetailController.cs +++ /dev/null @@ -1,96 +0,0 @@ -using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using Microsoft.Extensions.Logging;
-using System.Threading.Tasks;
-using Timeline.Authenticate;
-using Timeline.Models;
-using Timeline.Models.Http;
-using Timeline.Services;
-
-namespace Timeline.Controllers
-{
- [Route("users/{username}")]
- [ProducesErrorResponseType(typeof(CommonResponse))]
- [ApiController]
- public class UserDetailController : Controller
- {
- public static class ErrorCodes
- {
- public const int Get_UserNotExist = -1001;
-
- public const int Patch_Forbid = -2001;
- public const int Patch_UserNotExist = -2002;
-
- public const int GetNickname_UserNotExist = -3001;
- }
-
- private readonly ILogger<UserDetailController> _logger;
- private readonly IUserDetailService _service;
-
- public UserDetailController(ILogger<UserDetailController> logger, IUserDetailService service)
- {
- _logger = logger;
- _service = service;
- }
-
- [HttpGet("nickname")]
- [UserAuthorize]
- [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<IActionResult> GetNickname([FromRoute] string username)
- {
- try
- {
- var nickname = await _service.GetUserNickname(username);
- return Ok(new UserDetail
- {
- Nickname = nickname
- });
- }
- catch (UserNotExistException)
- {
- return NotFound(new CommonResponse(ErrorCodes.GetNickname_UserNotExist, "The user does not exist."));
- }
- }
-
- [HttpGet("details")]
- [UserAuthorize]
- [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<IActionResult> Get([FromRoute] string username)
- {
- try
- {
- var detail = await _service.GetUserDetail(username);
- return Ok(detail);
- }
- catch (UserNotExistException)
- {
- return NotFound(new CommonResponse(ErrorCodes.Get_UserNotExist, "The user does not exist."));
- }
- }
-
- [HttpPatch("details")]
- [Authorize]
- [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(void))]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [ProducesResponseType(StatusCodes.Status403Forbidden)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<IActionResult> Patch([FromRoute] string username, [FromBody] UserDetail detail)
- {
- if (!User.IsAdmin() && User.Identity.Name != username)
- return StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Patch_Forbid, "You can't change other's details unless you are admin."));
-
- try
- {
- await _service.UpdateUserDetail(username, detail);
- return Ok();
- }
- catch (UserNotExistException)
- {
- return NotFound(new CommonResponse(ErrorCodes.Patch_UserNotExist, "The user does not exist."));
- }
- }
- }
-}
|