-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiAuthenticator.java
More file actions
529 lines (476 loc) · 18.9 KB
/
MultiAuthenticator.java
File metadata and controls
529 lines (476 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
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package net.i2p.jetty;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.BitSet;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.security.ServerAuthException;
import org.eclipse.jetty.security.UserAuthentication;
import org.eclipse.jetty.security.authentication.DeferredAuthentication;
import org.eclipse.jetty.security.authentication.LoginAuthenticator;
import org.eclipse.jetty.server.Authentication;
import org.eclipse.jetty.server.Authentication.User;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.UserIdentity;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.QuotedStringTokenizer;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
/**
* We'd like to extend DigestAuthenticator but the
* Digest inner class is private, so this is a wholesale copy
* to add multi-scheme support, and some other minor changes.
*
* This supports both Digest and Basic schemes,
* and both SHA-256 and MD5 Digest algorithms,
* all in one.
* Ref: RFC 7616, RFC 2617.
*
* Jetty does not support multiple auth at once
* https://github.com/jetty/jetty.project/issues/5442
* but it's coming for Jetty 12.1.0
* https://github.com/jetty/jetty.project/pull/12393
* However, it's about different auth on different resources?
* Not multiple auth for the same resource.
*
* Any scheme must be compatible with what browsers support, see
* RFC 7616. We adapt Jetty DigestAuthenticator to support
* SHA-256 and Basic while still supporting MD5.
* Firefox supports SHA-256 as of FF93 (2021)
* Chrome supports it as of Chrome 117 (2023)
* See chart at bottom of https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/WWW-Authenticate
* But Jetty still claiming there's no support in 2024
* and refuses to implement it: https://github.com/jetty/jetty.project/issues/11489
* SHA256 is still NOT supported by Safari / IOS.
*
* See also SHA256Credential and MultiCredential.
*
* @since 0.9.67
*/
public class MultiAuthenticator extends LoginAuthenticator
{
private static final Logger LOG = Log.getLogger(MultiAuthenticator.class);
private final SecureRandom _random = new SecureRandom();
private long _maxNonceAgeMs = 60 * 1000;
private int _maxNC = 1024;
private final ConcurrentMap<String, Nonce> _nonceMap = new ConcurrentHashMap<>();
private final Queue<Nonce> _nonceQueue = new ConcurrentLinkedQueue<>();
private final boolean _enableSHA256, _enableMD5, _enableBasic;
public MultiAuthenticator() {
this(true, true, true);
}
public MultiAuthenticator(boolean enableSHA256, boolean enableMD5, boolean enableBasic)
{
_enableSHA256 = enableSHA256;
_enableMD5 = enableMD5;
_enableBasic = enableBasic;
}
@Override
public void setConfiguration(AuthConfiguration configuration)
{
super.setConfiguration(configuration);
String mna = configuration.getInitParameter("maxNonceAge");
if (mna != null)
setMaxNonceAge(Long.valueOf(mna));
String mnc = configuration.getInitParameter("maxNonceCount");
if (mnc != null)
setMaxNonceCount(Integer.valueOf(mnc));
}
public int getMaxNonceCount()
{
return _maxNC;
}
public void setMaxNonceCount(int maxNC)
{
_maxNC = maxNC;
}
public long getMaxNonceAge()
{
return _maxNonceAgeMs;
}
public void setMaxNonceAge(long maxNonceAgeInMillis)
{
_maxNonceAgeMs = maxNonceAgeInMillis;
}
@Override
public String getAuthMethod()
{
return Constraint.__DIGEST_AUTH;
}
@Override
public boolean secureResponse(ServletRequest req, ServletResponse res, boolean mandatory, User validatedUser) throws ServerAuthException
{
return true;
}
@Override
public Authentication validateRequest(ServletRequest req, ServletResponse res, boolean mandatory) throws ServerAuthException
{
if (!mandatory)
return new DeferredAuthentication(this);
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
String credentials = request.getHeader(HttpHeader.AUTHORIZATION.asString());
try
{
boolean stale = false;
if (credentials != null)
{
if (LOG.isDebugEnabled())
LOG.debug("Credentials: " + credentials);
QuotedStringTokenizer tokenizer = new QuotedStringTokenizer(credentials, "=, ", true, false);
Digest digest = null;
String last = null;
String name = null;
String scheme = null;
String credential = null;
while (tokenizer.hasMoreTokens())
{
String tok = tokenizer.nextToken();
// setup based on scheme
if (scheme == null) {
scheme = tok.toLowerCase();
continue;
}
if (scheme.equals("digest")) {
if (digest == null) {
digest = new Digest(request.getMethod());
}
} else if (scheme.equals("basic")) {
// collect the space, then the credential, then break
if (tok.equals(" ")) {
// skip the space
continue;
}
credential = tok;
// processed below
break;
} else {
// unknown scheme
break;
}
char c = (tok.length() == 1) ? tok.charAt(0) : '\0';
switch (c)
{
case '=':
name = last;
last = tok;
break;
case ',':
name = null;
break;
case ' ':
break;
default:
last = tok;
if (name != null)
{
if ("username".equalsIgnoreCase(name))
digest.username = tok;
else if ("realm".equalsIgnoreCase(name))
digest.realm = tok;
else if ("nonce".equalsIgnoreCase(name))
digest.nonce = tok;
else if ("nc".equalsIgnoreCase(name))
digest.nc = tok;
else if ("cnonce".equalsIgnoreCase(name))
digest.cnonce = tok;
else if ("qop".equalsIgnoreCase(name))
digest.qop = tok;
else if ("uri".equalsIgnoreCase(name))
digest.uri = tok;
else if ("response".equalsIgnoreCase(name))
digest.response = tok;
else if ("algorithm".equalsIgnoreCase(name))
digest.algorithm = tok.toLowerCase();
name = null;
}
}
}
// Now validate based on scheme type
if ("digest".equals(scheme) &&
((_enableSHA256 && digest.algorithm.equals("sha-256") ||
(_enableMD5 && digest.algorithm.equals("md5"))))) {
int n = checkNonce(digest, (Request)request);
if (n > 0)
{
UserIdentity user = login(digest.username, digest, req);
if (user != null)
{
return new UserAuthentication(Constraint.__DIGEST_AUTH, user);
}
}
else if (n == 0)
stale = true;
} else if (_enableBasic && "basic".equals(scheme)) {
if (credential != null) {
credential = B64Code.decode(credential, StandardCharsets.ISO_8859_1);
int i = credential.indexOf(':');
if (i>0)
{
String username = credential.substring(0,i);
String password = credential.substring(i+1);
UserIdentity user = login(username, password, request);
if (user!=null)
{
return new UserAuthentication(Constraint.__BASIC_AUTH, user);
}
}
}
} else {
LOG.warn("unsupported auth scheme " + scheme);
}
}
if (!DeferredAuthentication.isDeferred(response))
{
String domain = request.getContextPath();
if (domain == null)
domain = "/";
String nonce = newNonce((Request) request);
String realm = _loginService.getName();
// RFC 7616 preferred first
if (_enableSHA256) {
response.addHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Digest realm=\"" + realm
+ "\", domain=\""
+ domain
+ "\", nonce=\""
+ nonce
+ "\", algorithm=SHA-256, qop=\"auth\","
+ " stale=" + stale);
}
if (_enableMD5) {
response.addHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Digest realm=\"" + realm
+ "\", domain=\""
+ domain
+ "\", nonce=\""
+ nonce
+ "\", algorithm=MD5, qop=\"auth\","
+ " stale=" + stale);
}
if (_enableBasic) {
response.addHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"" + realm + '"');
}
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return Authentication.SEND_CONTINUE;
}
return Authentication.UNAUTHENTICATED;
}
catch (IOException e)
{
throw new ServerAuthException(e);
}
}
/**
* For digest
*/
@Override
public UserIdentity login(String username, Object credentials, ServletRequest request)
{
Digest digest = (Digest)credentials;
if (!Objects.equals(digest.realm, _loginService.getName()))
return null;
return super.login(username, credentials, request);
}
/**
* For basic
*/
public UserIdentity login(String username, String credentials, ServletRequest request)
{
return super.login(username, credentials, request);
}
public String newNonce(Request request)
{
Nonce nonce;
do
{
byte[] nounce = new byte[24];
_random.nextBytes(nounce);
nonce = new Nonce(new String(B64Code.encode(nounce)), request.getTimeStamp(), getMaxNonceCount());
}
while (_nonceMap.putIfAbsent(nonce._nonce, nonce) != null);
_nonceQueue.add(nonce);
return nonce._nonce;
}
/**
* @param digest the digest data to check
* @param request the request object
* @return -1 for a bad nonce, 0 for a stale none, 1 for a good nonce
*/
private int checkNonce(Digest digest, Request request)
{
// firstly let's expire old nonces
long expired = request.getTimeStamp() - getMaxNonceAge();
Nonce nonce = _nonceQueue.peek();
while (nonce != null && nonce._ts < expired)
{
_nonceQueue.remove(nonce);
_nonceMap.remove(nonce._nonce);
nonce = _nonceQueue.peek();
}
// Now check the requested nonce
try
{
nonce = _nonceMap.get(digest.nonce);
if (nonce == null)
return 0;
long count = Long.parseLong(digest.nc, 16);
if (count >= _maxNC)
return 0;
if (nonce.seen((int)count))
return -1;
return 1;
}
catch (Exception e)
{
LOG.ignore(e);
}
return -1;
}
private static class Nonce
{
final String _nonce;
final long _ts;
final BitSet _seen;
public Nonce(String nonce, long ts, int size)
{
_nonce = nonce;
_ts = ts;
_seen = new BitSet(size);
}
public boolean seen(int count)
{
synchronized (this)
{
if (count >= _seen.size())
return true;
boolean s = _seen.get(count);
_seen.set(count);
return s;
}
}
}
private static class Digest extends Credential
{
private static final long serialVersionUID = -1111639019549527724L;
final String method;
String username = "";
String realm = "";
String nonce = "";
String nc = "";
String cnonce = "";
String qop = "";
String uri = "";
String response = "";
// RFC 7616 default
String algorithm = "md5";
/* ------------------------------------------------------------ */
Digest(String m)
{
method = m;
}
/* ------------------------------------------------------------ */
@Override
public boolean check(Object credentials)
{
if (credentials instanceof char[])
credentials = new String((char[])credentials);
String password = (credentials instanceof String) ? (String)credentials : credentials.toString();
try
{
MessageDigest md;
byte[] ha1 = null;
if (algorithm.equals("sha-256")) {
md = MessageDigest.getInstance("SHA-256");
if (credentials instanceof SHA256Credential) {
ha1 = ((SHA256Credential)credentials).getDigest();
}
} else if (algorithm.equals("md5")) {
md = MessageDigest.getInstance("MD5");
if (credentials instanceof Credential.MD5) {
// Credentials are already a MD5 digest - assume it's in
// form user:realm:password (we have no way to know since
// it's a digest, alright?)
ha1 = ((Credential.MD5)credentials).getDigest();
}
} else {
LOG.warn("unsupported algorithm " + algorithm);
return false;
}
if (ha1 == null) {
// calc A1 digest
md.update(username.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(realm.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(password.getBytes(StandardCharsets.ISO_8859_1));
ha1 = md.digest();
}
// calc A2 digest
md.reset();
md.update(method.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(uri.getBytes(StandardCharsets.ISO_8859_1));
byte[] ha2 = md.digest();
// calc digest
// request-digest = <"> < KD ( H(A1), unq(nonce-value) ":"
// nc-value ":" unq(cnonce-value) ":" unq(qop-value) ":" H(A2) )
// <">
// request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2)
// ) > <">
md.update(TypeUtil.toString(ha1, 16).getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(nonce.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(nc.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(cnonce.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(qop.getBytes(StandardCharsets.ISO_8859_1));
md.update((byte)':');
md.update(TypeUtil.toString(ha2, 16).getBytes(StandardCharsets.ISO_8859_1));
byte[] digest = md.digest();
// check digest
return stringEquals(TypeUtil.toString(digest, 16).toLowerCase(), response == null ? null : response.toLowerCase());
}
catch (Exception e)
{
LOG.warn(e);
}
return false;
}
@Override
public String toString()
{
return username + "," + response;
}
}
}