From 393daddb124ab6eae7506fd7db48e8333f28ad9c Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Sun, 21 Jul 2019 22:58:27 +0800 Subject: WIP: change UserService. --- Timeline/Services/JwtService.cs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'Timeline/Services/JwtService.cs') diff --git a/Timeline/Services/JwtService.cs b/Timeline/Services/JwtService.cs index e7f5690d..b070ad62 100644 --- a/Timeline/Services/JwtService.cs +++ b/Timeline/Services/JwtService.cs @@ -11,7 +11,7 @@ namespace Timeline.Services { public class TokenInfo { - public string Name { get; set; } + public long Id { get; set; } public long Version { get; set; } } @@ -34,6 +34,7 @@ namespace Timeline.Services /// The info to generate token. /// The expire time. If null then use current time with offset in config. /// Return the generated token. + /// Thrown when is null. string GenerateJwtToken(TokenInfo tokenInfo, DateTime? expires = null); /// @@ -41,7 +42,8 @@ namespace Timeline.Services /// Return null is is null. /// /// The token string to verify. - /// Return null if is null. Return the saved info otherwise. + /// Return the saved info in token. + /// Thrown when is null. /// Thrown when the token is invalid. TokenInfo VerifyJwtToken(string token); @@ -53,25 +55,21 @@ namespace Timeline.Services private readonly IOptionsMonitor _jwtConfig; private readonly JwtSecurityTokenHandler _tokenHandler = new JwtSecurityTokenHandler(); - private readonly ILogger _logger; - public JwtService(IOptionsMonitor jwtConfig, ILogger logger) + public JwtService(IOptionsMonitor jwtConfig) { _jwtConfig = jwtConfig; - _logger = logger; } public string GenerateJwtToken(TokenInfo tokenInfo, DateTime? expires = null) { if (tokenInfo == null) throw new ArgumentNullException(nameof(tokenInfo)); - if (tokenInfo.Name == null) - throw new ArgumentException("Name of token info is null.", nameof(tokenInfo)); var config = _jwtConfig.CurrentValue; var identity = new ClaimsIdentity(); - identity.AddClaim(new Claim(identity.NameClaimType, tokenInfo.Name)); + identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, tokenInfo.Id.ToString(), ClaimValueTypes.Integer64)); identity.AddClaim(new Claim(VersionClaimType, tokenInfo.Version.ToString(), ClaimValueTypes.Integer64)); var tokenDescriptor = new SecurityTokenDescriptor() @@ -95,7 +93,7 @@ namespace Timeline.Services public TokenInfo VerifyJwtToken(string token) { if (token == null) - return null; + throw new ArgumentNullException(nameof(token)); var config = _jwtConfig.CurrentValue; try @@ -111,6 +109,12 @@ namespace Timeline.Services IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config.SigningKey)) }, out _); + var idClaim = principal.FindFirstValue(ClaimTypes.NameIdentifier); + if (idClaim == null) + throw new JwtTokenVerifyException("Id claim does not exist."); + if (!long.TryParse(idClaim, out var id)) + throw new JwtTokenVerifyException("Can't convert id claim into a integer number."); + var versionClaim = principal.FindFirstValue(VersionClaimType); if (versionClaim == null) throw new JwtTokenVerifyException("Version claim does not exist."); @@ -119,7 +123,7 @@ namespace Timeline.Services return new TokenInfo { - Name = principal.Identity.Name, + Id = id, Version = version }; } @@ -127,11 +131,6 @@ namespace Timeline.Services { throw new JwtTokenVerifyException("Validate token failed caused by a SecurityTokenException. See inner exception.", e); } - catch (ArgumentException e) // This usually means code logic error. - { - _logger.LogError(e, "Arguments passed to ValidateToken are bad."); - throw e; - } } } } -- cgit v1.2.3