]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserAdminLoginModule.java
f5883a54f1035fc10608f7a5152380affa486c7a
[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 {
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 =
51 // Boolean.parseBoolean(options.get("anonymous").toString());
52 } catch (Exception e) {
53 throw new CmsException("Cannot initialize login module", e);
54 }
55 }
56
57 @Override
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");
63 return false;
64 }
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);
69 return true;
70 }
71
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);
78 // TODO locale?
79 AuthenticatingUser authenticatingUser = new AuthenticatingUser(username, password);
80 authorization = userAdmin.getAuthorization(authenticatingUser);
81 } else {
82
83 // ask for username and password
84 NameCallback nameCallback = new NameCallback("User");
85 PasswordCallback passwordCallback = new PasswordCallback("Password", false);
86 LanguageCallback langCallback = new LanguageCallback();
87 try {
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
93 // died", e);
94 } catch (UnsupportedCallbackException e) {
95 return false;
96 }
97
98 // i18n
99 Locale locale = langCallback.getLocale();
100 if (locale == null)
101 locale = Locale.getDefault();
102 UiContext.setLocale(locale);
103
104 // authorization = (Authorization)
105 // sharedState.get(CmsAuthUtils.SHARED_STATE_AUTHORIZATION);
106 //
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");
113 }
114 // char[] password = {};
115 if (passwordCallback.getPassword() != null)
116 password = passwordCallback.getPassword();
117 else
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);
121 if (user == null)
122 throw new FailedLoginException("Invalid credentials");
123 if (!user.hasCredential(null, password))
124 throw new FailedLoginException("Invalid credentials");
125 // return false;
126
127 // Log and monitor new login
128 // if (log.isDebugEnabled())
129 // log.debug("Logged in to CMS with username [" + username +
130 // "]");
131
132 authorization = userAdmin.getAuthorization(user);
133 assert authorization != null;
134 }
135
136 // }
137 // if
138 // (!sharedState.containsKey(CmsAuthUtils.SHARED_STATE_AUTHORIZATION))
139 sharedState.put(CmsAuthUtils.SHARED_STATE_AUTHORIZATION, authorization);
140 return authorization != null;
141 }
142
143 @Override
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);
156 // }
157 if (authorization == null) {
158 return false;
159 // throw new LoginException("Authorization should not be null");
160 } else {
161 CmsAuthUtils.addAuthentication(subject, authorization);
162 return true;
163 }
164 }
165
166 @Override
167 public boolean abort() throws LoginException {
168 authorization = null;
169 return true;
170 }
171
172 @Override
173 public boolean logout() throws LoginException {
174 CmsAuthUtils.cleanUp(subject);
175 return true;
176 }
177 }