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 CallbackHandler callbackHandler
;
29 private Map
<String
, Object
> sharedState
= null;
31 private boolean isAnonymous
= false;
33 private BundleContext bc
;
35 @SuppressWarnings("unchecked")
37 public void initialize(Subject subject
, CallbackHandler callbackHandler
, Map
<String
, ?
> sharedState
,
38 Map
<String
, ?
> options
) {
40 bc
= FrameworkUtil
.getBundle(UserAdminLoginModule
.class).getBundleContext();
42 // this.subject = subject;
43 this.callbackHandler
= callbackHandler
;
44 this.sharedState
= (Map
<String
, Object
>) sharedState
;
45 if (options
.containsKey("anonymous"))
46 isAnonymous
= Boolean
.parseBoolean(options
.get("anonymous").toString());
47 } catch (Exception e
) {
48 throw new CmsException("Cannot initialize login module", e
);
53 public boolean login() throws LoginException
{
54 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
55 Authorization authorization
= null;
57 authorization
= userAdmin
.getAuthorization(null);
59 // ask for username and password
60 NameCallback nameCallback
= new NameCallback("User");
61 PasswordCallback passwordCallback
= new PasswordCallback("Password", false);
62 LanguageCallback langCallback
= new LanguageCallback();
64 callbackHandler
.handle(new Callback
[] { nameCallback
, passwordCallback
, langCallback
});
65 } catch (IOException e
) {
66 throw new LoginException("Cannot handle callback: " + e
.getMessage());
67 } catch (ThreadDeath e
) {
68 throw new ThreadDeathLoginException("Callbackhandler thread died", e
);
69 } catch (UnsupportedCallbackException e
) {
74 Locale locale
= langCallback
.getLocale();
76 locale
= Locale
.getDefault();
77 UiContext
.setLocale(locale
);
79 authorization
= (Authorization
) sharedState
.get(SHARED_STATE_AUTHORIZATION
);
81 if (authorization
== null) {
83 final String username
= nameCallback
.getName();
84 if (username
== null || username
.trim().equals("")) {
85 // authorization = userAdmin.getAuthorization(null);
86 throw new CredentialNotFoundException("No credentials provided");
89 if (passwordCallback
.getPassword() != null)
90 password
= passwordCallback
.getPassword();
92 throw new CredentialNotFoundException("No credentials provided");
94 User user
= userAdmin
.getUser(null, username
);
96 throw new FailedLoginException("Invalid credentials");
97 if (!user
.hasCredential(null, password
))
98 throw new FailedLoginException("Invalid credentials");
101 // Log and monitor new login
102 // if (log.isDebugEnabled())
103 // log.debug("Logged in to CMS with username [" + username +
106 authorization
= userAdmin
.getAuthorization(user
);
110 if (!sharedState
.containsKey(SHARED_STATE_AUTHORIZATION
))
111 sharedState
.put(SHARED_STATE_AUTHORIZATION
, authorization
);
116 public boolean commit() throws LoginException
{
121 public boolean abort() throws LoginException
{
126 public boolean logout() throws LoginException
{