using DigitalData.Core.Abstractions.Security.Key;
using DigitalData.Core.Security.RSAKey.Base;
using Microsoft.IdentityModel.Tokens;
namespace DigitalData.Core.Security.RSAKey.Auth;
///
/// Contains some information which used to create a security token. Designed to abstract
///
public class RSATokenDescriptor : RSAPrivateKey, IAsymmetricTokenDescriptor
{
private readonly Lazy _lazyTokenValidator;
public IAsymmetricTokenValidator Validator => _lazyTokenValidator.Value;
public required TimeSpan Lifetime { get; init; }
#region SecurityTokenDescriptor Map
///
/// Gets or sets the value of the 'audience' claim.
///
public required string Audience { get; set; }
///
/// Defines the compression algorithm that will be used to compress the JWT token payload.
///
public string CompressionAlgorithm { get; set; }
///
/// Gets or sets the used to create a encrypted security token.
///
public EncryptingCredentials EncryptingCredentials { get; set; }
///
/// Gets or sets the value of the 'expiration' claim. This value should be in UTC.
/// The expiration time is the sum of DateTime.Now and LifeTime.
///
public DateTime? Expires => DateTime.Now.AddTicks(Lifetime.Ticks);
///
/// Gets or sets the issuer of this .
///
public required string Issuer { get; set; }
///
/// Gets or sets the time the security token was issued. This value should be in UTC.
///
public DateTime? IssuedAt { get; set; }
///
/// Gets or sets the notbefore time for the security token. This value should be in UTC.
///
public DateTime? NotBefore { get; set; }
///
/// Gets or sets the token type.
/// If provided, this will be added as the value for the 'typ' header parameter. In the case of a JWE, this will be added to both the inner (JWS) and the outer token (JWE) header. By default, the value used is 'JWT'.
/// If also contains 'typ' header claim value, it will override the TokenType provided here.
/// This value is used only for JWT tokens and not for SAML/SAML2 tokens
///
public string TokenType { get; set; }
///
/// Gets or sets the which contains any custom header claims that need to be added to the JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the ,
/// , and/or provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
/// These claims are only added to the outer header (in case of a JWE).
///
public IDictionary AdditionalHeaderClaims { get; set; }
///
/// Gets or sets the which contains any custom header claims that need to be added to the inner JWT token header.
/// The 'alg', 'kid', 'x5t', 'enc', and 'zip' claims are added by default based on the ,
/// , and/or provided and SHOULD NOT be included in this dictionary as this
/// will result in an exception being thrown.
///
/// For JsonWebTokenHandler, these claims are merged with while adding to the inner JWT header.
///
///
public IDictionary AdditionalInnerHeaderClaims { get; set; }
///
/// Gets or sets the used to create a security token.
///
public SigningCredentials SigningCredentials => _lazySigningCredentials.Value;
#endregion SecurityTokenDescriptor
private readonly Lazy _lazyRsaSecurityKey;
public SecurityKey SecurityKey => _lazyRsaSecurityKey.Value;
private readonly Lazy _lazySigningCredentials;
///
/// Specifies the signature algorithm to be applied to the .
/// Default is .
///
public string SigningAlgorithm { get; init; } = SecurityAlgorithms.RsaSha256;
///
/// Optionally specifies the digest algorithm to be applied during the signing process for the .
/// If not provided, the default algorithm is used.
///
public string? SigningDigest { get; init; }
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public RSATokenDescriptor()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
_lazyTokenValidator = new(CreatePublicKey);
_lazyRsaSecurityKey = new(() => new RsaSecurityKey(RSA));
_lazySigningCredentials = new(() => SigningDigest is null
? new(SecurityKey, SigningAlgorithm)
: new(SecurityKey, SigningAlgorithm, SigningDigest));
}
}