SymmetricKeyHandler keyHandler = new SymmetricKeyHandler(tokenParameters);
keyHandler.createSymmetricKey();
try {
Document doc = DOMUtils.createDocument();
SecurityContextToken sct =
new SecurityContextToken(getWSCVersion(tokenRequirements.getTokenType()), doc);
TokenProviderResponse response = new TokenProviderResponse();
response.setToken(sct.getElement());
response.setTokenId(sct.getIdentifier());
if (returnEntropy) {
response.setEntropy(keyHandler.getEntropyBytes());
}
long keySize = keyHandler.getKeySize();
response.setKeySize(keySize);
response.setComputedKey(keyHandler.isComputedKey());
// putting the secret key into the cache
Date expires = null;
if (lifetime > 0) {
expires = new Date();
long currentTime = expires.getTime();
expires.setTime(currentTime + (lifetime * 1000L));
}
SecurityToken token = new SecurityToken(sct.getIdentifier(), null, expires);
token.setSecret(keyHandler.getSecret());
token.setPrincipal(tokenParameters.getPrincipal());
if (tokenParameters.getRealm() != null) {
Properties props = token.getProperties();
if (props == null) {
props = new Properties();
}
props.setProperty(STSConstants.TOKEN_REALM, tokenParameters.getRealm());
token.setProperties(props);
}
if (lifetime > 0) {
Integer lifetimeInteger = new Integer(Long.valueOf(lifetime).intValue());
tokenParameters.getTokenStore().add(token, lifetimeInteger);
} else {
tokenParameters.getTokenStore().add(token);
}
// Create the references
TokenReference attachedReference = new TokenReference();
attachedReference.setIdentifier(sct.getID());
attachedReference.setUseDirectReference(true);
attachedReference.setWsseValueType(tokenRequirements.getTokenType());
response.setAttachedReference(attachedReference);
TokenReference unAttachedReference = new TokenReference();
unAttachedReference.setIdentifier(sct.getIdentifier());
unAttachedReference.setUseDirectReference(true);
unAttachedReference.setWsseValueType(tokenRequirements.getTokenType());
response.setUnattachedReference(unAttachedReference);
response.setLifetime(lifetime);