1 package org
.argeo
.cms
.auth
;
3 import java
.io
.IOException
;
4 import java
.util
.Locale
;
7 import javax
.security
.auth
.Subject
;
8 import javax
.security
.auth
.callback
.Callback
;
9 import javax
.security
.auth
.callback
.CallbackHandler
;
10 import javax
.security
.auth
.callback
.LanguageCallback
;
11 import javax
.security
.auth
.callback
.NameCallback
;
12 import javax
.security
.auth
.callback
.PasswordCallback
;
13 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
14 import javax
.security
.auth
.login
.CredentialNotFoundException
;
15 import javax
.security
.auth
.login
.FailedLoginException
;
16 import javax
.security
.auth
.login
.LoginException
;
17 import javax
.security
.auth
.spi
.LoginModule
;
19 import org
.argeo
.cms
.CmsException
;
20 import org
.argeo
.eclipse
.ui
.specific
.UiContext
;
21 import org
.osgi
.framework
.BundleContext
;
22 import org
.osgi
.framework
.FrameworkUtil
;
23 import org
.osgi
.service
.useradmin
.Authorization
;
24 import org
.osgi
.service
.useradmin
.User
;
25 import org
.osgi
.service
.useradmin
.UserAdmin
;
27 public class UserAdminLoginModule
implements LoginModule
, AuthConstants
{
28 private Subject subject
;
29 private CallbackHandler callbackHandler
;
30 private Map
<String
, Object
> sharedState
= null;
32 private boolean isAnonymous
= false;
35 private BundleContext bc
;
36 private Authorization authorization
;
38 @SuppressWarnings("unchecked")
40 public void initialize(Subject subject
, CallbackHandler callbackHandler
, Map
<String
, ?
> sharedState
,
41 Map
<String
, ?
> options
) {
42 this.subject
= subject
;
44 bc
= FrameworkUtil
.getBundle(UserAdminLoginModule
.class).getBundleContext();
46 // this.subject = subject;
47 this.callbackHandler
= callbackHandler
;
48 this.sharedState
= (Map
<String
, Object
>) sharedState
;
49 if (options
.containsKey("anonymous"))
50 isAnonymous
= Boolean
.parseBoolean(options
.get("anonymous").toString());
51 } catch (Exception e
) {
52 throw new CmsException("Cannot initialize login module", e
);
57 public boolean login() throws LoginException
{
58 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
60 authorization
= userAdmin
.getAuthorization(null);
62 // ask for username and password
63 NameCallback nameCallback
= new NameCallback("User");
64 PasswordCallback passwordCallback
= new PasswordCallback("Password", false);
65 LanguageCallback langCallback
= new LanguageCallback();
67 callbackHandler
.handle(new Callback
[] { nameCallback
, passwordCallback
, langCallback
});
68 } catch (IOException e
) {
69 throw new LoginException("Cannot handle callback: " + e
.getMessage());
70 // } catch (ThreadDeath e) {
71 // throw new ThreadDeathLoginException("Callbackhandler thread
73 } catch (UnsupportedCallbackException e
) {
78 Locale locale
= langCallback
.getLocale();
80 locale
= Locale
.getDefault();
81 UiContext
.setLocale(locale
);
83 authorization
= (Authorization
) sharedState
.get(SHARED_STATE_AUTHORIZATION
);
85 if (authorization
== null) {
87 final String username
= nameCallback
.getName();
88 if (username
== null || username
.trim().equals("")) {
89 // authorization = userAdmin.getAuthorization(null);
90 throw new CredentialNotFoundException("No credentials provided");
93 if (passwordCallback
.getPassword() != null)
94 password
= passwordCallback
.getPassword();
96 throw new CredentialNotFoundException("No credentials provided");
98 User user
= userAdmin
.getUser(null, username
);
100 throw new FailedLoginException("Invalid credentials");
101 if (!user
.hasCredential(null, password
))
102 throw new FailedLoginException("Invalid credentials");
105 // Log and monitor new login
106 // if (log.isDebugEnabled())
107 // log.debug("Logged in to CMS with username [" + username +
110 authorization
= userAdmin
.getAuthorization(user
);
114 if (!sharedState
.containsKey(SHARED_STATE_AUTHORIZATION
))
115 sharedState
.put(SHARED_STATE_AUTHORIZATION
, authorization
);
116 return authorization
!= null;
120 public boolean commit() throws LoginException
{
121 // Set<KerberosPrincipal> kerberosPrincipals =
122 // subject.getPrincipals(KerberosPrincipal.class);
123 // if (kerberosPrincipals.size() != 0) {
124 // KerberosPrincipal kerberosPrincipal =
125 // kerberosPrincipals.iterator().next();
126 // System.out.println(kerberosPrincipal);
127 // UserAdmin userAdmin =
128 // bc.getService(bc.getServiceReference(UserAdmin.class));
129 // User user = userAdmin.getUser(null, kerberosPrincipal.getName());
130 // Authorization authorization = userAdmin.getAuthorization(user);
131 // sharedState.put(SHARED_STATE_AUTHORIZATION, authorization);
133 if (authorization
== null) {
135 // throw new LoginException("Authorization should not be null");
137 CmsAuthUtils
.addAuthentication(subject
, authorization
);
143 public boolean abort() throws LoginException
{
144 authorization
= null;
149 public boolean logout() throws LoginException
{
150 CmsAuthUtils
.cleanUp(subject
);