aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserController.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-02-14 23:05:04 +0800
committercrupest <crupest@outlook.com>2019-02-14 23:05:04 +0800
commit3c140656ebe6ed34dda9356a01dbff205651e641 (patch)
tree8b8ca7331c9510b897042737a5cbbc0f77b1b736 /Timeline/Controllers/UserController.cs
parentde90f0413553a23f8ebba1343c6e96c63e0c9748 (diff)
downloadtimeline-3c140656ebe6ed34dda9356a01dbff205651e641.tar.gz
timeline-3c140656ebe6ed34dda9356a01dbff205651e641.tar.bz2
timeline-3c140656ebe6ed34dda9356a01dbff205651e641.zip
Develop user token interface.
Diffstat (limited to 'Timeline/Controllers/UserController.cs')
-rw-r--r--Timeline/Controllers/UserController.cs36
1 files changed, 31 insertions, 5 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index 9d6970e7..1ffed22b 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+using System.IO;
+using Timeline.Entities;
using Timeline.Services;
namespace Timeline.Controllers
@@ -20,10 +23,15 @@ namespace Timeline.Controllers
public string Password { get; set; }
}
- public class LoginInfo
+ public class CreateTokenResult
+ {
+ public string Token { get; set; }
+ public UserInfo UserInfo { get; set; }
+ }
+
+ public class TokenValidationRequest
{
public string Token { get; set; }
- public string[] Roles { get; set; }
}
private readonly IUserService _userService;
@@ -39,7 +47,7 @@ namespace Timeline.Controllers
[HttpPost("[action]")]
[AllowAnonymous]
- public ActionResult<LoginInfo> LogIn([FromBody] UserCredentials credentials)
+ public ActionResult<CreateTokenResult> CreateToken([FromBody] UserCredentials credentials)
{
var user = _userService.Authenticate(credentials.Username, credentials.Password);
@@ -50,13 +58,31 @@ namespace Timeline.Controllers
_logger.LogInformation(LoggingEventIds.LogInSucceeded, "Login with username: {} succeeded.", credentials.Username);
- var result = new LoginInfo
+ var result = new CreateTokenResult
{
Token = _jwtService.GenerateJwtToken(user),
- Roles = user.Roles
+ UserInfo = user.GetUserInfo()
};
return Ok(result);
}
+
+ [HttpPost("[action]")]
+ [Consumes("text/plain")]
+ [AllowAnonymous]
+ public ActionResult<TokenValidationResult> ValidateToken([FromBody] string token)
+ {
+ var result = _jwtService.ValidateJwtToken(token);
+ return Ok(result);
+ }
+
+ [HttpPost("[action]")]
+ [Consumes("application/json")]
+ [AllowAnonymous]
+ public ActionResult<TokenValidationResult> ValidateToken([FromBody] TokenValidationRequest request)
+ {
+ var result = _jwtService.ValidateJwtToken(request.Token);
+ return Ok(result);
+ }
}
}