1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.client.authentication;
|
3
|
|
import org.apache.cactus.WebRequest;
|
4
|
|
import java.text.CharacterIterator;
|
5
|
|
import java.text.StringCharacterIterator;
|
6
|
|
|
7
|
|
/**
|
8
|
|
* Basic Authentication support.
|
9
|
|
*
|
10
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
11
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
12
|
|
*
|
13
|
|
* @since 1.3
|
14
|
|
* @see AbstractAuthentication
|
15
|
|
*
|
16
|
|
* @version $Id: BasicAuthentication.java,v 1.4 2002/07/24 20:46:48 vmassol Exp $
|
17
|
|
*/
|
18
|
|
public class BasicAuthentication extends AbstractAuthentication {
|
19
|
|
/**
|
20
|
|
* Provides encoding of raw bytes to base64-encoded characters, and
|
21
|
|
* decoding of base64 characters to raw bytes.
|
22
|
|
*/
|
23
|
|
private static char[] alphabet;
|
24
|
|
/**
|
25
|
|
* Lookup table for converting base64 characters to value in range 0..63
|
26
|
|
*/
|
27
|
|
private static byte[] codes;
|
28
|
|
/**
|
29
|
|
* @param theName user name of the Credential
|
30
|
|
* @param thePassword user password of the Credential
|
31
|
|
*/
|
32
|
0
|
public BasicAuthentication(String theName, String thePassword) {
|
33
|
0
|
super(theName, thePassword);
|
34
|
|
;
|
35
|
|
}
|
36
|
|
/**
|
37
|
|
* @see AbstractAuthentication#validateName(String)
|
38
|
|
*/
|
39
|
0
|
protected void validateName(String theName) {
|
40
|
0
|
if (theName == null) {
|
41
|
0
|
return;
|
42
|
|
}
|
43
|
0
|
String illegalChars = "()<>@,;:\\\"/[]?={} \t";
|
44
|
0
|
StringCharacterIterator iter = new StringCharacterIterator(theName);
|
45
|
0
|
for (char c = iter.first(); c != ((char)65535); c = iter.next()) {
|
46
|
0
|
if ((illegalChars.indexOf(c) != -1) || ((c >= 0) && (c <= 31)) || (c == 127)) {
|
47
|
0
|
throw new IllegalArgumentException("Given theName contains illegal characters.");
|
48
|
|
}
|
49
|
|
}
|
50
|
|
}
|
51
|
|
|
52
|
|
/**
|
53
|
|
* @see AbstractAuthentication#validatePassword(String)
|
54
|
|
*/
|
55
|
0
|
protected void validatePassword(String thePassword) {
|
56
|
0
|
if (thePassword == null) {
|
57
|
0
|
return;
|
58
|
|
}
|
59
|
0
|
String exceptionChars = "\r\n \t";
|
60
|
0
|
StringCharacterIterator iter = new StringCharacterIterator(thePassword);
|
61
|
0
|
for (char c = iter.first(); c != ((char)65535); c = iter.next()) {
|
62
|
0
|
if (((c >= 0) && (c <= 31)) || (c == 127)) {
|
63
|
0
|
if (exceptionChars.indexOf(c) != -1) {
|
64
|
0
|
continue;
|
65
|
|
}
|
66
|
0
|
throw new IllegalArgumentException("Given thePassword contains illegal characters.");
|
67
|
|
}
|
68
|
|
}
|
69
|
|
}
|
70
|
|
|
71
|
|
/**
|
72
|
|
* @see AbstractAuthentication#configure(WebRequest)
|
73
|
|
*/
|
74
|
0
|
public void configure(WebRequest theRequest) {
|
75
|
0
|
String basicCookie = this.getName() + ":" + this.getPassword();
|
76
|
0
|
String basicCredentials = "Basic " + new String(BasicAuthentication.base64Encode(
|
77
|
|
basicCookie.getBytes()));
|
78
|
0
|
theRequest.addHeader("Authorization", basicCredentials);
|
79
|
|
}
|
80
|
|
|
81
|
|
/**
|
82
|
|
* returns an array of base64-encoded characters to represent the
|
83
|
|
* passed theData array.
|
84
|
|
*
|
85
|
|
* @param theData the array of bytes to encode
|
86
|
|
* @return base64-coded character array.
|
87
|
|
*/
|
88
|
0
|
private static char[] base64Encode(byte[] theData) {
|
89
|
0
|
char[] out = new char[((theData.length + 2) / 3) * 4];
|
90
|
0
|
for (int i = 0, index = 0; i < theData.length; i += 3, index += 4) {
|
91
|
0
|
boolean quad = false;
|
92
|
0
|
boolean trip = false;
|
93
|
0
|
int val = (255 & (int)theData[i]);
|
94
|
0
|
val <<= 8;
|
95
|
0
|
if ((i + 1) < theData.length) {
|
96
|
0
|
val |= (255 & (int)theData[i + 1]);
|
97
|
0
|
trip = true;
|
98
|
|
}
|
99
|
0
|
val <<= 8;
|
100
|
0
|
if ((i + 2) < theData.length) {
|
101
|
0
|
val |= (255 & (int)theData[i + 2]);
|
102
|
0
|
quad = true;
|
103
|
|
}
|
104
|
0
|
out[index + 3] = BasicAuthentication.alphabet[(quad ? (val & 63) : 64)];
|
105
|
0
|
val >>= 6;
|
106
|
0
|
out[index + 2] = BasicAuthentication.alphabet[(trip ? (val & 63) : 64)];
|
107
|
0
|
val >>= 6;
|
108
|
0
|
out[index + 1] = BasicAuthentication.alphabet[val & 63];
|
109
|
0
|
val >>= 6;
|
110
|
0
|
out[index + 0] = BasicAuthentication.alphabet[val & 63];
|
111
|
|
}
|
112
|
0
|
return out;
|
113
|
|
}
|
114
|
|
|
115
|
|
/**
|
116
|
|
* Basic Authentication support.
|
117
|
|
*
|
118
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
119
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
120
|
|
*
|
121
|
|
* @since 1.3
|
122
|
|
* @see AbstractAuthentication
|
123
|
|
*
|
124
|
|
* @version $Id: BasicAuthentication.java,v 1.4 2002/07/24 20:46:48 vmassol Exp $
|
125
|
|
*/
|
126
|
|
static {
|
127
|
|
/**
|
128
|
|
* Provides encoding of raw bytes to base64-encoded characters, and
|
129
|
|
* decoding of base64 characters to raw bytes.
|
130
|
|
*/
|
131
|
0
|
BasicAuthentication.alphabet =
|
132
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
|
133
|
|
/**
|
134
|
|
* Lookup table for converting base64 characters to value in range 0..63
|
135
|
|
*/
|
136
|
0
|
BasicAuthentication.codes = new byte[256];
|
137
|
0
|
for (int i = 0; i < 256; i++) {
|
138
|
0
|
BasicAuthentication.codes[i] = -1;
|
139
|
|
}
|
140
|
0
|
for (int i = 'A'; i <= 'Z'; i++) {
|
141
|
0
|
BasicAuthentication.codes[i] = (byte)(i - 'A');
|
142
|
|
}
|
143
|
0
|
for (int i = 'a'; i <= 'z'; i++) {
|
144
|
0
|
BasicAuthentication.codes[i] = (byte)(26 + i - 'a');
|
145
|
|
}
|
146
|
0
|
for (int i = '0'; i <= '9'; i++) {
|
147
|
0
|
BasicAuthentication.codes[i] = (byte)(52 + i - '0');
|
148
|
|
}
|
149
|
0
|
BasicAuthentication.codes['+'] = 62;
|
150
|
0
|
BasicAuthentication.codes['/'] = 63;
|
151
|
|
}
|
152
|
|
|
153
|
|
}
|