-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAuthorizationInfo.java
More file actions
1382 lines (1204 loc) · 39.2 KB
/
AuthorizationInfo.java
File metadata and controls
1382 lines (1204 loc) · 39.2 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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* @(#)AuthorizationInfo.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ProtocolException;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* Holds the information for an authorization response.
*
* <P>There are 7 fields which make up this class: host, port, scheme,
* realm, cookie, params, and extra_info. The host and port select which
* server the info will be sent to. The realm is server specified string
* which groups various URLs under a given server together and which is
* used to select the correct info when a server issues an auth challenge;
* for schemes which don't use a realm (such as "NTLM", "PEM", and
* "Kerberos") the realm must be the empty string (""). The scheme is the
* authorization scheme used (such as "Basic" or "Digest").
*
* <P>There are basically two formats used for the Authorization header,
* the one used by the "Basic" scheme and derivatives, and the one used by
* the "Digest" scheme and derivatives. The first form contains just the
* the scheme and a "cookie":
*
* <PRE> Authorization: Basic aGVsbG86d29ybGQ=</PRE>
*
* The second form contains the scheme followed by a number of parameters
* in the form of name=value pairs:
*
* <PRE> Authorization: Digest username="hello", realm="test", nonce="42", ...</PRE>
*
* The two fields "cookie" and "params" correspond to these two forms.
* <A HREF="#toString()">toString()</A> is used by the AuthorizationModule
* when generating the Authorization header and will format the info
* accordingly. Note that "cookie" and "params" are mutually exclusive: if
* the cookie field is non-null then toString() will generate the first
* form; otherwise it will generate the second form.
*
* <P>In some schemes "extra" information needs to be kept which doesn't
* appear directly in the Authorization header. An example of this are the
* A1 and A2 strings in the Digest scheme. Since all elements in the params
* field will appear in the Authorization header this field can't be used
* for storing such info. This is what the extra_info field is for. It is
* an arbitrary object which can be manipulated by the corresponding
* setExtraInfo() and getExtraInfo() methods, but which will not be printed
* by toString().
*
* <P>The addXXXAuthorization(), removeXXXAuthorization(), and
* getAuthorization() methods manipulate and query an internal list of
* AuthorizationInfo instances. There can be only one instance per host,
* port, scheme, and realm combination (see <A HREF="#equals">equals()</A>).
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
* @since V0.1
*/
@SuppressWarnings("unchecked")
public class AuthorizationInfo implements Cloneable
{
// class fields
/** Holds the list of lists of authorization info structures */
private static Hashtable CntxtList = new Hashtable();
/** A pointer to the handler to be called when we need authorization info */
private static AuthorizationHandler
AuthHandler = new DefaultAuthHandler();
static
{
CntxtList.put(HTTPConnection.getDefaultContext(), new Hashtable());
}
// the instance oriented stuff
/** the host (lowercase) */
private String host;
/** the port */
private int port;
/** the scheme. (e.g. "Basic")
* Note: don't lowercase because some buggy servers use a case-sensitive
* match */
private String scheme;
/** the realm */
private String realm;
/** the string used for the "Basic", "NTLM", and other authorization
* schemes which don't use parameters */
private String cookie;
/** any parameters */
private NVPair[] auth_params = new NVPair[0];
/** additional info which won't be displayed in the toString() */
private Object extra_info = null;
/** a list of paths where this realm has been known to be required */
private String[] paths = new String[0];
// Constructors
/**
* Creates an new info structure for the specified host and port.
*
* @param host the host
* @param port the port
*/
AuthorizationInfo(String host, int port)
{
this.host = host.trim().toLowerCase();
this.port = port;
}
/**
* Creates a new info structure for the specified host and port with the
* specified scheme, realm, params. The cookie is set to null.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param params the parameters as an array of name/value pairs, or null
* @param info arbitrary extra info, or null
*/
public AuthorizationInfo(String host, int port, String scheme,
String realm, NVPair params[], Object info)
{
this.scheme = scheme.trim();
this.host = host.trim().toLowerCase();
this.port = port;
this.realm = realm;
this.cookie = null;
if (params != null)
auth_params = Util.resizeArray(params, params.length);
this.extra_info = info;
}
/**
* Creates a new info structure for the specified host and port with the
* specified scheme, realm and cookie. The params is set to a zero-length
* array, and the extra_info is set to null.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param cookie for the "Basic" scheme this is the base64-encoded
* username/password; for the "NTLM" scheme this is the
* base64-encoded username/password message.
*/
public AuthorizationInfo(String host, int port, String scheme,
String realm, String cookie)
{
this.scheme = scheme.trim();
this.host = host.trim().toLowerCase();
this.port = port;
this.realm = realm;
if (cookie != null)
this.cookie = cookie.trim();
else
this.cookie = null;
}
/**
* Creates a new copy of the given AuthorizationInfo.
*
* @param templ the info to copy
*/
AuthorizationInfo(AuthorizationInfo templ)
{
this.scheme = templ.scheme;
this.host = templ.host;
this.port = templ.port;
this.realm = templ.realm;
this.cookie = templ.cookie;
this.auth_params =
Util.resizeArray(templ.auth_params, templ.auth_params.length);
this.extra_info = templ.extra_info;
}
// Class Methods
/**
* Set's the authorization handler. This handler is called whenever
* the server requests authorization and no entry for the requested
* scheme and realm can be found in the list. The handler must implement
* the AuthorizationHandler interface.
*
* <P>If no handler is set then a {@link DefaultAuthHandler default
* handler} is used. This handler currently only handles the "Basic" and
* "Digest" schemes and brings up a popup which prompts for the username
* and password.
*
* <P>The default handler can be disabled by setting the auth handler to
* <var>null</var>.
*
* @param handler the new authorization handler
* @return the old authorization handler
* @see AuthorizationHandler
*/
public static AuthorizationHandler
setAuthHandler(AuthorizationHandler handler)
{
AuthorizationHandler tmp = AuthHandler;
AuthHandler = handler;
return tmp;
}
/**
* Get's the current authorization handler.
*
* @return the current authorization handler, or null if none is set.
* @see AuthorizationHandler
*/
public static AuthorizationHandler getAuthHandler()
{
return AuthHandler;
}
/**
* Searches for the authorization info using the given host, port,
* scheme and realm. The context is the default context.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @return a pointer to the authorization data or null if not found
*/
public static AuthorizationInfo getAuthorization(
String host, int port,
String scheme, String realm)
{
return getAuthorization(host, port, scheme, realm,
HTTPConnection.getDefaultContext());
}
/**
* Searches for the authorization info in the given context using the
* given host, port, scheme and realm.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param context the context this info is associated with
* @return a pointer to the authorization data or null if not found
*/
public static synchronized AuthorizationInfo getAuthorization(
String host, int port,
String scheme, String realm,
Object context)
{
Hashtable AuthList = Util.getList(CntxtList, context);
AuthorizationInfo auth_info =
new AuthorizationInfo(host, port, scheme, realm, (NVPair[]) null,
null);
return (AuthorizationInfo) AuthList.get(auth_info);
}
/**
* Queries the AuthHandler for authorization info. It also adds this
* info to the list.
*
* @param auth_info any info needed by the AuthHandler; at a minimum the
* host, scheme and realm should be set.
* @param req the request which initiated this query
* @param resp the full response
* @return a structure containing the requested info, or null if either
* no AuthHandler is set or the user canceled the request.
* @exception AuthSchemeNotImplException if this is thrown by
* the AuthHandler.
*/
static AuthorizationInfo queryAuthHandler(AuthorizationInfo auth_info,
RoRequest req, RoResponse resp, boolean proxy)
throws AuthSchemeNotImplException, IOException
{
if (AuthHandler == null)
return null;
AuthorizationInfo new_info =
AuthHandler.getAuthorization(auth_info, req, resp, proxy);
if (new_info != null)
{
if (req != null)
addAuthorization((AuthorizationInfo) new_info.clone(),
req.getConnection().getContext());
else
addAuthorization((AuthorizationInfo) new_info.clone(),
HTTPConnection.getDefaultContext());
}
return new_info;
}
/**
* Searches for the authorization info using the host, port, scheme and
* realm from the given info struct. If not found it queries the
* AuthHandler (if set).
*
* @param auth_info the AuthorizationInfo
* @param req the request which initiated this query
* @param resp the full response
* @param query_auth_h if true, query the auth-handler if no info found.
* @return a pointer to the authorization data or null if not found
* @exception AuthSchemeNotImplException If thrown by the AuthHandler.
*/
static synchronized AuthorizationInfo getAuthorization(
AuthorizationInfo auth_info, RoRequest req,
RoResponse resp, boolean query_auth_h, boolean proxy_auth)
throws AuthSchemeNotImplException, IOException
{
Hashtable AuthList;
if (req != null)
AuthList = Util.getList(CntxtList, req.getConnection().getContext());
else
AuthList = Util.getList(CntxtList, HTTPConnection.getDefaultContext());
AuthorizationInfo new_info =
(AuthorizationInfo) AuthList.get(auth_info);
if (new_info == null && query_auth_h)
new_info = queryAuthHandler(auth_info, req, resp, proxy_auth);
return new_info;
}
/**
* Searches for the authorization info given a host, port, scheme and
* realm. Queries the AuthHandler if not found in list.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param req the request which initiated this query
* @param resp the full response
* @param query_auth_h if true, query the auth-handler if no info found.
* @return a pointer to the authorization data or null if not found
* @exception AuthSchemeNotImplException If thrown by the AuthHandler.
*/
static AuthorizationInfo getAuthorization(String host, int port,
String scheme, String realm,
RoRequest req, RoResponse resp,
boolean query_auth_h, boolean proxy)
throws AuthSchemeNotImplException, IOException
{
return getAuthorization(new AuthorizationInfo(host, port, scheme,
realm, (NVPair[]) null, null),
req, resp, query_auth_h, proxy);
}
/**
* Adds an authorization entry to the list using the default context.
* If an entry for the specified scheme and realm already exists then
* its cookie and params are replaced with the new data.
*
* @param auth_info the AuthorizationInfo to add
*/
public static void addAuthorization(AuthorizationInfo auth_info)
{
addAuthorization(auth_info, HTTPConnection.getDefaultContext());
}
/**
* Adds an authorization entry to the list. If an entry for the
* specified scheme and realm already exists then its cookie and
* params are replaced with the new data.
*
* @param auth_info the AuthorizationInfo to add
* @param context the context to associate this info with
*/
@SuppressWarnings("unchecked")
public static void addAuthorization(AuthorizationInfo auth_info,
Object context)
{
Hashtable AuthList = Util.getList(CntxtList, context);
// merge path list
AuthorizationInfo old_info =
(AuthorizationInfo) AuthList.get(auth_info);
if (old_info != null)
{
int ol = old_info.paths.length,
al = auth_info.paths.length;
if (al == 0)
auth_info.paths = old_info.paths;
else
{
auth_info.paths = Util.resizeArray(auth_info.paths, al+ol);
System.arraycopy(old_info.paths, 0, auth_info.paths, al, ol);
}
}
AuthList.put(auth_info, auth_info);
}
/**
* Adds an authorization entry to the list using the default context.
* If an entry for the specified scheme and realm already exists then
* its cookie and params are replaced with the new data.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param cookie the cookie
* @param params an array of name/value pairs of parameters
* @param info arbitrary extra auth info
*/
public static void addAuthorization(String host, int port, String scheme,
String realm, String cookie,
NVPair params[], Object info)
{
addAuthorization(host, port, scheme, realm, cookie, params, info,
HTTPConnection.getDefaultContext());
}
/**
* Adds an authorization entry to the list. If an entry for the
* specified scheme and realm already exists then its cookie and
* params are replaced with the new data.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param cookie the cookie
* @param params an array of name/value pairs of parameters
* @param info arbitrary extra auth info
* @param context the context to associate this info with
*/
public static void addAuthorization(String host, int port, String scheme,
String realm, String cookie,
NVPair params[], Object info,
Object context)
{
AuthorizationInfo auth =
new AuthorizationInfo(host, port, scheme, realm, cookie);
if (params != null && params.length > 0)
auth.auth_params = Util.resizeArray(params, params.length);
auth.extra_info = info;
addAuthorization(auth, context);
}
/**
* Adds an authorization entry for the "Basic" authorization scheme to
* the list using the default context. If an entry already exists for
* the "Basic" scheme and the specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
*/
public static void addBasicAuthorization(String host, int port,
String realm, String user,
String passwd)
{
addAuthorization(host, port, "Basic", realm,
Codecs.base64Encode(user + ":" + passwd),
(NVPair[]) null, null);
}
/**
* Adds an authorization entry for the "Basic" authorization scheme to
* the list. If an entry already exists for the "Basic" scheme and the
* specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
* @param context the context to associate this info with
*/
public static void addBasicAuthorization(String host, int port,
String realm, String user,
String passwd, Object context)
{
addAuthorization(host, port, "Basic", realm,
Codecs.base64Encode(user + ":" + passwd),
(NVPair[]) null, null, context);
}
/**
* Adds an authorization entry for the "Digest" authorization scheme to
* the list using the default context. If an entry already exists for the
* "Digest" scheme and the specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
*/
public static void addDigestAuthorization(String host, int port,
String realm, String user,
String passwd)
{
addDigestAuthorization(host, port, realm, user, passwd,
HTTPConnection.getDefaultContext());
}
/**
* Adds an authorization entry for the "Digest" authorization scheme to
* the list. If an entry already exists for the "Digest" scheme and the
* specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
* @param context the context to associate this info with
*/
public static void addDigestAuthorization(String host, int port,
String realm, String user,
String passwd, Object context)
{
AuthorizationInfo prev =
getAuthorization(host, port, "Digest", realm, context);
NVPair[] params;
if (prev == null)
{
params = new NVPair[4];
params[0] = new NVPair("username", user);
params[1] = new NVPair("uri", "");
params[2] = new NVPair("nonce", "");
params[3] = new NVPair("response", "");
}
else
{
params = prev.getParams();
for (int idx=0; idx<params.length; idx++)
{
if (params[idx].getName().equalsIgnoreCase("username"))
{
params[idx] = new NVPair("username", user);
break;
}
}
}
String[] extra = { MD5.hexDigest(user + ":" + realm + ":" + passwd),
null, null };
addAuthorization(host, port, "Digest", realm, null, params, extra,
context);
}
/**
* Adds an authorization entry for the "NTLM" authorization scheme to
* the list using the default context. If an entry already exists for
* the "NTLM" scheme and the specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
*/
public static void addNTLMAuthorization(String host, int port,
String realm, String user,
String passwd)
{
addNTLMAuthorization(host, port, realm, user, passwd,
HTTPConnection.getDefaultContext());
}
/**
* Adds an authorization entry for the "NTLM" authorization scheme to
* the list. If an entry already exists for the "NTLM" scheme and the
* specified realm then it is overwritten.
*
* @param host the host
* @param port the port
* @param realm the realm
* @param user the username
* @param passwd the password
* @param context the context to associate this info with
*/
public static void addNTLMAuthorization(String host, int port,
String realm, String user,
String passwd, Object context)
{
// hash the password
byte[] lm_hpw = DefaultAuthHandler.calc_lm_hpw(passwd);
byte[] nt_hpw = DefaultAuthHandler.calc_ntcr_hpw(passwd);
// get the local host name
String lhost = null;
try
{ lhost = System.getProperty("HTTPClient.defAuthHandler.NTLM.host"); }
catch (SecurityException se)
{ }
if (lhost == null)
try
{ lhost = InetAddress.getLocalHost().getHostName(); }
catch (Exception e)
{ }
if (lhost == null)
lhost = "localhost"; // ???
int dot = lhost.indexOf('.');
if (dot != -1)
lhost = lhost.substring(0, dot);
// get user and domain name
String domain = null;
int slash;
if ((slash = user.indexOf('\\')) != -1)
domain = user.substring(0, slash);
else
{
try
{
domain =
System.getProperty("HTTPClient.defAuthHandler.NTLM.domain");
}
catch (SecurityException se)
{ }
if (domain == null)
domain = lhost; // ???
}
user = user.substring(slash+1);
// store info in extra_info field
Object[] info = { user, lhost.toUpperCase().trim(),
domain.toUpperCase().trim(), lm_hpw, nt_hpw };
addAuthorization(host, port, "NTLM", realm, null, null, info, context);
}
/**
* Removes an authorization entry from the list using the default context.
* If no entry for the specified host, port, scheme and realm exists then
* this does nothing.
*
* @param auth_info the AuthorizationInfo to remove
*/
public static void removeAuthorization(AuthorizationInfo auth_info)
{
removeAuthorization(auth_info, HTTPConnection.getDefaultContext());
}
/**
* Removes an authorization entry from the list. If no entry for the
* specified host, port, scheme and realm exists then this does nothing.
*
* @param auth_info the AuthorizationInfo to remove
* @param context the context this info is associated with
*/
public static void removeAuthorization(AuthorizationInfo auth_info,
Object context)
{
Hashtable AuthList = Util.getList(CntxtList, context);
AuthList.remove(auth_info);
}
/**
* Removes an authorization entry from the list using the default context.
* If no entry for the specified host, port, scheme and realm exists then
* this does nothing.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
*/
public static void removeAuthorization(String host, int port, String scheme,
String realm)
{
removeAuthorization(
new AuthorizationInfo(host, port, scheme, realm, (NVPair[]) null,
null));
}
/**
* Removes an authorization entry from the list. If no entry for the
* specified host, port, scheme and realm exists then this does nothing.
*
* @param host the host
* @param port the port
* @param scheme the scheme
* @param realm the realm
* @param context the context this info is associated with
*/
public static void removeAuthorization(String host, int port, String scheme,
String realm, Object context)
{
removeAuthorization(
new AuthorizationInfo(host, port, scheme, realm, (NVPair[]) null,
null), context);
}
/**
* Tries to find the candidate in the current list of auth info for the
* given request. The paths associated with each auth info are examined,
* and the one with either the nearest direct parent or child is chosen.
* This is used for preemptively sending auth info.
*
* @param req the Request
* @return an AuthorizationInfo containing the info for the best match,
* or null if none found.
*/
static AuthorizationInfo findBest(RoRequest req)
{
String path = Util.getPath(req.getRequestURI());
String host = req.getConnection().getHost();
int port = req.getConnection().getPort();
// First search for an exact match
Hashtable AuthList =
Util.getList(CntxtList, req.getConnection().getContext());
Enumeration list = AuthList.elements();
while (list.hasMoreElements())
{
AuthorizationInfo info = (AuthorizationInfo) list.nextElement();
if (!info.host.equals(host) || info.port != port)
continue;
/*String[] paths = info.paths;
for (int idx=0; idx<paths.length; idx++)
{
if (path.equals(paths[idx]))
return info;
} */
//No busco en el path porque el mismo siempre es vacio y sino nunca manda la autorizacion.
return info;
}
// Now find the closest parent or child
AuthorizationInfo best = null;
String base = path.substring(0, path.lastIndexOf('/')+1);
int min = Integer.MAX_VALUE;
list = AuthList.elements();
while (list.hasMoreElements())
{
AuthorizationInfo info = (AuthorizationInfo) list.nextElement();
if (!info.host.equals(host) || info.port != port)
continue;
String[] paths = info.paths;
for (int idx=0; idx<paths.length; idx++)
{
// strip the last path segment, leaving a trailing "/"
String ibase =
paths[idx].substring(0, paths[idx].lastIndexOf('/')+1);
if (base.equals(ibase))
return info;
if (base.startsWith(ibase)) // found a parent
{
int num_seg = 0, pos = ibase.length()-1;
while ((pos = base.indexOf('/', pos+1)) != -1) num_seg++;
if (num_seg < min)
{
min = num_seg;
best = info;
}
}
else if (ibase.startsWith(base)) // found a child
{
int num_seg = 0, pos = base.length();
while ((pos = ibase.indexOf('/', pos+1)) != -1) num_seg++;
if (num_seg < min)
{
min = num_seg;
best = info;
}
}
}
}
return best;
}
/**
* Adds the path from the given resource to our path list. The path
* list is used for deciding when to preemptively send auth info.
*
* @param resource the resource from which to extract the path
*/
public synchronized void addPath(String resource)
{
String path = Util.getPath(resource);
// First check that we don't already have this one
for (int idx=0; idx<paths.length; idx++)
if (paths[idx].equals(path)) return;
// Ok, add it
paths = Util.resizeArray(paths, paths.length+1);
paths[paths.length-1] = path;
}
/**
* Parses the authentication challenge(s) into an array of new info
* structures for the specified host and port.
*
* @param challenge a string containing authentication info. This must
* have the same format as value part of a
* WWW-authenticate response header field, and may
* contain multiple authentication challenges.
* @param req the original request.
* @exception ProtocolException if any error during the parsing occurs.
*/
static AuthorizationInfo[] parseAuthString(String challenge, RoRequest req,
RoResponse resp)
throws ProtocolException
{
int beg = 0,
end = 0;
char[] buf = challenge.toCharArray();
int len = buf.length;
int[] pos_ref = new int[2];
AuthorizationInfo auth_arr[] = new AuthorizationInfo[0],
curr;
while (Character.isWhitespace(buf[len-1])) len--;
while (true) // get all challenges
{
// get scheme
beg = Util.skipSpace(buf, beg);
if (beg == len) break;
end = Util.findSpace(buf, beg+1);
int sts;
try
{ sts = resp.getStatusCode(); }
catch (IOException ioe)
{ throw new ProtocolException(ioe.toString()); }
if (sts == 401)
curr = new AuthorizationInfo(req.getConnection().getHost(),
req.getConnection().getPort());
else
curr = new AuthorizationInfo(req.getConnection().getProxyHost(),
req.getConnection().getProxyPort());
if(Log.isEnabled(Log.EXTENDED_INFO))
{
Log.write(Log.EXTENDED_INFO, "ExtInfo: [Beg=" + beg + ", End=" + end + ", BufEnd=" + buf[end-1] + "]");
}
/* Hack for schemes like NTLM which don't have any params or cookie.
* Mickeysoft, hello? What were you morons thinking here? I suppose
* you weren't, as usual, huh?
*/
if (buf[end-1] == ',')
{
if(end > beg)curr.scheme = challenge.substring(beg, end-1);
else curr.scheme = challenge;
// @gusbro
// Algunos servidores IIS (Win2000) fallan aqui porque ponen el scheme en Negotiate y los par�metros en NTLM
// asi que lo chequeamos aca
try
{
if(curr.scheme.equalsIgnoreCase("Negotiate"))
{
beg = end;
end = Util.findSpace(buf, beg + 1);
curr.scheme = challenge.substring(beg, end).trim();
if (buf[end-1] == ',')
curr.scheme = curr.scheme.substring(0, curr.scheme.length() - 1);
}
}catch(Exception e)
{
if(Log.isEnabled(Log.EXTENDED_INFO))
{
Log.write(Log.EXTENDED_INFO, "ExtInfo: Exception ignored[1]: ", e);
end = beg;
}
}
// @gusbro\
beg = end;
}
else
{
if(end >= beg)curr.scheme = challenge.substring(beg, end);
else curr.scheme = challenge;
// @gusbro
// Algunos servidores IIS (Win2000) fallan aqui porque ponen el scheme en Negotiate y los par�metros en NTLM
// asi que lo chequeamos aca
try
{
if(curr.scheme.equalsIgnoreCase("Negotiate"))
{
beg = end;
end = Util.findSpace(buf, beg + 1);
if(end > challenge.length())
{ // @Hack: Si caigo aca tipicamente es porque el AuthString tiene SOLO Negotiate,
// as� que asumo que es NTLM
Log.write(Log.EXTENDED_INFO, "ExtInfo: No further challenge information. Assuming NTLM authentication");
curr.scheme = "NTLM";
end = beg; // Aparte en este caso dejo Begin = end
}
else
{
curr.scheme = challenge.substring(beg, end).trim();
if (buf[end-1] == ',')
curr.scheme = curr.scheme.substring(0, end-1);
}
}
}catch(Exception e)
{ // @Hack: Si caigo aca tipicamente es porque el AuthString tiene SOLO Negotiate,
// as� que asumo que es NTLM
if(Log.isEnabled(Log.EXTENDED_INFO))
{