]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/eclipse/plugins/org.argeo.security.equinox/src/main/java/org/argeo/security/equinox/SpringLoginModule.java
Remove generated target platform
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.equinox / src / main / java / org / argeo / security / equinox / SpringLoginModule.java
1 package org.argeo.security.equinox;
2
3 import java.util.Map;
4
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.callback.TextOutputCallback;
11 import javax.security.auth.login.LoginException;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.springframework.security.Authentication;
16 import org.springframework.security.AuthenticationManager;
17 import org.springframework.security.context.SecurityContextHolder;
18 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
19 import org.springframework.security.providers.jaas.SecurityContextLoginModule;
20
21 public class SpringLoginModule extends SecurityContextLoginModule {
22 private final static Log log = LogFactory.getLog(SpringLoginModule.class);
23
24 private AuthenticationManager authenticationManager;
25 private Subject subject;
26
27 private CallbackHandler callbackHandler;
28
29 public SpringLoginModule() {
30
31 }
32
33 @SuppressWarnings("rawtypes")
34 public void initialize(Subject subject, CallbackHandler callbackHandler,
35 Map sharedState, Map options) {
36 super.initialize(subject, callbackHandler, sharedState, options);
37 this.subject = subject;
38 this.callbackHandler = callbackHandler;
39 }
40
41 public boolean login() throws LoginException {
42 // thread already logged in
43 if (SecurityContextHolder.getContext().getAuthentication() != null)
44 return super.login();
45
46 if (subject.getPrincipals(Authentication.class).size() == 1) {
47 registerAuthentication(subject.getPrincipals(Authentication.class)
48 .iterator().next());
49 return super.login();
50 } else if (subject.getPrincipals(Authentication.class).size() > 1) {
51 throw new LoginException(
52 "Multiple Authentication principals not supported: "
53 + subject.getPrincipals(Authentication.class));
54 } else {
55 // ask for username and password
56 Callback label = new TextOutputCallback(
57 TextOutputCallback.INFORMATION, "Required login");
58 NameCallback nameCallback = new NameCallback("User");
59 PasswordCallback passwordCallback = new PasswordCallback(
60 "Password", false);
61
62 if (callbackHandler == null) {
63 throw new LoginException("No call back handler available");
64 // return false;
65 }
66 try {
67 callbackHandler.handle(new Callback[] { label, nameCallback,
68 passwordCallback });
69 } catch (Exception e) {
70 LoginException le = new LoginException(
71 "Callback handling failed");
72 le.initCause(e);
73 throw le;
74 }
75
76 // Set user name and password
77 String username = nameCallback.getName();
78 String password = "";
79 if (passwordCallback.getPassword() != null) {
80 password = String.valueOf(passwordCallback.getPassword());
81 }
82 UsernamePasswordAuthenticationToken credentials = new UsernamePasswordAuthenticationToken(
83 username, password);
84
85 try {
86 Authentication authentication = authenticationManager
87 .authenticate(credentials);
88 registerAuthentication(authentication);
89 return super.login();
90 } catch (Exception e) {
91 LoginException loginException = new LoginException(
92 "Bad credentials");
93 loginException.initCause(e);
94 throw loginException;
95 }
96 }
97 }
98
99 @Override
100 public boolean logout() throws LoginException {
101 if (log.isDebugEnabled())
102 log.debug("Log out "
103 + subject.getPrincipals().iterator().next().getName());
104 return super.logout();
105 }
106
107 /**
108 * Register an {@link Authentication} in the security context.
109 *
110 * @param authentication
111 * has to implement {@link Authentication}.
112 */
113 protected void registerAuthentication(Object authentication) {
114 SecurityContextHolder.getContext().setAuthentication(
115 (Authentication) authentication);
116 }
117
118 public void setAuthenticationManager(
119 AuthenticationManager authenticationManager) {
120 this.authenticationManager = authenticationManager;
121 }
122
123 }