Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

[PAN-2313] Fix authentication header #891

Merged
merged 3 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private JsonRequestFactories jsonRequestFactories() {
final String url = wsRpcBaseUrl().orElse("ws://" + LOCALHOST + ":" + 8546);
final Map<String, String> headers = new HashMap<>();
if (token != null) {
headers.put("Bearer", token);
headers.put("Authorization", "Bearer " + token);
}
final WebSocketClient wsClient = new WebSocketClient(URI.create(url), headers);

Expand All @@ -220,7 +220,7 @@ private JsonRequestFactories jsonRequestFactories() {
.map(HttpService::new)
.orElse(new HttpService("http://" + LOCALHOST + ":" + 8545));
if (token != null) {
((HttpService) web3jService).addHeader("Bearer", token);
((HttpService) web3jService).addHeader("Authorization", "Bearer " + token);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ private Handler<RoutingContext> checkWhitelistHostHeader() {
}

private String getAuthToken(final RoutingContext routingContext) {
return routingContext.request().getHeader("Bearer");
return AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(
routingContext.request().getHeader("Authorization"));
}

private Optional<String> getAndValidateHostHeader(final RoutingContext event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,14 @@ public static void getUser(
handler.handle(Optional.empty());
}
}

public static String getJwtTokenFromAuthorizationHeaderValue(String value) {
if (value != null) {
final String bearerSchemaName = "Bearer ";
if (value.startsWith(bearerSchemaName)) {
return value.substring(bearerSchemaName.length());
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private String socketAddressAsString(final SocketAddress socketAddress) {
}

private String getAuthToken(final ServerWebSocket websocket) {
return websocket.headers().get("Bearer");
return AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(
websocket.headers().get("Authorization"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private Request buildPostRequest(final RequestBody body, final String token) {

private Request buildPostRequest(final RequestBody body, final Optional<String> token) {
final Request.Builder request = new Request.Builder().post(body).url(baseUrl);
token.ifPresent(t -> request.addHeader("Bearer", t));
token.ifPresent(t -> request.addHeader("Authorization", "Bearer " + t));
return request.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 ConsenSys AG.
*
* 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 tech.pegasys.pantheon.ethereum.jsonrpc.authentication;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

public class AuthenticationUtilsTest {

@Test
public void getJwtTokenFromNullStringShouldReturnNull() {
final String headerValue = null;

final String token = AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(headerValue);

assertThat(token).isNull();
}

@Test
public void getJwtTokenFromEmptyStringShouldReturnNull() {
final String headerValue = "";

final String token = AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(headerValue);

assertThat(token).isNull();
}

@Test
public void getJwtTokenFromInvalidAuthorizationHeaderValueShouldReturnNull() {
final String headerValue = "Foo eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9";

final String token = AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(headerValue);

assertThat(token).isNull();
}

@Test
public void getJwtTokenFromValidAuthorizationHeaderValueShouldReturnToken() {
final String headerValue = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9";

final String token = AuthenticationUtils.getJwtTokenFromAuthorizationHeaderValue(headerValue);

assertThat(token).isEqualTo("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void websocketServiceWithBadHeaderAuthenticationToken(final TestContext c
final MultiMap headers = new VertxHttpHeaders();
String badtoken = "badtoken";
if (badtoken != null) {
headers.add("Bearer", badtoken);
headers.add("Authorization", "Bearer " + badtoken);
}
httpClient.websocket(
options,
Expand Down Expand Up @@ -225,7 +225,7 @@ public void websocketServiceWithGoodHeaderAuthenticationToken(final TestContext
options.setPort(websocketConfiguration.getPort());
final MultiMap headers = new VertxHttpHeaders();
if (goodToken != null) {
headers.add("Bearer", goodToken);
headers.add("Authorization", "Bearer " + goodToken);
}
httpClient.websocket(
options,
Expand Down