ClientCredentialsGrantHandler { public TokenResponseEntity handle(final Client client, final String scope, final String state) { if (!client.getType().equals(ClientType.ClientCredentials)) { throw new InvalidGrantException(); } if (StringUtils.isEmpty(client.getClientSecret())) { throw new UnauthorizedClientException(); } SortedMap<String, ApplicationScope> requestedScopes = ValidationUtil.validateScope(scope, client.getApplication().getScopes()); OAuthToken token = new OAuthToken(); token.setClient(client); token.setTokenType(OAuthTokenType.Bearer); token.setExpiresIn(client.getAccessTokenExpireIn()); token.setScopes(requestedScopes); token.setIssuer(uriInfo.getAbsolutePath().getHost()); session.save(token); return TokenResponseEntity.factory(token, state); } @Inject  ClientCredentialsGrantHandler(final Session session,
                                         @Context final UriInfo uriInfo); TokenResponseEntity handle(final Client client,
                                      final String scope,
                                      final String state);  }
@Test public void testValidRequest() { Client testClient = getSession() .get(Client.class, context.getClient().getId()); TokenResponseEntity token = handler.handle(testClient, "debug", null); assertEquals(OAuthTokenType.Bearer, token.getTokenType()); assertEquals((long) ClientConfig.ACCESS_TOKEN_EXPIRES_DEFAULT, (long) token.getExpiresIn()); assertNull(token.getRefreshToken()); assertEquals("debug", token.getScope()); assertNotNull(token.getAccessToken()); }
@Test(expected = KangarooException.class) public void testInvalidClientType() { handler.handle(implicitContext.getClient(), "debug", null); }
@Test(expected = KangarooException.class) public void testNoClientSecret() { handler.handle(noSecretContext.getClient(), "debug", null); }
@Test public void testInvalidScope() { Client testClient = getSession() .get(Client.class, context.getClient().getId()); TokenResponseEntity token = handler.handle(testClient, "debug", null); assertEquals(OAuthTokenType.Bearer, token.getTokenType()); assertEquals((long) ClientConfig.ACCESS_TOKEN_EXPIRES_DEFAULT, (long) token.getExpiresIn()); assertNull(token.getRefreshToken()); assertEquals("debug", token.getScope()); assertNotNull(token.getAccessToken()); }
@Test public void testNoScope() { Client c = noScopeContext.getClient(); TokenResponseEntity token = handler.handle(c, "debug", null); assertEquals(OAuthTokenType.Bearer, token.getTokenType()); assertEquals((long) ClientConfig.ACCESS_TOKEN_EXPIRES_DEFAULT, (long) token.getExpiresIn()); assertNull(token.getRefreshToken()); assertNull(token.getScope()); assertNotNull(token.getAccessToken()); }