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 currentDate = new Date();
response.setCreated(currentDate);
Date expires = null;
if (lifetime > 0) {
expires = new Date();
long currentTime = currentDate.getTime();
expires.setTime(currentTime + (lifetime * 1000L));
}
response.setExpires(expires);
SecurityToken token = new SecurityToken(sct.getIdentifier(), currentDate, expires);
token.setSecret(keyHandler.getSecret());
token.setPrincipal(tokenParameters.getPrincipal());
Properties props = token.getProperties();
if (props == null) {
props = new Properties();
}
token.setProperties(props);
if (tokenParameters.getRealm() != null) {
props.setProperty(STSConstants.TOKEN_REALM, tokenParameters.getRealm());
}
// Handle Renewing logic
Renewing renewing = tokenParameters.getTokenRequirements().getRenewing();
if (renewing != null) {
props.put(
STSConstants.TOKEN_RENEWING_ALLOW,
String.valueOf(renewing.isAllowRenewing())
);
props.put(
STSConstants.TOKEN_RENEWING_ALLOW_AFTER_EXPIRY,
String.valueOf(renewing.isAllowRenewingAfterExpiry())
);
} else {
props.setProperty(STSConstants.TOKEN_RENEWING_ALLOW, "true");
props.setProperty(STSConstants.TOKEN_RENEWING_ALLOW_AFTER_EXPIRY, "false");
}
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);
return response;