1 package org
.argeo
.cms
.auth
;
3 import java
.io
.IOException
;
4 import java
.security
.cert
.X509Certificate
;
5 import java
.util
.Collection
;
7 import java
.util
.StringTokenizer
;
9 import javax
.security
.auth
.Subject
;
10 import javax
.security
.auth
.callback
.Callback
;
11 import javax
.security
.auth
.callback
.CallbackHandler
;
12 import javax
.security
.auth
.callback
.UnsupportedCallbackException
;
13 import javax
.security
.auth
.login
.LoginException
;
14 import javax
.security
.auth
.spi
.LoginModule
;
15 import javax
.servlet
.http
.HttpServletRequest
;
16 import javax
.servlet
.http
.HttpServletResponse
;
18 import org
.apache
.commons
.codec
.binary
.Base64
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.argeo
.cms
.CmsException
;
22 import org
.osgi
.framework
.BundleContext
;
23 import org
.osgi
.framework
.FrameworkUtil
;
24 import org
.osgi
.framework
.InvalidSyntaxException
;
25 import org
.osgi
.framework
.ServiceReference
;
26 import org
.osgi
.service
.http
.HttpContext
;
27 import org
.osgi
.service
.useradmin
.Authorization
;
29 public class HttpSessionLoginModule
implements LoginModule
{
30 private final static Log log
= LogFactory
.getLog(HttpSessionLoginModule
.class);
32 private Subject subject
= null;
33 private CallbackHandler callbackHandler
= null;
34 private Map
<String
, Object
> sharedState
= null;
36 private HttpServletRequest request
= null;
37 private HttpServletResponse response
= null;
39 private BundleContext bc
;
41 private Authorization authorization
;
43 @SuppressWarnings("unchecked")
45 public void initialize(Subject subject
, CallbackHandler callbackHandler
, Map
<String
, ?
> sharedState
,
46 Map
<String
, ?
> options
) {
47 bc
= FrameworkUtil
.getBundle(HttpSessionLoginModule
.class).getBundleContext();
49 this.subject
= subject
;
50 this.callbackHandler
= callbackHandler
;
51 this.sharedState
= (Map
<String
, Object
>) sharedState
;
55 public boolean login() throws LoginException
{
56 if (callbackHandler
== null)
58 HttpRequestCallback httpCallback
= new HttpRequestCallback();
60 callbackHandler
.handle(new Callback
[] { httpCallback
});
61 } catch (IOException e
) {
62 throw new LoginException("Cannot handle http callback: " + e
.getMessage());
63 } catch (UnsupportedCallbackException e
) {
66 request
= httpCallback
.getRequest();
69 authorization
= (Authorization
) request
.getAttribute(HttpContext
.AUTHORIZATION
);
70 if (authorization
== null) {// search by session ID
71 String httpSessionId
= request
.getSession().getId();
72 // authorization = (Authorization)
73 // request.getSession().getAttribute(HttpContext.AUTHORIZATION);
74 // if (authorization == null) {
75 Collection
<ServiceReference
<CmsSession
>> sr
;
77 sr
= bc
.getServiceReferences(CmsSession
.class,
78 "(" + CmsSession
.SESSION_LOCAL_ID
+ "=" + httpSessionId
+ ")");
79 } catch (InvalidSyntaxException e
) {
80 throw new CmsException("Cannot get CMS session for id " + httpSessionId
, e
);
83 CmsSession cmsSession
= bc
.getService(sr
.iterator().next());
84 authorization
= cmsSession
.getAuthorization();
85 if (log
.isTraceEnabled())
86 log
.trace("Retrieved authorization from " + cmsSession
);
87 } else if (sr
.size() == 0)
90 throw new CmsException(sr
.size() + ">1 web sessions detected for http session " + httpSessionId
);
93 sharedState
.put(CmsAuthUtils
.SHARED_STATE_HTTP_REQUEST
, request
);
94 extractHttpAuth(request
);
95 extractClientCertificate(request
);
96 if (authorization
== null)
98 sharedState
.put(CmsAuthUtils
.SHARED_STATE_AUTHORIZATION
, authorization
);
103 public boolean commit() throws LoginException
{
104 // TODO create CmsSession in another module
105 Authorization authorizationToRegister
;
106 if (authorization
== null) {
107 authorizationToRegister
= (Authorization
) sharedState
.get(CmsAuthUtils
.SHARED_STATE_AUTHORIZATION
);
108 } else { // this login module did the authorization
109 CmsAuthUtils
.addAuthentication(subject
, authorization
);
110 authorizationToRegister
= authorization
;
112 if (authorizationToRegister
== null) {
117 CmsAuthUtils
.registerSessionAuthorization(bc
, request
, subject
, authorizationToRegister
);
119 byte[] outToken
= (byte[]) sharedState
.get(CmsAuthUtils
.SHARED_STATE_SPNEGO_OUT_TOKEN
);
120 if (outToken
!= null) {
121 response
.setHeader(CmsAuthUtils
.HEADER_WWW_AUTHENTICATE
,
122 "Negotiate " + java
.util
.Base64
.getEncoder().encodeToString(outToken
));
125 if (authorization
!= null) {
126 // CmsAuthUtils.addAuthentication(subject, authorization);
136 public boolean abort() throws LoginException
{
141 private void cleanUp() {
142 authorization
= null;
147 public boolean logout() throws LoginException
{
148 return CmsAuthUtils
.logoutSession(bc
, subject
);
151 private void extractHttpAuth(final HttpServletRequest httpRequest
) {
152 String authHeader
= httpRequest
.getHeader(CmsAuthUtils
.HEADER_AUTHORIZATION
);
153 if (authHeader
!= null) {
154 StringTokenizer st
= new StringTokenizer(authHeader
);
155 if (st
.hasMoreTokens()) {
156 String basic
= st
.nextToken();
157 if (basic
.equalsIgnoreCase("Basic")) {
159 // TODO manipulate char[]
160 String credentials
= new String(Base64
.decodeBase64(st
.nextToken()), "UTF-8");
161 // log.debug("Credentials: " + credentials);
162 int p
= credentials
.indexOf(":");
164 final String login
= credentials
.substring(0, p
).trim();
165 final char[] password
= credentials
.substring(p
+ 1).trim().toCharArray();
166 sharedState
.put(CmsAuthUtils
.SHARED_STATE_NAME
, login
);
167 sharedState
.put(CmsAuthUtils
.SHARED_STATE_PWD
, password
);
169 throw new CmsException("Invalid authentication token");
171 } catch (Exception e
) {
172 throw new CmsException("Couldn't retrieve authentication", e
);
174 } else if (basic
.equalsIgnoreCase("Negotiate")) {
175 String spnegoToken
= st
.nextToken();
176 byte[] authToken
= Base64
.decodeBase64(spnegoToken
);
177 sharedState
.put(CmsAuthUtils
.SHARED_STATE_SPNEGO_TOKEN
, authToken
);
183 private X509Certificate
[] extractClientCertificate(HttpServletRequest req
) {
184 X509Certificate
[] certs
= (X509Certificate
[]) req
.getAttribute("javax.servlet.request.X509Certificate");
185 if (null != certs
&& certs
.length
> 0) {