public void SignRequest_should_apply_signature_to_request_right_culture()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("th");

            var creds = new SessionAWSCredentials("ExampleKey", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY", "token1")
                        .GetCredentials();

            SignV4Util.SignRequest(_sampleRequest, _sampleBody, creds, "us-east-1", "iam");

            var amzDateValue = _sampleRequest.Headers.XAmzDate;

            Assert.False(String.IsNullOrEmpty(amzDateValue));
            var amzDates = amzDateValue.Split(',');

            Assert.AreEqual(2, amzDates.Length);
            Assert.True(amzDates[1].StartsWith(DateTime.UtcNow.Year.ToString()));
            Trace.WriteLine("X-Amz-Date: " + amzDateValue);

            var auth = _sampleRequest.Headers.Authorization;

            Assert.False(String.IsNullOrEmpty(auth));
            Trace.WriteLine("Authorize: " + auth);

            var token = _sampleRequest.Headers.XAmzSecurityToken;

            Assert.False(String.IsNullOrEmpty(token));
            Trace.WriteLine("Token: " + token);
        }
        public void SignRequest_should_apply_signature_to_request()
        {
            var creds = new Credentials
            {
                AccessKey = "ExampleKey",
                SecretKey = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
                Token     = "token1",
            };

            SignV4Util.SignRequest(_sampleRequest, _sampleBody, creds, "us-east-1", "iam");

            var amzDate = _sampleRequest.Headers["X-Amz-Date"];

            Assert.IsNotNullOrEmpty(amzDate);
            Trace.WriteLine("X-Amz-Date: " + amzDate);

            var auth = _sampleRequest.Headers[HttpRequestHeader.Authorization];

            Assert.IsNotNullOrEmpty(auth);
            Trace.WriteLine("Authorize: " + auth);

            var token = _sampleRequest.Headers["x-amz-security-token"];

            Assert.IsNotNullOrEmpty(token);
            Trace.WriteLine("Token: " + token);
        }
예제 #3
0
        //public static void Sign(this ICredentialsProvider credentialsProvider, System.Net.Http.HttpRequestMessage request, byte[] body)
        //    => credentialsProvider.Sign(new HttpRequestMessageAdapter(request), body);

        internal static void Sign(this ICredentialsProvider credentialsProvider, HttpRequestMessage request,
                                  byte[] body)
        {
            var credentials = credentialsProvider.GetCredentials();

            if (credentials == null)
            {
                throw new Exception("Unable to retrieve credentials.");
            }
            var regionService = ExtractRegionAndService(request.RequestUri);

            SignV4Util.SignRequest(request, body, credentials, regionService.Item1, regionService.Item2);
        }
        public void GetStringToSign_should_match_sample()
        {
            var stringToSign = SignV4Util.GetStringToSign(_sampleRequest, _sampleBody, "us-east-1", "iam");

            Trace.WriteLine("=== Actual ===");
            Trace.Write(stringToSign);

            const string expected = "AWS4-HMAC-SHA256\n20110909T233600Z\n20110909/us-east-1/iam/aws4_request\n3511de7e95d28ecd39e9513b642aee07e54f4941150d8df8bf94b328ef7e55e2";

            Trace.WriteLine("=== Expected ===");
            Trace.Write(expected);

            Assert.AreEqual(expected, stringToSign);
        }
        public void GetCanonicalRequest_should_match_sample()
        {
            var canonicalRequest = SignV4Util.GetCanonicalRequest(_sampleRequest, _sampleBody);

            Trace.WriteLine("=== Actual ===");
            Trace.Write(canonicalRequest);

            const string expected = "POST\n/\n\ncontent-type:application/x-www-form-urlencoded; charset=utf-8\nhost:iam.amazonaws.com\nx-amz-date:20110909T233600Z\n\ncontent-type;host;x-amz-date\nb6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f955085e2";

            Trace.WriteLine("=== Expected ===");
            Trace.Write(expected);

            Assert.AreEqual(expected, canonicalRequest);
        }
        public void GetSigningKey_should_match_sample()
        {
            // sample comes from - http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html
            var secretKey             = "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY";
            var date                  = "20120215";
            var region                = "us-east-1";
            var service               = "iam";
            var expectedSigningKey    = "f4780e2d9f65fa895f9c67b32ce1baf0b0d8a43505a000a1a9e090d414db404d";
            var actualSigningKeyBytes = SignV4Util.GetSigningKey(secretKey, date, region, service);
            var actualSigningKey      = BitConverter.ToString(actualSigningKeyBytes).Replace("-", "").ToLowerInvariant();

            Trace.WriteLine("Expected: " + expectedSigningKey);
            Trace.WriteLine("Actual:   " + actualSigningKey);
            Assert.AreEqual(expectedSigningKey, actualSigningKey);
        }
        public void SignRequest_should_apply_signature_to_request()
        {
            var creds = new SessionAWSCredentials("ExampleKey", "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY", "token1")
                        .GetCredentials();

            SignV4Util.SignRequest(_sampleRequest, _sampleBody, creds, "us-east-1", "iam");

            var amzDate = _sampleRequest.Headers.XAmzDate;

            Assert.False(String.IsNullOrEmpty(amzDate));
            Trace.WriteLine("X-Amz-Date: " + amzDate);

            var auth = _sampleRequest.Headers.Authorization;

            Assert.False(String.IsNullOrEmpty(auth));
            Trace.WriteLine("Authorize: " + auth);

            var token = _sampleRequest.Headers.XAmzSecurityToken;

            Assert.False(String.IsNullOrEmpty(token));
            Trace.WriteLine("Token: " + token);
        }