/// <summary>Initializes a new instance of <see cref="JwsDescriptor"/>.</summary> /// <param name="signingKey">The signing key.</param> /// <param name="alg">The signature algorithm.</param> /// <param name="typ">Optional. The media type.</param> /// <param name="cty">Optional. The content type.</param> public JwsDescriptor(Jwk signingKey, SignatureAlgorithm alg, string?typ = null, string?cty = null) { _alg = alg ?? throw new ArgumentNullException(nameof(alg)); _signingKey = signingKey ?? throw new ArgumentNullException(nameof(signingKey)); _payload = new JwtPayload(); var kid = signingKey.Kid; if (!kid.EncodedUtf8Bytes.IsEmpty) { _kid = kid; if (typ != null) { _typ = typ; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid), new JwtMember(JwtHeaderParameterNames.Typ, typ)); } else { Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid)); } } else { if (typ != null) { _typ = typ; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Typ, typ)); } else { Header.Add(JwtHeaderParameterNames.Alg, alg.Name); } } if (cty != null) { Header.Add(JwtHeaderParameterNames.Cty, cty); } }
/// <summary>Initializes a new instance of the <see cref="JweDescriptor{TPayload}"/> class.</summary> protected JweDescriptor(Jwk encryptionKey, KeyManagementAlgorithm alg, EncryptionAlgorithm enc, CompressionAlgorithm?zip = null, string?typ = null, string?cty = null) { _encryptionKey = encryptionKey ?? throw new ArgumentNullException(nameof(encryptionKey)); _alg = alg ?? throw new ArgumentNullException(nameof(alg)); _enc = enc ?? throw new ArgumentNullException(nameof(enc)); _zip = zip ?? CompressionAlgorithm.NoCompression; var kid = encryptionKey.Kid; if (!kid.EncodedUtf8Bytes.IsEmpty) { _kid = kid; if (zip != null) { if (cty != null) { _cty = cty; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid), new JwtMember(JwtHeaderParameterNames.Zip, zip.Name), new JwtMember(JwtHeaderParameterNames.Cty, cty)); } else { Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid), new JwtMember(JwtHeaderParameterNames.Zip, zip.Name)); } } else { if (cty != null) { _cty = cty; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid), new JwtMember(JwtHeaderParameterNames.Cty, cty)); } else { Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Kid, kid)); } } } else { if (zip != null) { if (cty != null) { _cty = cty; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Zip, zip.Name), new JwtMember(JwtHeaderParameterNames.Cty, cty)); } else { Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Zip, zip.Name)); } } else { if (cty != null) { _cty = cty; Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name), new JwtMember(JwtHeaderParameterNames.Cty, cty)); } else { Header.FastAdd( new JwtMember(JwtHeaderParameterNames.Alg, alg.Name), new JwtMember(JwtHeaderParameterNames.Enc, enc.Name)); } } } if (typ != null) { _typ = typ; Header.Add(JwtHeaderParameterNames.Typ, typ); } }