use of com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement in project OpenAM by OpenRock.
the class RPSigninRequest method process.
/**
* Processes the sign-in request, redirecting the browser to the identity
* provider via the HttpServletResponse passed to the constructor.
*/
public void process() throws WSFederationException, IOException {
String classMethod = "RPSigninRequest.process: ";
if (debug.messageEnabled()) {
debug.message(classMethod + "entered method");
}
if (wctx == null || wctx.length() == 0) {
// Exchange reply URL for opaque identifier
wctx = (wreply != null && (wreply.length() > 0)) ? WSFederationUtils.putReplyURL(wreply) : null;
}
String spMetaAlias = WSFederationMetaUtils.getMetaAliasByUri(request.getRequestURI());
if (spMetaAlias == null || spMetaAlias.length() == 0) {
throw new WSFederationException(WSFederationUtils.bundle.getString("MetaAliasNotFound"));
}
String spRealm = SAML2MetaUtils.getRealmByMetaAlias(spMetaAlias);
WSFederationMetaManager metaManager = WSFederationUtils.getMetaManager();
String spEntityId = metaManager.getEntityByMetaAlias(spMetaAlias);
if (spEntityId == null || spEntityId.length() == 0) {
String[] args = { spMetaAlias, spRealm };
throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "invalidMetaAlias", args);
}
SPSSOConfigElement spConfig = metaManager.getSPSSOConfig(spRealm, spEntityId);
if (spConfig == null) {
String[] args = { spEntityId, spRealm };
throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "badSPEntityID", args);
}
Map<String, List<String>> spConfigAttributes = WSFederationMetaUtils.getAttributes(spConfig);
String accountRealmSelection = spConfigAttributes.get(com.sun.identity.wsfederation.common.WSFederationConstants.ACCOUNT_REALM_SELECTION).get(0);
if (accountRealmSelection == null) {
accountRealmSelection = WSFederationConstants.ACCOUNT_REALM_SELECTION_DEFAULT;
}
String accountRealmCookieName = spConfigAttributes.get(WSFederationConstants.ACCOUNT_REALM_COOKIE_NAME).get(0);
if (accountRealmCookieName == null) {
accountRealmCookieName = WSFederationConstants.ACCOUNT_REALM_COOKIE_NAME_DEFAULT;
}
String homeRealmDiscoveryService = spConfigAttributes.get(WSFederationConstants.HOME_REALM_DISCOVERY_SERVICE).get(0);
if (debug.messageEnabled()) {
debug.message(classMethod + "account realm selection method is " + accountRealmSelection);
}
String idpIssuerName = null;
if (whr != null && whr.length() > 0) {
// whr parameter overrides other mechanisms...
idpIssuerName = whr;
if (accountRealmSelection.equals(WSFederationConstants.COOKIE)) {
// ...and overwrites cookie
Cookie cookie = new Cookie(accountRealmCookieName, whr);
// Set cookie to persist for a year
cookie.setMaxAge(60 * 60 * 24 * 365);
CookieUtils.addCookieToResponse(response, cookie);
}
} else {
if (accountRealmSelection.equals(WSFederationConstants.USERAGENT)) {
String uaHeader = request.getHeader(WSFederationConstants.USERAGENT);
if (debug.messageEnabled()) {
debug.message(classMethod + "user-agent is :" + uaHeader);
}
idpIssuerName = WSFederationUtils.accountRealmFromUserAgent(uaHeader, accountRealmCookieName);
} else if (accountRealmSelection.equals(WSFederationConstants.COOKIE)) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(accountRealmCookieName)) {
idpIssuerName = cookies[i].getValue();
break;
}
}
}
} else {
debug.error(classMethod + "unexpected value for " + WSFederationConstants.ACCOUNT_REALM_SELECTION + " : " + accountRealmSelection);
throw new WSFederationException(WSFederationUtils.bundle.getString("badAccountRealm"));
}
}
FederationElement sp = metaManager.getEntityDescriptor(spRealm, spEntityId);
String spIssuerName = metaManager.getTokenIssuerName(sp);
if (debug.messageEnabled()) {
debug.message(classMethod + "SP issuer name:" + spIssuerName);
}
String idpEntityId = null;
if (idpIssuerName != null && idpIssuerName.length() > 0) {
// Got the issuer name from the cookie/UA string - let's see if
// we know the entity ID
idpEntityId = metaManager.getEntityByTokenIssuerName(null, idpIssuerName);
}
if (idpEntityId == null) {
// See if there is only one trusted IdP configured...
List<String> allRemoteIdPs = metaManager.getAllRemoteIdentityProviderEntities(spRealm);
ArrayList<String> trustedRemoteIdPs = new ArrayList<String>();
for (String idp : allRemoteIdPs) {
if (metaManager.isTrustedProvider(spRealm, spEntityId, idp)) {
trustedRemoteIdPs.add(idp);
}
}
if (trustedRemoteIdPs.size() == 0) {
// Misconfiguration!
throw new WSFederationException(WSFederationUtils.bundle.getString("noIDPConfigured"));
} else if (trustedRemoteIdPs.size() == 1) {
idpEntityId = trustedRemoteIdPs.get(0);
}
}
FederationElement idp = null;
if (idpEntityId != null) {
idp = metaManager.getEntityDescriptor(null, idpEntityId);
}
// Set LB cookie here so it's done regardless of which redirect happens
// We want response to come back to this instance
WSFederationUtils.sessionProvider.setLoadBalancerCookie(request, response);
// If we still don't know the IdP, redirect to home realm discovery
if (idp == null) {
StringBuffer url = new StringBuffer(homeRealmDiscoveryService);
url.append("?wreply=");
url.append(URLEncDec.encode(request.getRequestURL().toString()));
if (wctx != null) {
url.append("&wctx=");
url.append(URLEncDec.encode(wctx));
}
if (debug.messageEnabled()) {
debug.message(classMethod + "no account realm - redirecting to :" + url);
}
response.sendRedirect(url.toString());
return;
}
if (debug.messageEnabled()) {
debug.message(classMethod + "account realm:" + idpEntityId);
}
String endpoint = metaManager.getTokenIssuerEndpoint(idp);
if (debug.messageEnabled()) {
debug.message(classMethod + "endpoint:" + endpoint);
}
String replyURL = metaManager.getTokenIssuerEndpoint(sp);
if (debug.messageEnabled()) {
debug.message(classMethod + "replyURL:" + replyURL);
}
StringBuffer url = new StringBuffer(endpoint);
url.append("?wa=");
url.append(URLEncDec.encode(WSFederationConstants.WSIGNIN10));
if (wctx != null) {
url.append("&wctx=");
url.append(URLEncDec.encode(wctx));
}
url.append("&wreply=");
url.append(URLEncDec.encode(replyURL));
url.append("&wct=");
url.append(URLEncDec.encode(DateUtils.toUTCDateFormat(new Date())));
url.append("&wtrealm=");
url.append(URLEncDec.encode(spIssuerName));
if (debug.messageEnabled()) {
debug.message(classMethod + "Redirecting to:" + url);
}
response.sendRedirect(url.toString());
}
use of com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement in project OpenAM by OpenRock.
the class RPSigninResponse method process.
/**
* Processes the sign-in response, redirecting the browser wreply URL
* supplied in the sign-in request via the HttpServletResponse passed to
* the constructor.
*/
public void process() throws WSFederationException, IOException {
String classMethod = "RPSigninResponse.process: ";
if ((wresult == null) || (wresult.length() == 0)) {
String[] data = { request.getQueryString() };
LogUtil.error(Level.INFO, LogUtil.MISSING_WRESULT, data, null);
throw new WSFederationException(WSFederationUtils.bundle.getString("nullWresult"));
}
RequestSecurityTokenResponse rstr = null;
try {
rstr = RequestSecurityTokenResponse.parseXML(wresult);
} catch (WSFederationException wsfe) {
String[] data = { wresult };
LogUtil.error(Level.INFO, LogUtil.INVALID_WRESULT, data, null);
throw new WSFederationException(WSFederationUtils.bundle.getString("invalidWresult"));
}
if (debug.messageEnabled()) {
debug.message(classMethod + "Received RSTR: " + rstr.toString());
}
String realm = null;
String requestURL = request.getRequestURL().toString();
// get entity id and orgName
String metaAlias = WSFederationMetaUtils.getMetaAliasByUri(requestURL);
realm = WSFederationMetaUtils.getRealmByMetaAlias(metaAlias);
WSFederationMetaManager metaManager = WSFederationUtils.getMetaManager();
String spEntityId = null;
try {
spEntityId = metaManager.getEntityByMetaAlias(metaAlias);
} catch (WSFederationException wsfe) {
String[] data = { wsfe.getLocalizedMessage(), metaAlias, realm };
LogUtil.error(Level.INFO, LogUtil.CONFIG_ERROR_GET_ENTITY_CONFIG, data, null);
String[] args = { metaAlias, realm };
throw new WSFederationException(WSFederationConstants.BUNDLE_NAME, "invalidMetaAlias", args);
}
if (realm == null || realm.length() == 0) {
realm = "/";
}
SPSSOConfigElement spssoconfig = metaManager.getSPSSOConfig(realm, spEntityId);
int timeskew = SAML2Constants.ASSERTION_TIME_SKEW_DEFAULT;
String timeskewStr = WSFederationMetaUtils.getAttribute(spssoconfig, SAML2Constants.ASSERTION_TIME_SKEW);
if (timeskewStr != null && timeskewStr.trim().length() > 0) {
timeskew = Integer.parseInt(timeskewStr);
if (timeskew < 0) {
timeskew = SAML2Constants.ASSERTION_TIME_SKEW_DEFAULT;
}
}
if (debug.messageEnabled()) {
debug.message(classMethod + "timeskew = " + timeskew);
}
// Subject, SOAPEntry for the partner and the List of Assertions.
if (debug.messageEnabled()) {
debug.message(classMethod + " - verifying assertion");
}
// verifyToken will throw an exception, rather than return null, so we
// need not test the return value
Map<String, Object> smap = rstr.getRequestedSecurityToken().verifyToken(realm, spEntityId, timeskew);
assert smap != null;
Map attributes = WSFederationMetaUtils.getAttributes(spssoconfig);
SPAccountMapper acctMapper = getSPAccountMapper(attributes);
SPAttributeMapper attrMapper = getSPAttributeMapper(attributes);
String userName = acctMapper.getIdentity(rstr, spEntityId, realm);
if (userName == null) {
throw new WSFederationException(WSFederationUtils.bundle.getString("nullUserID"));
}
String idpEntityId = metaManager.getEntityByTokenIssuerName(realm, rstr.getRequestedSecurityToken().getIssuer());
List attrs = rstr.getRequestedSecurityToken().getAttributes();
Map attrMap = null;
if (attrs != null) {
attrMap = attrMapper.getAttributes(attrs, userName, spEntityId, idpEntityId, realm);
}
String authLevel = smap.get(SAML2Constants.AUTH_LEVEL).toString();
// Set up Attributes for session creation
Map sessionInfoMap = new HashMap();
sessionInfoMap.put(SessionProvider.REALM, realm);
sessionInfoMap.put(SessionProvider.PRINCIPAL_NAME, userName);
sessionInfoMap.put(SessionProvider.AUTH_LEVEL, authLevel);
Object session = null;
try {
SessionProvider sessionProvider = SessionManager.getProvider();
session = sessionProvider.createSession(sessionInfoMap, request, response, null);
SPACSUtils.setAttrMapInSession(sessionProvider, attrMap, session);
String[] idpArray = { idpEntityId };
sessionProvider.setProperty(session, WSFederationConstants.SESSION_IDP, idpArray);
RequestedSecurityToken rst = rstr.getRequestedSecurityToken();
if (isAssertionCacheEnabled(spssoconfig)) {
String tokenID = rst.getTokenId();
String[] assertionID = { tokenID };
sessionProvider.setProperty(session, "AssertionID", assertionID);
SPCache.assertionByIDCache.put(tokenID, rst.toString());
}
} catch (SessionException se) {
String[] data = { se.getLocalizedMessage(), realm, userName, authLevel };
LogUtil.error(Level.INFO, LogUtil.CANT_CREATE_SESSION, data, null);
throw new WSFederationException(se);
}
String target = null;
if (wctx != null) {
target = WSFederationUtils.removeReplyURL(wctx);
} else {
target = WSFederationMetaUtils.getAttribute(spssoconfig, SAML2Constants.DEFAULT_RELAY_STATE);
}
String[] data = { wctx, LogUtil.isErrorLoggable(Level.FINER) ? wresult : rstr.getRequestedSecurityToken().getTokenId(), realm, userName, authLevel, target };
LogUtil.access(Level.INFO, LogUtil.SSO_SUCCESSFUL, data, session);
if (target == null) {
// What to do? There was no wreply URL specified, and there is no
// default target configured
PrintWriter pw = response.getWriter();
pw.println("Logged in");
return;
}
response.sendRedirect(target);
}
use of com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement in project OpenAM by OpenRock.
the class WSFederationMetaSecurityUtils method updateProviderKeyInfo.
/**
* Updates signing or encryption key info for SP or IDP.
* This will update both signing/encryption alias on extended metadata and
* certificates in standard metadata.
* @param realm Realm the entity resides.
* @param entityID ID of the entity to be updated.
* @param certAlias Alias of the certificate to be set to the entity. If
* null, will remove existing key information from the SP or IDP.
* @param isIDP true if this is for IDP signing/encryption alias, false
* if this is for SP signing/encryption alias
* @throws WSFederationMetaException if failed to update the certificate
* alias for the entity.
*/
public static void updateProviderKeyInfo(String realm, String entityID, String certAlias, boolean isIDP) throws WSFederationMetaException {
WSFederationMetaManager metaManager = new WSFederationMetaManager();
FederationConfigElement config = metaManager.getEntityConfig(realm, entityID);
if (!config.isHosted()) {
String[] args = { entityID, realm };
throw new WSFederationMetaException("entityNotHosted", args);
}
FederationElement desp = metaManager.getEntityDescriptor(realm, entityID);
if (isIDP) {
IDPSSOConfigElement idpConfig = metaManager.getIDPSSOConfig(realm, entityID);
if ((idpConfig == null) || (desp == null)) {
String[] args = { entityID, realm };
throw new WSFederationMetaException("entityNotIDP", args);
}
// update standard metadata
if ((certAlias == null) || (certAlias.length() == 0)) {
// remove key info
removeKeyDescriptor(desp);
setExtendedAttributeValue(idpConfig, SAML2Constants.SIGNING_CERT_ALIAS, null);
} else {
TokenSigningKeyInfoElement kde = getKeyDescriptor(certAlias);
updateKeyDescriptor(desp, kde);
// update extended metadata
Set value = new HashSet();
value.add(certAlias);
setExtendedAttributeValue(idpConfig, SAML2Constants.SIGNING_CERT_ALIAS, value);
}
} else {
SPSSOConfigElement spConfig = metaManager.getSPSSOConfig(realm, entityID);
if ((spConfig == null) || (desp == null)) {
String[] args = { entityID, realm };
throw new WSFederationMetaException("entityNotSP", args);
}
// update standard metadata
if ((certAlias == null) || (certAlias.length() == 0)) {
// remove key info
removeKeyDescriptor(desp);
setExtendedAttributeValue(spConfig, SAML2Constants.SIGNING_CERT_ALIAS, null);
} else {
TokenSigningKeyInfoElement kde = getKeyDescriptor(certAlias);
updateKeyDescriptor(desp, kde);
// update extended metadata
Set value = new HashSet();
value.add(certAlias);
setExtendedAttributeValue(spConfig, SAML2Constants.SIGNING_CERT_ALIAS, value);
}
}
metaManager.setFederation(realm, desp);
metaManager.setEntityConfig(realm, config);
}
use of com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement in project OpenAM by OpenRock.
the class WSFederationMetaManager method createEntityConfig.
/**
* Creates the extended entity configuration under the realm.
*
* @param realm The realm under which the entity configuration will be
* created.
* @param config The extended entity configuration object to be created.
* @throws WSFederationMetaException if unable to create the entity
* configuration.
*/
public void createEntityConfig(String realm, FederationConfigElement config) throws WSFederationMetaException {
String federationId = config.getFederationID();
if (federationId == null) {
debug.error("WSFederationMetaManager.createEntityConfig: " + "entity ID is null");
String[] data = { realm };
LogUtil.error(Level.INFO, LogUtil.NO_ENTITY_ID_CREATE_ENTITY_CONFIG, data, null);
throw new WSFederationMetaException("empty_entityid", null);
}
if (realm == null) {
realm = "/";
}
String[] objs = { federationId, realm };
try {
Map attrs = WSFederationMetaUtils.convertJAXBToAttrMap(ATTR_ENTITY_CONFIG, config);
Map oldAttrs = configInst.getConfiguration(realm, federationId);
if (oldAttrs == null) {
LogUtil.error(Level.INFO, LogUtil.NO_ENTITY_DESCRIPTOR_CREATE_ENTITY_CONFIG, objs, null);
throw new WSFederationMetaException("entity_descriptor_not_exist", objs);
}
Set oldValues = (Set) oldAttrs.get(ATTR_ENTITY_CONFIG);
if (oldValues != null && !oldValues.isEmpty()) {
LogUtil.error(Level.INFO, LogUtil.ENTITY_CONFIG_EXISTS, objs, null);
throw new WSFederationMetaException("entity_config_exists", objs);
}
configInst.setConfiguration(realm, federationId, attrs);
LogUtil.access(Level.INFO, LogUtil.ENTITY_CONFIG_CREATED, objs, null);
// Add the entity to cot
SPSSOConfigElement spconfig = getSPSSOConfig(realm, federationId);
if (spconfig != null) {
addToCircleOfTrust(spconfig, realm, federationId);
}
IDPSSOConfigElement idpconfig = getIDPSSOConfig(realm, federationId);
if (idpconfig != null) {
addToCircleOfTrust(idpconfig, realm, federationId);
}
} catch (ConfigurationException e) {
debug.error("WSFederationMetaManager.createEntityConfig:", e);
String[] data = { e.getMessage(), federationId, realm };
LogUtil.error(Level.INFO, LogUtil.CONFIG_ERROR_CREATE_ENTITY_CONFIG, data, null);
throw new WSFederationMetaException(e);
} catch (JAXBException jaxbe) {
debug.error("WSFederationMetaManager.createEntityConfig:", jaxbe);
LogUtil.error(Level.INFO, LogUtil.CREATE_INVALID_ENTITY_CONFIG, objs, null);
throw new WSFederationMetaException("invalid_config", objs);
}
}
use of com.sun.identity.wsfederation.jaxb.entityconfig.SPSSOConfigElement in project OpenAM by OpenRock.
the class WSFederationMetaManager method getAllHostedServiceProviderMetaAliases.
/**
* Returns metaAliases of all hosted service providers under the realm.
*
* @param realm The realm under which the service provider metaAliases
* reside.
* @return a <code>List</code> of metaAliases <code>String</code>.
* @throws WSFederationMetaException if unable to retrieve meta aliases.
*/
public List<String> getAllHostedServiceProviderMetaAliases(String realm) throws WSFederationMetaException {
List<String> metaAliases = new ArrayList<String>();
SPSSOConfigElement spConfig = null;
List<String> hostedEntityIds = getAllHostedServiceProviderEntities(realm);
for (String federationId : hostedEntityIds) {
if ((spConfig = getSPSSOConfig(realm, federationId)) != null) {
metaAliases.add(spConfig.getMetaAlias());
}
}
return metaAliases;
}
Aggregations