]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/auth/IdentLoginModule.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / auth / IdentLoginModule.java
1 package org.argeo.cms.auth;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 import javax.security.auth.Subject;
7 import javax.security.auth.callback.Callback;
8 import javax.security.auth.callback.CallbackHandler;
9 import javax.security.auth.callback.UnsupportedCallbackException;
10 import javax.security.auth.login.LoginException;
11 import javax.security.auth.spi.LoginModule;
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.cms.internal.kernel.Activator;
17 import org.argeo.ident.IdentClient;
18
19 public class IdentLoginModule implements LoginModule {
20 private final static Log log = LogFactory.getLog(IdentLoginModule.class);
21
22 private Subject subject = null;
23 private CallbackHandler callbackHandler = null;
24 private Map<String, Object> sharedState = null;
25
26 @Override
27 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
28 Map<String, ?> options) {
29 this.subject = subject;
30 this.callbackHandler = callbackHandler;
31 this.sharedState = (Map<String, Object>) sharedState;
32 }
33
34 @Override
35 public boolean login() throws LoginException {
36 if (callbackHandler == null)
37 return false;
38 HttpRequestCallback httpCallback = new HttpRequestCallback();
39 try {
40 callbackHandler.handle(new Callback[] { httpCallback });
41 } catch (IOException e) {
42 throw new LoginException("Cannot handle http callback: " + e.getMessage());
43 } catch (UnsupportedCallbackException e) {
44 return false;
45 }
46 HttpServletRequest request = httpCallback.getRequest();
47 IdentClient identClient = Activator.getIdentClient(request.getRemoteAddr());
48 if (identClient == null)
49 return false;
50 String identUsername;
51 try {
52 identUsername = identClient.getUsername(request.getLocalPort(), request.getRemotePort());
53 } catch (Exception e) {
54 e.printStackTrace();
55 return false;
56 }
57 if (identUsername != null) {
58 if (log.isDebugEnabled())
59 log.debug("Ident username: " + identUsername + " (local port: " + request.getLocalPort()
60 + ", remote port: " + request.getRemotePort() + ")");
61 sharedState.put(CmsAuthUtils.SHARED_STATE_NAME, identUsername);
62 sharedState.put(CmsAuthUtils.SHARED_STATE_REMOTE_ADDR, request.getRemoteAddr());
63 sharedState.put(CmsAuthUtils.SHARED_STATE_REMOTE_PORT, request.getRemotePort());
64 return true;
65 } else {
66 return false;
67 }
68 }
69
70 @Override
71 public boolean commit() throws LoginException {
72 return true;
73 }
74
75 @Override
76 public boolean abort() throws LoginException {
77 return true;
78 }
79
80 @Override
81 public boolean logout() throws LoginException {
82 return true;
83 }
84
85 }