]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/HttpSessionLoginModule.java
Fix automated Kerberos config
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / HttpSessionLoginModule.java
1 package org.argeo.cms.auth;
2
3 import java.io.IOException;
4 import java.security.cert.X509Certificate;
5 import java.util.Collection;
6 import java.util.Map;
7 import java.util.StringTokenizer;
8
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;
17
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;
28
29 public class HttpSessionLoginModule implements LoginModule {
30 private final static Log log = LogFactory.getLog(HttpSessionLoginModule.class);
31
32 private Subject subject = null;
33 private CallbackHandler callbackHandler = null;
34 private Map<String, Object> sharedState = null;
35
36 private HttpServletRequest request = null;
37 private HttpServletResponse response = null;
38
39 private BundleContext bc;
40
41 private Authorization authorization;
42
43 @SuppressWarnings("unchecked")
44 @Override
45 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
46 Map<String, ?> options) {
47 bc = FrameworkUtil.getBundle(HttpSessionLoginModule.class).getBundleContext();
48 assert bc != null;
49 this.subject = subject;
50 this.callbackHandler = callbackHandler;
51 this.sharedState = (Map<String, Object>) sharedState;
52 }
53
54 @Override
55 public boolean login() throws LoginException {
56 if (callbackHandler == null)
57 return false;
58 HttpRequestCallback httpCallback = new HttpRequestCallback();
59 try {
60 callbackHandler.handle(new Callback[] { httpCallback });
61 } catch (IOException e) {
62 throw new LoginException("Cannot handle http callback: " + e.getMessage());
63 } catch (UnsupportedCallbackException e) {
64 return false;
65 }
66 request = httpCallback.getRequest();
67 if (request == null)
68 return false;
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;
76 try {
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);
81 }
82 if (sr.size() == 1) {
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)
88 authorization = null;
89 else
90 throw new CmsException(sr.size() + ">1 web sessions detected for http session " + httpSessionId);
91
92 }
93 sharedState.put(CmsAuthUtils.SHARED_STATE_HTTP_REQUEST, request);
94 extractHttpAuth(request);
95 extractClientCertificate(request);
96 if (authorization == null)
97 return false;
98 sharedState.put(CmsAuthUtils.SHARED_STATE_AUTHORIZATION, authorization);
99 return true;
100 }
101
102 @Override
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;
111 }
112 if (authorizationToRegister == null) {
113 return false;
114 }
115 if (request == null)
116 return false;
117 CmsAuthUtils.registerSessionAuthorization(bc, request, subject, authorizationToRegister);
118
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));
123 }
124
125 if (authorization != null) {
126 // CmsAuthUtils.addAuthentication(subject, authorization);
127 cleanUp();
128 return true;
129 } else {
130 cleanUp();
131 return false;
132 }
133 }
134
135 @Override
136 public boolean abort() throws LoginException {
137 cleanUp();
138 return false;
139 }
140
141 private void cleanUp() {
142 authorization = null;
143 request = null;
144 }
145
146 @Override
147 public boolean logout() throws LoginException {
148 return CmsAuthUtils.logoutSession(bc, subject);
149 }
150
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")) {
158 try {
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(":");
163 if (p != -1) {
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);
168 } else {
169 throw new CmsException("Invalid authentication token");
170 }
171 } catch (Exception e) {
172 throw new CmsException("Couldn't retrieve authentication", e);
173 }
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);
178 }
179 }
180 }
181 }
182
183 private X509Certificate[] extractClientCertificate(HttpServletRequest req) {
184 X509Certificate[] certs = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
185 if (null != certs && certs.length > 0) {
186 return certs;
187 }
188 return null;
189 }
190
191 }