Skip to content

Commit 48a4644

Browse files
author
Vladimir Kotal
committed
bearer token
1 parent b118f02 commit 48a4644

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

apiary.apib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ HIFORMAT: 1A
55
OpenGrok RESTful API documentation. The following endpoints are accessible under `/api/v1` with the exception of `/metrics`.
66

77
Besides `/suggester`, `/search` and `/metrics` endpoints, everything is accessible from `localhost` only
8-
unless authentication tokens are configured in the web application and used via the 'Authorization' HTTP header
8+
unless authentication bearer tokens are configured in the web application and used via the 'Authorization' HTTP header
99
(within HTTPS connection).
1010

1111
## Annotation [/annotation{?path}]

opengrok-web/src/main/java/org/opengrok/web/api/v1/filter/IncomingFilter.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
import java.util.logging.Logger;
4949

5050
/**
51-
* This filter allows the request in case it contains the correct authentication token
51+
* This filter allows the request in case it contains the correct authentication bearer token
5252
* (needs to come in via HTTPS) or it is coming from localhost or its path matches the list
5353
* of built in paths.
54+
* If the request does not contain valid token and appears to come from localhost however is proxied
55+
* (contains either X-Forwarded-For or Forwarded HTTP headers) it is denied.
5456
*/
5557
@Provider
5658
@PreMatching
@@ -76,6 +78,8 @@ public class IncomingFilter implements ContainerRequestFilter {
7678
"127.0.0.1", "0:0:0:0:0:0:0:1", "localhost"
7779
));
7880

81+
static final String BEARER = "Bearer "; // Authorization header value prefix
82+
7983
@PostConstruct
8084
public void init() {
8185
try {
@@ -97,11 +101,14 @@ public void filter(final ContainerRequestContext context) {
97101
String path = context.getUriInfo().getPath();
98102

99103
if (request.isSecure()) {
100-
String authHeader;
101-
if (((authHeader = request.getHeader(HttpHeaders.AUTHORIZATION)) != null) &&
102-
RuntimeEnvironment.getInstance().getAuthenticationTokens().contains(authHeader)) {
103-
logger.log(Level.FINEST, "allowing request to {0} based on authentication header", path);
104-
return;
104+
String authHeaderValue;
105+
if ((authHeaderValue = request.getHeader(HttpHeaders.AUTHORIZATION)) != null &&
106+
authHeaderValue.startsWith(BEARER)) {
107+
String tokenValue = authHeaderValue.substring(BEARER.length());
108+
if (RuntimeEnvironment.getInstance().getAuthenticationTokens().contains(tokenValue)) {
109+
logger.log(Level.FINEST, "allowing request to {0} based on authentication header token", path);
110+
return;
111+
}
105112
}
106113
}
107114

opengrok-web/src/test/java/org/opengrok/web/api/v1/filter/IncomingFilterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ private void nonLocalhostTestWithToken(boolean allowed) throws Exception {
6363
RuntimeEnvironment.getInstance().setAuthenticationTokens(tokens);
6464

6565
Map<String, String> headers = new TreeMap<>();
66-
headers.put(HttpHeaders.AUTHORIZATION, allowed ? allowedToken : allowedToken + "_");
66+
final String authHeaderValue = IncomingFilter.BEARER + allowedToken;
67+
headers.put(HttpHeaders.AUTHORIZATION, allowed ? authHeaderValue : authHeaderValue + "_");
6768
IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
6869

6970
ContainerRequestContext context = mockContainerRequestContext("test");

0 commit comments

Comments
 (0)