Element onBehalfOfToken,
Element actAsToken,
String appliesTo,
boolean enableAppliesTo
) throws Exception {
TokenStore tokenStore = getTokenStore(message);
String key = appliesTo;
if (!enableAppliesTo || key == null || "".equals(key)) {
key = ASSOCIATED_TOKEN;
}
// See if the token corresponding to the OnBehalfOf Token is stored in the cache
// and if it points to an issued token
if (onBehalfOfToken != null) {
String id = getIdFromToken(onBehalfOfToken);
SecurityToken cachedToken = tokenStore.getToken(id);
if (cachedToken != null) {
Properties properties = cachedToken.getProperties();
if (properties != null && properties.containsKey(key)) {
String associatedToken = properties.getProperty(key);
SecurityToken issuedToken = tokenStore.getToken(associatedToken);
if (issuedToken != null) {
return issuedToken;
}
}
}
}
// See if the token corresponding to the ActAs Token is stored in the cache
// and if it points to an issued token
if (actAsToken != null) {
String id = getIdFromToken(actAsToken);
SecurityToken cachedToken = tokenStore.getToken(id);
if (cachedToken != null) {
Properties properties = cachedToken.getProperties();
if (properties != null && properties.containsKey(key)) {
String associatedToken = properties.getProperty(key);
SecurityToken issuedToken = tokenStore.getToken(associatedToken);
if (issuedToken != null) {
return issuedToken;
}
}
}