1 package org
.argeo
.cms
.auth
;
3 import java
.io
.IOException
;
4 import java
.security
.PrivilegedAction
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
8 import java
.util
.Locale
;
12 import javax
.naming
.ldap
.LdapName
;
13 import javax
.security
.auth
.Subject
;
14 import javax
.security
.auth
.callback
.Callback
;
15 import javax
.security
.auth
.callback
.CallbackHandler
;
16 import javax
.security
.auth
.callback
.LanguageCallback
;
17 import javax
.security
.auth
.callback
.NameCallback
;
18 import javax
.security
.auth
.callback
.PasswordCallback
;
19 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
20 import javax
.security
.auth
.kerberos
.KerberosPrincipal
;
21 import javax
.security
.auth
.login
.CredentialNotFoundException
;
22 import javax
.security
.auth
.login
.FailedLoginException
;
23 import javax
.security
.auth
.login
.LoginException
;
24 import javax
.security
.auth
.spi
.LoginModule
;
25 import javax
.servlet
.http
.HttpServletRequest
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.cms
.CmsException
;
30 import org
.argeo
.naming
.LdapAttrs
;
31 import org
.argeo
.osgi
.useradmin
.IpaUtils
;
32 import org
.osgi
.framework
.BundleContext
;
33 import org
.osgi
.framework
.FrameworkUtil
;
34 import org
.osgi
.service
.useradmin
.Authorization
;
35 import org
.osgi
.service
.useradmin
.User
;
36 import org
.osgi
.service
.useradmin
.UserAdmin
;
38 public class UserAdminLoginModule
implements LoginModule
{
39 private final static Log log
= LogFactory
.getLog(UserAdminLoginModule
.class);
41 private Subject subject
;
42 private CallbackHandler callbackHandler
;
43 private Map
<String
, Object
> sharedState
= null;
45 private List
<String
> indexedUserProperties
= Arrays
46 .asList(new String
[] { LdapAttrs
.uid
.name(), LdapAttrs
.mail
.name(), LdapAttrs
.cn
.name() });
49 private BundleContext bc
;
50 private User authenticatedUser
= null;
51 private Locale locale
;
53 @SuppressWarnings("unchecked")
55 public void initialize(Subject subject
, CallbackHandler callbackHandler
, Map
<String
, ?
> sharedState
,
56 Map
<String
, ?
> options
) {
57 this.subject
= subject
;
59 bc
= FrameworkUtil
.getBundle(UserAdminLoginModule
.class).getBundleContext();
60 this.callbackHandler
= callbackHandler
;
61 this.sharedState
= (Map
<String
, Object
>) sharedState
;
62 } catch (Exception e
) {
63 throw new CmsException("Cannot initialize login module", e
);
68 public boolean login() throws LoginException
{
69 // Authorization sharedAuth = (Authorization)
70 // sharedState.get(CmsAuthUtils.SHARED_STATE_AUTHORIZATION);
71 // if (sharedAuth != null) {
72 // if (callbackHandler == null && sharedAuth.getName() != null)
73 // throw new LoginException("Shared authorization should be anonymous");
76 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
77 if (callbackHandler
== null) {// anonymous
78 // authorization = userAdmin.getAuthorization(null);
79 // sharedState.put(CmsAuthUtils.SHARED_STATE_AUTHORIZATION,
84 final String username
;
85 final char[] password
;
86 if (sharedState
.containsKey(CmsAuthUtils
.SHARED_STATE_NAME
)
87 && sharedState
.containsKey(CmsAuthUtils
.SHARED_STATE_PWD
)) {
88 // NB: required by Basic http auth
89 username
= (String
) sharedState
.get(CmsAuthUtils
.SHARED_STATE_NAME
);
90 password
= (char[]) sharedState
.get(CmsAuthUtils
.SHARED_STATE_PWD
);
92 // // NB: raw user name is used
93 // AuthenticatingUser authenticatingUser = new
94 // AuthenticatingUser(username, password);
95 // authorization = userAdmin.getAuthorization(authenticatingUser);
97 // ask for username and password
98 NameCallback nameCallback
= new NameCallback("User");
99 PasswordCallback passwordCallback
= new PasswordCallback("Password", false);
100 LanguageCallback langCallback
= new LanguageCallback();
102 callbackHandler
.handle(new Callback
[] { nameCallback
, passwordCallback
, langCallback
});
103 } catch (IOException e
) {
104 throw new LoginException("Cannot handle callback: " + e
.getMessage());
105 } catch (UnsupportedCallbackException e
) {
110 locale
= langCallback
.getLocale();
112 locale
= Locale
.getDefault();
113 // FIXME add it to Subject
114 // UiContext.setLocale(locale);
116 username
= nameCallback
.getName();
117 if (username
== null || username
.trim().equals("")) {
118 // authorization = userAdmin.getAuthorization(null);
119 throw new CredentialNotFoundException("No credentials provided");
121 if (passwordCallback
.getPassword() != null)
122 password
= passwordCallback
.getPassword();
124 throw new CredentialNotFoundException("No credentials provided");
127 // User user = userAdmin.getUser(null, username);
128 User user
= searchForUser(userAdmin
, username
);
130 return true;// expect Kerberos
131 // throw new FailedLoginException("Invalid credentials");
132 if (!user
.hasCredential(null, password
))
133 throw new FailedLoginException("Invalid credentials");
134 authenticatedUser
= user
;
139 public boolean commit() throws LoginException
{
140 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
141 Authorization authorization
;
142 if (callbackHandler
== null) {// anonymous
143 authorization
= userAdmin
.getAuthorization(null);
145 User authenticatingUser
;
146 Set
<KerberosPrincipal
> kerberosPrincipals
= subject
.getPrincipals(KerberosPrincipal
.class);
147 if (kerberosPrincipals
.isEmpty()) {
148 if (authenticatedUser
== null) {
149 if (log
.isTraceEnabled())
150 log
.trace("Neither kerberos nor user admin login succeeded. Login failed.");
153 authenticatingUser
= authenticatedUser
;
156 KerberosPrincipal kerberosPrincipal
= kerberosPrincipals
.iterator().next();
157 LdapName dn
= IpaUtils
.kerberosToDn(kerberosPrincipal
.getName());
158 authenticatingUser
= new AuthenticatingUser(dn
);
159 if (authenticatedUser
!= null && !authenticatingUser
.getName().equals(authenticatedUser
.getName()))
160 throw new LoginException("Kerberos login " + authenticatingUser
.getName()
161 + " is inconsistent with user admin login " + authenticatedUser
.getName());
163 authorization
= Subject
.doAs(subject
, new PrivilegedAction
<Authorization
>() {
166 public Authorization
run() {
167 Authorization authorization
= userAdmin
.getAuthorization(authenticatingUser
);
168 return authorization
;
172 if (authorization
== null)
173 throw new LoginException(
174 "User admin found no authorization for authenticated user " + authenticatingUser
.getName());
176 // Log and monitor new login
177 CmsAuthUtils
.addAuthorization(subject
, authorization
, locale
,
178 (HttpServletRequest
) sharedState
.get(CmsAuthUtils
.SHARED_STATE_HTTP_REQUEST
));
179 if (log
.isDebugEnabled())
180 log
.debug("Logged in to CMS: " + subject
);
185 public boolean abort() throws LoginException
{
190 public boolean logout() throws LoginException
{
191 if (log
.isTraceEnabled())
192 log
.trace("Logging out from CMS... " + subject
);
193 // boolean httpSessionLogoutOk = CmsAuthUtils.logoutSession(bc,
195 CmsAuthUtils
.cleanUp(subject
);
199 protected User
searchForUser(UserAdmin userAdmin
, String providedUsername
) {
201 // TODO check value null or empty
202 List
<User
> collectedUsers
= new ArrayList
<User
>();
206 user
= (User
) userAdmin
.getRole(providedUsername
);
208 collectedUsers
.add(user
);
209 } catch (Exception e
) {
213 for (String attr
: indexedUserProperties
) {
214 user
= userAdmin
.getUser(attr
, providedUsername
);
216 collectedUsers
.add(user
);
218 if (collectedUsers
.size() == 1)
219 return collectedUsers
.get(0);
220 else if (collectedUsers
.size() > 1)
221 log
.warn(collectedUsers
.size() + " users for provided username" + providedUsername
);
223 } catch (Exception e
) {
224 if (log
.isTraceEnabled())
225 log
.warn("Cannot search for user " + providedUsername
, e
);