2using System.Collections.Generic;
3using System.Globalization;
4using System.IdentityModel.Tokens.Jwt;
6using System.Security.Claims;
8using Microsoft.Extensions.Options;
9using Microsoft.IdentityModel.Tokens;
48 IOptions<SecurityConfiguration> securityConfigurationOptions)
50 ArgumentNullException.ThrowIfNull(cryptographySuite);
51 ArgumentNullException.ThrowIfNull(assemblyInformationProvider);
53 securityConfiguration = securityConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(securityConfigurationOptions));
61 ValidateIssuerSigningKey =
true,
62 IssuerSigningKey =
new SymmetricSecurityKey(signingKeyBytes),
64 ValidateIssuer =
true,
65 ValidIssuer = assemblyInformationProvider.
AssemblyName.Name,
67 ValidateLifetime =
true,
68 ValidateAudience =
true,
69 ValidAudience = typeof(
TokenResponse).Assembly.GetName().Name,
73 RequireSignedTokens =
true,
75 RequireExpirationTime =
true,
79 new SigningCredentials(
81 SecurityAlgorithms.HmacSha256));
88 ArgumentNullException.ThrowIfNull(user);
90 var uid = user.Require(x => x.Id);
91 var now = DateTimeOffset.UtcNow;
92 var nowUnix = now.ToUnixTimeSeconds();
100 DateTimeOffset notBefore;
101 if (nowUnix == userLastPassworUpdateUnix)
102 notBefore = now.AddSeconds(1);
106 var expiry = now.AddMinutes(oAuth
110 var securityToken =
new JwtSecurityToken(
115 Enumerable.Empty<Claim>(),
116 new Dictionary<string, object>
118 { JwtRegisteredClaimNames.Sub, uid.ToString(CultureInfo.InvariantCulture) },
120 notBefore.UtcDateTime,
129 return tokenResponse;
Represents a JWT returned by the API.
Configuration options pertaining to user security.
uint TokenSigningKeyByteCount
Amount of bytes to use in the Microsoft.IdentityModel.Tokens.TokenValidationParameters....
string? CustomTokenSigningKeyBase64
A custom token signing key. Overrides TokenSigningKeyByteCount.
uint TokenClockSkewMinutes
Amount of minutes to skew the clock for Api.Models.Response.TokenResponse validation.
uint OAuthTokenExpiryMinutes
Amount of minutes until Api.Models.Response.TokenResponses generated from OAuth logins expire.
uint TokenExpiryMinutes
Amount of minutes until Api.Models.Response.TokenResponses generated from passwords expire.
DateTimeOffset? LastPasswordUpdate
When PasswordHash was last changed.
TokenValidationParameters ValidationParameters
The TokenValidationParameters for the ITokenFactory.
readonly JwtHeader tokenHeader
The JwtHeader for generating tokens.
readonly JwtSecurityTokenHandler tokenHandler
The JwtSecurityTokenHandler used to generate TokenResponse.Bearer strings.
readonly SecurityConfiguration securityConfiguration
The SecurityConfiguration for the TokenFactory.
TokenFactory(ICryptographySuite cryptographySuite, IAssemblyInformationProvider assemblyInformationProvider, IOptions< SecurityConfiguration > securityConfigurationOptions)
Initializes a new instance of the TokenFactory class.
TokenResponse CreateToken(User user, bool oAuth)
Contains various cryptographic functions.
byte[] GetSecureBytes(uint amount)
Generates a secure set of bytes.
For creating TokenResponses.