Exemplo n.º 1
0
        public async Task RequestAuthUrl_PinCode_ReturnsFromRequestExecutorAsync()
        {
            // Arrange
            var controller     = CreateAuthController();
            var request        = A.Fake <ITwitterRequest>();
            var expectedResult = A.Fake <ITwitterResult>();
            var parameters     = A.Fake <IRequestAuthUrlParameters>();
            var response       = "oauth_token=MY_TOKEN&oauth_token_secret=MY_SECRET&oauth_callback_confirmed=true";

            A.CallTo(() => _fakeAuthQueryExecutor.RequestAuthUrlAsync(It.IsAny <RequestAuthUrlInternalParameters>(), request)).Returns(expectedResult);
            A.CallTo(() => expectedResult.Content).Returns(response);

            // Act
            var result = await controller.RequestAuthUrlAsync(parameters, request);

            // Assert
            Assert.Equal("MY_TOKEN", result.Model.AuthorizationKey);
            Assert.Equal("MY_SECRET", result.Model.AuthorizationSecret);
        }
Exemplo n.º 2
0
        public async Task <ITwitterResult <IAuthenticationRequest> > RequestAuthUrlAsync(IRequestAuthUrlParameters parameters, ITwitterRequest request)
        {
            var authToken = new AuthenticationRequest(request.Query.TwitterCredentials);

            var authProcessParams = new RequestAuthUrlInternalParameters(parameters, authToken);

            if (string.IsNullOrEmpty(parameters.CallbackUrl))
            {
                authProcessParams.CallbackUrl = Resources.Auth_PinCodeUrl;
            }

            var requestTokenResponse = await _authQueryExecutor.RequestAuthUrlAsync(authProcessParams, request).ConfigureAwait(false);

            if (string.IsNullOrEmpty(requestTokenResponse.Content) || requestTokenResponse.Content == Resources.Auth_RequestToken)
            {
                throw new TwitterAuthException(requestTokenResponse, "Invalid authentication response");
            }

            var tokenInformation = _parseRequestUrlResponseRegex.Match(requestTokenResponse.Content);

            if (!bool.TryParse(tokenInformation.Groups["oauth_callback_confirmed"].Value, out var callbackConfirmed) || !callbackConfirmed)
            {
                throw new TwitterAuthAbortedException(requestTokenResponse);
            }

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

            var authorizationUrl = new StringBuilder(Resources.Auth_AuthorizeBaseUrl);

            authorizationUrl.AddParameterToQuery("oauth_token", authToken.AuthorizationKey);
            authorizationUrl.AddParameterToQuery("force_login", parameters.ForceLogin);
            authorizationUrl.AddParameterToQuery("screen_name", parameters.ScreenName);

            authToken.AuthorizationURL = authorizationUrl.ToString();

            return(new TwitterResult <IAuthenticationRequest>
            {
                Request = requestTokenResponse.Request,
                Response = requestTokenResponse.Response,
                Model = authToken
            });
        }