]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/UserAdminLoginModule.java
Continue framework clean up.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / 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.ArgeoException;
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 final static Log log =
29 // LogFactory.getLog(UserAdminLoginModule.class);
30 //
31 // private Subject subject;
32 private CallbackHandler callbackHandler;
33 private Map<String, Object> sharedState = null;
34
35 private boolean isAnonymous = false;
36
37 // private HttpServletRequest request = null;
38 private BundleContext bc;
39
40 @SuppressWarnings("unchecked")
41 @Override
42 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
43 Map<String, ?> options) {
44 try {
45 bc = FrameworkUtil.getBundle(UserAdminLoginModule.class).getBundleContext();
46 assert bc != null;
47 // this.subject = subject;
48 this.callbackHandler = callbackHandler;
49 this.sharedState = (Map<String, Object>) sharedState;
50 if (options.containsKey("anonymous"))
51 isAnonymous = Boolean.parseBoolean(options.get("anonymous").toString());
52 } catch (Exception e) {
53 throw new ArgeoException("Cannot initialize login module", e);
54 }
55 }
56
57 @Override
58 public boolean login() throws LoginException {
59 UserAdmin userAdmin = bc.getService(bc.getServiceReference(UserAdmin.class));
60 Authorization authorization = null;
61 if (isAnonymous) {
62 authorization = userAdmin.getAuthorization(null);
63 } else {
64 // HttpRequestCallback httpCallback = new HttpRequestCallback();
65 // ask for username and password
66 NameCallback nameCallback = new NameCallback("User");
67 PasswordCallback passwordCallback = new PasswordCallback("Password", false);
68 LanguageCallback langCallback = new LanguageCallback();
69 try {
70 callbackHandler.handle(new Callback[] { nameCallback, passwordCallback, langCallback });
71 } catch (IOException e) {
72 throw new LoginException("Cannot handle callback: " + e.getMessage());
73 } catch (ThreadDeath e) {
74 throw new ThreadDeathLoginException("Callbackhandler thread died", e);
75 } catch (UnsupportedCallbackException e) {
76 return false;
77 }
78
79 // check http
80 // request = httpCallback.getRequest();
81 // authorization = checkHttp();
82
83 // i18n
84 Locale locale = langCallback.getLocale();
85 if (locale == null)
86 locale = Locale.getDefault();
87 UiContext.setLocale(locale);
88
89 authorization = (Authorization) sharedState.get(SHARED_STATE_AUTHORIZATION);
90
91 if (authorization == null) {
92 // create credentials
93 final String username = nameCallback.getName();
94 if (username == null || username.trim().equals("")) {
95 // authorization = userAdmin.getAuthorization(null);
96 throw new CredentialNotFoundException("No credentials provided");
97 } else {
98 char[] password = {};
99 if (passwordCallback.getPassword() != null)
100 password = passwordCallback.getPassword();
101 else
102 throw new CredentialNotFoundException("No credentials provided");
103
104 User user = userAdmin.getUser(null, username);
105 if (user == null)
106 throw new FailedLoginException("Invalid credentials");
107 if (!user.hasCredential(null, password))
108 throw new FailedLoginException("Invalid credentials");
109 // return false;
110
111 // Log and monitor new login
112 // if (log.isDebugEnabled())
113 // log.debug("Logged in to CMS with username [" + username +
114 // "]");
115
116 authorization = userAdmin.getAuthorization(user);
117 }
118 }
119 }
120 if (!sharedState.containsKey(SHARED_STATE_AUTHORIZATION))
121 sharedState.put(SHARED_STATE_AUTHORIZATION, authorization);
122 // subject.getPrivateCredentials().add(authorization);
123 return true;
124 }
125
126 // private Authorization checkHttp() {
127 // Authorization authorization = null;
128 // if (request != null) {
129 // authorization = (Authorization)
130 // request.getAttribute(HttpContext.AUTHORIZATION);
131 // if (authorization == null) {
132 // String sessionId = request.getSession().getId();
133 // authorization = (Authorization)
134 // request.getSession().getAttribute(HttpContext.AUTHORIZATION);
135 // if (authorization == null) {
136 // Collection<ServiceReference<CmsSession>> sr;
137 // try {
138 // sr = bc.getServiceReferences(CmsSession.class,
139 // "(" + CmsSession.CMS_SESSION_ID + "=" + sessionId + ")");
140 // } catch (InvalidSyntaxException e) {
141 // throw new CmsException("Cannot get CMS session for id " + sessionId, e);
142 // }
143 // if (sr.size() == 1) {
144 // CmsSession cmsSession = bc.getService(sr.iterator().next());
145 // authorization = cmsSession.getAuthorization();
146 // if (log.isTraceEnabled())
147 // log.trace("Retrieved authorization from " + cmsSession);
148 // }
149 // }
150 // }
151 // }
152 // return authorization;
153 // }
154
155 @Override
156 public boolean commit() throws LoginException {
157 // Authorization authorization =
158 // subject.getPrivateCredentials(Authorization.class).iterator().next();
159 // if (request != null && authorization.getName() != null) {
160 // request.setAttribute(HttpContext.REMOTE_USER,
161 // authorization.getName());
162 // request.setAttribute(HttpContext.AUTHORIZATION, authorization);
163 //
164 // HttpSession httpSession = request.getSession();
165 // if (httpSession.getAttribute(HttpContext.AUTHORIZATION) == null) {
166 //
167 // String sessionId = request.getSession().getId();
168 // Collection<ServiceReference<CmsSession>> sr;
169 // try {
170 // sr = bc.getServiceReferences(CmsSession.class,
171 // "(" + CmsSession.CMS_SESSION_ID + "=" + sessionId + ")");
172 // } catch (InvalidSyntaxException e) {
173 // throw new CmsException("Cannot get CMS session for id " + sessionId,
174 // e);
175 // }
176 // CmsSession cmsSession;
177 // if (sr.size() == 1) {
178 // cmsSession = bc.getService(sr.iterator().next());
179 // } else if (sr.size() == 0) {
180 // Hashtable<String, String> props = new Hashtable<>();
181 // props.put(CmsSession.CMS_DN, authorization.getName());
182 // props.put(CmsSession.CMS_SESSION_ID, sessionId);
183 // cmsSession = new CmsSessionImpl(sessionId, authorization);
184 // bc.registerService(CmsSession.class, cmsSession, props);
185 // if (log.isDebugEnabled())
186 // log.debug("Initialized " + cmsSession + " for " +
187 // authorization.getName());
188 // } else
189 // throw new CmsException(sr.size() + " CMS sessions registered for " +
190 // sessionId);
191 // cmsSession.addHttpSession(request);
192 // if (log.isTraceEnabled())
193 // log.trace("Added " + request.getServletPath() + " to " + cmsSession +
194 // " (" + request.getRequestURI()
195 // + ")");
196 // httpSession.setAttribute(HttpContext.AUTHORIZATION, authorization);
197 // }
198 // subject.getPrivateCredentials().add(request.getSession());
199 // }
200 return true;
201 }
202
203 @Override
204 public boolean abort() throws LoginException {
205 // cleanUp();
206 return true;
207 }
208
209 @Override
210 public boolean logout() throws LoginException {
211 // Set<HttpSession> httpSession =
212 // subject.getPrivateCredentials(HttpSession.class);
213 // Iterator<HttpSession> it = httpSession.iterator();
214 // while (it.hasNext()) {
215 // HttpSession sess = it.next();
216 // sess.setAttribute(HttpContext.AUTHORIZATION, null);
217 // // sess.setMaxInactiveInterval(1);// invalidate session
218 //
219 // // TODO log out CMS session
220 // }
221 // subject.getPrivateCredentials().removeAll(httpSession);
222 //
223 // cleanUp();
224 return true;
225 }
226
227 // private void cleanUp() {
228 // subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(Authorization.class));
229 // subject = null;
230 // }
231
232 }