|
| 1 | + |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IdentityModel.Tokens.Jwt; |
| 5 | +using System.Security.Claims; |
| 6 | +using System.Security.Principal; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using AngularASPNETCore2WebApiAuth.Models; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | + |
| 11 | + |
| 12 | +namespace AngularASPNETCore2WebApiAuth.Auth |
| 13 | +{ |
| 14 | + public class JwtFactory : IJwtFactory |
| 15 | + { |
| 16 | + private readonly JwtIssuerOptions _jwtOptions; |
| 17 | + |
| 18 | + public JwtFactory(IOptions<JwtIssuerOptions> jwtOptions) |
| 19 | + { |
| 20 | + _jwtOptions = jwtOptions.Value; |
| 21 | + ThrowIfInvalidOptions(_jwtOptions); |
| 22 | + } |
| 23 | + |
| 24 | + public async Task<string> GenerateEncodedToken(string userName, ClaimsIdentity identity) |
| 25 | + { |
| 26 | + var claims = new[] |
| 27 | + { |
| 28 | + new Claim(JwtRegisteredClaimNames.Sub, userName), |
| 29 | + new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()), |
| 30 | + new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64), |
| 31 | + identity.FindFirst(Helpers.Constants.Strings.JwtClaimIdentifiers.Rol), |
| 32 | + identity.FindFirst(Helpers.Constants.Strings.JwtClaimIdentifiers.Id) |
| 33 | + }; |
| 34 | + |
| 35 | + // Create the JWT security token and encode it. |
| 36 | + var jwt = new JwtSecurityToken( |
| 37 | + issuer: _jwtOptions.Issuer, |
| 38 | + audience: _jwtOptions.Audience, |
| 39 | + claims: claims, |
| 40 | + notBefore: _jwtOptions.NotBefore, |
| 41 | + expires: _jwtOptions.Expiration, |
| 42 | + signingCredentials: _jwtOptions.SigningCredentials); |
| 43 | + |
| 44 | + var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); |
| 45 | + |
| 46 | + return encodedJwt; |
| 47 | + } |
| 48 | + |
| 49 | + public ClaimsIdentity GenerateClaimsIdentity(string userName, string id) |
| 50 | + { |
| 51 | + return new ClaimsIdentity(new GenericIdentity(userName, "Token"), new[] |
| 52 | + { |
| 53 | + new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Id, id), |
| 54 | + new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Rol, Helpers.Constants.Strings.JwtClaims.ApiAccess) |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /// <returns>Date converted to seconds since Unix epoch (Jan 1, 1970, midnight UTC).</returns> |
| 59 | + private static long ToUnixEpochDate(DateTime date) |
| 60 | + => (long)Math.Round((date.ToUniversalTime() - |
| 61 | + new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)) |
| 62 | + .TotalSeconds); |
| 63 | + |
| 64 | + private static void ThrowIfInvalidOptions(JwtIssuerOptions options) |
| 65 | + { |
| 66 | + if (options == null) throw new ArgumentNullException(nameof(options)); |
| 67 | + |
| 68 | + if (options.ValidFor <= TimeSpan.Zero) |
| 69 | + { |
| 70 | + throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(JwtIssuerOptions.ValidFor)); |
| 71 | + } |
| 72 | + |
| 73 | + if (options.SigningCredentials == null) |
| 74 | + { |
| 75 | + throw new ArgumentNullException(nameof(JwtIssuerOptions.SigningCredentials)); |
| 76 | + } |
| 77 | + |
| 78 | + if (options.JtiGenerator == null) |
| 79 | + { |
| 80 | + throw new ArgumentNullException(nameof(JwtIssuerOptions.JtiGenerator)); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments