-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticator.java
More file actions
534 lines (442 loc) · 18.9 KB
/
Authenticator.java
File metadata and controls
534 lines (442 loc) · 18.9 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package javaxt.express;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import javaxt.http.servlet.HttpServletRequest;
import javaxt.http.servlet.HttpServletResponse;
import javaxt.http.servlet.ServletException;
//******************************************************************************
//** Authenticator
//******************************************************************************
/**
* This class is an implementation of a javaxt.http.servlet.Authenticator
* used to authenticate requests in a javaxt.http.servlet.HttpServlet.
* Instances of this class are passed to the setAuthenticator() method in
* the HttpServlet class. Once the HttpServlet has an Authenticator, it can
* be used to retrieve credentials, get user principle, etc. Example:
*
<pre>
public class WebApp extends HttpServlet {
public WebApp(){
setAuthenticator(new javaxt.express.Authenticator());
}
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[] credentials = getCredentials(); //Calls Authenticator!
String username = credentials[0];
String password = credentials[1];
}
}
</pre>
*
* The getCredentials() method provided by this class supports both "BASIC"
* and "NTLM" authentication methods.
*
* The following snippit demonstrates how to perform "BASIC" authentication
* with a username and password. This example assumes that there is a User
* class that implements the java.security.Principal interface. The getUser()
* and setUser() methods in this example are used to update an internal cache.
* The cache is used to map a username to a User and is primarily used to
* help reduce database queries. Entries in the cache are invalidated after
* 30 seconds.
*
<pre>
setAuthenticator(new javaxt.express.Authenticator(){
public java.security.Principal getPrinciple(){
User user = (User) getUser();
if (user!=null) return user;
try{
String[] credentials = getCredentials();
String username = credentials[0];
String password = credentials[1];
if (username!=null && password!=null){
//TODO: Find user in the database
}
}
catch(Exception e){
}
setUser(user);
return user;
}
});
</pre>
*
* With "NTLM" authentication, only the username is returned by the
* getCredentials() method. The password will be null. This is because the
* user has already been authenticated by another provider (e.g. AD). All
* that's left to do is to map the username to a User/Principal via the
* getPrinciple() method.
*
* Additional authentication methods can be added by overriding the
* getCredentials() method.
*
* This class also includes a handleRequest() method that can be used to
* simplify authentication workflows used in JavaXT web applications.
*
******************************************************************************/
public class Authenticator implements javaxt.http.servlet.Authenticator, Cloneable {
//Local variables
private String authType;
private String authInfo;
private String[] credentials;
private HttpServletRequest request;
//Global variables
private static final ConcurrentHashMap<String, Object[]> cache = new ConcurrentHashMap<>();
private static final long cacheExpiration = 30000; //30 seconds
//**************************************************************************
//** newInstance
//**************************************************************************
/** Creates a new instance of this class. This method is called by the
* HttpServletRequest class every time an HTTP request is made to the
* server.
*/
public Authenticator newInstance(HttpServletRequest request){
try{
//Create new Authenticator
Object obj = this.clone();
//Get Authenticator class
Class c = this.getClass();
java.lang.reflect.Field field;
try{
c.getDeclaredField("request");
}
catch(NoSuchFieldException e){
c = c.getSuperclass();
}
//Update private fields
field = c.getDeclaredField("request");
field.setAccessible(true);
field.set(obj, request);
return (Authenticator) obj;
}
catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
//**************************************************************************
//** authenticate
//**************************************************************************
/** Used to authenticate an HTTP request. If the getPrinciple() method fails
* to return a user, this method throws a ServletException. Override as
* needed.
*/
public void authenticate() throws ServletException {
java.security.Principal user = getPrinciple();
if (user==null) throw new ServletException();
}
//**************************************************************************
//** getPrinciple
//**************************************************************************
/** Returns the java.security.Principal associated with an HTTP request.
* This method should be overridden as shown in the example above.
* Otherwise, this method will return null and any calls to the
* authenticate() method will throw a ServletException.
*/
public java.security.Principal getPrinciple(){
return null;
}
//**************************************************************************
//** getCredentials
//**************************************************************************
/** Returns an array representing user credentials associated with an HTTP
* request. In the case of "BASIC" authentication, the credentials contain
* the username and password. In the case of "NTLM" authentication, the
* credentials only contain a username. Override as needed.
*/
public String[] getCredentials() {
if (credentials==null){
String authType = getAuthType();
if (authType==null) authType = "";
if (authType.equals("BASIC")){
//Decode the string
String auth = new String(
javaxt.utils.Base64.decode(authInfo)
);
//Parse credentials
String username = auth.substring(0, auth.indexOf(":"));
String password = auth.substring(auth.indexOf(":")+1);
credentials = new String[]{username, password};
}
else if (authType.equals("NTLM")){
byte[] msg = javaxt.utils.Base64.decode(authInfo);
int off = 0, length, offset;
if (msg[8] == 3) {
off = 30;
length = msg[off+17]*256 + msg[off+16];
offset = msg[off+19]*256 + msg[off+8];
//String computerName = new String(msg, offset, length);
//System.out.println("computerName: " + computerName);
//Get domain name
length = msg[off+1]*256 + msg[off];
offset = msg[off+3]*256 + msg[off+2];
String str = new String(msg, offset, length);
StringBuilder domainName = new StringBuilder();
for (int i=0; i<str.length(); i++){
int c = str.charAt(i);
if (c!=0) domainName.append((char) c);
}
if (domainName.length()==0) domainName = null;
//Get username
length = msg[off+9]*256 + msg[off+8];
offset = msg[off+11]*256 + msg[off+10];
str = new String(msg, offset, length);
StringBuilder username = new StringBuilder();
for (int i=0; i<str.length(); i++){
int c = str.charAt(i);
if (c!=0) username.append((char) c);
}
if (username.length()==0) username = null;
if (domainName!=null && username!=null){
credentials = new String[]{username.toString(), null};
}
}
}
}
return credentials;
}
//**************************************************************************
//** getAuthType
//**************************************************************************
/** Returns the authentication scheme used to authenticate clients (e.g.
* "BASIC", "NTLM", etc). By default, this class parses the "Authorization"
* HTTP request header to determine the authentication scheme. Override as
* needed.
*/
public String getAuthType(){
if (authType==null){
String authorization = request.getHeader("Authorization");
if (authorization!=null){
int idx = authorization.indexOf(" ");
authType = authorization.substring(0, idx).toUpperCase();
authInfo = authorization.substring(idx+1);
}
}
return authType;
}
//**************************************************************************
//** isUserInRole
//**************************************************************************
/** This method is a legacy feature from the Java Servlet API. Returns
* false. Override as needed.
*/
public boolean isUserInRole(String role){
return false;
}
//**************************************************************************
//** getUsername
//**************************************************************************
/** Returns the first entry in the array returned by getCredentials().
* Override as needed.
*/
protected String getUsername(){
String[] credentials = getCredentials();
return (credentials!=null) ? credentials[0] : null;
}
//**************************************************************************
//** getUser
//**************************************************************************
/** Returns a User from the cache. The username returned from getUsername()
* is used to find the User in the cache. Override as needed.
*/
protected User getUser(){
User user = null;
String username = getUsername();
if (username!=null){
//Check if the credentials correspond to a logout request. See the
//handleRequest() method for more information.
//if (username.equals("logout") && password.equals("logout")) return;
synchronized(cache){
Object[] arr = cache.get(username);
if (arr!=null){
long lastUpdate = (long) arr[1];
if (System.currentTimeMillis()-lastUpdate<cacheExpiration){
user = (User) arr[0];
}
else{
cache.remove(username);
cache.notifyAll();
}
}
}
}
return user;
}
//**************************************************************************
//** setUser
//**************************************************************************
/** Used to add a User to the cache. The username returned from getUsername()
* is used as the key in the cache. Override as needed.
*/
protected void setUser(User user){
if (user!=null){
String username = getUsername();
if (username!=null){
synchronized(cache){
cache.put(username, new Object[]{user, System.currentTimeMillis()});
cache.notifyAll();
}
}
}
}
//**************************************************************************
//** handleRequest
//**************************************************************************
/** Used to process an authentication workflow. Returns true if a response
* was generated for the client. Example usage:
<pre>
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Authenticator authenticator = (Authenticator) getAuthenticator(request);
if (authenticator.handleRequest(service, response)) return;
//If we're still here, generate a response!
}
</pre>
*
* @param service Directive, typically extracted from the first path segment
* after the servlet path in the request URL. Options include "login",
* "logoff" (or "logout"), and "whoami".
* @param response An HttpServletResponse object to be updated.
* @return Returns true if this method was able to process the directive and
* update the HttpServletResponse. Nothing else should be written to the
* response. If false is returned, continue processing the request and
* generate your own response.
*/
public boolean handleRequest(String service, HttpServletResponse response)
throws ServletException, IOException {
//Send NTLM response as needed
boolean ntlm = (authType!=null && authType.equals("NTLM"));
if (ntlm){
String ua = request.getHeader("user-agent");
if (ua!=null){
if (ua.contains("MSIE ") || ua.contains("Trident/") || ua.contains("Edge/") || ua.contains("Edg/")){
if (sendNTLMResponse(request, response)) return true;
}
else{
ntlm = false;
}
}
}
boolean requestHandled = true;
if (service.equals("login")){
String username = getUsername();
if (username==null){
if (ntlm){
response.setStatus(401, "Access Denied");
response.setHeader("WWW-Authenticate", "NTLM");
}
else{
response.setStatus(401, "Access Denied");
response.setHeader("WWW-Authenticate", "Basic realm=\"Access Denied\""); //<--Prompt the user for thier credentials
response.setHeader("Cache-Control", "no-cache, no-transform");
response.setContentType("text/plain");
response.write("Unauthorized");
}
}
else{
try{
request.authenticate();
User user = getUser();
response.setContentType("text/plain");
response.write(user.getID()+"");
}
catch(Exception e){
response.setStatus(403, "Not Authorized");
response.setHeader("Cache-Control", "no-cache, no-transform");
response.setContentType("text/plain");
response.write("Unauthorized");
}
}
}
else if (service.equals("logoff") || service.equalsIgnoreCase("logout")){
String username = getUsername();
if (username!=null){
synchronized(cache){
cache.remove(username);
cache.notifyAll();
}
}
if (ntlm){
response.setStatus(401, "Access Denied");
response.setHeader("WWW-Authenticate", "NTLM");
}
else{
response.setStatus(401, "Access Denied");
Boolean prompt = new javaxt.utils.Value(request.getParameter("prompt")).toBoolean(); //<--Hack for Firefox
if (prompt!=null && prompt==true){
response.setHeader("WWW-Authenticate", "Basic realm=\"" +
"This site is restricted. Please enter your username and password.\"");
}
response.setHeader("Cache-Control", "no-cache, no-transform");
response.setContentType("text/plain");
response.write("Unauthorized");
}
}
else if (service.equals("whoami")){
User user = getUser();
if (user==null){
//If the request has credentials, try authenticating the user
String username = getUsername();
if (!(username==null || username.equals("logout"))){
try{
request.authenticate();
user = getUser();
}
catch(Exception e){}
}
}
if (user==null){
response.setStatus(400, "Bad Request");
response.write("");
}
else{
response.setHeader("Cache-Control", "no-cache, no-transform");
response.setContentType("text/plain");
response.write(user.getID()+"");
}
}
else{
requestHandled = false;
}
return requestHandled;
}
//**************************************************************************
//** sendNTLMResponse
//**************************************************************************
/** Returns true if an NTLM response was returned to the client
*/
public static boolean sendNTLMResponse(HttpServletRequest request, HttpServletResponse response){
String authorization = request.getHeader("Authorization");
if (authorization==null){
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM");
response.setContentLength(0);
return true;
}
else{
byte[] msg = javaxt.utils.Base64.decode(authorization.substring(5));
if (msg[8] == 1) {
//Send NTLM type2 response
response.setStatus(response.SC_UNAUTHORIZED);
response.setHeader("WWW-Authenticate", "NTLM " + NTLM_TYPE_2);
response.setContentLength(0);
return true;
}
}
return false;
}
private static String NTLM_TYPE_2;
static{
byte z = 0;
byte[] msg1 = {
(byte) 'N', (byte) 'T', (byte) 'L', (byte) 'M', //ntlm
(byte) 'S', (byte) 'S', (byte) 'P', //ssp
z, (byte) 2, //type 2
z, z, z, z, z, z, z, (byte) 40, z, z, z,
(byte) 1, (byte) 130,
(byte) 8, //super important!
z, z, (byte) 2, (byte) 2,
(byte) 2, z, z, z, z, z, z, z, z, z, z, z, z
};
NTLM_TYPE_2 = javaxt.utils.Base64.encode(msg1).trim();
}
}