예제 #1
0
        public void When_Passing_No_Kid_To_GetJsonWebKey_Then_Exception_Is_Thrown()
        {
            // ARRANGE
            InitializeFakeObjects();

            // ACT & ASSERT
            Assert.ThrowsAsync <ArgumentNullException>(() => _jsonWebKeyHelper.GetJsonWebKey(null, null)).ConfigureAwait(false);
        }
        public async Task <string> ExecuteAsync(CreateJweParameter createJweParameter)
        {
            if (createJweParameter == null)
            {
                throw new ArgumentNullException(nameof(createJweParameter));
            }

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

            if (string.IsNullOrWhiteSpace(createJweParameter.Jws))
            {
                throw new ArgumentNullException(nameof(createJweParameter.Jws));
            }

            if (string.IsNullOrWhiteSpace(createJweParameter.Kid))
            {
                throw new ArgumentNullException(nameof(createJweParameter.Kid));
            }

            Uri uri = null;

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

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

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

            var result = string.Empty;

            if (!string.IsNullOrWhiteSpace(createJweParameter.Password))
            {
                result = _jweGenerator.GenerateJweByUsingSymmetricPassword(createJweParameter.Jws,
                                                                           createJweParameter.Alg,
                                                                           createJweParameter.Enc,
                                                                           jsonWebKey,
                                                                           createJweParameter.Password);
            }
            else
            {
                result = _jweGenerator.GenerateJwe(createJweParameter.Jws,
                                                   createJweParameter.Alg,
                                                   createJweParameter.Enc,
                                                   jsonWebKey);
            }

            return(result);
        }
예제 #3
0
        public async Task <string> Execute(CreateJwsParameter createJwsParameter)
        {
            if (createJwsParameter == null)
            {
                throw new ArgumentNullException(nameof(createJwsParameter));
            }

            if (createJwsParameter.Payload == null ||
                !createJwsParameter.Payload.Any())
            {
                throw new ArgumentNullException(nameof(createJwsParameter.Payload));
            }

            if (createJwsParameter.Alg != JwsAlg.none &&
                (string.IsNullOrWhiteSpace(createJwsParameter.Kid) || string.IsNullOrWhiteSpace(createJwsParameter.Url)))
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidRequestCode,
                                                         ErrorDescriptions.TheJwsCannotBeGeneratedBecauseMissingParameters);
            }

            Uri uri = null;

            if (createJwsParameter.Alg != JwsAlg.none && !Uri.TryCreate(createJwsParameter.Url, UriKind.Absolute, out uri))
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidRequestCode,
                                                         ErrorDescriptions.TheUrlIsNotWellFormed);
            }

            JsonWebKey jsonWebKey = null;

            if (createJwsParameter.Alg != JwsAlg.none)
            {
                jsonWebKey = await _jsonWebKeyHelper.GetJsonWebKey(createJwsParameter.Kid, uri).ConfigureAwait(false);

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

            return(_jwsGenerator.Generate(createJwsParameter.Payload,
                                          createJwsParameter.Alg,
                                          jsonWebKey));
        }
예제 #4
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);
        }
예제 #5
0
        public async Task <JwsInformationResult> Execute(GetJwsParameter getJwsParameter)
        {
            if (getJwsParameter == null || string.IsNullOrWhiteSpace(getJwsParameter.Jws))
            {
                throw new ArgumentNullException(nameof(getJwsParameter));
            }

            Uri uri = null;

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

            var jws       = getJwsParameter.Jws;
            var jwsHeader = _jwsParser.GetHeader(jws);

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

            if (!string.Equals(jwsHeader.Alg, Constants.JwsAlgNames.NONE, StringComparison.CurrentCultureIgnoreCase) &&
                uri == null)
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidRequestCode,
                          ErrorDescriptions.TheSignatureCannotBeChecked);
            }

            var result = new JwsInformationResult
            {
                Header = jwsHeader
            };

            JwsPayload payload = null;

            if (!string.Equals(jwsHeader.Alg, Constants.JwsAlgNames.NONE, StringComparison.CurrentCultureIgnoreCase))
            {
                var jsonWebKey = await _jsonWebKeyHelper.GetJsonWebKey(jwsHeader.Kid, uri).ConfigureAwait(false);

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

                payload = _jwsParser.ValidateSignature(jws, jsonWebKey);
                if (payload == null)
                {
                    throw new IdentityServerManagerException(
                              ErrorCodes.InvalidRequestCode,
                              ErrorDescriptions.TheSignatureIsNotCorrect);
                }

                var jsonWebKeyDic = _jsonWebKeyEnricher.GetJsonWebKeyInformation(jsonWebKey);
                jsonWebKeyDic.AddRange(_jsonWebKeyEnricher.GetPublicKeyInformation(jsonWebKey));
                result.JsonWebKey = jsonWebKeyDic;
            }
            else
            {
                payload = _jwsParser.GetPayload(jws);
            }


            result.Payload = payload;
            return(result);
        }