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 package org.apache.http.impl.auth;
28
29 import java.nio.charset.Charset;
30
31 import org.apache.commons.codec.binary.Base64;
32 import org.apache.http.Consts;
33 import org.apache.http.Header;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.auth.AUTH;
36 import org.apache.http.auth.AuthenticationException;
37 import org.apache.http.auth.ChallengeState;
38 import org.apache.http.auth.Credentials;
39 import org.apache.http.auth.MalformedChallengeException;
40 import org.apache.http.message.BufferedHeader;
41 import org.apache.http.protocol.BasicHttpContext;
42 import org.apache.http.protocol.HttpContext;
43 import org.apache.http.util.Args;
44 import org.apache.http.util.CharArrayBuffer;
45 import org.apache.http.util.EncodingUtils;
46
47
48
49
50
51
52 public class BasicScheme extends RFC2617Scheme {
53
54 private static final long serialVersionUID = -1931571557597830536L;
55
56
57 private boolean complete;
58
59
60
61
62 public BasicScheme(final Charset credentialsCharset) {
63 super(credentialsCharset);
64 this.complete = false;
65 }
66
67
68
69
70
71
72
73
74
75 @Deprecated
76 public BasicScheme(final ChallengeState challengeState) {
77 super(challengeState);
78 }
79
80 public BasicScheme() {
81 this(Consts.ASCII);
82 }
83
84
85
86
87
88
89 @Override
90 public String getSchemeName() {
91 return "basic";
92 }
93
94
95
96
97
98
99
100
101
102 @Override
103 public void processChallenge(
104 final Header header) throws MalformedChallengeException {
105 super.processChallenge(header);
106 this.complete = true;
107 }
108
109
110
111
112
113
114
115 @Override
116 public boolean isComplete() {
117 return this.complete;
118 }
119
120
121
122
123
124
125 @Override
126 public boolean isConnectionBased() {
127 return false;
128 }
129
130
131
132
133
134 @Override
135 @Deprecated
136 public Header authenticate(
137 final Credentials credentials, final HttpRequest request) throws AuthenticationException {
138 return authenticate(credentials, request, new BasicHttpContext());
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @Override
154 public Header authenticate(
155 final Credentials credentials,
156 final HttpRequest request,
157 final HttpContext context) throws AuthenticationException {
158
159 Args.notNull(credentials, "Credentials");
160 Args.notNull(request, "HTTP request");
161 final StringBuilder tmp = new StringBuilder();
162 tmp.append(credentials.getUserPrincipal().getName());
163 tmp.append(":");
164 tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
165
166 final Base64 base64codec = new Base64(0);
167 final byte[] base64password = base64codec.encode(
168 EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request)));
169
170 final CharArrayBuffer buffer = new CharArrayBuffer(32);
171 if (isProxy()) {
172 buffer.append(AUTH.PROXY_AUTH_RESP);
173 } else {
174 buffer.append(AUTH.WWW_AUTH_RESP);
175 }
176 buffer.append(": Basic ");
177 buffer.append(base64password, 0, base64password.length);
178
179 return new BufferedHeader(buffer);
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 @Deprecated
194 public static Header authenticate(
195 final Credentials credentials,
196 final String charset,
197 final boolean proxy) {
198 Args.notNull(credentials, "Credentials");
199 Args.notNull(charset, "charset");
200
201 final StringBuilder tmp = new StringBuilder();
202 tmp.append(credentials.getUserPrincipal().getName());
203 tmp.append(":");
204 tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
205
206 final byte[] base64password = Base64.encodeBase64(
207 EncodingUtils.getBytes(tmp.toString(), charset), false);
208
209 final CharArrayBuffer buffer = new CharArrayBuffer(32);
210 if (proxy) {
211 buffer.append(AUTH.PROXY_AUTH_RESP);
212 } else {
213 buffer.append(AUTH.WWW_AUTH_RESP);
214 }
215 buffer.append(": Basic ");
216 buffer.append(base64password, 0, base64password.length);
217
218 return new BufferedHeader(buffer);
219 }
220
221 @Override
222 public String toString() {
223 final StringBuilder builder = new StringBuilder();
224 builder.append("BASIC [complete=").append(complete)
225 .append("]");
226 return builder.toString();
227 }
228 }