use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.
the class TokenAuthenticationTest method testGetUserPrincipal.
@Test
public void testGetUserPrincipal() throws Exception {
TokenInfo info = tokenProvider.createToken(userId, Collections.<String, Object>emptyMap());
assertTrue(authentication.authenticate(new TokenCredentials(info.getToken())));
assertEquals(getTestUser().getPrincipal(), authentication.getUserPrincipal());
}
use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.
the class TokenAuthenticationTest method testAuthenticateExpiredToken.
@Test
public void testAuthenticateExpiredToken() throws Exception {
TokenProvider tp = new TokenProviderImpl(root, ConfigurationParameters.of(TokenProvider.PARAM_TOKEN_EXPIRATION, 1), getUserConfiguration());
TokenInfo info = tp.createToken(userId, Collections.<String, Object>emptyMap());
waitUntilExpired(info);
try {
new TokenAuthentication(tp).authenticate(new TokenCredentials(info.getToken()));
fail("LoginException expected");
} catch (LoginException e) {
// success
}
// expired token must have been removed
assertNull(tp.getTokenInfo(info.getToken()));
}
use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.
the class Jackrabbit2ConfigurationTest method testValidTokenCredentials.
@Test
public void testValidTokenCredentials() throws Exception {
Root root = adminSession.getLatestRoot();
TokenConfiguration tc = getSecurityProvider().getConfiguration(TokenConfiguration.class);
TokenProvider tp = tc.getTokenProvider(root);
SimpleCredentials sc = (SimpleCredentials) getAdminCredentials();
TokenInfo info = tp.createToken(sc.getUserID(), Collections.<String, Object>emptyMap());
ContentSession cs = login(new TokenCredentials(info.getToken()));
try {
assertEquals(sc.getUserID(), cs.getAuthInfo().getUserID());
} finally {
cs.close();
}
}
use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.
the class TokenLoginModule method commit.
@Override
public boolean commit() throws LoginException {
if (tokenCredentials != null && userId != null) {
Set<? extends Principal> principals = (principal != null) ? getPrincipals(principal) : getPrincipals(userId);
updateSubject(tokenCredentials, getAuthInfo(tokenInfo, principals), principals);
return true;
}
try {
if (tokenProvider != null && sharedState.containsKey(SHARED_KEY_CREDENTIALS)) {
Credentials shared = getSharedCredentials();
if (shared != null && tokenProvider.doCreateToken(shared)) {
Root r = getRoot();
if (r != null) {
// refresh root, in case the external login module created users
r.refresh();
}
TokenInfo ti = tokenProvider.createToken(shared);
if (ti != null) {
TokenCredentials tc = new TokenCredentials(ti.getToken());
Map<String, String> attributes = ti.getPrivateAttributes();
for (String name : attributes.keySet()) {
tc.setAttribute(name, attributes.get(name));
}
attributes = ti.getPublicAttributes();
for (String name : attributes.keySet()) {
tc.setAttribute(name, attributes.get(name));
}
sharedState.put(SHARED_KEY_ATTRIBUTES, attributes);
updateSubject(tc, null, null);
} else {
// failed to create token -> fail commit()
Object logId = (userId != null) ? userId : sharedState.get(SHARED_KEY_LOGIN_NAME);
log.debug("TokenProvider failed to create a login token for user " + logId);
throw new LoginException("Failed to create login token for user " + logId);
}
}
}
} finally {
// the login attempt on this module did not succeed: clear state
clearState();
}
return false;
}
use of org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo in project jackrabbit-oak by apache.
the class TokenProviderImpl method createToken.
/**
* Create a separate token node underneath a dedicated token store within
* the user home node. That token node contains the hashed token, the
* expiration time and additional mandatory attributes that will be verified
* during login.
*
* @param userId The identifier of the user for which a new token should
* be created.
* @param attributes The attributes associated with the new token.
* @return A new {@code TokenInfo} or {@code null} if the token could not
* be created.
*/
@Override
public TokenInfo createToken(@Nonnull String userId, @Nonnull Map<String, ?> attributes) {
String error = "Failed to create login token. {}";
User user = getUser(userId);
Tree tokenParent = (user == null) ? null : getTokenParent(user);
if (tokenParent != null) {
try {
String id = user.getID();
long creationTime = new Date().getTime();
long exp;
if (attributes.containsKey(PARAM_TOKEN_EXPIRATION)) {
exp = Long.parseLong(attributes.get(PARAM_TOKEN_EXPIRATION).toString());
} else {
exp = tokenExpiration;
}
long expTime = createExpirationTime(creationTime, exp);
String uuid = UUID.randomUUID().toString();
TokenInfo tokenInfo;
try {
String tokenName = generateTokenName(creationTime);
tokenInfo = createTokenNode(tokenParent, tokenName, expTime, uuid, id, attributes);
root.commit(CommitMarker.asCommitAttributes());
} catch (CommitFailedException e) {
// conflict while creating token node -> retry
log.debug("Failed to create token node. Using random name as fallback.");
root.refresh();
tokenInfo = createTokenNode(tokenParent, UUID.randomUUID().toString(), expTime, uuid, id, attributes);
root.commit(CommitMarker.asCommitAttributes());
}
return tokenInfo;
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
// error while generating login token
log.error(error, e.getMessage());
} catch (CommitFailedException | RepositoryException e) {
// conflict while committing changes
log.warn(error, e.getMessage());
}
} else {
log.warn("Unable to get/create token store for user " + userId);
}
return null;
}
Aggregations