]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserAdminLoginModule.java
515f4dc0daee943e880b740f01bb69e9027b2c07
[lgpl/argeo-commons.git] / UserAdminLoginModule.java
1 package org.argeo.cms.auth;
2
3 import java.io.IOException;
4 import java.util.Locale;
5 import java.util.Map;
6
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;
18
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;
26
27 public class UserAdminLoginModule implements LoginModule, AuthConstants {
28 private Subject subject;
29 private CallbackHandler callbackHandler;
30 private Map<String, Object> sharedState = null;
31
32 private boolean isAnonymous = false;
33
34 // private state
35 private BundleContext bc;
36 private Authorization authorization;
37
38 @SuppressWarnings("unchecked")
39 @Override
40 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
41 Map<String, ?> options) {
42 this.subject = subject;
43 try {
44 bc = FrameworkUtil.getBundle(UserAdminLoginModule.class).getBundleContext();
45 assert bc != null;
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);
53 }
54 }
55
56 @Override
57 public boolean login() throws LoginException {
58 UserAdmin userAdmin = bc.getService(bc.getServiceReference(UserAdmin.class));
59 if (isAnonymous) {
60 authorization = userAdmin.getAuthorization(null);
61 } else {
62 // ask for username and password
63 NameCallback nameCallback = new NameCallback("User");
64 PasswordCallback passwordCallback = new PasswordCallback("Password", false);
65 LanguageCallback langCallback = new LanguageCallback();
66 try {
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
72 // died", e);
73 } catch (UnsupportedCallbackException e) {
74 return false;
75 }
76
77 // i18n
78 Locale locale = langCallback.getLocale();
79 if (locale == null)
80 locale = Locale.getDefault();
81 UiContext.setLocale(locale);
82
83 authorization = (Authorization) sharedState.get(SHARED_STATE_AUTHORIZATION);
84
85 if (authorization == null) {
86 // create credentials
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");
91 } else {
92 char[] password = {};
93 if (passwordCallback.getPassword() != null)
94 password = passwordCallback.getPassword();
95 else
96 throw new CredentialNotFoundException("No credentials provided");
97
98 User user = userAdmin.getUser(null, username);
99 if (user == null)
100 throw new FailedLoginException("Invalid credentials");
101 if (!user.hasCredential(null, password))
102 throw new FailedLoginException("Invalid credentials");
103 // return false;
104
105 // Log and monitor new login
106 // if (log.isDebugEnabled())
107 // log.debug("Logged in to CMS with username [" + username +
108 // "]");
109
110 authorization = userAdmin.getAuthorization(user);
111 }
112 }
113 }
114 if (!sharedState.containsKey(SHARED_STATE_AUTHORIZATION))
115 sharedState.put(SHARED_STATE_AUTHORIZATION, authorization);
116 return authorization != null;
117 }
118
119 @Override
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);
132 // }
133 if (authorization == null) {
134 return false;
135 // throw new LoginException("Authorization should not be null");
136 } else {
137 CmsAuthUtils.addAuthentication(subject, authorization);
138 return true;
139 }
140 }
141
142 @Override
143 public boolean abort() throws LoginException {
144 authorization = null;
145 return true;
146 }
147
148 @Override
149 public boolean logout() throws LoginException {
150 CmsAuthUtils.cleanUp(subject);
151 return true;
152 }
153 }