aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/JwtService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-21 23:23:49 +0800
committercrupest <crupest@outlook.com>2019-04-21 23:23:49 +0800
commit0920d6ca8d8f92e612148aa1d3c4eaea5f407d94 (patch)
treee139e794df8cd20c1cf4f60c668dd1d94bf239e1 /Timeline/Services/JwtService.cs
parent748aa44ccaf88686ffbaf9e31d025be24e2d200a (diff)
downloadtimeline-0920d6ca8d8f92e612148aa1d3c4eaea5f407d94.tar.gz
timeline-0920d6ca8d8f92e612148aa1d3c4eaea5f407d94.tar.bz2
timeline-0920d6ca8d8f92e612148aa1d3c4eaea5f407d94.zip
Allow ordinary user to patch his password.
Diffstat (limited to 'Timeline/Services/JwtService.cs')
-rw-r--r--Timeline/Services/JwtService.cs22
1 files changed, 13 insertions, 9 deletions
diff --git a/Timeline/Services/JwtService.cs b/Timeline/Services/JwtService.cs
index 91e7f879..bf470354 100644
--- a/Timeline/Services/JwtService.cs
+++ b/Timeline/Services/JwtService.cs
@@ -7,25 +7,28 @@ using System.Linq;
using System.Security.Claims;
using System.Text;
using Timeline.Configs;
+using Timeline.Entities;
namespace Timeline.Services
{
public interface IJwtService
{
/// <summary>
- /// Create a JWT token for a given user id.
+ /// Create a JWT token for a given user info.
/// </summary>
- /// <param name="userId">The user id used to generate token.</param>
+ /// <param name="userId">The user id contained in generate token.</param>
+ /// <param name="username">The username contained in token.</param>
+ /// <param name="roles">The roles contained in token.</param>
/// <returns>Return the generated token.</returns>
- string GenerateJwtToken(long userId, string[] roles);
+ string GenerateJwtToken(long userId, string username, string[] roles);
/// <summary>
/// Verify a JWT token.
/// Return null is <paramref name="token"/> is null.
/// </summary>
/// <param name="token">The token string to verify.</param>
- /// <returns>Return null if <paramref name="token"/> is null or token is invalid. Return the saved user id otherwise.</returns>
- long? VerifyJwtToken(string token);
+ /// <returns>Return null if <paramref name="token"/> is null or token is invalid. Return the saved user info otherwise.</returns>
+ UserInfo VerifyJwtToken(string token);
}
@@ -41,12 +44,13 @@ namespace Timeline.Services
_logger = logger;
}
- public string GenerateJwtToken(long id, string[] roles)
+ public string GenerateJwtToken(long id, string username, string[] roles)
{
var jwtConfig = _jwtConfig.CurrentValue;
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, id.ToString()));
+ identity.AddClaim(new Claim(identity.NameClaimType, username));
identity.AddClaims(roles.Select(role => new Claim(identity.RoleClaimType, role)));
var tokenDescriptor = new SecurityTokenDescriptor()
@@ -67,13 +71,12 @@ namespace Timeline.Services
}
- public long? VerifyJwtToken(string token)
+ public UserInfo VerifyJwtToken(string token)
{
if (token == null)
return null;
var config = _jwtConfig.CurrentValue;
-
try
{
var principal = _tokenHandler.ValidateToken(token, new TokenValidationParameters
@@ -87,7 +90,8 @@ namespace Timeline.Services
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SigningKey))
}, out SecurityToken validatedToken);
- return long.Parse(principal.FindAll(ClaimTypes.NameIdentifier).Single().Value);
+ return new UserInfo(principal.Identity.Name,
+ principal.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray());
}
catch (Exception e)
{