Skip to content

Commit e3d8d43

Browse files
agherardialessandro.gherardi
authored andcommitted
Exclude query parameters when caching authentication info
Signed-off-by: agherardi <[email protected]>
1 parent dc8d4e7 commit e3d8d43

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client.authentication;
18+
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
22+
import javax.ws.rs.client.ClientRequestContext;
23+
24+
/**
25+
* Common authentication utilities
26+
*/
27+
class AuthenticationUtil {
28+
static URI getCacheKey(ClientRequestContext request) {
29+
URI requestUri = request.getUri();
30+
if (requestUri.getRawQuery() != null) {
31+
// Return a URI without the query part of the request URI
32+
try {
33+
return new URI(
34+
requestUri.getScheme(),
35+
requestUri.getAuthority(),
36+
requestUri.getPath(),
37+
null,
38+
requestUri.getFragment());
39+
} catch (URISyntaxException e) {
40+
// Ignore and fall through
41+
}
42+
}
43+
return requestUri;
44+
}
45+
}

core-client/src/main/java/org/glassfish/jersey/client/authentication/DigestAuthenticator.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected boolean removeEldestEntry(final Map.Entry eldest) {
9191
* @throws IOException When error with encryption occurs.
9292
*/
9393
boolean filterRequest(final ClientRequestContext request) throws IOException {
94-
final DigestScheme digestScheme = digestCache.get(getCacheKey(request));
94+
final DigestScheme digestScheme = digestCache.get(AuthenticationUtil.getCacheKey(request));
9595
if (digestScheme != null) {
9696
final HttpAuthenticationFilter.Credentials cred = HttpAuthenticationFilter.getCredentials(request,
9797
this.credentials, HttpAuthenticationFilter.Type.DIGEST);
@@ -132,7 +132,7 @@ public boolean filterResponse(final ClientRequestContext request, final ClientRe
132132

133133
final boolean success = HttpAuthenticationFilter.repeatRequest(request, response, createNextAuthToken(digestScheme,
134134
request, cred));
135-
URI cacheKey = getCacheKey(request);
135+
URI cacheKey = AuthenticationUtil.getCacheKey(request);
136136
if (success) {
137137
digestCache.put(cacheKey, digestScheme);
138138
} else {
@@ -351,24 +351,6 @@ private String randomBytes(final int nbBytes) {
351351
return bytesToHex(bytes);
352352
}
353353

354-
private URI getCacheKey(ClientRequestContext request) {
355-
URI requestUri = request.getUri();
356-
if (requestUri.getRawQuery() != null) {
357-
// Return a URI without the query part of the request URI
358-
try {
359-
return new URI(
360-
requestUri.getScheme(),
361-
requestUri.getAuthority(),
362-
requestUri.getPath(),
363-
null,
364-
requestUri.getFragment());
365-
} catch (URISyntaxException e) {
366-
// Ignore and fall through
367-
}
368-
}
369-
return requestUri;
370-
}
371-
372354
private enum QOP {
373355

374356
UNSPECIFIED(null),

core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFilter.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,22 +246,7 @@ public void filter(ClientRequestContext request, ClientResponseContext response)
246246
}
247247

248248
private String getCacheKey(ClientRequestContext request) {
249-
URI requestUri = request.getUri();
250-
if (requestUri.getRawQuery() != null) {
251-
// Build a URI without the query part of the request URI
252-
try {
253-
URI requestUriWithoutQuery = new URI(
254-
requestUri.getScheme(),
255-
requestUri.getAuthority(),
256-
requestUri.getPath(),
257-
null,
258-
requestUri.getFragment());
259-
return requestUriWithoutQuery.toString() + ":" + request.getMethod();
260-
} catch (URISyntaxException e) {
261-
// Ignore and fall through
262-
}
263-
}
264-
return requestUri.toString() + ":" + request.getMethod();
249+
return AuthenticationUtil.getCacheKey(request).toString() + ":" + request.getMethod();
265250
}
266251

267252
private void updateCache(ClientRequestContext request, boolean success, Type operation) {

0 commit comments

Comments
 (0)