Map<String, Object> sessionAttributes = new HashMap<String, Object>();
ExecutionContext execContext = null;
if (credentials == null) {
Subject subject = Subject.getSubject(AccessController.getContext());
if (subject != null) {
execContext = executionContext.with(new JaasSecurityContext(subject));
}
// Well. There's no JAAS subject. Try using an anonymous user (if that's enabled).
else if (anonymousUserContext != null) {
execContext = executionContext.with(this.anonymousUserContext);
} else {
throw new javax.jcr.LoginException(JcrI18n.mustBeInPrivilegedAction.text());
}
} else {
try {
if (credentials instanceof SimpleCredentials) {
SimpleCredentials simple = (SimpleCredentials)credentials;
execContext = executionContext.with(new JaasSecurityContext(options.get(Option.JAAS_LOGIN_CONFIG_NAME),
simple.getUserID(), simple.getPassword()));
for (String attributeName : simple.getAttributeNames()) {
Object attributeValue = simple.getAttribute(attributeName);
sessionAttributes.put(attributeName, attributeValue);
}
} else if (credentials instanceof SecurityContextCredentials) {
execContext = executionContext.with(((SecurityContextCredentials)credentials).getSecurityContext());
} else {
// Check if credentials provide a login context
try {
Method method = credentials.getClass().getMethod("getLoginContext");
if (method.getReturnType() != LoginContext.class) {
throw new IllegalArgumentException(
JcrI18n.credentialsMustReturnLoginContext.text(credentials.getClass()));
}
LoginContext loginContext = (LoginContext)method.invoke(credentials);
if (loginContext == null) {
throw new IllegalArgumentException(
JcrI18n.credentialsMustReturnLoginContext.text(credentials.getClass()));
}
execContext = executionContext.with(new JaasSecurityContext(loginContext));
} catch (NoSuchMethodException error) {
throw new IllegalArgumentException(JcrI18n.credentialsMustProvideJaasMethod.text(credentials.getClass()),
error);
}
}