コード例 #1
0
        public void TestExchangingRefreshTokenForWrongClient()
        {
            ClientBase client = new ClientBase {
                ClientId = "id", ClientSecret = "secret"
            };

            TokenContext context = new TokenContext
            {
                RefreshToken = "refresh_token",
                Client       = new ClientBase {
                    ClientId = "id"
                },
                ResourceOwnerUsername = "******"
            };
            RefreshTokenBase refreshToken = new RefreshTokenBase
            {
                ClientId = "123",
                Scope    = new string[] { "create", "delete" },
                Token    = "refresh_token"
            };

            Mock <IClientService> mckClientService = new Mock <IClientService>();

            mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true);
            mckClientService.Setup(x => x.FindClient("id")).Returns(client);

            Mock <ITokenService> mckTokenService = new Mock <ITokenService>();

            mckTokenService.Setup(x => x.FindRefreshToken("refresh_token")).Returns(refreshToken);

            Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>();

            mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object);
            mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object);

            RefreshTokenProcessor processor = new RefreshTokenProcessor(mckFactory.Object);

            try
            {
                processor.Process(context);
                Assert.Fail("no exception trhown");
            }
            catch (OAuthErrorResponseException <IOAuthContext> x)
            {
                Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.InvalidClient, x.Error);
            }
            catch (Exception x)
            {
                Assert.Fail("unexpected exception: " + x.Message);
            }

            mckFactory.VerifyAll();
            mckClientService.VerifyAll();
            mckTokenService.VerifyAll();
        }
コード例 #2
0
        public void TestExchangingRefreshTokenForAccessToken()
        {
            ClientBase client = new ClientBase {
                ClientId = "id", ClientSecret = "secret"
            };
            AccessTokenBase token = new AccessTokenBase {
                ExpiresIn    = 120,
                RefreshToken = "refresh_token",
                Token        = "new-token",
                TokenType    = Parameters.AccessTokenTypeValues.Bearer
            };
            TokenContext context = new TokenContext
            {
                RefreshToken          = "refresh_token",
                Client                = client,
                ResourceOwnerUsername = "******"
            };
            RefreshTokenBase refreshToken = new RefreshTokenBase
            {
                ClientId = "id",
                Scope    = new string[] { "create", "delete" },
                Token    = "refresh_token"
            };

            Mock <IClientService> mckClientService = new Mock <IClientService>();

            mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true);
            mckClientService.Setup(x => x.FindClient("id")).Returns(client);

            Mock <ITokenService> mckTokenService = new Mock <ITokenService>();

            mckTokenService.Setup(x => x.FindRefreshToken("refresh_token")).Returns(refreshToken);
            mckTokenService.Setup(x => x.IssueAccessToken(refreshToken)).Returns(token);

            Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>();

            mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object);
            mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object);

            RefreshTokenProcessor processor = new RefreshTokenProcessor(mckFactory.Object);

            processor.Process(context);

            Assert.AreEqual(token, context.Token);

            mckFactory.VerifyAll();
            mckClientService.VerifyAll();
            mckTokenService.VerifyAll();
        }
コード例 #3
0
 private static void AssertExceptionResponse(TokenContext context, RefreshTokenProcessor processor, string parameter, int httpStatusCode)
 {
     try
     {
         processor.Process(context);
         Assert.Fail("no exception thrown");
     }
     catch (OAuthErrorResponseException <ITokenContext> x)
     {
         Assert.AreEqual(parameter, x.Error);
         Assert.AreEqual(httpStatusCode, x.HttpStatusCode);
     }
     catch (Exception x)
     {
         Assert.Fail("Unexpected exception: " + x.Message);
     }
 }
コード例 #4
0
        public void TestExchangingRefreshTokenForAccessToken()
        {
            ClientBase client = new ClientBase{ ClientId = "id", ClientSecret = "secret" };
            AccessTokenBase token = new AccessTokenBase{
                ExpiresIn = 120,
                RefreshToken = "refresh_token",
                Token = "new-token",
                TokenType = Parameters.AccessTokenTypeValues.Bearer
            };
            TokenContext context = new TokenContext
            {
                RefreshToken = "refresh_token",
                Client = client,
                ResourceOwnerUsername = "******"
            };
            RefreshTokenBase refreshToken = new RefreshTokenBase
            {
                ClientId = "id",
                Scope = new string[] { "create", "delete" },
                Token = "refresh_token"
            };

            Mock<IClientService> mckClientService = new Mock<IClientService>();
            mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true);
            mckClientService.Setup(x => x.FindClient("id")).Returns(client);

            Mock<ITokenService> mckTokenService = new Mock<ITokenService>();
            mckTokenService.Setup(x => x.FindRefreshToken("refresh_token")).Returns(refreshToken);
            mckTokenService.Setup(x => x.IssueAccessToken(refreshToken)).Returns(token);

            Mock<IServiceFactory> mckFactory = new Mock<IServiceFactory>();
            mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object);
            mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object);

            RefreshTokenProcessor processor = new RefreshTokenProcessor(mckFactory.Object);

            processor.Process(context);

            Assert.AreEqual(token, context.Token);

            mckFactory.VerifyAll();
            mckClientService.VerifyAll();
            mckTokenService.VerifyAll();
        }
コード例 #5
0
        public void TestExchangingRefreshTokenForWrongClient()
        {
            ClientBase client = new ClientBase { ClientId = "id", ClientSecret = "secret" };
            
            TokenContext context = new TokenContext
            {
                RefreshToken = "refresh_token",
                Client = new ClientBase { ClientId = "id" },
                ResourceOwnerUsername = "******"
            };
            RefreshTokenBase refreshToken = new RefreshTokenBase
            {
                ClientId = "123",
                Scope = new string[] { "create", "delete" },
                Token = "refresh_token"
            };

            Mock<IClientService> mckClientService = new Mock<IClientService>();
            mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true);
            mckClientService.Setup(x => x.FindClient("id")).Returns(client);

            Mock<ITokenService> mckTokenService = new Mock<ITokenService>();
            mckTokenService.Setup(x => x.FindRefreshToken("refresh_token")).Returns(refreshToken);

            Mock<IServiceFactory> mckFactory = new Mock<IServiceFactory>();
            mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object);
            mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object);

            RefreshTokenProcessor processor = new RefreshTokenProcessor(mckFactory.Object);

            try
            {
                processor.Process(context);
                Assert.Fail("no exception trhown");
            }
            catch (OAuthErrorResponseException<IOAuthContext> x)
            {
                Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.InvalidClient, x.Error);
            }
            catch (Exception x)
            {
                Assert.Fail("unexpected exception: " + x.Message);
            }

            mckFactory.VerifyAll();
            mckClientService.VerifyAll();
            mckTokenService.VerifyAll();
        }
コード例 #6
0
 private static void AssertExceptionResponse(TokenContext context, RefreshTokenProcessor processor, string parameter, int httpStatusCode)
 {
     try
     {
         processor.Process(context);
         Assert.Fail("no exception thrown");
     }
     catch (OAuthErrorResponseException<ITokenContext> x)
     {
         Assert.AreEqual(parameter, x.Error);
         Assert.AreEqual(httpStatusCode, x.HttpStatusCode);
     }
     catch (Exception x)
     {
         Assert.Fail("Unexpected exception: " + x.Message);
     }
 }