public void TestProcessingContext() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "BEARER my-token"; AccessTokenBase result = new AccessTokenBase() { Token = "my-token" }; Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.FindToken("my-token")).Returns(result); Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>(); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); BearerProcessor processor = new BearerProcessor(mckFactory.Object); processor.Process(context); Assert.AreEqual(result, context.Token); mckTokenService.VerifyAll(); mckFactory.VerifyAll(); }
public void TestAccessingExpiredTokenResource() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "bearer my-token"; IAccessToken expiredToken = new AccessTokenBase { Token = "my-token", ExpiresIn = 2, IssuedOn = DateTime.Now.AddMinutes(-1).ToEpoch() }; Mock <ContextProcessor <IResourceContext> > mckProvider = new Mock <ContextProcessor <IResourceContext> >(new Mock <IServiceFactory>().Object); mckProvider.Setup(x => x.IsSatisfiedBy(context)).Returns(true); mckProvider.Setup(x => x.Process(context)).Callback(() => context.Token = expiredToken); SimpleServiceLocator container = new SimpleServiceLocator(); container.RegisterAll <ContextProcessor <IResourceContext> >(mckProvider.Object); ServiceLocator.SetLocatorProvider(() => container); ResourceProvider provider = new ResourceProvider(); CommonErrorAssert(context, provider, Parameters.ErrorParameters.ErrorValues.InvalidToken); mckProvider.Verify(); }
public void IssueAccessTokenForClientCredentials() { ClientBase cl = new ClientBase { ClientId = "123", ClientSecret = "secret" }; AccessTokenBase token = new AccessTokenBase{ ExpiresIn = 120, Token = Guid.NewGuid().ToString()}; TokenContext context = new TokenContext { Client = new ClientBase { ClientId = "123", ClientSecret = "secret" }, GrantType = Parameters.GrantTypeValues.ClientCredentials }; Mock<IClientService> mckClientService = new Mock<IClientService>(); mckClientService.Setup(x=>x.AuthenticateClient(context)).Returns(true); mckClientService.Setup(x => x.FindClient("123")).Returns(cl); Mock<ITokenService> mckTokenService = new Mock<ITokenService>(); mckTokenService.Setup(x=>x.IssueAccessToken(cl)).Returns(token); Mock<IServiceFactory> mckServiceFactory = new Mock<IServiceFactory>(); mckServiceFactory.SetupGet(x=>x.ClientService).Returns(mckClientService.Object); mckServiceFactory.SetupGet(x=>x.TokenService).Returns(mckTokenService.Object); ClientCredentialsProcessor processor = new ClientCredentialsProcessor(mckServiceFactory.Object); processor.Process(context); mckClientService.VerifyAll(); mckServiceFactory.VerifyAll(); mckTokenService.VerifyAll(); Assert.AreEqual(token, context.Token); }
public void TestProcessTokenRequest() { AccessTokenBase token = new AccessTokenBase { Scope = new string[] { "create", "delete" }, ExpiresIn = 120, RefreshToken = "refresh", Token = "token", TokenType = "bearer" }; ClientBase client = new ClientBase { ClientId = "123", ClientSecret = "secret" }; IAuthorizationGrant grant = new AuthorizationGrantBase { Code = "123" }; AuthorizationContext context = new AuthorizationContext { Client = new ClientBase { ClientId = "123" }, IsApproved = true, RedirectUri = new Uri("http://www.mysite.com/callback"), Scope = new string[] { "create", "delete" }, ResourceOwnerUsername = "******" }; Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.FindClient("123")).Returns(client); Mock <IAuthorizationGrantService> mckGrantService = new Mock <IAuthorizationGrantService>(); mckGrantService.Setup(x => x.IssueAuthorizationGrant(context)).Returns(grant); Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.IssueAccessToken(grant)).Returns(token); Mock <IServiceFactory> mckFactory = new Mock <IServiceFactory>(); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckFactory.SetupGet(x => x.AuthorizationGrantService).Returns(mckGrantService.Object); ImplicitFlowProcessor processor = new ImplicitFlowProcessor(mckFactory.Object); processor.Process(context); Assert.AreEqual(token, context.Token); mckClientService.VerifyAll(); mckFactory.VerifyAll(); mckTokenService.VerifyAll(); }
public void TestProcessTokenRequest() { AccessTokenBase token = new AccessTokenBase { Scope = new string[] { "create", "delete" }, ExpiresIn = 120, RefreshToken = "refresh", Token = "token", TokenType = "bearer" }; ClientBase client = new ClientBase { ClientId = "123", ClientSecret = "secret" }; IAuthorizationGrant grant = new AuthorizationGrantBase { Code = "123" }; AuthorizationContext context = new AuthorizationContext { Client = new ClientBase { ClientId = "123" }, IsApproved = true, RedirectUri = new Uri("http://www.mysite.com/callback"), Scope = new string[] { "create", "delete" }, ResourceOwnerUsername = "******" }; Mock<IClientService> mckClientService = new Mock<IClientService>(); mckClientService.Setup(x => x.FindClient("123")).Returns(client); Mock<IAuthorizationGrantService> mckGrantService = new Mock<IAuthorizationGrantService>(); mckGrantService.Setup(x => x.IssueAuthorizationGrant(context)).Returns(grant); Mock<ITokenService> mckTokenService = new Mock<ITokenService>(); mckTokenService.Setup(x => x.IssueAccessToken(grant)).Returns(token); Mock<IServiceFactory> mckFactory = new Mock<IServiceFactory>(); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckFactory.SetupGet(x => x.AuthorizationGrantService).Returns(mckGrantService.Object); ImplicitFlowProcessor processor = new ImplicitFlowProcessor(mckFactory.Object); processor.Process(context); Assert.AreEqual(token, context.Token); mckClientService.VerifyAll(); mckFactory.VerifyAll(); mckTokenService.VerifyAll(); }
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(); }
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(); }
public void TestIssueingAccessToken() { TokenContext context = new TokenContext(); context.GrantType = Parameters.GrantTypeValues.Password; context.Client = new ClientBase { ClientId = "123", ClientSecret = "secret" }; context.ResourceOwnerPassword = "******"; context.ResourceOwnerUsername = "******"; AccessTokenBase token = new AccessTokenBase(); Mock <IResourceOwnerService> mckResourceOwnerService = new Mock <IResourceOwnerService>(); mckResourceOwnerService.Setup(x => x.CredentialsAreValid(context)).Returns(true); Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true); Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.IssueAccessTokenForResourceOwner(context)).Returns(token); Mock <IServiceFactory> mckServicFactory = new Mock <IServiceFactory>(); mckServicFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckServicFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckServicFactory.SetupGet(x => x.ResourceOwnerService).Returns(mckResourceOwnerService.Object); ResourceOwnerPasswordCredentialProcessor processor = new ResourceOwnerPasswordCredentialProcessor(mckServicFactory.Object); processor.Process(context); Assert.AreEqual(token, context.Token); mckClientService.VerifyAll(); mckResourceOwnerService.VerifyAll(); mckServicFactory.VerifyAll(); mckTokenService.VerifyAll(); }
public void IssueAccessTokenForInvalidClientCredentials() { ClientBase cl = new ClientBase { ClientId = "123", ClientSecret = "secret" }; AccessTokenBase token = new AccessTokenBase { ExpiresIn = 120, Token = Guid.NewGuid().ToString() }; TokenContext context = new TokenContext { Client = cl, GrantType = Parameters.GrantTypeValues.ClientCredentials }; Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(false); Mock <IServiceFactory> mckServiceFactory = new Mock <IServiceFactory>(); mckServiceFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); ClientCredentialsProcessor processor = new ClientCredentialsProcessor(mckServiceFactory.Object); try { processor.Process(context); Assert.Fail("no exception thrown"); } catch (OAuthErrorResponseException <ITokenContext> x) { Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.UnauthorizedClient, x.Error); Assert.AreEqual(401, x.HttpStatusCode); } catch (Exception x) { Assert.Fail("unexpected exception was thrown: " + x.Message); } mckClientService.VerifyAll(); mckServiceFactory.VerifyAll(); }
public void TestIssueingAccessToken() { TokenContext context = new TokenContext(); context.GrantType = Parameters.GrantTypeValues.Password; context.Client = new ClientBase { ClientId = "123", ClientSecret = "secret" }; context.ResourceOwnerPassword = "******"; context.ResourceOwnerUsername = "******"; AccessTokenBase token = new AccessTokenBase(); Mock<IResourceOwnerService> mckResourceOwnerService = new Mock<IResourceOwnerService>(); mckResourceOwnerService.Setup(x => x.CredentialsAreValid(context)).Returns(true); Mock<IClientService> mckClientService = new Mock<IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true); Mock<ITokenService> mckTokenService = new Mock<ITokenService>(); mckTokenService.Setup(x => x.IssueAccessTokenForResourceOwner(context)).Returns(token); Mock<IServiceFactory> mckServicFactory = new Mock<IServiceFactory>(); mckServicFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckServicFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); mckServicFactory.SetupGet(x => x.ResourceOwnerService).Returns(mckResourceOwnerService.Object); ResourceOwnerPasswordCredentialProcessor processor = new ResourceOwnerPasswordCredentialProcessor(mckServicFactory.Object); processor.Process(context); Assert.AreEqual(token, context.Token); mckClientService.VerifyAll(); mckResourceOwnerService.VerifyAll(); mckServicFactory.VerifyAll(); mckTokenService.VerifyAll(); }
public void ConvertAccessTokenToNameValuePairs() { AccessTokenBase token = new AccessTokenBase { ExpiresIn = 120, RefreshToken = "refresh_token", Scope = new string[] { "create", "delete" }, Token = "token", TokenType = "bearer" }; token.Parameters["token_secret"] = "secret_val"; IDictionary <string, object> results = token.ToResponseValues(); Assert.AreEqual(120, results[Parameters.AccessTokenExpiresIn], Parameters.AccessTokenExpiresIn); Assert.AreEqual("refresh_token", results[Parameters.RefreshToken], Parameters.RefreshToken); Assert.AreEqual("create delete", results[Parameters.Scope], Parameters.Scope); Assert.AreEqual("token", results[Parameters.AccessToken], Parameters.AccessToken); Assert.AreEqual("bearer", results[Parameters.AccessTokenType], Parameters.AccessTokenType); Assert.AreEqual("secret_val", results["token_secret"]); }
public void IssueAccessTokenForClientCredentials() { ClientBase cl = new ClientBase { ClientId = "123", ClientSecret = "secret" }; AccessTokenBase token = new AccessTokenBase { ExpiresIn = 120, Token = Guid.NewGuid().ToString() }; TokenContext context = new TokenContext { Client = new ClientBase { ClientId = "123", ClientSecret = "secret" }, GrantType = Parameters.GrantTypeValues.ClientCredentials }; Mock <IClientService> mckClientService = new Mock <IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(true); mckClientService.Setup(x => x.FindClient("123")).Returns(cl); Mock <ITokenService> mckTokenService = new Mock <ITokenService>(); mckTokenService.Setup(x => x.IssueAccessToken(cl)).Returns(token); Mock <IServiceFactory> mckServiceFactory = new Mock <IServiceFactory>(); mckServiceFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); mckServiceFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); ClientCredentialsProcessor processor = new ClientCredentialsProcessor(mckServiceFactory.Object); processor.Process(context); mckClientService.VerifyAll(); mckServiceFactory.VerifyAll(); mckTokenService.VerifyAll(); Assert.AreEqual(token, context.Token); }
public void IssueAccessTokenForInvalidClientCredentials() { ClientBase cl = new ClientBase { ClientId = "123", ClientSecret = "secret" }; AccessTokenBase token = new AccessTokenBase { ExpiresIn = 120, Token = Guid.NewGuid().ToString() }; TokenContext context = new TokenContext { Client = cl, GrantType = Parameters.GrantTypeValues.ClientCredentials }; Mock<IClientService> mckClientService = new Mock<IClientService>(); mckClientService.Setup(x => x.AuthenticateClient(context)).Returns(false); Mock<IServiceFactory> mckServiceFactory = new Mock<IServiceFactory>(); mckServiceFactory.SetupGet(x => x.ClientService).Returns(mckClientService.Object); ClientCredentialsProcessor processor = new ClientCredentialsProcessor(mckServiceFactory.Object); try { processor.Process(context); Assert.Fail("no exception thrown"); } catch (OAuthErrorResponseException<ITokenContext> x) { Assert.AreEqual(Parameters.ErrorParameters.ErrorValues.UnauthorizedClient, x.Error); Assert.AreEqual(401, x.HttpStatusCode); } catch (Exception x) { Assert.Fail("unexpected exception was thrown: " + x.Message); } mckClientService.VerifyAll(); mckServiceFactory.VerifyAll(); }
public void TestProcessingContext() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "BEARER my-token"; AccessTokenBase result = new AccessTokenBase() { Token = "my-token" }; Mock<ITokenService> mckTokenService = new Mock<ITokenService>(); mckTokenService.Setup(x => x.FindToken("my-token")).Returns(result); Mock<IServiceFactory> mckFactory = new Mock<IServiceFactory>(); mckFactory.SetupGet(x => x.TokenService).Returns(mckTokenService.Object); BearerProcessor processor = new BearerProcessor(mckFactory.Object); processor.Process(context); Assert.AreEqual(result, context.Token); mckTokenService.VerifyAll(); mckFactory.VerifyAll(); }
public void TestAccessingExpiredTokenResource() { ResourceContext context = new ResourceContext(); context.Headers = new NameValueCollection(); context.Headers["Authorization"] = "bearer my-token"; IAccessToken expiredToken = new AccessTokenBase { Token = "my-token", ExpiresIn = 2, IssuedOn = DateTime.Now.AddMinutes(-1).ToEpoch() }; Mock<ContextProcessor<IResourceContext>> mckProvider = new Mock<ContextProcessor<IResourceContext>>(new Mock<IServiceFactory>().Object); mckProvider.Setup(x => x.IsSatisfiedBy(context)).Returns(true); mckProvider.Setup(x => x.Process(context)).Callback(() => context.Token = expiredToken); SimpleServiceLocator container = new SimpleServiceLocator(); container.RegisterAll<ContextProcessor<IResourceContext>>(mckProvider.Object); ServiceLocator.SetLocatorProvider(() => container); ResourceProvider provider = new ResourceProvider(); CommonErrorAssert(context, provider, Parameters.ErrorParameters.ErrorValues.InvalidToken); mckProvider.Verify(); }