示例#1
0
        public ActionResult Authentication(string error_reason,
                                           string error,
                                           string error_description,
                                           string code)
        {
            if (string.IsNullOrEmpty(error_reason))
            {
                if (!string.IsNullOrEmpty(code))
                {
                    var oAuthAuthentication = new OAuthAuthentication(new WebRequestService(),
                                                                      new FacebookUriAuthentication(Request.IsSecureConnection),
                                                                      new FacebookExtractTokenFromText());

                    if (oAuthAuthentication.Authenticate(code, RequestType.Get))
                    {
                        var client = new FacebookClient(oAuthAuthentication.Token);

                        var info         = client.Get("me?fields=id,name");
                        var facebookId   = (string)info.id;
                        var facebookName = (string)info.name;

                        Response.Write("id:" + facebookId + "- name::" + facebookName);
                    }
                }
            }

            return(null);
        }
示例#2
0
 public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_UnknownResponseUri()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.unknown.com"))
     };
     await AssertEx.ThrowsAsync <OperationCanceledException>(async() => await oAuth.Authenticate());
 }
 public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_UnknownResponseUri()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.unknown.com"))
     };
     await AssertEx.ThrowsAsync<OperationCanceledException>(async () => await oAuth.Authenticate());
 }
        public void Authenticate_WhenResponseTypeIsPost_ShouldCallWebRequestServicePostMethod()
        {
            WebRequestServiceFake mockWebRequestServiceFake;
            OAuthAuthentication   oAuthAuthentication = CreateOAuthAuthentication(out mockWebRequestServiceFake);

            oAuthAuthentication.Authenticate("any authoritzation code", RequestType.Post);

            mockWebRequestServiceFake.PostCalls.Should().Be(1);
        }
 public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_Exception()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = async uri => await Task.Run(new Func<Task<Uri>>(() => { throw new Exception("Test message."); })),
     };
     var ex = await AssertEx.ThrowsAsync<Exception>(async () => await oAuth.Authenticate());
     Assert.AreEqual("Test message.", ex.Message);
 }
        public void Authenticate_WhenTokenReturnedByExtractClassIsNull_ShouldReturnFalse()
        {
            var extractTokenFromTextFake = new ExtractTokenFromTextFake {
                ExtractStringReturn = null
            };
            OAuthAuthentication oAuthAuthentication = CreateOAuthAuthenticationWith(extractTokenFromTextFake);
            var gotToken = oAuthAuthentication.Authenticate("any authoritzation code", RequestType.Post);

            gotToken.Should().Be(false);
        }
示例#7
0
        public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_Exception()
        {
            var serverConnectionMock = CreateServerConnectionMockWithContext();
            var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
            {
                GetRedirectedUri = async uri => await Task.Run(new Func <Task <Uri> >(() => { throw new Exception("Test message."); })),
            };
            var ex = await AssertEx.ThrowsAsync <Exception>(async() => await oAuth.Authenticate());

            Assert.AreEqual("Test message.", ex.Message);
        }
        public void Can_Authenticate_Netflix_With_OAuth()
        {
            const string consumerKey    = "L7gdVVLFZ24LkQlPXxPk3WBjd";
            const string consumerSecret = "t0q0QWPAquKblDoRFoI9VmzH8DGrRNtGrdXPbrYfsE1WolVqBP";

            AuthContext     context = new OAuth1Context(consumerKey, consumerSecret);
            XAuthentication xas     = new OAuthAuthentication(context);

            var request = new RestRequest("oauth/request_token", Method.POST);

            xas.Authenticate(client, request);
            var response = client.Execute(request);

            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            // For Access Token >
            var qs               = HttpUtility.ParseQueryString(response.Content);
            var oauthToken       = qs["oauth_token"];
            var oauthTokenSecret = qs["oauth_token_secret"];

            request = new RestRequest("oauth/authorize");
            request.AddParameter("oauth_token", oauthToken);
            request.AddParameter("user_name", "*****@*****.**");
            request.AddParameter("password", "twitprachi");
            request.AddParameter("grant_type", "password");

            var url = client.BuildUri(request)
                      .ToString();

            Process.Start(url);

            const string requestUrl = "https://www.prachi.com/?oauth_token=g6o8mAAAAAAA46YAAAABYhuA2DU&oauth_verifier=ZUSV9exrCch0jTOQYxWmszh6aLYyaveo";

            // get the access token
            string e = "https://www.prachi.com/?oauth_token=yu489AAAAAAA46YAAAABYifW0Ac&oauth_verifier=rNZFFL25DHvxmo0qmijN2mKf4SNaszJc";
            var    requestTokenQueryParameters = HttpUtility.ParseQueryString(new Uri(e).Query);
            var    requestVerifier             = requestTokenQueryParameters["oauth_verifier"];


            client.Authenticator = OAuth1Authenticator.ForAccessToken(consumerKey, consumerSecret, oauthToken,
                                                                      oauthTokenSecret, requestVerifier);

            context = new OAuth1Context(consumerKey, consumerSecret, oauthToken, oauthTokenSecret, requestVerifier);
            xas     = new OAuthAuthentication(context);

            request = new RestRequest("oauth/access_token", Method.POST);

            xas.Authenticate(client, request);
            response = client.Execute(request);

            Assert.NotNull(oauthToken);
            Assert.NotNull(oauthTokenSecret);
        }
 public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_ErrorMessage()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?error=access_denied"))
     };
     var ex = await AssertEx.ThrowsAsync<ServerException>(async () => await oAuth.Authenticate());
     Assert.IsNotNull(ex);
     Assert.AreEqual(-1, ex.Code);
     Assert.AreEqual("access_denied", ex.ErrorMessage);
 }
示例#10
0
        public async void TestAuthenticateFailure_FailedToGetAuthorizationCode_ErrorMessage()
        {
            var serverConnectionMock = CreateServerConnectionMockWithContext();
            var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
            {
                GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?error=access_denied"))
            };
            var ex = await AssertEx.ThrowsAsync <ServerException>(async() => await oAuth.Authenticate());

            Assert.IsNotNull(ex);
            Assert.AreEqual(-1, ex.Code);
            Assert.AreEqual("access_denied", ex.ErrorMessage);
        }
示例#11
0
        public async void TestAuthenticateFailure_FailedToGetAccessToken_InvalidJson()
        {
            var serverConnectionMock = CreateServerConnectionMockWithContext();

            serverConnectionMock.Setup(s => s.Post(It.IsAny <Uri>(), null)).ReturnsAsync("###").Verifiable();
            var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
            {
                GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
            };
            await AssertEx.ThrowsAsync <JsonReaderException>(async() => await oAuth.Authenticate());

            serverConnectionMock.Verify();
        }
示例#12
0
        public async void TestAuthenticateFailure_FailedToGetAccessToken_Exception()
        {
            var serverConnectionMock = CreateServerConnectionMockWithContext();

            serverConnectionMock.Setup(s => s.Post(It.IsAny <Uri>(), null)).ThrowsAsync(new Exception("Test message.")).Verifiable();
            var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
            {
                GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
            };
            var ex = await AssertEx.ThrowsAsync <Exception>(async() => await oAuth.Authenticate());

            Assert.AreEqual("Test message.", ex.Message);
            serverConnectionMock.Verify();
        }
示例#13
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var authorizationCode = Request["code"];

            if (!string.IsNullOrEmpty(authorizationCode))
            {
                var oAuthAuthentication = new OAuthAuthentication(new WebRequestService(), new OrkutUriAuthentication(), new OrkutExtractTokenFromText());

                if (oAuthAuthentication.Authenticate(authorizationCode, RequestType.Post))
                {
                    string info;
                    using (var webClient = new WebClient()) {
                        info = webClient.DownloadString(string.Format("https://www.orkut.com/social/rest/people/@me?access_token={0}", oAuthAuthentication.Token));
                    }

                    Response.Write(info);
                }
            }
        }
示例#14
0
        public ActionResult Authentication(string code)
        {
            if (!string.IsNullOrEmpty(code))
            {
                var oAuthAuthentication = new OAuthAuthentication(new WebRequestService(), new OrkutUriAuthentication(), new OrkutExtractTokenFromText());

                if (oAuthAuthentication.Authenticate(code, RequestType.Post))
                {
                    string info;
                    using (var webClient = new WebClient()) {
                        info = webClient.DownloadString(string.Format("https://www.orkut.com/social/rest/people/@me?access_token={0}", oAuthAuthentication.Token));
                    }

                    Response.Write(info);
                }
            }

            return(null);
        }
 public async void TestAuthenticateSuccess()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     var expectedPostUri = new Uri("https://www.douban.com/service/auth2/token?client_id=testClientId12345&client_secret=testClientSecret12345&redirect_uri=http%3A%2F%2Fwww.testredirecturi.com%2F&grant_type=authorization_code&code=testCode");
     serverConnectionMock.Setup(s => s.Post(It.Is<Uri>(u=>u == expectedPostUri), null)).ReturnsAsync(Resource.TestOAuthResponse).Verifiable();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
     };
     var result = await oAuth.Authenticate();
     Assert.IsNotNull(result);
     Assert.IsNotNull(result);
     Assert.IsNotNull(result.AccessToken);
     Assert.IsNotNull(result.RefreshToken);
     Assert.IsNotNull(result.Username);
     Assert.AreNotEqual(0, result.UserId);
     Assert.AreNotEqual(0, result.ExpiresIn);
     serverConnectionMock.Verify();
 }
示例#16
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request["error_reason"]))
            {
                var oAuthAuthentication = new OAuthAuthentication(new WebRequestService(), new FacebookUriAuthentication(Request.IsSecureConnection), new FacebookExtractTokenFromText());
                if (oAuthAuthentication.Authenticate(Request["code"], RequestType.Get))
                {
                    var client       = new FacebookClient(oAuthAuthentication.Token);
                    var info         = client.Get("me?fields=id,name");
                    var facebookId   = (string)info.id;
                    var facebookName = (string)info.name;

                    Response.Write("id:" + facebookId + "- name::" + facebookName);
                }
            }

            Response.Flush();
            Response.End();
        }
示例#17
0
        public async void TestAuthenticateSuccess()
        {
            var serverConnectionMock = CreateServerConnectionMockWithContext();
            var expectedPostUri      = new Uri("https://www.douban.com/service/auth2/token?client_id=testClientId12345&client_secret=testClientSecret12345&redirect_uri=http%3A%2F%2Fwww.testredirecturi.com%2F&grant_type=authorization_code&code=testCode");

            serverConnectionMock.Setup(s => s.Post(It.Is <Uri>(u => u == expectedPostUri), null)).ReturnsAsync(Resource.TestOAuthResponse).Verifiable();
            var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
            {
                GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
            };
            var result = await oAuth.Authenticate();

            Assert.IsNotNull(result);
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.AccessToken);
            Assert.IsNotNull(result.RefreshToken);
            Assert.IsNotNull(result.Username);
            Assert.AreNotEqual(0, result.UserId);
            Assert.AreNotEqual(0, result.ExpiresIn);
            serverConnectionMock.Verify();
        }
 public async void TestAuthenticateFailure_FailedToGetAccessToken_Exception()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     serverConnectionMock.Setup(s => s.Post(It.IsAny<Uri>(), null)).ThrowsAsync(new Exception("Test message.")).Verifiable();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
     };
     var ex = await AssertEx.ThrowsAsync<Exception>(async () => await oAuth.Authenticate());
     Assert.AreEqual("Test message.", ex.Message);
     serverConnectionMock.Verify();
 }
 public async void TestAuthenticateFailure_FailedToGetAccessToken_InvalidJson()
 {
     var serverConnectionMock = CreateServerConnectionMockWithContext();
     serverConnectionMock.Setup(s => s.Post(It.IsAny<Uri>(), null)).ReturnsAsync("###").Verifiable();
     var oAuth = new OAuthAuthentication(serverConnectionMock.Object)
     {
         GetRedirectedUri = uri => Task.FromResult(new Uri("http://www.testRedirectUri.com?code=testCode"))
     };
     await AssertEx.ThrowsAsync<JsonReaderException>(async () => await oAuth.Authenticate());
     serverConnectionMock.Verify();
 }