예제 #1
0
        public static OAuthParameters Parse(OAuthResource response)
        {
            if (response == null)
            {
                return(null);
            }

            NameValueCollection bodyParams = new NameValueCollection();

            using (MemoryStream ms = new MemoryStream()) {
                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(DoParse(null, response.Headers[WwwAuthenticateHeaderParameter], bodyParams, null, OAuthParameterSources.ConsumerDefault, false));
        }
예제 #2
0
        private OAuthResponse GetResource(NameValueCollection parameters, string contentType, Stream bodyStream)
        {
            OAuthResponse response;

            HttpWebRequest request = 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(RequestToken);
            }
            else
            {
                OAuthParameters responseParameters;

                try {
                    OAuthResource 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(AccessToken ?? 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);
        }
예제 #3
0
		private OAuthResponse GetResource(NameValueCollection parameters, string contentType, Stream bodyStream) {
			OAuthResponse response;

			HttpWebRequest request = 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(RequestToken);
			else {
				OAuthParameters responseParameters;

				try {
					OAuthResource 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(AccessToken ?? 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;
		}
예제 #4
0
 internal OAuthResponse(IToken token, OAuthResource resource)
 {
     this.token = token;
     this.resource = resource;
     hasResource = resource != null;
 }
예제 #5
0
 internal OAuthResponse(IToken token, OAuthResource resource)
 {
     this.token    = token;
     this.resource = resource;
     hasResource   = resource != null;
 }
예제 #6
0
        public static OAuthParameters Parse(OAuthResource response)
        {
            if (response == null)
                return null;

            NameValueCollection bodyParams = new NameValueCollection();

            using (MemoryStream ms = new MemoryStream()) {
                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 DoParse(null, response.Headers[WwwAuthenticateHeaderParameter], bodyParams, null, OAuthParameterSources.ConsumerDefault, false);
        }