use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class CoreTokenAdapter method read.
/**
* Read the Token based on its Token ID.
*
* @param tokenId The non null Token ID to read from the Token store.
* @return Null if the Token could not be found, otherwise a non null Token.
* @throws CoreTokenException If there was an unexpected problem with the request.
*/
public Token read(String tokenId) throws CoreTokenException {
debug("Read: queued {0}", tokenId);
ResultHandler<Token, CoreTokenException> handler = handlerFactory.getReadHandler();
dispatcher.read(tokenId, handler);
try {
Token token = handler.getResults();
if (token == null) {
debug("Read: no Token found for {0}", tokenId);
} else {
reverseBlobStrategy(token);
debug("Read: returned for {0}\n{1}", tokenId, token);
}
return token;
} catch (CoreTokenException e) {
throw new ReadFailedException(tokenId, e);
}
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SAML2CTSPersistentStore method retrieveSAML2TokensWithSecondaryKey.
/**
*{@inheritDoc}
*/
@Override
public List<Object> retrieveSAML2TokensWithSecondaryKey(String secondaryKey) throws SAML2TokenRepositoryException {
secondaryKey = tokenIdFactory.toSAMLSecondaryTokenId(secondaryKey);
try {
TokenFilter filter = new TokenFilterBuilder().withAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey).build();
Collection<Token> tokens = persistentStore.query(filter);
List<Object> results = new ArrayList<Object>(tokens.size());
for (Token token : tokens) {
SAMLToken samlToken = tokenAdapter.fromToken(token);
results.add(samlToken.getToken());
}
return results;
} catch (CoreTokenException e) {
debug.error("SAML2CTSPersistentStore.retrieveSAML2TokensWithSecondaryKey(): failed to retrieve SAML2 " + "tokens using secondary key:" + secondaryKey, e);
throw new SAML2TokenRepositoryException(e.getMessage(), e);
}
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SAMLAdapter method toToken.
/**
* Convert the SAMLToken to a Token.
*
*
* This conversion performs the additional mapping needed when dealing with SAMLTokens.
*
* @param samlToken Non null.
* @return Non null Token.
*/
public Token toToken(SAMLToken samlToken) {
String tokenId = tokenIdFactory.toSAMLPrimaryTokenId(samlToken.getPrimaryKey());
Token token = new Token(tokenId, TokenType.SAML2);
// Expiry Date
Calendar timestamp = TimeUtils.fromUnixTime(samlToken.getExpiryTime());
token.setExpiryTimestamp(timestamp);
// Persist the SAML token class, because there is no obvious hierarchy to the SAML tokens.
String className = samlToken.getToken().getClass().getName();
token.setAttribute(SAMLTokenField.OBJECT_CLASS.getField(), className);
// Persist the SAML secondary key because it can be queried over.
String secondaryKey = samlToken.getSecondaryKey();
if (secondaryKey != null) {
secondaryKey = tokenIdFactory.toSAMLSecondaryTokenId(secondaryKey);
token.setAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey);
}
// Binary data
String jsonBlob = serialisation.serialise(samlToken.getToken());
blobUtils.setBlobFromString(token, jsonBlob);
return token;
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SessionAdapter method toToken.
/**
* Convert from InternalSession to a Token.
*
* The InternalSession SessionID instance provides the primary key for the Token.
*
* Expiry time is a combination of the InternalSession expiration time and a grace
* period.
*
* @param session Non null.
* @return Non null populated Token.
*/
public Token toToken(InternalSession session) {
String tokenId = tokenIdFactory.toSessionTokenId(session);
Token token = new Token(tokenId, TokenType.SESSION);
// User Id
String userId = config.getUserId(session);
token.setUserId(userId);
// Expiry Date
long unixTimeMillis = session.getExpirationTime(TimeUnit.MILLISECONDS) + config.getSessionExpiryGracePeriod(TimeUnit.MILLISECONDS);
Calendar expiryTimeStamp = TimeUtils.fromUnixTime(unixTimeMillis, TimeUnit.MILLISECONDS);
token.setExpiryTimestamp(expiryTimeStamp);
// SessionID
token.setAttribute(SessionTokenField.SESSION_ID.getField(), session.getID().toString());
// Binary data
String jsonBlob = serialisation.serialise(session);
blobUtils.setBlobFromString(token, jsonBlob);
String latestAccessTime = filterLatestAccessTime(token);
if (latestAccessTime != null) {
token.setAttribute(SessionTokenField.LATEST_ACCESS_TIME.getField(), latestAccessTime);
}
// Session handle
token.setAttribute(SessionTokenField.SESSION_HANDLE.getField(), session.getSessionHandle());
return token;
}
use of org.forgerock.openam.cts.api.tokens.Token in project OpenAM by OpenRock.
the class SAMLAdapterTest method shouldSerialiseAndDeserialiseToken.
@Test
public void shouldSerialiseAndDeserialiseToken() {
// Given
// Need real delegates for this test.
serialisation = new JSONSerialisation(new ObjectMapper());
adapter = new SAMLAdapter(new TokenIdFactory(encoding), new JSONSerialisation(new ObjectMapper()), new TokenBlobUtils());
String tokenId = encoding.encodeKey("badger");
Token token = new Token(tokenId, TokenType.SAML2);
// SAML tokens only store time to seconds resolution
Calendar now = Calendar.getInstance();
now.set(Calendar.MILLISECOND, 0);
token.setExpiryTimestamp(now);
// SAML implementation detail around stored object
String blob = "woodland forrest";
token.setBlob(serialisation.serialise(blob).getBytes());
token.setAttribute(SAMLTokenField.OBJECT_CLASS.getField(), String.class.getName());
// SAML detail for secondary key
String secondaryKey = encoding.encodeKey("weasel");
token.setAttribute(SAMLTokenField.SECONDARY_KEY.getField(), secondaryKey);
// When
Token result = adapter.toToken(adapter.fromToken(token));
// Then
TokenTestUtils.assertTokenEquals(result, token);
}
Aggregations