Ejemplo n.º 1
0
        // Step 1 - Generate Authorization URL
        public IAuthenticationContext InitAuthenticationProcess(IConsumerCredentials appCredentials, string callbackURL, bool updateQueryIsAuthorized)
        {
            try
            {
                var authContext = new AuthenticationContext(appCredentials);
                var token       = authContext.Token;

                if (string.IsNullOrEmpty(callbackURL))
                {
                    callbackURL = Resources.OAuth_PINCode_CallbackURL;
                }
                else if (updateQueryIsAuthorized)
                {
                    var credsIdentifier = Guid.NewGuid();
                    _credentialsStore.CallbackAuthenticationContextStore.Add(credsIdentifier, authContext);

                    callbackURL = callbackURL.AddParameterToQuery(Resources.RedirectRequest_CredsParamId, credsIdentifier.ToString());
                }

                var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_callback", callbackURL, true, true, false);

                var authHandler          = new AuthHttpHandler(callbackParameter, authContext.Token);
                var requestTokenResponse = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestToken, HttpMethod.POST, authHandler,
                                                                               new TwitterCredentials(appCredentials));

                if (!string.IsNullOrEmpty(requestTokenResponse) && requestTokenResponse != Resources.OAuthRequestToken)
                {
                    Match tokenInformation = Regex.Match(requestTokenResponse, Resources.OAuthTokenRequestRegex);

                    bool callbackConfirmed = Boolean.Parse(tokenInformation.Groups["oauth_callback_confirmed"].Value);
                    if (!callbackConfirmed)
                    {
                        return(null);
                    }

                    token.AuthorizationKey    = tokenInformation.Groups["oauth_token"].Value;
                    token.AuthorizationSecret = tokenInformation.Groups["oauth_token_secret"].Value;

                    authContext.AuthorizationURL = string.Format("{0}?oauth_token={1}", Resources.OAuthRequestAuthorize, token.AuthorizationKey);

                    return(authContext);
                }
            }
            catch (TwitterException ex)
            {
                LogExceptionOrThrow(ex);
            }

            return(null);
        }
Ejemplo n.º 2
0
        // Step 2 - Generate User Credentials
        public IOAuthCredentials GetCredentialsFromVerifierCode(string verifierCode, ITemporaryCredentials temporaryCredentials)
        {
            var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true, true, false);
            var response          = _twitterRequester.ExecuteQueryWithTemporaryCredentials(Resources.OAuthRequestAccessToken, HttpMethod.POST, temporaryCredentials: temporaryCredentials, headers: new[] { callbackParameter });

            if (response == null)
            {
                return(null);
            }

            Match responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);

            if (responseInformation.Groups["oauth_token"] == null || responseInformation.Groups["oauth_token_secret"] == null)
            {
                return(null);
            }

            var credentials = _credentialsFactory.CreateOAuthCredentials(
                responseInformation.Groups["oauth_token"].Value,
                responseInformation.Groups["oauth_token_secret"].Value,
                temporaryCredentials.ConsumerKey,
                temporaryCredentials.ConsumerSecret);

            return(credentials);
        }
Ejemplo n.º 3
0
        // Step 2 - Generate User Credentials
        public ITwitterCredentials GetCredentialsFromVerifierCode(string verifierCode, IAuthenticationToken authToken)
        {
            try
            {
                if (authToken == null)
                {
                    throw new ArgumentNullException("Authentication Token cannot be null.");
                }

                if (verifierCode == null)
                {
                    throw new ArgumentNullException("VerifierCode",
                                                    "If you've received a verifier code that is null, " +
                                                    "it means that authentication has failed!");
                }

                var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true,
                                                                                    true, false);

                var authHandler = new AuthHttpHandler(callbackParameter, authToken);
                var response    = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST,
                                                                      authHandler,
                                                                      new TwitterCredentials(authToken.ConsumerCredentials));

                if (response == null)
                {
                    return(null);
                }

                var responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);
                if (responseInformation.Groups["oauth_token"] == null ||
                    responseInformation.Groups["oauth_token_secret"] == null)
                {
                    return(null);
                }

                var credentials = new TwitterCredentials(
                    authToken.ConsumerKey,
                    authToken.ConsumerSecret,
                    responseInformation.Groups["oauth_token"].Value,
                    responseInformation.Groups["oauth_token_secret"].Value);

                return(credentials);
            }
            catch (TwitterException ex)
            {
                if (_exceptionHandler.LogExceptions)
                {
                    _exceptionHandler.AddTwitterException(ex);
                }

                if (!_exceptionHandler.SwallowWebExceptions)
                {
                    throw;
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        public string GetAuthorizationURL(ITemporaryCredentials temporaryCredentials, string callbackURL)
        {
            var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_callback", callbackURL, true, true, false);

            var requestTokenResponse = _oAuthToken.ExecuteQueryWithSpecificParametersAndTemporaryCredentials(Resources.OAuthRequestToken, HttpMethod.POST, new[] { callbackParameter }, temporaryCredentials);

            if (!string.IsNullOrEmpty(requestTokenResponse) && requestTokenResponse != Resources.OAuthRequestToken)
            {
                Match tokenInformation = Regex.Match(requestTokenResponse, Resources.OAuthTokenRequestRegex);

                bool callbackConfirmed = Boolean.Parse(tokenInformation.Groups["oauth_callback_confirmed"].Value);
                if (!callbackConfirmed)
                {
                    return(null);
                }

                temporaryCredentials.AuthorizationKey    = tokenInformation.Groups["oauth_token"].Value;
                temporaryCredentials.AuthorizationSecret = tokenInformation.Groups["oauth_token_secret"].Value;

                return(String.Format("{0}?oauth_token={1}", Resources.OAuthRequestAuthorize, temporaryCredentials.AuthorizationKey));
            }

            return(null);
        }
Ejemplo n.º 5
0
        // Step 2 - Generate User Credentials
        public ITwitterCredentials GetCredentialsFromVerifierCode(string verifierCode, IConsumerCredentials appCredentials)
        {
            try
            {
                var callbackParameter = _oAuthWebRequestGenerator.GenerateParameter("oauth_verifier", verifierCode, true, true, false);

                var authHandler = new AuthHttpHandler(callbackParameter);
                var response    = _twitterRequestHandler.ExecuteQuery(Resources.OAuthRequestAccessToken, HttpMethod.POST, authHandler,
                                                                      new TwitterCredentials(appCredentials));

                if (response == null)
                {
                    return(null);
                }

                Match responseInformation = Regex.Match(response, Resources.OAuthTokenAccessRegex);
                if (responseInformation.Groups["oauth_token"] == null || responseInformation.Groups["oauth_token_secret"] == null)
                {
                    return(null);
                }

                var credentials = new TwitterCredentials(
                    appCredentials.ConsumerKey,
                    appCredentials.ConsumerSecret,
                    responseInformation.Groups["oauth_token"].Value,
                    responseInformation.Groups["oauth_token_secret"].Value);

                return(credentials);
            }
            catch (TwitterException ex)
            {
                if (_exceptionHandler.LogExceptions)
                {
                    _exceptionHandler.AddTwitterException(ex);
                }

                if (!_exceptionHandler.SwallowWebExceptions)
                {
                    throw;
                }
            }

            return(null);
        }