Esempio n. 1
0
        /// <summary>
        /// Signs an HTTP request with the MAuth-specific authentication information and sends the request to the
        /// inner handler to send to the server as an asynchronous operation.
        /// </summary>
        /// <param name="request">The HTTP request message to sign and send to the server.</param>
        /// <param name="cancellationToken">A cancellation token to cancel operation.</param>
        /// <returns>Returns <see cref="Task{HttpResponseMessage}"/>. The task object representing the asynchronous
        /// operation.</returns>
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (InnerHandler == null)
            {
                InnerHandler = new HttpClientHandler();
            }

            if (options.DisableV1 == false) // default
            {
                // Add headers for V1 protocol as well
                var mAuthCoreV1 = MAuthCoreFactory.Instantiate(MAuthVersion.MWS);
                request = await mAuthCoreV1.Sign(request, options).ConfigureAwait(false);
            }

            // Add headers for V2 protocol
            mAuthCore = MAuthCoreFactory.Instantiate(MAuthVersion.MWSV2);
            request   = await mAuthCore.Sign(request, options).ConfigureAwait(false);

            return(await base
                   .SendAsync(request, cancellationToken)
                   .ConfigureAwait(continueOnCapturedContext: false));
        }
        /// <summary>
        /// Extracts the authentication information from a <see cref="HttpRequestMessage"/>.
        /// </summary>
        /// <param name="request">The request that has the authentication information.</param>
        /// <param name="mAuthCore">Instantiation of mAuthCore class.</param>
        /// <returns>The authentication information with the payload from the request.</returns>
        internal static PayloadAuthenticationInfo GetAuthenticationInfo(HttpRequestMessage request, IMAuthCore mAuthCore)
        {
            var headerKeys = mAuthCore.GetHeaderKeys();
            var authHeader = request.Headers.GetFirstValueOrDefault <string>(headerKeys.mAuthHeaderKey);

            if (authHeader == null)
            {
                throw new ArgumentNullException(nameof(authHeader), "The MAuth header is missing from the request.");
            }

            var signedTime = request.Headers.GetFirstValueOrDefault <long>(headerKeys.mAuthTimeHeaderKey);

            if (signedTime == default(long))
            {
                throw new ArgumentException("Invalid MAuth signed time header value.", nameof(signedTime));
            }

            var(uuid, payload) = authHeader.ParseAuthenticationHeader();

            return(new PayloadAuthenticationInfo
            {
                ApplicationUuid = uuid,
                Payload = Convert.FromBase64String(payload),
                SignedTime = signedTime.FromUnixTimeSeconds()
            });
        }