Principal pr = user.getPrincipal();
if (pr instanceof ItemBasedPrincipal) {
userPath = ((ItemBasedPrincipal) pr).getPath();
}
TokenCredentials tokenCredentials;
if (userPath != null && session.nodeExists(userPath)) {
Node userNode = session.getNode(userPath);
Node tokenParent;
if (userNode.hasNode(TOKENS_NODE_NAME)) {
tokenParent = userNode.getNode(TOKENS_NODE_NAME);
} else {
tokenParent = userNode.addNode(TOKENS_NODE_NAME, TOKENS_NT_NAME);
}
long creationTime = new Date().getTime();
long expirationTime = creationTime + tokenExpiration;
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(creationTime);
// generate key part of the login token
String key = generateKey(8);
// create the token node
String tokenName = Text.replace(ISO8601.format(cal), ":", ".");
Node tokenNode;
// avoid usage of sequential nodeIDs
if (System.getProperty(NodeIdFactory.SEQUENTIAL_NODE_ID) == null) {
tokenNode = tokenParent.addNode(tokenName);
} else {
tokenNode = ((NodeImpl) tokenParent).addNodeWithUuid(tokenName, NodeId.randomId().toString());
}
StringBuilder sb = new StringBuilder(tokenNode.getIdentifier());
sb.append(DELIM).append(key);
String token = sb.toString();
tokenCredentials = new TokenCredentials(token);
credentials.setAttribute(TOKEN_ATTRIBUTE, token);
// add key property
tokenNode.setProperty(TOKEN_ATTRIBUTE_KEY, getDigestedKey(key));
// add expiration time property
cal.setTimeInMillis(expirationTime);
tokenNode.setProperty(TOKEN_ATTRIBUTE_EXPIRY, session.getValueFactory().createValue(cal));
// add additional attributes passed in by the credentials.
for (String name : credentials.getAttributeNames()) {
if (!TOKEN_ATTRIBUTE.equals(name)) {
String value = credentials.getAttribute(name).toString();
tokenNode.setProperty(name, value);
tokenCredentials.setAttribute(name, value);
}
}
session.save();
return tokenCredentials;
} else {