/
BasicAuthHttpServiceFilter.java
415 lines (379 loc) · 19.9 KB
/
BasicAuthHttpServiceFilter.java
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
/*
* Copyright © 2018, 2021 Apple Inc. and the ServiceTalk project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.servicetalk.http.utils.auth;
import io.servicetalk.concurrent.api.AsyncCloseable;
import io.servicetalk.concurrent.api.AsyncContext;
import io.servicetalk.concurrent.api.AsyncContextMap;
import io.servicetalk.concurrent.api.Completable;
import io.servicetalk.concurrent.api.Single;
import io.servicetalk.context.api.ContextMap;
import io.servicetalk.http.api.HttpExecutionStrategy;
import io.servicetalk.http.api.HttpExecutionStrategyInfluencer;
import io.servicetalk.http.api.HttpHeaderNames;
import io.servicetalk.http.api.HttpHeaders;
import io.servicetalk.http.api.HttpMetaData;
import io.servicetalk.http.api.HttpRequestMetaData;
import io.servicetalk.http.api.HttpServiceContext;
import io.servicetalk.http.api.StreamingHttpRequest;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpResponseFactory;
import io.servicetalk.http.api.StreamingHttpService;
import io.servicetalk.http.api.StreamingHttpServiceFilter;
import io.servicetalk.http.api.StreamingHttpServiceFilterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Iterator;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
import static io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable;
import static io.servicetalk.concurrent.api.Single.failed;
import static io.servicetalk.concurrent.api.Single.succeeded;
import static io.servicetalk.http.api.HttpExecutionStrategyInfluencer.defaultStreamingInfluencer;
import static io.servicetalk.http.api.HttpHeaderNames.AUTHORIZATION;
import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH;
import static io.servicetalk.http.api.HttpHeaderNames.PROXY_AUTHENTICATE;
import static io.servicetalk.http.api.HttpHeaderNames.PROXY_AUTHORIZATION;
import static io.servicetalk.http.api.HttpHeaderNames.WWW_AUTHENTICATE;
import static io.servicetalk.http.api.HttpHeaderValues.ZERO;
import static io.servicetalk.http.api.HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED;
import static io.servicetalk.http.api.HttpResponseStatus.UNAUTHORIZED;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
/**
* A {@link StreamingHttpServiceFilter}, which filters HTTP requests using
* <a href="https://tools.ietf.org/html/rfc7617">RFC7617</a>: The 'Basic' HTTP Authentication Scheme.
*
* @param <UserInfo> a type for authenticated user info object
* @see Builder
*/
public final class BasicAuthHttpServiceFilter<UserInfo>
implements StreamingHttpServiceFilterFactory, HttpExecutionStrategyInfluencer {
/**
* Verifies {@code user-id} and {@code password}, parsed from the 'Basic' HTTP Authentication Scheme credentials.
* <p>
* This is an {@link AutoCloseable} {@link BiFunction}, which accepts {@code user-id} and {@code password} pair and
* returns {@link Single}<{@link UserInfo}> with a user info object of authenticated user. In case of denied
* access {@link Single} must fail with {@link AuthenticationException}.
*
* @param <UserInfo> a type for authenticated user info object
*/
public interface CredentialsVerifier<UserInfo> extends BiFunction<String, String, Single<UserInfo>>,
AsyncCloseable {
/**
* Verifies {@code user-id} and {@code password}, parsed from the 'Basic' HTTP Authentication Scheme
* credentials.
*
* @param userId a {@code user-id} parsed from the authentication token
* @param password a {@code password} parsed from the authentication token
* @return {@link Single}<{@link UserInfo}> with a user info object of authenticated user or
* {@link Single} failed with an {@link AuthenticationException} if access was denied
*/
@Override
Single<UserInfo> apply(String userId, String password);
}
/**
* A builder for an {@link StreamingHttpServiceFilter}, which filters HTTP requests using <a
* href="https://tools.ietf.org/html/rfc7617">RFC7617</a>: The 'Basic' HTTP Authentication Scheme.
*
* @param <UserInfo> a type for authenticated user info object
*/
public static final class Builder<UserInfo> {
private final CredentialsVerifier<UserInfo> credentialsVerifier;
private final String realm;
@Nullable
private AsyncContextMap.Key<UserInfo> userInfoKey;
@Nullable
private ContextMap.Key<UserInfo> userInfoAsyncContextKey;
@Nullable
private ContextMap.Key<UserInfo> userInfoRequestContextKey;
private boolean utf8;
/**
* Creates a new builder for an {@link StreamingHttpServiceFilter}, which filters HTTP requests using <a
* href="https://tools.ietf.org/html/rfc7617">RFC7617</a>: The 'Basic' HTTP Authentication Scheme.
* <p>
* It accepts credentials as {@code user-id:password} pairs, encoded using {@link Base64} for {@link
* HttpHeaderNames#AUTHORIZATION Authorization} or {@link HttpHeaderNames#PROXY_AUTHORIZATION
* Proxy-Authorization} header values. Use of the format {@code user:password} in the {@link
* HttpRequestMetaData#userInfo() userinfo} field is deprecated by <a
* href="https://tools.ietf.org/html/rfc3986#section-3.2.1">RFC3986</a>.
* <p>
* User info object of authenticated user can be stored in {@link AsyncContext} if
* {@link ContextMap.Key} is configured via {@link Builder#userInfoAsyncContextKey(ContextMap.Key)} or
* {@link HttpRequestMetaData#context() request context} if {@link ContextMap.Key} is configured via
* {@link Builder#userInfoRequestContextKey(ContextMap.Key)}. The same {@link ContextMap.Key key} can be reused
* for both context storages.
* <p>
* <b>Note:</b> This scheme is not considered to be a secure method of user authentication unless used in
* conjunction with some external secure system such as TLS (Transport Layer Security, [<a
* href="https://tools.ietf.org/html/rfc5246">RFC5246</a>]), as the {@code user-id} and {@code password} are
* passed over the network as cleartext.
*
* @param credentialsVerifier a {@link CredentialsVerifier} for {@code user-id} and {@code passwords} pair
* @param realm a <a href="https://tools.ietf.org/html/rfc7235#section-2.2">protection space (realm)</a>
*/
public Builder(final CredentialsVerifier<UserInfo> credentialsVerifier,
final String realm) {
this.credentialsVerifier = requireNonNull(credentialsVerifier);
this.realm = requireNonNull(realm);
}
/**
* Sets a {@link AsyncContextMap.Key key} to store a user info object of authenticated user in
* {@link AsyncContext}.
*
* @param userInfoKey a key to store a user info object in {@link AsyncContext}
* @return {@code this}
* @deprecated Use {@link #userInfoAsyncContextKey(ContextMap.Key)}
*/
@Deprecated
public Builder<UserInfo> userInfoKey(final AsyncContextMap.Key<UserInfo> userInfoKey) {
this.userInfoKey = requireNonNull(userInfoKey);
return this;
}
/**
* Sets a {@link ContextMap.Key key} to store a user info object of authenticated user in {@link AsyncContext}.
*
* @param userInfoAsyncContextKey a key to store a user info object in {@link AsyncContext}
* @return {@code this}
*/
public Builder<UserInfo> userInfoAsyncContextKey(final ContextMap.Key<UserInfo> userInfoAsyncContextKey) {
this.userInfoAsyncContextKey = requireNonNull(userInfoAsyncContextKey);
return this;
}
/**
* Sets a {@link ContextMap.Key key} to store a user info object of authenticated user in
* {@link HttpRequestMetaData#context() request context}.
*
* @param userInfoRequestContextKey a key to store a user info object in
* {@link HttpRequestMetaData#context() request context}
* @return {@code this}
*/
public Builder<UserInfo> userInfoRequestContextKey(final ContextMap.Key<UserInfo> userInfoRequestContextKey) {
this.userInfoRequestContextKey = requireNonNull(userInfoRequestContextKey);
return this;
}
/**
* Sets an advice for a user agent to use {@link StandardCharsets#UTF_8 UTF-8} charset when it generates
* {@code user-id:password} pair.
* <p>
* It will result in adding an optional <a href="https://tools.ietf.org/html/rfc7617#section-2.1">
* charset="UTF-8"</a> parameter for an authenticate header.
*
* @param utf8 if {@code true}, an optional {@code charset="UTF-8"} parameter will be added for an authenticate
* header
* @return {@code this}
*/
public Builder<UserInfo> setCharsetUtf8(final boolean utf8) {
this.utf8 = utf8;
return this;
}
/**
* Creates a new instance for non-proxy service.
* <p>
* It will use the following constants to handle authentication:
*
* <blockquote><table>
* <caption>Response status code, authenticate and authorization headers for non-proxy Basic auth</caption>
* <tr>
* <td>Response status code</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-3.1">401 (Unauthorized)</a></td>
* </tr>
* <tr>
* <td>Authenticate header</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-4.1">WWW-Authenticate</a></td>
* </tr>
* <tr>
* <td>Authorization header</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-4.2">Authorization</a></td>
* </tr>
* </table></blockquote>
*
* @return a new {@link Builder}
*/
public StreamingHttpServiceFilterFactory buildServer() {
return new BasicAuthHttpServiceFilter<>(credentialsVerifier, realm, false, userInfoKey,
userInfoAsyncContextKey, userInfoRequestContextKey, utf8);
}
/**
* Creates a new instance for proxy service.
* <p>
* It will use the following constants to handle authentication:
*
* <blockquote><table>
* <caption>Response status code, authenticate and authorization headers for proxy Basic auth</caption>
* <tr>
* <td>Response status code</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-3.2">407 (Proxy Authentication Required)</a></td>
* </tr>
* <tr>
* <td>Authenticate header</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-4.3">Proxy-Authenticate</a></td>
* </tr>
* <tr>
* <td>Authorization header</td>
* <td><a href="https://tools.ietf.org/html/rfc7235#section-4.5">Proxy-Authorization</a></td>
* </tr>
* </table></blockquote>
*
* @return a new {@link StreamingHttpServiceFilterFactory}
*/
public StreamingHttpServiceFilterFactory buildProxy() {
return new BasicAuthHttpServiceFilter<>(credentialsVerifier, realm, true, userInfoKey,
userInfoAsyncContextKey, userInfoRequestContextKey, utf8);
}
}
private final CredentialsVerifier<UserInfo> credentialsVerifier;
private final HttpExecutionStrategyInfluencer influencer;
private final String realm;
private final boolean proxy;
@Nullable
private final AsyncContextMap.Key<UserInfo> userInfoKey;
@Nullable
private final ContextMap.Key<UserInfo> userInfoAsyncContextKey;
@Nullable
private final ContextMap.Key<UserInfo> userInfoRequestContextKey;
private final boolean utf8;
private BasicAuthHttpServiceFilter(final CredentialsVerifier<UserInfo> credentialsVerifier,
final String realm,
final boolean proxy,
@Nullable final AsyncContextMap.Key<UserInfo> userInfoKey,
@Nullable final ContextMap.Key<UserInfo> userInfoAsyncContextKey,
@Nullable final ContextMap.Key<UserInfo> userInfoRequestContextKey,
final boolean utf8) {
this.credentialsVerifier = credentialsVerifier;
this.realm = realm;
this.proxy = proxy;
this.userInfoKey = userInfoKey;
this.userInfoAsyncContextKey = userInfoAsyncContextKey;
this.userInfoRequestContextKey = userInfoRequestContextKey;
this.utf8 = utf8;
influencer = credentialsVerifier instanceof HttpExecutionStrategyInfluencer ?
(HttpExecutionStrategyInfluencer) credentialsVerifier :
defaultStreamingInfluencer();
}
@Override
public StreamingHttpServiceFilter create(final StreamingHttpService service) {
return new BasicAuthStreamingHttpService<>(this, service);
}
@Override
public HttpExecutionStrategy influenceStrategy(final HttpExecutionStrategy strategy) {
return influencer.influenceStrategy(strategy);
}
private static final class BasicAuthStreamingHttpService<UserInfo> extends StreamingHttpServiceFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(BasicAuthStreamingHttpService.class);
private static final String AUTH_SCHEME = "basic ";
private static final int AUTH_SCHEME_LENGTH = AUTH_SCHEME.length();
private final BasicAuthHttpServiceFilter<UserInfo> config;
private final String authenticateHeader;
private final AsyncCloseable closeable;
BasicAuthStreamingHttpService(final BasicAuthHttpServiceFilter<UserInfo> config,
final StreamingHttpService next) {
super(next);
this.config = config;
this.authenticateHeader = "Basic realm=\"" + config.realm + (config.utf8 ? "\", charset=\"UTF-8\"" : '"');
closeable = newCompositeCloseable().appendAll(next, config.credentialsVerifier);
}
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx,
final StreamingHttpRequest request,
final StreamingHttpResponseFactory factory) {
// Use of the format "user:password" in the userinfo field is deprecated:
// - https://tools.ietf.org/html/rfc3986#section-3.2.1
// - https://tools.ietf.org/html/rfc3986#section-7.5
// Check only "Authorization/Proxy-Authorization" headers, according to the format described in:
// - https://tools.ietf.org/html/rfc7617#section-2
// - https://tools.ietf.org/html/rfc2617#section-2
final Iterator<? extends CharSequence> authorizations = request.headers()
.valuesIterator(config.proxy ? PROXY_AUTHORIZATION : AUTHORIZATION);
String token = "";
while (authorizations.hasNext()) {
final CharSequence authorization = authorizations.next();
if (authorization.length() <= AUTH_SCHEME_LENGTH) {
continue;
}
final String strAuth = authorization.toString();
final int schemeIdx = strAuth.toLowerCase().indexOf(AUTH_SCHEME);
if (schemeIdx < 0) {
continue;
}
final int beginIdx = schemeIdx + AUTH_SCHEME_LENGTH;
final int commaIdx = strAuth.indexOf(',', beginIdx);
final int endIdx = commaIdx < 0 ? strAuth.length() : commaIdx;
if (endIdx > beginIdx) {
token = strAuth.substring(beginIdx, endIdx);
break;
}
}
if (token.isEmpty()) {
return onAccessDenied(request, factory);
}
// Because most browsers use UTF-8 encoding for usernames and passwords (see:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Character_encoding_of_HTTP_authentication)
// and RFC7617 says: the default encoding is undefined (most implementations chose UTF-8), as long as it is
// compatible with US-ASCII, we can use UTF-8 to decode a "user-id:password" pair here:
final String userIdAndPassword = new String(Base64.getDecoder().decode(token), UTF_8);
final int colonIdx = userIdAndPassword.indexOf(':');
if (colonIdx < 1) {
return onAccessDenied(request, factory);
}
final String userId = userIdAndPassword.substring(0, colonIdx);
final String password = userIdAndPassword.length() - 1 == colonIdx ? "" :
userIdAndPassword.substring(colonIdx + 1);
return config.credentialsVerifier.apply(userId, password)
.flatMap(userInfo -> onAuthenticated(ctx, request, factory, userInfo).shareContextOnSubscribe())
.onErrorResume(t -> {
if (t instanceof AuthenticationException) {
return onAccessDenied(request, factory);
}
LOGGER.debug("Unexpected exception during authentication", t);
return failed(t);
});
}
@Override
public Completable closeAsync() {
return closeable.closeAsync();
}
@Override
public Completable closeAsyncGracefully() {
return closeable.closeAsyncGracefully();
}
private Single<StreamingHttpResponse> onAccessDenied(final HttpMetaData requestMetaData,
final StreamingHttpResponseFactory factory) {
final StreamingHttpResponse response = factory.newResponse(
config.proxy ? PROXY_AUTHENTICATION_REQUIRED : UNAUTHORIZED).version(requestMetaData.version());
HttpHeaders headers = response.headers();
headers.set(config.proxy ? PROXY_AUTHENTICATE : WWW_AUTHENTICATE, authenticateHeader);
headers.set(CONTENT_LENGTH, ZERO);
return succeeded(response);
}
private Single<StreamingHttpResponse> onAuthenticated(final HttpServiceContext ctx,
final StreamingHttpRequest request,
final StreamingHttpResponseFactory factory,
final UserInfo userInfo) {
if (config.userInfoKey != null) {
AsyncContext.put(config.userInfoKey, userInfo);
}
if (config.userInfoAsyncContextKey != null) {
AsyncContext.put(config.userInfoAsyncContextKey, userInfo);
}
if (config.userInfoRequestContextKey != null) {
request.context().put(config.userInfoRequestContextKey, userInfo);
}
return delegate().handle(ctx, request, factory);
}
}
}