예제 #1
0
            public async Task CalculatesSignatureForExpectedRequestForSigning()
            {
                var composedString = "{the composed string}";
                HttpRequestForSigning interceptedRequest = null;

                A.CallTo(() => _signingStringComposer.Compose(
                             A <HttpRequestForSigning> ._,
                             _settings.SignatureAlgorithm.Name,
                             _settings.Headers,
                             _timeOfSigning,
                             _settings.Expires,
                             _nonce))
                .Invokes(call => interceptedRequest = call.GetArgument <HttpRequestForSigning>(0))
                .Returns(composedString);

                await _sut.CreateSignature(_httpRequestMessage, _settings, _timeOfSigning);

                interceptedRequest.Should().BeEquivalentTo(new HttpRequestForSigning {
                    Method     = HttpMethod.Post,
                    RequestUri = "/api/resource/id1",
                    Headers    = new HeaderDictionary(new Dictionary <string, StringValues> {
                        { "H1", "v1" }
                    })
                });
            }
예제 #2
0
            public async Task CalculatesSignatureForExpectedRequest()
            {
                var compositionRequest = new SigningStringCompositionRequest();
                HttpRequestForSigning interceptedRequest         = null;
                SigningSettings       interceptedSigningSettings = null;
                DateTimeOffset?       interceptedTimeOfSigning   = null;
                TimeSpan?interceptedExpires = null;

                A.CallTo(() => _stringCompositionRequestFactory.CreateForSigning(A <HttpRequestForSigning> ._, A <SigningSettings> ._, A <DateTimeOffset?> ._, A <TimeSpan?> ._))
                .Invokes(call => {
                    interceptedRequest         = call.GetArgument <HttpRequestForSigning>(0);
                    interceptedSigningSettings = call.GetArgument <SigningSettings>(1);
                    interceptedTimeOfSigning   = call.GetArgument <DateTimeOffset?>(2);
                    interceptedExpires         = call.GetArgument <TimeSpan?>(3);
                })
                .Returns(compositionRequest);

                var composedString = "{the composed string}";

                A.CallTo(() => _signingStringComposer.Compose(A <SigningStringCompositionRequest> ._))
                .Returns(composedString);

                await _sut.CreateSignature(_httpRequestMessage, _settings, _timeOfSigning, _expires);

                interceptedRequest.Should().BeEquivalentTo(new HttpRequestForSigning {
                    Method     = HttpMethod.Post,
                    RequestUri = "http://dalion.eu/api/resource/id1".ToUri(),
                    Headers    = new HeaderDictionary(new Dictionary <string, StringValues> {
                        { "H1", "v1" }
                    })
                });
                interceptedSigningSettings.Should().Be(_settings);
                interceptedTimeOfSigning.Should().Be(_timeOfSigning);
                interceptedExpires.Should().Be(_expires);
            }
        internal static HttpRequestForSigning ToHttpRequestForSigning(this HttpRequestMessage httpRequestMessage)
        {
            if (httpRequestMessage == null)
            {
                return(null);
            }

            var requestForSigning = new HttpRequestForSigning {
                Method     = httpRequestMessage.Method,
                RequestUri = httpRequestMessage.RequestUri
            };

            foreach (var header in httpRequestMessage.Headers)
            {
                requestForSigning.Headers[header.Key] = new StringValues(header.Value.ToArray());
            }

            if (httpRequestMessage.Content?.Headers?.Any() ?? false)
            {
                foreach (var contentHeader in httpRequestMessage.Content.Headers)
                {
                    requestForSigning.Headers[contentHeader.Key] = new StringValues(contentHeader.Value.ToArray());
                }
            }

            return(requestForSigning);
        }
        public SigningStringCompositionRequest CreateForSigning(
            HttpRequestForSigning request,
            SigningSettings signingSettings,
            DateTimeOffset?timeOfComposing,
            TimeSpan?expires)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (signingSettings == null)
            {
                throw new ArgumentNullException(nameof(signingSettings));
            }

            return(new SigningStringCompositionRequest {
                Request = request.ToHttpRequestForSignatureString(),
                RequestTargetEscaping = signingSettings.RequestTargetEscaping,
                HeadersToInclude = signingSettings.Headers,
                TimeOfComposing = timeOfComposing,
                Expires = expires,
                Nonce = signingSettings.EnableNonce
                    ? _nonceGenerator.GenerateNonce()
                    : null
            });
        }
 public HttpRequestForSigningTests()
 {
     _sut = new HttpRequestForSigning {
         Headers = new HeaderDictionary {
             { "h1", new StringValues(new[] { "v1", "v2" }) },
             { "h2", new StringValues(new[] { "v3" }) },
             { "h3", StringValues.Empty }
         },
         Method     = HttpMethod.Options,
         RequestUri = "https://unittest.com:9000/api?test=true".ToUri()
     };
 }
 public CreateForSigning()
 {
     _request  = new HttpRequestForSigning();
     _settings = new SigningSettings {
         Expires            = TimeSpan.FromMinutes(5),
         KeyId              = new KeyId("client1"),
         SignatureAlgorithm = new HMACSignatureAlgorithm("s3cr3t", HashAlgorithmName.SHA512),
         Headers            = new[] {
             HeaderName.PredefinedHeaderNames.RequestTarget,
             HeaderName.PredefinedHeaderNames.Date,
             HeaderName.PredefinedHeaderNames.Expires,
             new HeaderName("dalion_app_id")
         },
         DigestHashAlgorithm   = HashAlgorithmName.SHA256,
         EnableNonce           = true,
         RequestTargetEscaping = RequestTargetEscaping.RFC2396,
         AutomaticallyAddRecommendedHeaders = true
     };
     _timeOfComposingUtc = new DateTimeOffset(2020, 9, 8, 13, 21, 14, TimeSpan.Zero);
     _expires            = TimeSpan.FromMinutes(3);
 }