1 package org
.argeo
.security
.equinox
;
5 import javax
.security
.auth
.Subject
;
6 import javax
.security
.auth
.callback
.Callback
;
7 import javax
.security
.auth
.callback
.CallbackHandler
;
8 import javax
.security
.auth
.callback
.NameCallback
;
9 import javax
.security
.auth
.callback
.PasswordCallback
;
10 import javax
.security
.auth
.login
.LoginException
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.security
.SiteAuthenticationToken
;
15 import org
.springframework
.security
.Authentication
;
16 import org
.springframework
.security
.AuthenticationManager
;
17 import org
.springframework
.security
.BadCredentialsException
;
18 import org
.springframework
.security
.context
.SecurityContextHolder
;
19 import org
.springframework
.security
.providers
.jaas
.SecurityContextLoginModule
;
21 /** Login module which caches one subject per thread. */
22 public class SpringLoginModule
extends SecurityContextLoginModule
{
23 final static String NODE_REPO_URI
= "argeo.node.repo.uri";
25 private final static Log log
= LogFactory
.getLog(SpringLoginModule
.class);
27 private AuthenticationManager authenticationManager
;
29 private CallbackHandler callbackHandler
;
31 private Subject subject
;
33 private Long waitBetweenFailedLoginAttempts
= 5 * 1000l;
35 private Boolean remote
= false;
37 public SpringLoginModule() {
41 @SuppressWarnings("rawtypes")
42 public void initialize(Subject subject
, CallbackHandler callbackHandler
,
43 Map sharedState
, Map options
) {
44 super.initialize(subject
, callbackHandler
, sharedState
, options
);
45 this.callbackHandler
= callbackHandler
;
46 this.subject
= subject
;
49 public boolean login() throws LoginException
{
51 // thread already logged in
52 if (SecurityContextHolder
.getContext().getAuthentication() != null)
55 // reset all principals and credentials
56 if (log
.isTraceEnabled())
57 log
.trace("Resetting all principals and credentials of "
59 if (subject
.getPrincipals() != null)
60 subject
.getPrincipals().clear();
61 if (subject
.getPrivateCredentials() != null)
62 subject
.getPrivateCredentials().clear();
63 if (subject
.getPublicCredentials() != null)
64 subject
.getPublicCredentials().clear();
66 // ask for username and password
67 NameCallback nameCallback
= new NameCallback("User");
68 PasswordCallback passwordCallback
= new PasswordCallback(
71 NameCallback urlCallback
= new NameCallback("Site URL");
73 if (callbackHandler
== null)
74 throw new LoginException("No call back handler available");
76 callbackHandler
.handle(new Callback
[] { nameCallback
,
77 passwordCallback
, urlCallback
});
79 callbackHandler
.handle(new Callback
[] { nameCallback
,
82 // Set user name and password
83 String username
= nameCallback
.getName();
84 if (username
== null || username
.trim().equals(""))
88 if (passwordCallback
.getPassword() != null)
89 password
= String
.valueOf(passwordCallback
.getPassword());
91 String url
= remote ? urlCallback
.getName() : null;
92 if (remote
&& (url
== null || url
.trim().equals("")))
93 // for convenience, may be removed in the future
94 url
= System
.getProperty(NODE_REPO_URI
);
96 // TODO: set it via system properties
97 String workspace
= null;
99 SiteAuthenticationToken credentials
= new SiteAuthenticationToken(
100 username
, password
, url
, workspace
);
102 Authentication authentication
;
104 authentication
= authenticationManager
105 .authenticate(credentials
);
106 } catch (BadCredentialsException e
) {
107 // wait between failed login attempts
108 Thread
.sleep(waitBetweenFailedLoginAttempts
);
111 registerAuthentication(authentication
);
112 boolean res
= super.login();
114 } catch (LoginException e
) {
116 } catch (ThreadDeath e
) {
117 LoginException le
= new LoginException(
118 "Spring Security login thread died");
121 } catch (Exception e
) {
122 LoginException le
= new LoginException(
123 "Spring Security login failed");
130 public boolean logout() throws LoginException
{
131 subject
.getPrincipals().clear();
132 return super.logout();
136 * Register an {@link Authentication} in the security context.
138 * @param authentication
139 * has to implement {@link Authentication}.
141 protected void registerAuthentication(Object authentication
) {
142 SecurityContextHolder
.getContext().setAuthentication(
143 (Authentication
) authentication
);
146 public void setAuthenticationManager(
147 AuthenticationManager authenticationManager
) {
148 this.authenticationManager
= authenticationManager
;
151 public void setRemote(Boolean remote
) {
152 this.remote
= remote
;