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.annotation.NotThreadSafe;
36 import org.apache.http.auth.AUTH;
37 import org.apache.http.auth.AuthenticationException;
38 import org.apache.http.auth.ChallengeState;
39 import org.apache.http.auth.ContextAwareAuthScheme;
40 import org.apache.http.auth.Credentials;
41 import org.apache.http.auth.InvalidCredentialsException;
42 import org.apache.http.auth.MalformedChallengeException;
43 import org.apache.http.message.BufferedHeader;
44 import org.apache.http.protocol.BasicHttpContext;
45 import org.apache.http.protocol.HttpContext;
46 import org.apache.http.util.Args;
47 import org.apache.http.util.CharArrayBuffer;
48 import org.apache.http.util.EncodingUtils;
49
50
51
52
53
54
55 @NotThreadSafe
56 public class BasicScheme extends RFC2617Scheme {
57
58 private final Base64 base64codec;
59
60 private boolean complete;
61
62
63
64
65 public BasicScheme(final Charset credentialsCharset) {
66 super(credentialsCharset);
67 this.base64codec = new Base64(0);
68 this.complete = false;
69 }
70
71
72
73
74
75
76
77
78
79 @Deprecated
80 public BasicScheme(final ChallengeState challengeState) {
81 super(challengeState);
82 this.base64codec = new Base64(0);
83 }
84
85 public BasicScheme() {
86 this(Consts.ASCII);
87 }
88
89
90
91
92
93
94 public String getSchemeName() {
95 return "basic";
96 }
97
98
99
100
101
102
103
104
105
106 @Override
107 public void processChallenge(
108 final Header header) throws MalformedChallengeException {
109 super.processChallenge(header);
110 this.complete = true;
111 }
112
113
114
115
116
117
118
119 public boolean isComplete() {
120 return this.complete;
121 }
122
123
124
125
126
127
128 public boolean isConnectionBased() {
129 return false;
130 }
131
132
133
134
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 byte[] base64password = base64codec.encode(
167 EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request)));
168
169 final CharArrayBuffer buffer = new CharArrayBuffer(32);
170 if (isProxy()) {
171 buffer.append(AUTH.PROXY_AUTH_RESP);
172 } else {
173 buffer.append(AUTH.WWW_AUTH_RESP);
174 }
175 buffer.append(": Basic ");
176 buffer.append(base64password, 0, base64password.length);
177
178 return new BufferedHeader(buffer);
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192 @Deprecated
193 public static Header authenticate(
194 final Credentials credentials,
195 final String charset,
196 final boolean proxy) {
197 Args.notNull(credentials, "Credentials");
198 Args.notNull(charset, "charset");
199
200 final StringBuilder tmp = new StringBuilder();
201 tmp.append(credentials.getUserPrincipal().getName());
202 tmp.append(":");
203 tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
204
205 final byte[] base64password = Base64.encodeBase64(
206 EncodingUtils.getBytes(tmp.toString(), charset), false);
207
208 final CharArrayBuffer buffer = new CharArrayBuffer(32);
209 if (proxy) {
210 buffer.append(AUTH.PROXY_AUTH_RESP);
211 } else {
212 buffer.append(AUTH.WWW_AUTH_RESP);
213 }
214 buffer.append(": Basic ");
215 buffer.append(base64password, 0, base64password.length);
216
217 return new BufferedHeader(buffer);
218 }
219
220 }