Esempio n. 1
0
        public TimeSpan GetAge(CallerIdentityVerificationParameters token)
        {
            DateTime date = DateTime.ParseExact(token.Headers["x-amz-date"], "yyyyMMddTHHmmssZ", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

            TimeSpan age = DateTime.UtcNow - date;

            return((age < TimeSpan.Zero) ? TimeSpan.Zero : age);
        }
Esempio n. 2
0
        public async Task <GetCallerIdentityResult> VerifyCallerIdentityAsync(CallerIdentityVerificationParameters token)
        {
            #region Preconditions

            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            var uri = new Uri(token.Url);

            if (uri.Scheme != "https")
            {
                throw new ArgumentException("endpoint must be HTTPS. was :" + uri.Scheme);
            }

            // https://sts.us-east-1.amazonaws.com/

            if (!(uri.Host.StartsWith("sts.") && uri.Host.EndsWith(".amazonaws.com")))
            {
                throw new Exception("Must be an STS endpoint: was:" + token.Url);
            }

            #endregion

            var request = new HttpRequestMessage(HttpMethod.Post, token.Url)
            {
                Content = new StringContent(token.Body, Encoding.UTF8, "application/x-www-form-urlencoded")
            };

            foreach (var header in token.Headers)
            {
                request.Headers.TryAddWithoutValidation(header.Key, header.Value.ToString());
            }

            request.Headers.UserAgent.ParseAdd("Carbon/1.6.0");
            request.Headers.Host = uri.Host;

            // throw new Exception(JsonObject.FromObject(request).ToString(true));

            // Our message should be signed

            using (var response = await httpClient.SendAsync(request).ConfigureAwait(false))
            {
                var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("ERROR:" + response.StatusCode + "/" + responseText);
                }

                return(StsResponseHelper <GetCallerIdentityResponse> .ParseXml(responseText).GetCallerIdentityResult);
            }
        }
Esempio n. 3
0
        public async Task <GetCallerIdentityResult> VerifyCallerIdentityAsync(CallerIdentityVerificationParameters token)
        {
            var uri = new Uri(token.Url);

            if (uri.Scheme != "https")
            {
                throw new ArgumentException("Endpoint scheme be https. Was " + uri.Scheme);
            }

            // https://sts.us-east-1.amazonaws.com/

            if (!(uri.Host.StartsWith("sts.", StringComparison.Ordinal) && uri.Host.EndsWith(".amazonaws.com", StringComparison.Ordinal)))
            {
                throw new Exception("Must be an STS endpoint: was:" + token.Url);
            }

            var request = new HttpRequestMessage(HttpMethod.Post, token.Url)
            {
                Content = new StringContent(token.Body, Encoding.UTF8, "application/x-www-form-urlencoded")
            };

            foreach (var header in token.Headers)
            {
                request.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }

            request.Headers.Host = uri.Host;

            // Our message should be signed

            using HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);

            string responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                throw new StsException(response.StatusCode, responseText);
            }

            return(StsSerializer <GetCallerIdentityResponse> .ParseXml(responseText).GetCallerIdentityResult);
        }