Beispiel #1
0
        /// <remarks>This method has a asynchronous alternative.</remarks>
        public static Result Sign(
            HttpRequestMessage request,
            Uri?baseAddress,
            IEnumerable <KeyValuePair <string, IEnumerable <string> > > defaultRequestHeaders,
            DateTime now,
            string regionName,
            string serviceName,
            ImmutableCredentials credentials)
        {
            ValidateArguments(request, regionName, serviceName, credentials);

            UpdateRequestUri(request, baseAddress);

            var contentHash = ContentHash.Calculate(request.Content);

            AddHeaders(request, now, serviceName, credentials, contentHash);

            var(canonicalRequest, signedHeaders) = CanonicalRequest.Build(
                serviceName,
                request,
                defaultRequestHeaders,
                contentHash);

            var(stringToSign, credentialScope) = StringToSign.Build(
                now,
                regionName,
                serviceName,
                canonicalRequest);

            var authorizationHeader = AuthorizationHeader.Build(
                now,
                regionName,
                serviceName,
                credentials,
                signedHeaders,
                credentialScope,
                stringToSign);

            // Add the authorization header
            request.Headers.TryAddWithoutValidation(HeaderKeys.AuthorizationHeader, authorizationHeader);

            return(new Result(canonicalRequest, stringToSign, authorizationHeader));
        }
        public static async Task <Result> SignAsync(
            HttpClient httpClient,
            HttpRequestMessage request,
            DateTime now,
            string regionName,
            string serviceName,
            ImmutableCredentials credentials)
        {
            if (httpClient == null)
            {
                throw new ArgumentNullException(nameof(httpClient));
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (request.Headers.Contains(HeaderKeys.XAmzDateHeader))
            {
                throw new ArgumentException(ErrorMessages.XAmzDateHeaderExists, nameof(request));
            }
            if (request.Headers.Authorization != null)
            {
                throw new ArgumentException(ErrorMessages.AuthorizationHeaderExists, nameof(request));
            }
            if (request.Headers.Contains(HeaderKeys.AuthorizationHeader))
            {
                throw new ArgumentException(ErrorMessages.AuthorizationHeaderExists, nameof(request));
            }
            if (regionName == null)
            {
                throw new ArgumentNullException(nameof(regionName));
            }
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            if (serviceName == "s3")
            {
                throw new NotSupportedException(ErrorMessages.S3NotSupported);
            }
            if (credentials == null)
            {
                throw new ArgumentNullException(nameof(credentials));
            }

            UpdateRequestUri(httpClient, request);

            // Add required headers
            request.AddHeader(HeaderKeys.XAmzDateHeader, now.ToIso8601BasicDateTime());

            // Add conditional headers
            request.AddHeaderIf(credentials.UseToken, HeaderKeys.XAmzSecurityTokenHeader, credentials.Token);
            request.AddHeaderIf(!request.Headers.Contains(HeaderKeys.HostHeader), HeaderKeys.HostHeader, request.RequestUri.Host);

            // Build the canonical request
            var(canonicalRequest, signedHeaders) = await CanonicalRequest.BuildAsync(request, httpClient.DefaultRequestHeaders);

            // Build the string to sign
            var(stringToSign, credentialScope) = StringToSign.Build(
                now,
                regionName,
                serviceName,
                canonicalRequest);

            // Build the authorization header
            var authorizationHeader = AuthorizationHeader.Build(
                now,
                regionName,
                serviceName,
                credentials,
                signedHeaders,
                credentialScope,
                stringToSign);

            // Add the authorization header
            request.Headers.TryAddWithoutValidation(HeaderKeys.AuthorizationHeader, authorizationHeader);

            return(new Result(canonicalRequest, stringToSign, authorizationHeader));
        }