@Nullable @Override public String getUserId() { if (tokenInfo == null) { throw new IllegalStateException("UserId can only be retrieved after successful authentication."); } return tokenInfo.getUserId(); }
@Nullable @Override public String getUserId() { if (tokenInfo == null) { throw new IllegalStateException("UserId can only be retrieved after successful authentication."); } return tokenInfo.getUserId(); }
@CheckForNull @Override public String getUserId() { if (tokenInfo == null) { throw new IllegalStateException("UserId can only be retrieved after successful authentication."); } return tokenInfo.getUserId(); }
/** * Create the {@code AuthInfo} for the specified {@code tokenInfo} as well as * userId and principals, that have been set upon {@link #login}. * * @param tokenInfo The tokenInfo to retrieve attributes from. * @return The {@code AuthInfo} resulting from the successful login. */ @Nullable private static AuthInfo getAuthInfo(@Nullable TokenInfo tokenInfo, @NotNull Set<? extends Principal> principals) { if (tokenInfo != null) { Map<String, Object> attributes = new HashMap<>(); tokenInfo.getPublicAttributes().forEach((key, value) -> attributes.put(key, value)); return new AuthInfoImpl(tokenInfo.getUserId(), attributes, principals); } else { return null; } }
/** * Create the {@code AuthInfo} for the specified {@code tokenInfo} as well as * userId and principals, that have been set upon {@link #login}. * * @param tokenInfo The tokenInfo to retrieve attributes from. * @return The {@code AuthInfo} resulting from the successful login. */ @Nullable private static AuthInfo getAuthInfo(@Nullable TokenInfo tokenInfo, @NotNull Set<? extends Principal> principals) { if (tokenInfo != null) { Map<String, Object> attributes = new HashMap<>(); tokenInfo.getPublicAttributes().forEach((key, value) -> attributes.put(key, value)); return new AuthInfoImpl(tokenInfo.getUserId(), attributes, principals); } else { return null; } }
/** * Create the {@code AuthInfo} for the specified {@code tokenInfo} as well as * userId and principals, that have been set upon {@link #login}. * * @param tokenInfo The tokenInfo to retrieve attributes from. * @return The {@code AuthInfo} resulting from the successful login. */ @CheckForNull private AuthInfo getAuthInfo(@Nullable TokenInfo tokenInfo, @Nonnull Set<? extends Principal> principals) { if (tokenInfo != null) { Map<String, Object> attributes = new HashMap<String, Object>(); Map<String, String> publicAttributes = tokenInfo.getPublicAttributes(); for (String attrName : publicAttributes.keySet()) { attributes.put(attrName, publicAttributes.get(attrName)); } return new AuthInfoImpl(tokenInfo.getUserId(), attributes, principals); } else { return null; } }
private static void assertTokenInfo(TokenInfo info, String userId) { assertNotNull(info); assertNotNull(info.getToken()); assertEquals(userId, info.getUserId()); assertFalse(info.isExpired(new Date().getTime())); }
@Test public void testGetTokenInfoAfterAuthenticate() throws Exception { TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap()); authentication.authenticate(new TokenCredentials(info.getToken())); TokenInfo info2 = authentication.getTokenInfo(); assertNotNull(info2); assertEquals(info.getUserId(), info2.getUserId()); }
/** * @see <a href="https://issues.apache.org/jira/browse/OAK-1985">OAK-1985</a> */ @Test public void testTokenValidationIsCaseInsensitive() throws Exception { Root root = adminSession.getLatestRoot(); TokenConfiguration tokenConfig = getSecurityProvider().getConfiguration(TokenConfiguration.class); TokenProvider tp = tokenConfig.getTokenProvider(root); String userId = ((SimpleCredentials) getAdminCredentials()).getUserID(); TokenInfo info = tp.createToken(userId.toUpperCase(), Collections.<String, Object>emptyMap()); assertTrue(info.matches(new TokenCredentials(info.getToken()))); assertEquals(userId, info.getUserId()); info = tp.getTokenInfo(info.getToken()); assertTrue(info.matches(new TokenCredentials(info.getToken()))); assertEquals(userId, info.getUserId()); }