+ if (locale != null)
+ subject.getPublicCredentials().add(locale);
+
+// if (singleUser) {
+// OsUserUtils.loginAsSystemUser(subject);
+// }
+ UserAdmin userAdmin = CmsContextImpl.getCmsContext().getUserAdmin();
+ Authorization authorization;
+ if (callbackHandler == null && !sharedState.containsKey(CmsAuthUtils.SHARED_STATE_OS_USERNAME)) {// anonymous
+ authorization = userAdmin.getAuthorization(null);
+ } else if (bindAuthorization != null) {// bind
+ authorization = bindAuthorization;
+ } else {// Kerberos
+ User authenticatingUser;
+ Set<KerberosPrincipal> kerberosPrincipals = subject.getPrincipals(KerberosPrincipal.class);
+ if (kerberosPrincipals.isEmpty()) {
+ if (authenticatedUser == null) {
+ if (log.isTraceEnabled())
+ log.trace("Neither kerberos nor user admin login succeeded. Login failed.");
+ throw new CredentialNotFoundException("Bad credentials.");
+ } else {
+ authenticatingUser = authenticatedUser;
+ }
+ } else {
+ KerberosPrincipal kerberosPrincipal = kerberosPrincipals.iterator().next();
+ LdapName dn = IpaUtils.kerberosToDn(kerberosPrincipal.getName());
+ authenticatingUser = new AuthenticatingUser(dn);
+ if (authenticatedUser != null && !authenticatingUser.getName().equals(authenticatedUser.getName()))
+ throw new LoginException("Kerberos login " + authenticatingUser.getName()
+ + " is inconsistent with user admin login " + authenticatedUser.getName());
+ }
+ if (log.isTraceEnabled())
+ log.trace("Retrieve authorization for " + authenticatingUser + "... ");
+ authorization = Subject.doAs(subject, new PrivilegedAction<Authorization>() {
+
+ @Override
+ public Authorization run() {
+ Authorization authorization = userAdmin.getAuthorization(authenticatingUser);
+ return authorization;
+ }
+
+ });
+ if (authorization == null)
+ throw new LoginException(
+ "User admin found no authorization for authenticated user " + authenticatingUser.getName());
+ }
+
+ // Log and monitor new login
+ RemoteAuthRequest request = (RemoteAuthRequest) sharedState.get(CmsAuthUtils.SHARED_STATE_HTTP_REQUEST);
+ CmsAuthUtils.addAuthorization(subject, authorization);
+
+ // Unlock keyring (underlying login to the JCR repository)
+// char[] password = (char[]) sharedState.get(CmsAuthUtils.SHARED_STATE_PWD);
+// if (password != null) {
+// ServiceReference<CryptoKeyring> keyringSr = bc.getServiceReference(CryptoKeyring.class);
+// if (keyringSr != null) {
+// CryptoKeyring keyring = bc.getService(keyringSr);
+// Subject.doAs(subject, new PrivilegedAction<Void>() {
+//
+// @Override
+// public Void run() {
+// try {
+// keyring.unlock(password);
+// } catch (Exception e) {
+// e.printStackTrace();
+// log.warn("Could not unlock keyring with the password provided by " + authorization.getName()
+// + ": " + e.getMessage());
+// }
+// return null;
+// }
+//
+// });
+// }
+// }
+
+ // Register CmsSession with initial subject
+ CmsAuthUtils.registerSessionAuthorization(request, subject, authorization, locale);
+
+ if (log.isDebugEnabled()) {
+ StringBuilder msg = new StringBuilder();
+ msg.append("Logged in to CMS: " + authorization.getName() + "(" + authorization + ")\n");
+ for (Principal principal : subject.getPrincipals()) {
+ msg.append(" Principal: " + principal.getName()).append(" (")
+ .append(principal.getClass().getSimpleName()).append(")\n");
+ }
+ for (Object credential : subject.getPublicCredentials()) {
+ msg.append(" Public Credential: " + credential).append(" (")
+ .append(credential.getClass().getSimpleName()).append(")\n");
+ }
+ log.debug(msg);