diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java index e2bc7694da..c7df34b603 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpService.java @@ -27,6 +27,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcNoResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponseType; +import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcUnauthorizedResponse; import tech.pegasys.pantheon.metrics.LabelledMetric; import tech.pegasys.pantheon.metrics.MetricCategory; import tech.pegasys.pantheon.metrics.MetricsSystem; @@ -230,7 +231,7 @@ private Handler checkWhitelistHostHeader() { }; } - private boolean requiresAuthentication(final RoutingContext routingContext) { + private boolean requiresAuthentication() { return authenticationService.isPresent(); } @@ -239,24 +240,26 @@ public boolean isPermitted(final Optional optionalUser, final JsonRpcMetho AtomicBoolean foundMatchingPermission = new AtomicBoolean(); - if (optionalUser.isPresent()) { - User user = optionalUser.get(); - for (String perm : jsonRpcMethod.getPermissions()) { - user.isAuthorized( - perm, - (authed) -> { - if (authed.result()) { - LOG.trace( - "user {} authorized : {} via permission {}", - user, - jsonRpcMethod.getName(), - perm); - foundMatchingPermission.set(true); - } - }); + if (requiresAuthentication()) { + if (optionalUser.isPresent()) { + User user = optionalUser.get(); + for (String perm : jsonRpcMethod.getPermissions()) { + user.isAuthorized( + perm, + (authed) -> { + if (authed.result()) { + LOG.trace( + "user {} authorized : {} via permission {}", + user, + jsonRpcMethod.getName(), + perm); + foundMatchingPermission.set(true); + } + }); + } } } else { - // no user means no auth provider configured thus anything is permitted + // no auth provider configured thus anything is permitted foundMatchingPermission.set(true); } @@ -272,7 +275,7 @@ private String getToken(final RoutingContext routingContext) { private void getUser(final String token, final Handler> handler) { try { - if (!authenticationService.isPresent()) { + if (!requiresAuthentication()) { handler.handle(Optional.empty()); } else { authenticationService @@ -343,7 +346,7 @@ public String url() { private void handleJsonRPCRequest(final RoutingContext routingContext) { // first check token if authentication is required String token = getToken(routingContext); - if (requiresAuthentication(routingContext) && token == null) { + if (requiresAuthentication() && token == null) { // no auth token when auth required handleJsonRpcUnauthorizedError(routingContext, null, JsonRpcError.UNAUTHORIZED); } else { @@ -404,6 +407,8 @@ private void handleJsonSingleRequest( private HttpResponseStatus status(final JsonRpcResponse response) { switch (response.getType()) { + case UNAUTHORIZED: + return HttpResponseStatus.UNAUTHORIZED; case ERROR: return HttpResponseStatus.BAD_REQUEST; case SUCCESS: @@ -506,7 +511,7 @@ private JsonRpcResponse process(final JsonObject requestJson, final Optional token) { + final Request.Builder request = new Request.Builder().post(body).url(baseUrl); + token.ifPresent(t -> request.addHeader("Bearer", t)); + return request.build(); } }