TokenProviderImpl implements TokenProvider, TokenConstants { @Nullable @Override public TokenInfo createToken(@NotNull Credentials credentials) { Credentials creds = extractCredentials(credentials); String uid = (creds != null) ? credentialsSupport.getUserId(creds) : null; TokenInfo tokenInfo = null; if (uid != null) { Map<String, ?> attributes = credentialsSupport.getAttributes(creds); tokenInfo = createToken(uid, attributes); if (tokenInfo != null) { if (!credentialsSupport.setAttributes(creds, ImmutableMap.of(TOKEN_ATTRIBUTE, tokenInfo.getToken()))) { log.debug("Cannot set token attribute to {}", creds); } } } return tokenInfo; } TokenProviderImpl(@NotNull Root root, @NotNull ConfigurationParameters options, @NotNull UserConfiguration userConfiguration);   TokenProviderImpl(@NotNull Root root, @NotNull ConfigurationParameters options, @NotNull UserConfiguration userConfiguration, @NotNull CredentialsSupport credentialsSupport); @Override boolean doCreateToken(@NotNull Credentials credentials); @Nullable @Override TokenInfo createToken(@NotNull Credentials credentials); @Nullable @Override TokenInfo createToken(@NotNull String userId, @NotNull Map<String, ?> attributes); @Nullable @Override TokenInfo getTokenInfo(@NotNull String token);  }
@Test public void testCreateTokenFromInvalidCredentials() { List<Credentials> invalid = new ArrayList<Credentials>(); invalid.add(new GuestCredentials()); invalid.add(new TokenCredentials("sometoken")); invalid.add(new ImpersonationCredentials(new GuestCredentials(), null)); invalid.add(new SimpleCredentials("unknownUserId", new char[0])); for (Credentials creds : invalid) { assertNull(tokenProvider.createToken(creds)); } }
@Test public void testCreateTokenFromCredentials() { SimpleCredentials sc = new SimpleCredentials(userId, new char[0]); List<Credentials> valid = new ArrayList<Credentials>(); valid.add(sc); valid.add(new ImpersonationCredentials(sc, null)); for (Credentials creds : valid) { TokenInfo info = tokenProvider.createToken(creds); assertTokenInfo(info, userId); } }
@Test public void testCreateTokenFromCredentialsSetsAttribute() { SimpleCredentials sc = new SimpleCredentials(userId, new char[0]); tokenProvider.createToken(sc); assertArrayEquals(new String[] {TOKEN_ATTRIBUTE}, sc.getAttributeNames()); }
@Test public void testCreateTokenCredentialsSupportDoesntSetAttribute() { SimpleCredentials sc = new SimpleCredentials(userId, new char[0]); CredentialsSupport credentialsSupport = mock(CredentialsSupport.class); when(credentialsSupport.getCredentialClasses()).thenReturn(SimpleCredentialsSupport.getInstance().getCredentialClasses()); when(credentialsSupport.getUserId(sc)).thenReturn(SimpleCredentialsSupport.getInstance().getUserId(sc)); when(credentialsSupport.setAttributes(any(Credentials.class), any(Map.class))).thenReturn(false); TokenProvider tp = createTokenProvider(root, getTokenConfig(), getUserConfiguration(), credentialsSupport); tp.createToken(sc); assertEquals(0, sc.getAttributeNames().length); }
@Test public void testCreateTokenInvalidAlgorithm() { SimpleCredentials sc = new SimpleCredentials(userId, new char[0]); ConfigurationParameters options = ConfigurationParameters.of(UserConstants.PARAM_PASSWORD_HASH_ALGORITHM, "invalid"); TokenProvider tp = createTokenProvider(root, options, getUserConfiguration(), SimpleCredentialsSupport.getInstance()); assertNull(tp.createToken(sc)); }
@Test public void testCreateTokenFromInvalidUserId() { TokenInfo info = tokenProvider.createToken("unknownUserId", Collections.<String, Object>emptyMap()); assertNull(info); }
@Test public void testCreateTokenFromGroupId() throws Exception { Group gr = getUserManager(root).createGroup("groupId"); assertNull(tokenProvider.createToken("groupId", Collections.<String, Object>emptyMap())); }
@Test public void testCreateTokenFromUserId() { TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap()); assertTokenInfo(info, userId); }
@Test public void testCreateTokenWithInvalidExpirationParam() { SimpleCredentials sc = new SimpleCredentials(userId, new char[0]); sc.setAttribute(TokenProvider.PARAM_TOKEN_EXPIRATION, "invalid"); try { tokenProvider.createToken(sc); fail(); } catch (NumberFormatException e) { } }