Esempio n. 1
0
        private void DoGetRequestToken()
        {
            // Fire the OnBeforeGetRequestToken event
            PreRequestEventArgs args = new PreRequestEventArgs(Service.RequestTokenUrl, Service.RequestTokenEndPoint.HttpMethod, CallbackUrl);

            if (BeforeGetRequestToken != null)
            {
                BeforeGetRequestToken(this, args);
            }

            OAuthParameters authParams = CreateOAuthParameters(args.AdditionalParameters);

            authParams.Callback = args.CallbackUrl == null ? OAuthOutOfBandCallback : args.CallbackUrl.AbsoluteUri;

            SignParameters(args.RequestUri, args.HttpMethod, authParams, null);

            // Create and sign the request
            HttpWebRequest request = CreateRequest(
                args.RequestUri,
                authParams,
                args.HttpMethod,
                args.HttpMethod == "POST" ? HttpPostUrlEncodedContentType : String.Empty,
                null);

            OAuthParameters responseParameters;

            // Get the service provider response
            try {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(response);
                OAuthRequestException.TryRethrow(responseParameters);
            } 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
                throw;
            }

            // Store the request token
            RequestToken = new OAuthToken(TokenType.Request, responseParameters.Token, responseParameters.TokenSecret, Service.Consumer);

            // Fire the OnReceiveRequestToken event
            RequestTokenReceivedEventArgs responseArgs = new RequestTokenReceivedEventArgs(RequestToken);

            responseArgs.Parameters.Add(responseParameters.AdditionalParameters);

            if (ReceiveRequestToken != null)
            {
                ReceiveRequestToken(this, responseArgs);
            }
        }
        protected override void ParseParameters(IHttpContext httpContext, OAuthRequestContext requestContext)
        {
            // Try to parse the parameters
            OAuthParameters parameters = OAuthParameters.Parse(httpContext.Request, OAuthParameterSources.ServiceProviderDefault);

            /*
             * Check for missing required parameters:
             *
             * The consumer key, signature method, signature, timestamp and nonce parameters
             * are all required
             */
            parameters.RequireAllOf(
                OAuthParameterKeys.ConsumerKey,
                OAuthParameterKeys.Token,
                OAuthParameterKeys.SignatureMethod,
                OAuthParameterKeys.Signature,
                OAuthParameterKeys.Timestamp,
                OAuthParameterKeys.Nonce,
                OAuthParameterKeys.Verifier);

            /*
             * The version parameter is optional, but it if is present its value must be 1.0
             */
            if (parameters.Version != null)
            {
                parameters.RequireVersion("1.0");
            }

            /*
             * Check that there are no other parameters except for realm, version and
             * the required parameters
             */
            parameters.AllowOnly(
                OAuthParameterKeys.ConsumerKey,
                OAuthParameterKeys.Token,
                OAuthParameterKeys.SignatureMethod,
                OAuthParameterKeys.Signature,
                OAuthParameterKeys.Timestamp,
                OAuthParameterKeys.Nonce,
                OAuthParameterKeys.Verifier,
                OAuthParameterKeys.Version,                        // (optional)
                OAuthParameterKeys.Realm);                         // (optional)

            requestContext.Parameters = parameters;
        }
Esempio n. 3
0
        private void ParseParameters(IHttpContext httpContext, OAuthRequestContext context)
        {
            // Try to parse the parameters
            OAuthParameters parameters = OAuthParameters.Parse(httpContext.Request);

            /*
             * Check for missing required parameters:
             *
             * The consumer key, token, signature method, signature, timestamp and nonce parameters
             * are all required
             */
            if (consumerRequests)
            {
                parameters.RequireAllOf(
                    OAuthParameterKeys.ConsumerKey,
                    OAuthParameterKeys.SignatureMethod,
                    OAuthParameterKeys.Signature,
                    OAuthParameterKeys.Timestamp,
                    OAuthParameterKeys.Nonce);
            }
            else
            {
                // For 3 legged TokenParameter is required
                parameters.RequireAllOf(
                    OAuthParameterKeys.ConsumerKey,
                    OAuthParameterKeys.Token,
                    OAuthParameterKeys.SignatureMethod,
                    OAuthParameterKeys.Signature,
                    OAuthParameterKeys.Timestamp,
                    OAuthParameterKeys.Nonce);
            }

            /*
             * The version parameter is optional, but it if is present its value must be 1.0
             */
            if (parameters.Version != null)
            {
                parameters.RequireVersion("1.0");
            }

            context.Parameters = parameters;
        }
Esempio n. 4
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);
        }
        protected override void ParseParameters(IHttpContext httpContext, OAuthRequestContext requestContext)
        {
            // Try to parse the parameters
            OAuthParameters parameters = OAuthParameters.Parse(httpContext.Request, OAuthParameterSources.ServiceProviderDefault);

            /*
             * Check for missing required parameters:
             *
             * The consumer key, signature method, signature, timestamp and nonce parameters
             * are all required
             */
            parameters.RequireAllOf(OAuthParameterKeys.ConsumerKey, OAuthParameterKeys.SignatureMethod,
                                    OAuthParameterKeys.Signature, OAuthParameterKeys.Timestamp, OAuthParameterKeys.Nonce,
                                    OAuthParameterKeys.Callback);

            // The version parameter is optional, but it if is present its value must be 1.0
            if (parameters.Version != null)
            {
                parameters.RequireVersion("1.0");
            }

            requestContext.Parameters = parameters;
        }
Esempio n. 6
0
        private bool DoGetAccessToken()
        {
            // Fire the OnBeforeGetAccessToken event
            PreAccessTokenRequestEventArgs preArgs = new PreAccessTokenRequestEventArgs(Service.AccessTokenUrl,
                                                                                        Service.AccessTokenEndPoint.HttpMethod,
                                                                                        RequestToken, RequestTokenVerifier);

            if (BeforeGetAccessToken != null)
            {
                BeforeGetAccessToken(this, preArgs);
            }

            // Create and sign the request
            OAuthParameters authParams = CreateOAuthParameters(null);

            authParams.Verifier = preArgs.Verifier;

            // We don't have a verifier so something has gone wrong in the process.
            if (string.IsNullOrEmpty(authParams.Verifier))
            {
                return(false);
            }

            SignParameters(preArgs.RequestUri, preArgs.HttpMethod, authParams, RequestToken);

            HttpWebRequest request = CreateRequest(preArgs.RequestUri, authParams, preArgs.HttpMethod,
                                                   preArgs.HttpMethod == "POST" ? HttpPostUrlEncodedContentType : String.Empty,
                                                   null);

            OAuthParameters responseParameters;

            // Get the service provider response
            try {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Parse the parameters and re-throw any OAuthRequestException from the service provider
                responseParameters = OAuthParameters.Parse(response);
                OAuthRequestException.TryRethrow(responseParameters);
            } 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
                throw;
            }

            // Store the access token
            AccessToken = new OAuthToken(TokenType.Access, responseParameters.Token, responseParameters.TokenSecret, Service.Consumer);

            // Fire the OnReceiveAccessToken event
            AccessTokenReceivedEventArgs responseArgs = new AccessTokenReceivedEventArgs(RequestToken, AccessToken);

            responseArgs.AdditionalParameters.Add(responseParameters.AdditionalParameters);

            if (ReceiveAccessToken != null)
            {
                ReceiveAccessToken(this, responseArgs);
            }

            return(true);
        }