Exemple #1
0
        /// <summary>
        /// Gets all required signature data, if found, from the RestSharp client or request.
        /// </summary>
        /// <param name="client">The client to get the data from.</param>
        /// <param name="request">The request to get the data from.</param>
        /// <returns>The extracted data as an <see cref="HmacSignatureData"/> object.</returns>
        /// <remarks>
        /// Note 1:
        /// The headers of the client are inspected before those of the request.
        /// Therefore, if a header is both in the client and request, the one from the client will be used.
        /// In case of the body, it's the other way around.
        ///
        /// Note 2:
        /// The Content-Type is extracted from the body parameter (from the <see cref="Parameter.Name"/> property), NOT from a header parameter.
        ///
        /// Note 3:
        /// Keep in mind that when signing additional canonicalized headers, some will possibly not be available for signing, which may cause validation to fail.
        /// This is because RestSharps itself adds some headers after authentication and immediately before sending the request (the 'User-Agent' header for example).
        /// </remarks>
        /// <exception cref="ArgumentNullException">The client or request is null.</exception>
        public virtual HmacSignatureData GetSignatureDataFromRestRequest(IRestClient client, IRestRequest request)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client), "The client cannot be null.");
            }
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request), "The request cannot be null.");
            }

            HmacSignatureData signatureData = new HmacSignatureData
            {
                HttpMethod = request.Method.ToString().ToUpperInvariant()
            };

            // Get the request URI if configured
            if (HmacConfiguration.SignRequestUri)
            {
                signatureData.RequestUri = client.BuildUri(request).AbsoluteUri;
            }

            // Get date if a maximum request age is configured
            if (HmacConfiguration.MaxRequestAge.HasValue)
            {
                Parameter dateParameter = client.DefaultParameters.GetHeaderParameter(HmacConstants.DateHeaderName, request.Parameters);
                if (dateParameter?.Value != null)
                {
                    signatureData.Date = dateParameter.Value.ToString();
                }
            }

            // Get content type
            Parameter bodyParameter = request.Parameters.GetBodyParameter(client.DefaultParameters);

            if (bodyParameter != null)
            {
                signatureData.ContentType = bodyParameter.Name;

                // Get content MD5 if configured
                if (HmacConfiguration.ValidateContentMd5)
                {
                    Parameter contentMd5Parameter = client.DefaultParameters.GetHeaderParameter(HmacConstants.ContentMd5HeaderName, request.Parameters);
                    if (contentMd5Parameter?.Value != null)
                    {
                        signatureData.ContentMd5 = contentMd5Parameter.Value.ToString();
                    }
                }
            }

            // Get username
            Parameter usernameParameter = client.DefaultParameters.GetHeaderParameter(HmacConfiguration.UserHeaderName, request.Parameters);

            if (usernameParameter?.Value != null)
            {
                signatureData.Username = usernameParameter.Value.ToString();
            }

            // Get the key
            try
            {
                signatureData.Key = HmacKeyRepository.GetHmacKeyForUsername(signatureData.Username);
            }
            catch (Exception ex)
            {
                throw new HmacKeyRepositoryException("Failed to retrieve the key.", ex);
            }

            // Add additional headers
            if (HmacConfiguration.Headers != null && HmacConfiguration.Headers.Count > 0)
            {
                signatureData.Headers = new NameValueCollection();

                foreach (string headerName in HmacConfiguration.Headers.Distinct(StringComparer.OrdinalIgnoreCase))
                {
                    IEnumerable <string> headerValues;

                    if (!client.DefaultParameters.TryGetHeaderValues(headerName, out headerValues, request.Parameters))
                    {
                        continue;
                    }

                    foreach (string headerValue in headerValues)
                    {
                        signatureData.Headers.Add(headerName, headerValue);
                    }
                }
            }

            return(signatureData);
        }
Exemple #2
0
        private HmacSignatureData GetSignatureDataFromHttpRequest(HmacRequestWrapper request)
        {
            HmacSignatureData signatureData = new HmacSignatureData
            {
                HttpMethod = request.Method.ToUpperInvariant()
            };

            // Get the request URI if configured
            if (HmacConfiguration.SignRequestUri)
            {
                signatureData.RequestUri = request.RequestUri.AbsoluteUri;
            }

            // Get the request date if a maximum request age is configured
            if (HmacConfiguration.MaxRequestAge.HasValue && request.Date.HasValue)
            {
                DateTime date = request.Date.Value.UtcDateTime;
                signatureData.Date = date.ToString(HmacConstants.DateHeaderFormat, DateHeaderCulture);
            }

            // Get the content type and, if configured, the MD5 body hash
            signatureData.ContentType = request.ContentType;
            if (HmacConfiguration.ValidateContentMd5)
            {
                signatureData.ContentMd5 = request.ContentMd5;
            }

            // Get the username
            if (!string.IsNullOrEmpty(HmacConfiguration.UserHeaderName))
            {
                signatureData.Username = request.Headers[HmacConfiguration.UserHeaderName];
            }

            // Get the key
            try
            {
                signatureData.Key = HmacKeyRepository.GetHmacKeyForUsername(signatureData.Username);
            }
            catch (Exception ex)
            {
                throw new HmacKeyRepositoryException("Failed to retrieve the key.", ex);
            }

            // Add full additional headers
            if (HmacConfiguration.Headers != null && HmacConfiguration.Headers.Count > 0)
            {
                signatureData.Headers = new NameValueCollection();

                foreach (string headerName in HmacConfiguration.Headers.Distinct(StringComparer.OrdinalIgnoreCase))
                {
                    if (string.IsNullOrEmpty(headerName))
                    {
                        continue;
                    }

                    IList <string> headerValues = request.Headers.GetValues(headerName);
                    if (headerValues == null || headerValues.Count == 0)
                    {
                        continue;
                    }

                    foreach (string headerValue in headerValues)
                    {
                        signatureData.Headers.Add(headerName, headerValue);
                    }
                }
            }

            return(signatureData);
        }