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
.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
;
27 public class UserAdminLoginModule
implements LoginModule
, AuthConstants
{
28 // private final static Log log =
29 // LogFactory.getLog(UserAdminLoginModule.class);
31 // private Subject subject;
32 private CallbackHandler callbackHandler
;
33 private Map
<String
, Object
> sharedState
= null;
35 private boolean isAnonymous
= false;
37 // private HttpServletRequest request = null;
38 private BundleContext bc
;
40 @SuppressWarnings("unchecked")
42 public void initialize(Subject subject
, CallbackHandler callbackHandler
, Map
<String
, ?
> sharedState
,
43 Map
<String
, ?
> options
) {
45 bc
= FrameworkUtil
.getBundle(UserAdminLoginModule
.class).getBundleContext();
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
);
58 public boolean login() throws LoginException
{
59 UserAdmin userAdmin
= bc
.getService(bc
.getServiceReference(UserAdmin
.class));
60 Authorization authorization
= null;
62 authorization
= userAdmin
.getAuthorization(null);
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();
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
) {
80 // request = httpCallback.getRequest();
81 // authorization = checkHttp();
84 Locale locale
= langCallback
.getLocale();
86 locale
= Locale
.getDefault();
87 UiContext
.setLocale(locale
);
89 authorization
= (Authorization
) sharedState
.get(SHARED_STATE_AUTHORIZATION
);
91 if (authorization
== null) {
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");
99 if (passwordCallback
.getPassword() != null)
100 password
= passwordCallback
.getPassword();
102 throw new CredentialNotFoundException("No credentials provided");
104 User user
= userAdmin
.getUser(null, username
);
106 throw new FailedLoginException("Invalid credentials");
107 if (!user
.hasCredential(null, password
))
108 throw new FailedLoginException("Invalid credentials");
111 // Log and monitor new login
112 // if (log.isDebugEnabled())
113 // log.debug("Logged in to CMS with username [" + username +
116 authorization
= userAdmin
.getAuthorization(user
);
120 if (!sharedState
.containsKey(SHARED_STATE_AUTHORIZATION
))
121 sharedState
.put(SHARED_STATE_AUTHORIZATION
, authorization
);
122 // subject.getPrivateCredentials().add(authorization);
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;
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);
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);
152 // return authorization;
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);
164 // HttpSession httpSession = request.getSession();
165 // if (httpSession.getAttribute(HttpContext.AUTHORIZATION) == null) {
167 // String sessionId = request.getSession().getId();
168 // Collection<ServiceReference<CmsSession>> sr;
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,
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());
189 // throw new CmsException(sr.size() + " CMS sessions registered for " +
191 // cmsSession.addHttpSession(request);
192 // if (log.isTraceEnabled())
193 // log.trace("Added " + request.getServletPath() + " to " + cmsSession +
194 // " (" + request.getRequestURI()
196 // httpSession.setAttribute(HttpContext.AUTHORIZATION, authorization);
198 // subject.getPrivateCredentials().add(request.getSession());
204 public boolean abort() throws LoginException
{
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
219 // // TODO log out CMS session
221 // subject.getPrivateCredentials().removeAll(httpSession);
227 // private void cleanUp() {
228 // subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(Authorization.class));