From 3de4179449a209646e0e5a967d270f7fa0878c03 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 16 May 2019 21:57:56 +0800 Subject: Change roles in UserInfo into isadmin. --- Timeline/Services/JwtService.cs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'Timeline/Services/JwtService.cs') diff --git a/Timeline/Services/JwtService.cs b/Timeline/Services/JwtService.cs index bf470354..f5df59a5 100644 --- a/Timeline/Services/JwtService.cs +++ b/Timeline/Services/JwtService.cs @@ -11,24 +11,28 @@ using Timeline.Entities; namespace Timeline.Services { + public class TokenInfo + { + public string Name { get; set; } + public string[] Roles { get; set; } + } + public interface IJwtService { /// /// Create a JWT token for a given user info. /// - /// The user id contained in generate token. - /// The username contained in token. - /// The roles contained in token. + /// The info to generate token. /// Return the generated token. - string GenerateJwtToken(long userId, string username, string[] roles); + string GenerateJwtToken(TokenInfo tokenInfo); /// /// Verify a JWT token. /// Return null is is null. /// /// The token string to verify. - /// Return null if is null or token is invalid. Return the saved user info otherwise. - UserInfo VerifyJwtToken(string token); + /// Return null if is null or token is invalid. Return the saved info otherwise. + TokenInfo VerifyJwtToken(string token); } @@ -44,14 +48,20 @@ namespace Timeline.Services _logger = logger; } - public string GenerateJwtToken(long id, string username, string[] roles) + public string GenerateJwtToken(TokenInfo tokenInfo) { + if (tokenInfo == null) + throw new ArgumentNullException(nameof(tokenInfo)); + if (tokenInfo.Name == null) + throw new ArgumentException("Name is null.", nameof(tokenInfo)); + if (tokenInfo.Roles == null) + throw new ArgumentException("Roles is null.", nameof(tokenInfo)); + 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))); + identity.AddClaim(new Claim(identity.NameClaimType, tokenInfo.Name)); + identity.AddClaims(tokenInfo.Roles.Select(role => new Claim(identity.RoleClaimType, role))); var tokenDescriptor = new SecurityTokenDescriptor() { @@ -71,7 +81,7 @@ namespace Timeline.Services } - public UserInfo VerifyJwtToken(string token) + public TokenInfo VerifyJwtToken(string token) { if (token == null) return null; @@ -90,8 +100,11 @@ namespace Timeline.Services IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SigningKey)) }, out SecurityToken validatedToken); - return new UserInfo(principal.Identity.Name, - principal.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray()); + return new TokenInfo + { + Name = principal.Identity.Name, + Roles = principal.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray() + }; } catch (Exception e) { -- cgit v1.2.3