Ejemplo n.º 1
0
        /// <summary>
        /// Parses the OAuth parameters from a OAuthProtectedResponse.
        /// </summary>
        /// <param name="response">The OAuthProtectedResponse</param>
        /// <returns>
        /// An OAuthParameters object containing the parsed reserved OAuth parameters and any additional, valid parameters.</returns>
        public static OAuthParameters Parse(OAuthResource response)
        {
            if (response == null)
            {
                return(null);
            }

            NameValueCollection bodyParams = new NameValueCollection();

            using (MemoryStream ms = new MemoryStream())
            {
                System.IO.Stream stream = response.GetResponseStream();
                byte[]           buffer = new byte[32768];

                int read;

                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                Encoding bodyEncoding = Encoding.ASCII;
                if (!String.IsNullOrEmpty(response.ContentEncoding))
                {
                    bodyEncoding = Encoding.GetEncoding(response.ContentEncoding);
                }

                string responseBody = bodyEncoding.GetString(ms.ToArray());

                string[] nameValuePairs = responseBody.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string nameValuePair in nameValuePairs)
                {
                    string[] nameValuePairParts = nameValuePair.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nameValuePairParts.Length == 2)
                    {
                        bodyParams.Add(HttpUtility.UrlDecode(nameValuePairParts[0]), HttpUtility.UrlDecode(nameValuePairParts[1]));
                    }
                }

                // Reset the stream
                stream.Position = 0;
            }

            return(OAuthParameters.DoParse(null, response.Headers[Constants.WwwAuthenticateHeaderParameter], bodyParams, null, OAuthParameterSources.ConsumerDefault, false));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses the OAuth parameters from a OAuthProtectedResponse.
        /// </summary>
        /// <param name="response">The OAuthProtectedResponse</param>
        /// <returns>
        /// An OAuthParameters object containing the parsed reserved OAuth parameters and any additional, valid parameters.</returns>
        public static OAuthParameters Parse(OAuthResource response)
        {
            if (response == null)
                return null;

            NameValueCollection bodyParams = new NameValueCollection();

            using (MemoryStream ms = new MemoryStream())
            {
                System.IO.Stream stream = response.GetResponseStream();
                byte[] buffer = new byte[32768];

                int read;

                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                Encoding bodyEncoding = Encoding.ASCII;
                if (!String.IsNullOrEmpty(response.ContentEncoding))
                    bodyEncoding = Encoding.GetEncoding(response.ContentEncoding);

                string responseBody = bodyEncoding.GetString(ms.ToArray());

                string[] nameValuePairs = responseBody.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string nameValuePair in nameValuePairs)
                {
                    string[] nameValuePairParts = nameValuePair.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (nameValuePairParts.Length == 2)
                        bodyParams.Add(HttpUtility.UrlDecode(nameValuePairParts[0]), HttpUtility.UrlDecode(nameValuePairParts[1]));
                }

                // Reset the stream
                stream.Position = 0;
            }

            return OAuthParameters.DoParse(null, response.Headers[Constants.WwwAuthenticateHeaderParameter], bodyParams, null, OAuthParameterSources.ConsumerDefault, false);
        }
Ejemplo n.º 3
0
        private OAuthResponse GetResource(NameValueCollection parameters, string contentType, System.IO.Stream bodyStream)
        {
            OAuthResponse response;

            HttpWebRequest request = this.PrepareProtectedResourceRequest(parameters, contentType, bodyStream);

            // A null value for the HttpWebRequest is returned when a ResponseToken is returned
            // and no one has returned in the AuthorizationHandler continue with getting an AccessToken
            // or an RequestToken exists but the AccessToken request was refused.
            if (request == null)
                response = new OAuthResponse(this.RequestToken);
            else
            {
                OAuthResource resource;
                OAuthParameters responseParameters;

                try
                {
                    resource = new OAuthResource((HttpWebResponse)request.GetResponse());

                    // Parse the parameters and re-throw any OAuthRequestException from the service provider
                    responseParameters = OAuthParameters.Parse(resource);
                    OAuthRequestException.TryRethrow(responseParameters);

                    // If nothing is thrown then we should have a valid resource.
                    response = new OAuthResponse(this.AccessToken ?? this.RequestToken, resource);
                }
                catch (WebException e)
                {
                    // Parse the parameters and re-throw any OAuthRequestException from the service provider
                    responseParameters = OAuthParameters.Parse(e.Response as HttpWebResponse);
                    OAuthRequestException.TryRethrow(responseParameters);

                    // If no OAuthRequestException, rethrow the WebException
                    #warning TODO: We have consumer the WebException's body so rethrowing it is pretty pointless; wrap the WebException in an OAuthProtocolException and store the body (create an OAuthResource before parsing parameters)
                    throw;
                }
            }

            return response;
        }
Ejemplo n.º 4
0
 internal OAuthResponse(IToken token, OAuthResource resource)
 {
     this.Token = token;
     this.ProtectedResource = resource;
     this.HasProtectedResource = resource != null;
 }