]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/IdentLoginModule.java
Make tree view more robust
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / 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
13 import org.argeo.api.cms.CmsLog;
14 import org.argeo.cms.auth.ident.IdentClient;
15 import org.argeo.cms.internal.runtime.CmsStateImpl;
16
17 /** Use an ident service to identify. */
18 public class IdentLoginModule implements LoginModule {
19 private final static CmsLog log = CmsLog.getLog(IdentLoginModule.class);
20
21 private CallbackHandler callbackHandler = null;
22 private Map<String, Object> sharedState = null;
23
24 @SuppressWarnings("unchecked")
25 @Override
26 public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState,
27 Map<String, ?> options) {
28 this.callbackHandler = callbackHandler;
29 this.sharedState = (Map<String, Object>) sharedState;
30 }
31
32 @Override
33 public boolean login() throws LoginException {
34 if (callbackHandler == null)
35 return false;
36 RemoteAuthCallback httpCallback = new RemoteAuthCallback();
37 try {
38 callbackHandler.handle(new Callback[] { httpCallback });
39 } catch (IOException e) {
40 throw new LoginException("Cannot handle http callback: " + e.getMessage());
41 } catch (UnsupportedCallbackException e) {
42 return false;
43 }
44 RemoteAuthRequest request = httpCallback.getRequest();
45 if (request == null)
46 return false;
47 IdentClient identClient = CmsStateImpl.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 }