Example #1
0
        private JwtDescriptor BuildJwe(Jwk encryptionKey)
        {
            var alg = encryptionKey.KeyManagementAlgorithm ?? _keyManagementAlgorithm;

            if (alg is null)
            {
                throw new InvalidOperationException($"No algorithm is defined for the key management encryption. Set the 'KeyManagementAlgorithm' property on the encryption key, or specify a '{nameof(KeyManagementAlgorithm)}' to the '{nameof(EncryptWith)}' method.");
            }

            if (_binaryPayload != null)
            {
                var jwe = new BinaryJweDescriptor(_header, _binaryPayload)
                {
                    EncryptionKey       = encryptionKey,
                    EncryptionAlgorithm = _encryptionAlgorithm,
                    Algorithm           = _keyManagementAlgorithm
                };
                return(jwe);
            }
            else if (_textPayload != null)
            {
                var jwe = new PlaintextJweDescriptor(_header, _textPayload)
                {
                    EncryptionKey       = encryptionKey,
                    EncryptionAlgorithm = _encryptionAlgorithm,
                    Algorithm           = _keyManagementAlgorithm
                };
                return(jwe);
            }
            else if (_jsonPayload != null)
            {
                JwsDescriptor jws = CreateJws(new JwtObject(3));

                var jwe = new JweDescriptor(_header, jws)
                {
                    EncryptionKey       = encryptionKey,
                    EncryptionAlgorithm = _encryptionAlgorithm,
                    Algorithm           = _keyManagementAlgorithm
                };

                return(jwe);
            }
            else
            {
                throw new InvalidOperationException("Not JSON, plaintext or binary payload is defined.");
            }
        }
Example #2
0
        private JwtDescriptor BuilJws()
        {
            if (_binaryPayload != null)
            {
                throw new InvalidOperationException($"A binary payload is defined, but not encryption key is set. Add to the call chain the method '{nameof(EncryptWith)}' with valid JWK, encryption algorithm & key management algorithm.");
            }

            if (_textPayload != null)
            {
                throw new InvalidOperationException($"A plaintext payload is defined, but not encryption key is set. Add to the call chain the method '{nameof(EncryptWith)}' with valid JWK, encryption algorithm & key management algorithm.");
            }

            if (_jsonPayload is null)
            {
                throw new InvalidOperationException("No JSON payload defined.");
            }

            JwsDescriptor jws = CreateJws(_header);

            return(jws);
        }