1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 package net.sf.jguard.core.authentication;
30
31 import net.sf.jguard.core.CoreConstants;
32 import net.sf.jguard.core.authentication.bindings.*;
33 import net.sf.jguard.core.authentication.callbacks.AuthenticationSchemeHandlerCallback;
34 import net.sf.jguard.core.authentication.callbacks.InetAddressCallback;
35 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
36 import net.sf.jguard.core.authentication.schemes.AuthenticationSchemeHandler;
37 import net.sf.jguard.core.authentication.schemes.HookFormSchemeHandler;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import javax.security.auth.Subject;
42 import javax.security.auth.callback.*;
43 import javax.security.auth.login.LoginException;
44 import java.security.Permission;
45 import java.util.*;
46
47
48
49
50
51
52
53
54 public class AuthenticationServicePoint {
55 private static final String PASSWORD = "password";
56 private static final String LOGIN = "login";
57
58
59 private static final Logger logger = LoggerFactory.getLogger(AuthenticationServicePoint.class.getName());
60
61
62
63
64
65
66
67
68
69 public static boolean answerToChallenge(AccessContext context, AuthenticationBindingsFactory factory) {
70 List<AuthenticationSchemeHandler> authSchemeHandlers = factory.getAuthenticationSchemeHandlers();
71 for (AuthenticationSchemeHandler handler : authSchemeHandlers) {
72 boolean answerToChallenge = handler.answerToChallenge(context);
73 if (answerToChallenge) {
74 return true;
75 }
76 }
77
78 return false;
79 }
80
81
82
83
84
85
86
87 public static AuthenticationUtils authenticate(AuthenticationBindings authenticationBindings, String applicationName, String authenticationScope) throws AuthenticationException {
88 authenticationBindings.setRequestAttribute(CoreConstants.REGISTRATION_DONE, Boolean.FALSE);
89
90
91 CallbackHandler cbh = authenticationBindings.getCallbackHandler();
92 AuthenticationUtils authNUtils = null;
93 try {
94
95 authNUtils = authenticationBindings.getAuthenticationUtils();
96
97
98
99 authNUtils.login(applicationName, cbh);
100
101
102
103
104
105 if (authenticationBindings instanceof StatefulAuthenticationBindings) {
106 Permission lastAccessDeniedPermission = null;
107
108
109 ((StatefulAuthenticationBindings) authenticationBindings).removeSessionAttribute(CoreConstants.AUTHN_UTILS);
110 lastAccessDeniedPermission = (Permission) ((StatefulAuthenticationBindings) authenticationBindings).getSessionAttribute(CoreConstants.LAST_ACCESS_DENIED_PERMISSION);
111 ((StatefulAuthenticationBindings) authenticationBindings).invalidateSession();
112
113 ((StatefulAuthenticationBindings) authenticationBindings).setSessionAttribute(CoreConstants.AUTHN_UTILS, authNUtils);
114 ((StatefulAuthenticationBindings) authenticationBindings).setSessionAttribute(CoreConstants.LAST_ACCESS_DENIED_PERMISSION, lastAccessDeniedPermission);
115
116 }
117 AuthenticationSchemeHandler authSchemeHandler = getAuthenticationSchemeHandler(authNUtils.getSubject(), authenticationBindings.getAuthenticationBindingsFactory());
118 authSchemeHandler.authenticationSucceed(authenticationBindings.getContext());
119 authNUtils.setStatus(AuthenticationStatus.SUCCESS);
120 return authNUtils;
121
122 } catch (AuthenticationContinueException ace) {
123
124 logger.debug("authentication is not yet complete. a new exchange between client and server is required " + ace.getMessage());
125 authNUtils.setStatus(AuthenticationStatus.CONTINUE);
126 return authNUtils;
127 } catch (AuthenticationChallengeException ace) {
128
129 logger.debug("authentication challenge built. a new exchange between client and server is required " + ace.getMessage());
130 authNUtils.setStatus(AuthenticationStatus.FAILURE);
131 return authNUtils;
132
133 } catch (LoginException e) {
134
135 logger.debug("authentication failed " + e.getMessage(), e);
136 String messageError = null;
137 messageError = e.getLocalizedMessage();
138
139 authenticationBindings.setRequestAttribute(CoreConstants.LOGIN_EXCEPTION_MESSAGE, messageError);
140 authenticationBindings.setRequestAttribute(CoreConstants.LOGIN_EXCEPTION_CLASS, e.getClass());
141 AuthenticationSchemeHandler authSchemeHandler = (AuthenticationSchemeHandler) authenticationBindings.getRequestAttribute(CoreConstants.AUTHENTICATION_SCHEME_HANDLER);
142 authSchemeHandler.authenticationFailed(authenticationBindings.getContext());
143 authNUtils.setStatus(AuthenticationStatus.FAILURE);
144 return authNUtils;
145
146 }
147 }
148
149
150
151
152
153
154
155
156 public static AuthenticationSchemeHandler getAuthenticationSchemeHandler(Subject subject, AuthenticationBindingsFactory authNBindingsFactory) {
157 String authSchemeHandlerName = getAuthSchemeHandlerName(subject);
158 if (authSchemeHandlerName == null) {
159 throw new IllegalArgumentException(" Subject does not contains a JGuardCredential with a key='authSchemeHandlerName' and a value not null ");
160 }
161 List<AuthenticationSchemeHandler> authSchemeHandlers = authNBindingsFactory.getAuthenticationSchemeHandlers();
162 AuthenticationSchemeHandler authSchemeHandler = null;
163 for (AuthenticationSchemeHandler authHandler : authSchemeHandlers) {
164 if (authSchemeHandlerName.equals(authHandler.getName())) {
165 authSchemeHandler = authHandler;
166 break;
167 }
168 }
169 return authSchemeHandler;
170 }
171
172
173
174
175
176
177
178 private static String getAuthSchemeHandlerName(Subject subject) {
179 Set<JGuardCredential> credentials = subject.getPublicCredentials(JGuardCredential.class);
180 for (JGuardCredential cred : credentials) {
181 if (CoreConstants.AUTHENTICATION_SCHEME_HANDLER_NAME.equals(cred.getName())) {
182 return (String) cred.getValue();
183 }
184 }
185 return null;
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public static AuthenticationUtils authenticateWithImpersonation(AuthenticationBindings authenticationBindings, List<AuthenticationSchemeHandler> authHandlers, String applicationName, List<Callback> callbacks, String authenticationScope) throws AuthenticationException {
202 AuthenticationSchemeHandlerCallback cb = new AuthenticationSchemeHandlerCallback();
203 cb.setAuthenticationSchemeHandlerName("HOOK");
204 callbacks.add(cb);
205 ImpersonationAuthenticationBindings impersonatedAuthenticationBindings = null;
206 if (authenticationBindings instanceof StatefulAuthenticationBindings) {
207 impersonatedAuthenticationBindings = new StatefulImpersonationAuthenticationBindings(authenticationBindings, callbacks);
208 } else {
209 impersonatedAuthenticationBindings = new ImpersonationAuthenticationBindings(authenticationBindings, callbacks);
210 }
211 impersonatedAuthenticationBindings.addAuthenticationSchemeHandlerToFactory(authHandlers);
212 return AuthenticationServicePoint.authenticate(impersonatedAuthenticationBindings, applicationName, authenticationScope);
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 public static AuthenticationUtils impersonateAsGuest(AuthenticationBindings authenticationBindings, String applicationName, String authenticationScope) throws AuthenticationException {
229
230 List<Callback> callbacks = new ArrayList<Callback>(4);
231 NameCallback nameCallback = new NameCallback(LOGIN);
232 nameCallback.setName(CoreConstants.GUEST);
233 callbacks.add(nameCallback);
234
235 PasswordCallback pwdCbk = new PasswordCallback(PASSWORD, false);
236 pwdCbk.setPassword(CoreConstants.GUEST.toCharArray());
237 callbacks.add(pwdCbk);
238
239 InetAddressCallback address = new InetAddressCallback();
240 final String localIP = "127.0.0.1";
241 address.setHostAdress(localIP);
242 address.setHostName("localhost");
243 callbacks.add(address);
244
245
246 LanguageCallback languageCallback = new LanguageCallback();
247 languageCallback.setLocale(Locale.getDefault());
248 callbacks.add(languageCallback);
249
250
251 List<AuthenticationSchemeHandler> authenticationSchemeHandlers = new ArrayList<AuthenticationSchemeHandler>(1);
252 Map<String, String> parameters = new HashMap<String, String>(2);
253 authenticationSchemeHandlers.add(new HookFormSchemeHandler(parameters, authenticationBindings.getAuthenticationBindingsFactory()));
254 return authenticateWithImpersonation(authenticationBindings, authenticationSchemeHandlers, applicationName, callbacks, authenticationScope);
255 }
256
257
258 }