use of com.sun.identity.authentication.config.AMAuthenticationInstance in project OpenAM by OpenRock.
the class HTTPBasic method init.
public void init(Subject subject, Map sharedState, Map options) {
java.util.Locale locale = getLoginLocale();
bundle = amCache.getResBundle(amAuthHTTPBasic, locale);
if (debug.messageEnabled()) {
debug.message("HttpBasicAuth resbundle locale=" + locale);
}
this.options = options;
instanceName = CollectionHelper.getMapAttr(options, MODCONFIG);
String authLevel = CollectionHelper.getMapAttr(options, AUTHLEVEL);
if (authLevel != null) {
try {
setAuthLevel(Integer.parseInt(authLevel));
} catch (Exception e) {
debug.error("Unable to set auth level " + authLevel, e);
}
}
try {
SSOToken adminToken = (SSOToken) AccessController.doPrivileged(AdminTokenAction.getInstance());
AMAuthenticationManager amAM = new AMAuthenticationManager(adminToken, getRequestOrg());
AMAuthenticationInstance amInstance = amAM.getAuthenticationInstance(instanceName);
currentConfig = amInstance.getAttributeValues();
String moduleType = amInstance.getType();
String moduleClassName = AuthD.getAuth().getAuthenticatorForName(moduleType);
Class moduleClass = Class.forName(moduleClassName);
amLoginModule = (AMLoginModule) moduleClass.newInstance();
CallbackHandler handler = getCallbackHandler();
amLoginModule.initialize(subject, handler, sharedState, currentConfig);
} catch (Exception exp) {
debug.error("Could not initialize the module instance" + instanceName, exp);
}
}
use of com.sun.identity.authentication.config.AMAuthenticationInstance in project OpenAM by OpenRock.
the class AddAuthConfigurationEntry method getInstanceNames.
private Set getInstanceNames(String realm, SSOToken adminSSOToken) throws AMConfigurationException {
Set names = new HashSet();
AMAuthenticationManager mgr = new AMAuthenticationManager(adminSSOToken, realm);
Set instances = mgr.getAuthenticationInstances();
for (Iterator i = instances.iterator(); i.hasNext(); ) {
AMAuthenticationInstance instance = (AMAuthenticationInstance) i.next();
names.add(instance.getName());
}
return names;
}
use of com.sun.identity.authentication.config.AMAuthenticationInstance in project OpenAM by OpenRock.
the class LoginState method setSessionProperties.
/**
* Populates session with properties.
*
* @param session
* @throws AuthException
*/
public void setSessionProperties(InternalSession session) throws AuthException {
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState getSession = " + session + " \nrequest token = " + token);
}
if (token == null) {
throw new AuthException(AMAuthErrorCode.AUTH_ERROR, null);
}
String cookieSupport = (cookieSupported) ? "true" : "false";
// for user based DN is already set
if (userDN == null) {
userDN = getUserDN(amIdentityUser);
}
AMIdentity newAMIdentity = null;
String oldUserDN = null;
String oldAuthenticationModuleInstanceName = null;
AMIdentity oldAMIdentity = null;
if (oldSession != null || oldStatelessSession != null) {
if (oldSession != null) {
oldUserDN = oldSession.getProperty(ISAuthConstants.PRINCIPAL);
oldAuthenticationModuleInstanceName = oldSession.getProperty(ISAuthConstants.AUTH_TYPE);
} else {
try {
oldUserDN = oldStatelessSession.getProperty(ISAuthConstants.PRINCIPAL);
oldAuthenticationModuleInstanceName = oldStatelessSession.getProperty(ISAuthConstants.AUTH_TYPE);
} catch (SessionException e) {
throw new AuthException(AMAuthErrorCode.SESSION_UPGRADE_FAILED, null);
}
}
if (!ignoreUserProfile) {
newAMIdentity = LazyConfig.AUTHD.getIdentity(IdType.USER, userDN, getOrgDN());
oldAMIdentity = LazyConfig.AUTHD.getIdentity(IdType.USER, oldUserDN, getOrgDN());
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState.setSessionProperties()" + " newAMIdentity is: " + newAMIdentity);
DEBUG.message("LoginState.setSessionProperties()" + " oldAMIdentity is: " + oldAMIdentity);
}
}
}
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState.setSessionProperties()" + " userDN is: " + userDN);
DEBUG.message("LoginState.setSessionProperties()" + " oldUserDN is: " + oldUserDN);
DEBUG.message("LoginState.setSessionProperties()" + " sessionUpgrade is: " + sessionUpgrade);
}
if (sessionUpgrade) {
String oldAuthenticationModuleClassName = null;
if ((oldAuthenticationModuleInstanceName != null) && (!oldAuthenticationModuleInstanceName.contains("|"))) {
try {
SSOToken adminToken = AccessController.doPrivileged(AdminTokenAction.getInstance());
AMAuthenticationManager authManager = new AMAuthenticationManager(adminToken, getOrgName());
AMAuthenticationInstance authInstance = authManager.getAuthenticationInstance(oldAuthenticationModuleInstanceName);
oldAuthenticationModuleClassName = authInstance.getType();
} catch (AMConfigurationException ace) {
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState.setSessionProperties()" + ":Unable to create AMAuthenticationManager" + "Instance:" + ace.getMessage());
}
throw new AuthException(ace);
}
}
if ("Anonymous".equalsIgnoreCase(oldAuthenticationModuleClassName)) {
sessionUpgrade();
} else if (!ignoreUserProfile) {
if ((oldAMIdentity != null) && oldAMIdentity.equals(newAMIdentity)) {
sessionUpgrade();
} else {
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState.setSessionProperties()" + "Resetting session upgrade to false " + "since oldAMIdentity and newAMIdentity doesn't match");
}
throw new AuthException(AMAuthErrorCode.SESSION_UPGRADE_FAILED, null);
}
} else {
if ((oldUserDN != null) && (DNUtils.normalizeDN(userDN)).equals(DNUtils.normalizeDN(oldUserDN))) {
sessionUpgrade();
} else {
if (DEBUG.messageEnabled()) {
DEBUG.message("LoginState.setSessionProperties()" + "Resetting session upgrade to false " + "since Old UserDN and New UserDN doesn't match");
}
throw new AuthException(AMAuthErrorCode.SESSION_UPGRADE_FAILED, null);
}
}
}
if (forceAuth && sessionUpgrade) {
session = oldSession;
}
Date authInstantDate = new Date();
String authInstant = DateUtils.toUTCDateFormat(authInstantDate);
String moduleAuthTime = null;
if (sessionUpgrade) {
try {
if (oldStatelessSession != null) {
oldSSOToken = SSOTokenManager.getInstance().createSSOToken(oldStatelessSession.getID().toString());
} else {
oldSSOToken = SSOTokenManager.getInstance().createSSOToken(oldSession.getID().toString());
}
} catch (SSOException ssoExp) {
DEBUG.error("LoginState.setSessionProperties: Cannot get " + "oldSSOToken.");
}
Map<String, String> moduleTimeMap = null;
if (oldSSOToken != null) {
moduleTimeMap = AMAuthUtils.getModuleAuthTimeMap(oldSSOToken);
}
if (moduleTimeMap == null) {
moduleTimeMap = new HashMap<String, String>();
}
StringTokenizer tokenizer = new StringTokenizer(authMethName, ISAuthConstants.PIPE_SEPARATOR);
while (tokenizer.hasMoreTokens()) {
String moduleName = tokenizer.nextToken();
moduleTimeMap.put(moduleName, authInstant);
}
boolean firstElement = true;
for (Map.Entry<String, String> entry : moduleTimeMap.entrySet()) {
String moduleName = entry.getKey();
String authTime = entry.getValue();
StringBuilder sb = new StringBuilder();
if (!firstElement) {
sb.append(ISAuthConstants.PIPE_SEPARATOR);
}
firstElement = false;
if (moduleAuthTime == null) {
moduleAuthTime = (sb.append(moduleName).append("+").append(authTime)).toString();
} else {
moduleAuthTime += sb.append(moduleName).append("+").append(authTime);
}
}
}
//Sets the User profile option used, in session.
String userProfile = ISAuthConstants.REQUIRED;
if (dynamicProfileCreation) {
userProfile = ISAuthConstants.CREATE;
} else if (ignoreUserProfile) {
userProfile = ISAuthConstants.IGNORE;
} else if (createWithAlias) {
userProfile = ISAuthConstants.CREATE_WITH_ALIAS;
}
session.putProperty(ISAuthConstants.USER_PROFILE, userProfile);
String defaultLoginURL = null;
if (loginURL != null) {
int questionMark = loginURL.indexOf("?");
defaultLoginURL = loginURL;
if (questionMark != -1) {
defaultLoginURL = loginURL.substring(0, questionMark);
}
session.putProperty(ISAuthConstants.LOGIN_URL, defaultLoginURL);
session.putProperty(ISAuthConstants.FULL_LOGIN_URL, loginURL);
}
String sessionSuccessURL = LazyConfig.AUTHD.processURL(successLoginURL, servletRequest);
sessionSuccessURL = encodeURL(sessionSuccessURL, servletResponse, true);
if (sessionSuccessURL != null) {
session.putProperty(ISAuthConstants.SUCCESS_URL, sessionSuccessURL);
}
// Get the universal ID
String univId = null;
if (amIdentityUser != null) {
univId = IdUtils.getUniversalId(amIdentityUser);
}
String userId = DNUtils.DNtoName(userDN);
if (DEBUG.messageEnabled()) {
DEBUG.message("setSessionProperties Principal = " + userDN + "\n" + "UserId = " + token + "\n" + "client = " + getClient() + "\n" + "Organization = " + orgDN + "\n" + "locale = " + localeContext.getLocale() + "\n" + "charset = " + localeContext.getMIMECharset() + "\n" + "idleTime = " + idleTime + "\n" + "cacheTime = " + cacheTime + "\n" + "maxSession = " + maxSession + "\n" + "AuthLevel = " + authLevel + "\n" + "AuthType = " + authMethName + "\n" + "Subject = " + subject.toString() + "\n" + "UniversalId = " + univId + "\n" + "cookieSupport = " + cookieSupport + "\n" + "principals = " + principalList + "\n" + "defaultLoginURL = " + defaultLoginURL + "\n" + "successURL = " + sessionSuccessURL + "\n" + "IndexType = " + indexType + "\n" + "UserProfile = " + userProfile + "\n" + "AuthInstant = " + authInstant + "\n" + "ModuleAuthTime = " + moduleAuthTime);
}
try {
if ((isApplicationModule(authMethName) && (LazyConfig.AUTHD.isSuperUser(userDN) || LazyConfig.AUTHD.isSpecialUser(userDN))) || isAgent(amIdentityUser)) {
session.setClientID(token);
session.setType(APPLICATION_SESSION);
if (isAgent(amIdentityUser) && AGENT_SESSION_IDLE_TIME > 0) {
if (DEBUG.messageEnabled()) {
DEBUG.message("setSessionProperties for agent " + userDN + " with idletimeout to " + AGENT_SESSION_IDLE_TIME);
}
session.setMaxSessionTime(Long.MAX_VALUE / 60);
session.setMaxIdleTime(AGENT_SESSION_IDLE_TIME);
session.setMaxCachingTime(AGENT_SESSION_IDLE_TIME);
} else {
if (DEBUG.messageEnabled()) {
DEBUG.message("setSessionProperties for non-expiring session");
}
session.setExpire(false);
}
} else {
DEBUG.message("request: in putProperty stuff");
session.setClientID(userDN);
session.setType(USER_SESSION);
session.setMaxSessionTime(maxSession);
session.setMaxIdleTime(idleTime);
session.setMaxCachingTime(cacheTime);
}
session.setClientDomain(getOrgDN());
if ((client = getClient()) != null) {
session.putProperty(ISAuthConstants.HOST, client);
}
if (!sessionUpgrade) {
session.putProperty(ISAuthConstants.AUTH_LEVEL, Integer.toString(authLevel));
session.putProperty(ISAuthConstants.AUTH_TYPE, authMethName);
}
session.putProperty(ISAuthConstants.PRINCIPAL, userDN);
if (userId == null && userDN != null) {
DN dnObj = DN.valueOf(userDN);
if (dnObj.size() > 0) {
userId = LDAPUtils.rdnValueFromDn(dnObj);
}
}
session.putProperty(ISAuthConstants.USER_ID, userId);
session.putProperty(ISAuthConstants.USER_TOKEN, token);
session.putProperty(ISAuthConstants.ORGANIZATION, getOrgDN());
session.putProperty(ISAuthConstants.LOCALE, localeContext.getLocale().toString());
session.putProperty(ISAuthConstants.CHARSET, localeContext.getMIMECharset());
session.putProperty(ISAuthConstants.CLIENT_TYPE, getClientType());
session.putProperty(ISAuthConstants.COOKIE_SUPPORT_PROPERTY, cookieSupport);
session.putProperty(ISAuthConstants.AUTH_INSTANT, authInstant);
if ((moduleAuthTime != null) && (moduleAuthTime.length() != 0)) {
session.putProperty(ISAuthConstants.MODULE_AUTH_TIME, moduleAuthTime);
}
if (principalList != null) {
session.putProperty(ISAuthConstants.PRINCIPALS, principalList);
}
if (indexType != null) {
session.putProperty(ISAuthConstants.INDEX_TYPE, indexType.toString());
}
if (univId != null) {
session.putProperty(Constants.UNIVERSAL_IDENTIFIER, univId);
} else if (userDN != null) {
session.putProperty(Constants.UNIVERSAL_IDENTIFIER, userDN);
}
if ((indexType == AuthContext.IndexType.ROLE) && (indexName != null)) {
if (!sessionUpgrade) {
session.putProperty(ISAuthConstants.ROLE, indexName);
}
}
if (!sessionUpgrade) {
String finalAuthConfig = getAuthConfigName(indexType, indexName);
if ((finalAuthConfig != null) && (finalAuthConfig.length() != 0)) {
session.putProperty(ISAuthConstants.SERVICE, finalAuthConfig);
}
}
if ((userSessionMapping != null) && !(userSessionMapping.isEmpty()) && !ignoreUserProfile) {
for (final String mapping : userSessionMapping) {
if ((mapping != null) && (mapping.length() != 0)) {
StringTokenizer tokenizer = new StringTokenizer(mapping, "|");
String userAttribute = null;
String sessionAttribute = null;
if (tokenizer.hasMoreTokens()) {
userAttribute = tokenizer.nextToken();
}
if (tokenizer.hasMoreTokens()) {
sessionAttribute = tokenizer.nextToken();
}
if ((userAttribute != null) && (userAttribute.length() != 0)) {
Set userAttrValueSet = amIdentityUser.getAttribute(userAttribute);
if ((userAttrValueSet != null) && !(userAttrValueSet.isEmpty())) {
Iterator valueIter = userAttrValueSet.iterator();
StringBuilder strBuffValues = new StringBuilder();
while (valueIter.hasNext()) {
String userAttrValue = (String) valueIter.next();
if (strBuffValues.length() == 0) {
strBuffValues.append(userAttrValue);
} else {
strBuffValues.append("|").append(userAttrValue);
}
}
if (sessionAttribute != null) {
session.putProperty(Constants.AM_PROTECTED_PROPERTY_PREFIX + "." + sessionAttribute, strBuffValues.toString());
} else {
session.putProperty(Constants.AM_PROTECTED_PROPERTY_PREFIX + "." + userAttribute, strBuffValues.toString());
}
}
}
}
}
}
// Set Attribute Map for Authentication module
AuthenticationPrincipalDataRetriever principalDataRetriever = AuthenticationPrincipalDataRetrieverFactory.getPrincipalDataRetriever();
if (principalDataRetriever != null) {
Map<String, String> attrMap = principalDataRetriever.getAttrMapForAuthenticationModule(subject);
if (attrMap != null && !attrMap.isEmpty()) {
for (Map.Entry<String, String> entry : attrMap.entrySet()) {
String attrName = entry.getKey();
String attrValue = entry.getValue();
session.putProperty(attrName, attrValue);
if (DEBUG.messageEnabled()) {
DEBUG.message("AttrMap for SAML : " + attrName + " , " + attrValue);
}
}
}
}
} catch (Exception e) {
DEBUG.error("Exception in setSession ", e);
throw new AuthException(e);
}
}
use of com.sun.identity.authentication.config.AMAuthenticationInstance in project OpenAM by OpenRock.
the class UpdateAuthConfigurationEntries method getInstanceNames.
private Set getInstanceNames(String realm, SSOToken adminSSOToken, String[] params) throws CLIException {
Set names = new HashSet();
try {
AMAuthenticationManager mgr = new AMAuthenticationManager(adminSSOToken, realm);
Set instances = mgr.getAuthenticationInstances();
for (Iterator i = instances.iterator(); i.hasNext(); ) {
AMAuthenticationInstance instance = (AMAuthenticationInstance) i.next();
names.add(instance.getName());
}
} catch (AMConfigurationException e) {
debugError("ListAuthInstances.handleRequest", e);
writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_SET_AUTH_CONFIG_ENTRIES", params);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
return names;
}
use of com.sun.identity.authentication.config.AMAuthenticationInstance in project OpenAM by OpenRock.
the class UpdateAuthInstance method handleRequest.
/**
* Handles request.
*
* @param rc Request Context.
* @throws CLIException if request cannot be processed.
*/
public void handleRequest(RequestContext rc) throws CLIException {
super.handleRequest(rc);
ldapLogin();
SSOToken adminSSOToken = getAdminSSOToken();
String realm = getStringOptionValue(IArgument.REALM_NAME);
String instanceName = getStringOptionValue(AuthOptions.AUTH_INSTANCE_NAME);
String datafile = getStringOptionValue(IArgument.DATA_FILE);
List attrValues = rc.getOption(IArgument.ATTRIBUTE_VALUES);
if ((datafile == null) && (attrValues == null)) {
throw new CLIException(getResourceString("missing-attributevalues"), ExitCodes.INCORRECT_OPTION, rc.getSubCommand().getName());
}
Map<String, Set<String>> attributeValues = AttributeValues.parse(getCommandManager(), datafile, attrValues);
attributeValues = processFileAttributes(attributeValues);
String[] params = { realm, instanceName };
writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_UPDATE_AUTH_INSTANCE", params);
try {
AMAuthenticationManager mgr = new AMAuthenticationManager(adminSSOToken, realm);
AMAuthenticationInstance ai = mgr.getAuthenticationInstance(instanceName);
if (ai != null) {
ai.setAttributeValues(attributeValues);
getOutputWriter().printlnMessage(getResourceString("authentication-update-auth-instance-succeeded"));
writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_UPDATE_AUTH_INSTANCE", params);
} else {
writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_UPDATE_AUTH_INSTANCE", params);
throw new CLIException(getResourceString("authentication-update-auth-instance-not-found"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
} catch (AMConfigurationException e) {
debugError("UpdateAuthInstance.handleRequest", e);
writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_UPDATE_AUTH_INSTANCE", params);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (SMSException e) {
debugError("UpdateAuthInstance.handleRequest", e);
writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_UPDATE_AUTH_INSTANCE", params);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (SSOException e) {
debugError("UpdateAuthInstance.handleRequest", e);
writeLog(LogWriter.LOG_ERROR, Level.INFO, "FAILED_UPDATE_AUTH_INSTANCE", params);
throw new CLIException(e, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
}
Aggregations