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
{
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"))
51 // Boolean.parseBoolean(options.get("anonymous").toString());
52 } catch (Exception e
) {
53 throw new CmsException("Cannot initialize login module", e
);
58 public boolean login() throws LoginException
{
59 Authorization sharedAuth
= (Authorization
) sharedState
.get(CmsAuthUtils
.SHARED_STATE_AUTHORIZATION
);
60 if (sharedAuth
!= null) {
61 if (callbackHandler
== null && sharedAuth
.getName() != null)
62 throw new LoginException("Shared authorization should be anonymous");
65 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
66 if (callbackHandler
== null) {// anonymous
67 authorization
= userAdmin
.getAuthorization(null);
68 sharedState
.put(CmsAuthUtils
.SHARED_STATE_AUTHORIZATION
, authorization
);
72 final String username
;
73 final char[] password
;
74 if (sharedState
.containsKey(CmsAuthUtils
.SHARED_STATE_NAME
)
75 && sharedState
.containsKey(CmsAuthUtils
.SHARED_STATE_PWD
)) {
76 username
= (String
) sharedState
.get(CmsAuthUtils
.SHARED_STATE_NAME
);
77 password
= (char[]) sharedState
.get(CmsAuthUtils
.SHARED_STATE_PWD
);
79 AuthenticatingUser authenticatingUser
= new AuthenticatingUser(username
, password
);
80 authorization
= userAdmin
.getAuthorization(authenticatingUser
);
83 // ask for username and password
84 NameCallback nameCallback
= new NameCallback("User");
85 PasswordCallback passwordCallback
= new PasswordCallback("Password", false);
86 LanguageCallback langCallback
= new LanguageCallback();
88 callbackHandler
.handle(new Callback
[] { nameCallback
, passwordCallback
, langCallback
});
89 } catch (IOException e
) {
90 throw new LoginException("Cannot handle callback: " + e
.getMessage());
91 // } catch (ThreadDeath e) {
92 // throw new ThreadDeathLoginException("Callbackhandler thread
94 } catch (UnsupportedCallbackException e
) {
99 Locale locale
= langCallback
.getLocale();
101 locale
= Locale
.getDefault();
102 UiContext
.setLocale(locale
);
104 // authorization = (Authorization)
105 // sharedState.get(CmsAuthUtils.SHARED_STATE_AUTHORIZATION);
107 // if (authorization == null) {
108 // create credentials
109 username
= nameCallback
.getName();
110 if (username
== null || username
.trim().equals("")) {
111 // authorization = userAdmin.getAuthorization(null);
112 throw new CredentialNotFoundException("No credentials provided");
114 // char[] password = {};
115 if (passwordCallback
.getPassword() != null)
116 password
= passwordCallback
.getPassword();
118 throw new CredentialNotFoundException("No credentials provided");
119 // FIXME move Argeo specific convention from user admin to here
120 User user
= userAdmin
.getUser(null, username
);
122 throw new FailedLoginException("Invalid credentials");
123 if (!user
.hasCredential(null, password
))
124 throw new FailedLoginException("Invalid credentials");
127 // Log and monitor new login
128 // if (log.isDebugEnabled())
129 // log.debug("Logged in to CMS with username [" + username +
132 authorization
= userAdmin
.getAuthorization(user
);
133 assert authorization
!= null;
138 // (!sharedState.containsKey(CmsAuthUtils.SHARED_STATE_AUTHORIZATION))
139 sharedState
.put(CmsAuthUtils
.SHARED_STATE_AUTHORIZATION
, authorization
);
140 return authorization
!= null;
144 public boolean commit() throws LoginException
{
145 // Set<KerberosPrincipal> kerberosPrincipals =
146 // subject.getPrincipals(KerberosPrincipal.class);
147 // if (kerberosPrincipals.size() != 0) {
148 // KerberosPrincipal kerberosPrincipal =
149 // kerberosPrincipals.iterator().next();
150 // System.out.println(kerberosPrincipal);
151 // UserAdmin userAdmin =
152 // bc.getService(bc.getServiceReference(UserAdmin.class));
153 // User user = userAdmin.getUser(null, kerberosPrincipal.getName());
154 // Authorization authorization = userAdmin.getAuthorization(user);
155 // sharedState.put(SHARED_STATE_AUTHORIZATION, authorization);
157 if (authorization
== null) {
159 // throw new LoginException("Authorization should not be null");
161 CmsAuthUtils
.addAuthentication(subject
, authorization
);
167 public boolean abort() throws LoginException
{
168 authorization
= null;
173 public boolean logout() throws LoginException
{
174 CmsAuthUtils
.cleanUp(subject
);