예제 #1
0
 public static JwsInformationResponse ToDto(this JwsInformationResult jwsInformationResult)
 {
     return(new JwsInformationResponse
     {
         Header = jwsInformationResult.Header,
         JsonWebKey = jwsInformationResult.JsonWebKey,
         Payload = jwsInformationResult.Payload
     });
 }
예제 #2
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);
        }