public async Task When_JsonWebKey_Cannot_Be_Extracted_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            var jweProtectedHeader = new JweProtectedHeader
            {
                Kid = "kid"
            };
            var getJweParameter = new GetJweParameter
            {
                Jwe = "jwe",
                Url = "http://google.be/"
            };

            _jweParserStub.Setup(j => j.GetHeader(It.IsAny <string>()))
            .Returns(jweProtectedHeader);
            _jsonWebKeyHelperStub.Setup(j => j.GetJsonWebKey(It.IsAny <string>(), It.IsAny <Uri>()))
            .Returns(Task.FromResult <JsonWebKey>(null));

            // ACT & ASSERTS
            var exception = await Assert.ThrowsAsync <IdentityServerManagerException>(async() => await _getJweInformationAction.ExecuteAsync(getJweParameter)).ConfigureAwait(false);

            Assert.True(exception.Code == ErrorCodes.InvalidRequestCode);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.TheJsonWebKeyCannotBeFound, jweProtectedHeader.Kid, getJweParameter.Url));
        }
        public async Task When_Decrypting_Jwe_With_Symmetric_Key_Then_Result_Is_Returned()
        {
            // ARRANGE
            InitializeFakeObjects();
            const string content            = "jws";
            var          jweProtectedHeader = new JweProtectedHeader
            {
                Kid = "kid"
            };
            var jwsProtectedHeader = new JwsProtectedHeader();
            var jsonWebKey         = new JsonWebKey();
            var getJweParameter    = new GetJweParameter
            {
                Jwe      = "jwe",
                Url      = "http://google.be/",
                Password = "******"
            };

            _jweParserStub.Setup(j => j.GetHeader(It.IsAny <string>()))
            .Returns(jweProtectedHeader);
            _jsonWebKeyHelperStub.Setup(j => j.GetJsonWebKey(It.IsAny <string>(), It.IsAny <Uri>()))
            .Returns(Task.FromResult <JsonWebKey>(jsonWebKey));
            _jweParserStub.Setup(j => j.ParseByUsingSymmetricPassword(It.IsAny <string>(), It.IsAny <JsonWebKey>(), It.IsAny <string>()))
            .Returns(content);
            _jwsParserStub.Setup(j => j.GetHeader(It.IsAny <string>()))
            .Returns(jwsProtectedHeader);

            // ACT & ASSERTS
            var result = await _getJweInformationAction.ExecuteAsync(getJweParameter);

            Assert.True(result.IsContentJws);
            Assert.True(result.Content == content);
        }
        public Task <JweInformationResult> GetJweInformation(GetJweParameter getJweParameter)
        {
            if (getJweParameter == null)
            {
                throw new ArgumentNullException(nameof(getJweParameter));
            }

            return(_getJweInformationAction.ExecuteAsync(getJweParameter));
        }
        public void When_Passing_Null_Parameter_Then_Exception_Are_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            var getJweParameter        = new GetJweParameter();
            var getJweParameterWithJwe = new GetJweParameter
            {
                Jwe = "jwe"
            };

            // ACT & ASSERTS
            Assert.ThrowsAsync <ArgumentNullException>(() => _getJweInformationAction.ExecuteAsync(null)).ConfigureAwait(false);
            Assert.ThrowsAsync <ArgumentNullException>(() => _getJweInformationAction.ExecuteAsync(getJweParameter)).ConfigureAwait(false);
            Assert.ThrowsAsync <ArgumentNullException>(() => _getJweInformationAction.ExecuteAsync(getJweParameterWithJwe)).ConfigureAwait(false);
        }
        public async Task When_Url_Is_Not_Well_Formed_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            const string url             = "not_well_formed";
            var          getJweParameter = new GetJweParameter
            {
                Jwe = "jwe",
                Url = url
            };

            // ACT & ASSERTS
            var exception = await Assert.ThrowsAsync <IdentityServerManagerException>(async() => await _getJweInformationAction.ExecuteAsync(getJweParameter)).ConfigureAwait(false);

            Assert.True(exception.Code == ErrorCodes.InvalidRequestCode);
            Assert.True(exception.Message == string.Format(ErrorDescriptions.TheUrlIsNotWellFormed, url));
        }
        public async Task When_Header_Is_Not_Correct_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();
            var getJweParameter = new GetJweParameter
            {
                Jwe = "jwe",
                Url = "http://google.be"
            };

            _jweParserStub.Setup(j => j.GetHeader(It.IsAny <string>()))
            .Returns(() => null);

            // ACT & ASSERTS
            var exception = await Assert.ThrowsAsync <IdentityServerManagerException>(async() => await _getJweInformationAction.ExecuteAsync(getJweParameter)).ConfigureAwait(false);

            Assert.True(exception.Code == ErrorCodes.InvalidRequestCode);
            Assert.True(exception.Message == ErrorDescriptions.TheTokenIsNotAValidJwe);
        }
Esempio n. 7
0
        public async Task <JweInformationResult> ExecuteAsync(GetJweParameter getJweParameter)
        {
            if (getJweParameter == null)
            {
                throw new ArgumentNullException(nameof(getJweParameter));
            }

            if (string.IsNullOrWhiteSpace(getJweParameter.Jwe))
            {
                throw new ArgumentNullException(nameof(getJweParameter.Jwe));
            }

            if (string.IsNullOrWhiteSpace(getJweParameter.Url))
            {
                throw new ArgumentNullException(nameof(getJweParameter.Url));
            }

            Uri uri = null;

            if (!Uri.TryCreate(getJweParameter.Url, UriKind.Absolute, out uri))
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidRequestCode,
                          string.Format(ErrorDescriptions.TheUrlIsNotWellFormed, getJweParameter.Url));
            }

            var jwe       = getJweParameter.Jwe;
            var jweHeader = _jweParser.GetHeader(jwe);

            if (jweHeader == null)
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidRequestCode,
                          ErrorDescriptions.TheTokenIsNotAValidJwe);
            }

            var jsonWebKey = await _jsonWebKeyHelper.GetJsonWebKey(jweHeader.Kid, uri).ConfigureAwait(false);

            if (jsonWebKey == null)
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidRequestCode,
                          string.Format(ErrorDescriptions.TheJsonWebKeyCannotBeFound, jweHeader.Kid, uri.AbsoluteUri));
            }

            var content = string.Empty;

            if (!string.IsNullOrWhiteSpace(getJweParameter.Password))
            {
                content = _jweParser.ParseByUsingSymmetricPassword(jwe, jsonWebKey, getJweParameter.Password);
            }
            else
            {
                content = _jweParser.Parse(jwe, jsonWebKey);
            }

            if (string.IsNullOrWhiteSpace(content))
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidRequestCode,
                          ErrorDescriptions.TheContentCannotBeExtractedFromJweToken);
            }

            var result = new JweInformationResult
            {
                Content      = content,
                IsContentJws = false
            };

            var jwsHeader = _jwsParser.GetHeader(content);

            if (jwsHeader != null)
            {
                result.IsContentJws = true;
            }

            return(result);
        }