public async Task IetfIntegrationDefaultTest() { Uri requestUri = new Uri("https://example.com/foo?param=value&pet=dog"); SignatureSpecification signatureSpecification = new SignatureSpecification() { Algorithm = "rsa-sha256", HashAlgorithm = "SHA-256", KeyId = "Test", Headers = new [] { "(request-target)", "host", "date", "content-type", "digest", "content-length" } }; IStringSigner stringSigner = new RSAStringSigner(signatureSpecification, itefPrivateKey.ToRSAParameters()); HttpSignatureStringExtractor httpSignatureExtractor = new HttpSignatureStringExtractor(); ISignatureGenerator signatureGenerator = new SignatureGenerator(stringSigner, httpSignatureExtractor, signatureSpecification.KeyId); var httpSignatureHandler = new SignatureDelegatingHandler(signatureGenerator, signatureSpecification); var httpDigestDelegate = new DigestDelegatingHandler(new DigestGenerator(signatureSpecification)); httpDigestDelegate.InnerHandler = httpSignatureHandler; httpSignatureHandler.InnerHandler = new Mocks.DelegatingHandlerMock(); var invoker = new HttpMessageInvoker(httpDigestDelegate); HttpRequestMessage requestMessage = new HttpRequestMessage(); var content = new StringContent("{\"hello\": \"world\"}"); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); content.Headers.ContentLength = 18; requestMessage.RequestUri = requestUri; requestMessage.Method = HttpMethod.Post; requestMessage.Content = content; requestMessage.Headers.TryAddWithoutValidation("Date", "Thu, 05 Jan 2014 21:31:40 GMT"); requestMessage.Headers.Add("Host", "example.com"); var result = await invoker.SendAsync(requestMessage, CancellationToken.None); string expectedSignature = "keyId=\"Test\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date content-type digest content-length\",signature=\"Ef7MlxLXoBovhil3AlyjtBwAL9g4TN3tibLj7uuNB3CROat/9KaeQ4hW2NiJ+pZ6HQEOx9vYZAyi+7cmIkmJszJCut5kQLAwuX+Ms/mUFvpKlSo9StS2bMXDBNjOh4Auj774GFj4gwjS+3NhFeoqyr/MuN6HsEnkvn6zdgfE2i0=\""; Assert.Equal(expectedSignature, requestMessage.Headers.Authorization.Parameter); }
public void HeaderIsMadeLowercase() { // Arrange IHttpSignatureStringExtractor signatureStringExtractor = new HttpSignatureStringExtractor(); Request request = new Request(); request.SetHeader("Test", new[] { "value1" }); ISignatureSpecification specification = new SignatureSpecification() { Headers = new[] { "Test" } }; // Act string result = signatureStringExtractor.ExtractSignatureString(request, specification); // Assert Assert.Equal("test: value1", result); }
public void GivenSameHeaderStringIncludeBothInSignature() { // Arrange IHttpSignatureStringExtractor signatureStringExtractor = new HttpSignatureStringExtractor(); Request request = new Request(); request.SetHeader("test", new[] { "value1", "value2" }); ISignatureSpecification specification = new SignatureSpecification() { Headers = new[] { "test" } }; // Act string result = signatureStringExtractor.ExtractSignatureString(request, specification); // Assert Assert.Equal("test: value1, value2", result); }
public void IfHeaderNotInSpecificationDontIncludeInOutput() { // Arrange IHttpSignatureStringExtractor signatureStringExtractor = new HttpSignatureStringExtractor(); Request request = new Request(); request.SetHeader("test", new[] { "value1" }); request.SetHeader("ignoredheader", new[] { "value1" }); ISignatureSpecification specification = new SignatureSpecification() { Headers = new[] { "test" } }; // Act string result = signatureStringExtractor.ExtractSignatureString(request, specification); // Assert Assert.Equal("test: value1", result); }
public void CorrectlyGeneratesRequestTarget() { // Arrange IHttpSignatureStringExtractor signatureStringExtractor = new HttpSignatureStringExtractor(); Request request = new Request(); request.Method = "GET"; request.Path = "/test"; ISignatureSpecification specification = new SignatureSpecification() { Headers = new[] { "(request-target)" } }; // Act string result = signatureStringExtractor.ExtractSignatureString(request, specification); // Assert Assert.Equal("(request-target): get /test", result); }
public async Task TellerIntegrationTest() { string privateKey = @"-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAunDvDG5JeWU5qaI0evW3Pf+NYJu3xjmet+EYitnSLSC3tVfu SttzpmiO00B1AZ3S7r9D02E86I850icPLICHoApbvtvQtmH5+YmVAYlSJoyFEDyd i8ag0KnY3k9v0H3LhUYd4B6/0YwGF44u/Fv9j07fJ1TjF1jSw+QIR6+gpXIhKYV+ bd3wJKZhT8HpYXKDUKKFDXwelZBCdT38r6K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qpuoy9eXiJalovMeC7jHrhSm9IJoCNBJ92ZqZE4RWBEeFKsMaqMx kzP032akhY1xCFfvfr43Izp76poQllWUm8xJHdAAqyRy5ttpCCMGExUVXA6YoRqa tobKVFECgYBjppJEgar+LXWloOfFEMJhPCS7Cfa37IVOCnRoSORoZTmvonPQAxS1 yoXQN/1XxDMHBp/rLdMJ5jQiW0AB3b+JV5G9pMsYP7wpRpaL/zJoDBD7IfVlV9Tr AiTxCpr35nV2PPYaFYwE+mfeT5jFZnhZuRyTlX9cfYx73rukFMrSmg== -----END RSA PRIVATE KEY-----"; Guid idempotencyToken = Guid.Empty; string paymentUri = "https://api.teller.io/accounts/00000000-0000-0000-0000-000000000000/payments"; Uri requestUri = new Uri($"{paymentUri}/{idempotencyToken}"); SignatureSpecification signatureSpecification = new SignatureSpecification() { Algorithm = "rsa-sha256", HashAlgorithm = "SHA-256", KeyId = "certificate", Headers = new List <string> { "(request-target)", "accept", "content-type", "date", "digest", "authorization", "user-agent" }, IncludePseduoHeaderInSigantureString = false, UseSignatureAsAuthroizationHeader = false }; IStringSigner stringSigner = new RSAStringSigner(signatureSpecification, privateKey.ToRSAParameters()); HttpSignatureStringExtractor httpSignatureExtractor = new HttpSignatureStringExtractor() { HeaderSeperationString = " " }; ISignatureGenerator signatureGenerator = new SignatureGenerator(stringSigner, httpSignatureExtractor, signatureSpecification.KeyId); var httpSignatureHandler = new SignatureDelegatingHandler(signatureGenerator, signatureSpecification); var httpDigestDelegate = new DigestDelegatingHandler(new DigestGenerator(signatureSpecification)); httpDigestDelegate.InnerHandler = httpSignatureHandler; httpSignatureHandler.InnerHandler = new Mocks.DelegatingHandlerMock(); var invoker = new HttpMessageInvoker(httpDigestDelegate); HttpRequestMessage requestMessage = new HttpRequestMessage(); Dictionary <string, string> data = new Dictionary <string, string> { { "bank_code", "00-00-00" }, { "account_number", "00000000" }, { "amount", "0.01" } }; var content = new FormUrlEncodedContent(data); requestMessage.RequestUri = requestUri; requestMessage.Method = HttpMethod.Put; requestMessage.Content = content; requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); requestMessage.Headers.Add("Date", "Mon, 09 Apr 2018 15:24:16 GMT"); requestMessage.Headers.Add("accept", "application/json"); requestMessage.Headers.UserAgent.ParseAdd("Super Rad App/0.0.1"); var result = await invoker.SendAsync(requestMessage, CancellationToken.None); string expectedSignature = "keyId=\"certificate\",algorithm=\"rsa-sha256\",headers=\"accept content-type date digest authorization user-agent\",signature=\"g0nWC3Q4EnzX+gYhbxG38dhyVjlewsgA7xJjrllYc3GVuiYEE0KLEugQ2JhbeJK2zK2FtL1476wsS0QmTd+V+HOs0jdIy+aAmBIOAx6urIvsRYTmgOzUMnPfLDTtda1PYvUkwQMMZcZ2jmVN3lPs4ZWuGc9HwWfBlTtrhdjXPdyvMG8SgfTjM11MLl7b8UYURB4aWA7FG7aaoEGV3d8F5QY85pwgMtzSsRcpZL9cGIh8zr7p79fjeJ0M+arD/5geaATJRLsFovmpBFcdZKve44muLNCNmTO+Uu18sHhX5zi+mkINw4G1wxNXbyrPRaFPhcN9o6ZDlMPgtAogvyqbSQ==\""; Assert.Contains(requestMessage.Headers, header => header.Key == "signature" && header.Value.First() == expectedSignature); }