You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2021/07/31 04:40:10 UTC
[incubator-tuweni] branch main updated: Pass credentials to endpoint
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/main by this push:
new 3b4da3c Pass credentials to endpoint
new 407d1eb Merge pull request #328 from atoulme/add_basic_auth
3b4da3c is described below
commit 3b4da3cb2bd43f7e6b004cf31a2a229c1a0b65da
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Fri Jul 30 21:00:28 2021 -0700
Pass credentials to endpoint
---
.../org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt | 2 +-
.../org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt | 7 ++++++
.../org/apache/tuweni/jsonrpc/JSONRPCClient.kt | 27 ++++++++++++++--------
3 files changed, 26 insertions(+), 10 deletions(-)
diff --git a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
index c835a9e..2cc0453 100644
--- a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
+++ b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
@@ -74,7 +74,7 @@ class JSONRPCApplication(
) {
fun run() {
- val client = JSONRPCClient(vertx, config.endpointPort(), config.endpointHost())
+ val client = JSONRPCClient(vertx, config.endpointPort(), config.endpointHost(), basicAuthenticationEnabled = config.endpointBasicAuthEnabled(), basicAuthenticationUsername = config.endpointBasicAuthUsername(), basicAuthenticationPassword = config.endpointBasicAuthPassword())
// TODO allow more options such as allowlist of certificates, enforce client authentication.
val trustOptions = VertxTrustOptions.recordClientFingerprints(config.clientFingerprintsFile())
diff --git a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt
index 4ba4122..8cd38d8 100644
--- a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt
+++ b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCConfig.kt
@@ -49,6 +49,9 @@ class JSONRPCConfig(val filePath: Path) {
.addListOfString("rejectedRanges", Collections.emptyList(), "Rejected IP ranges", null)
.addInteger("endpointPort", 8545, "JSON-RPC endpoint port", PropertyValidator.isValidPort())
.addString("endpointHost", "localhost", "JSON-RPC endpoint host", null)
+ .addBoolean("endpointBasicAuthEnabled", false, "Enable basic authentication for the endpoint", null)
+ .addString("endpointBasicAuthUsername", "", "Basic authentication username for the endpoint", null)
+ .addString("endpointBasicAuthPassword", "", "Basic authentication password for the endpoint", null)
.toSchema()
}
@@ -74,4 +77,8 @@ class JSONRPCConfig(val filePath: Path) {
fun endpointPort() = config.getInteger("endpointPort")
fun endpointHost() = config.getString("endpointHost")
+
+ fun endpointBasicAuthEnabled() = config.getBoolean("endpointBasicAuthEnabled")
+ fun endpointBasicAuthUsername() = config.getString("endpointBasicAuthUsername")
+ fun endpointBasicAuthPassword() = config.getString("endpointBasicAuthPassword")
}
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
index 045f28d..8389388 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
@@ -30,6 +30,7 @@ import org.apache.tuweni.eth.JSONRPCResponse
import org.apache.tuweni.eth.Transaction
import org.apache.tuweni.units.bigints.UInt256
import java.io.Closeable
+import java.util.Base64
import java.util.concurrent.atomic.AtomicInteger
val mapper = ObjectMapper()
@@ -42,24 +43,32 @@ class JSONRPCClient(
val serverPort: Int,
val serverHost: String,
val userAgent: String = "Apache Tuweni JSON-RPC Client",
+ val basicAuthenticationEnabled: Boolean = false,
+ val basicAuthenticationUsername: String = "",
+ val basicAuthenticationPassword: String = ""
) : Closeable {
val requestCounter = AtomicInteger(1)
val client = WebClient.create(vertx, WebClientOptions().setUserAgent(userAgent).setTryUseCompression(true).setTracingPolicy(TracingPolicy.ALWAYS) as WebClientOptions)
+ val authorizationHeader = "Basic " + Base64.getEncoder().encode((basicAuthenticationUsername + ":" + basicAuthenticationPassword).toByteArray())
suspend fun sendRequest(request: JSONRPCRequest): Deferred<JSONRPCResponse> {
val deferred = CompletableDeferred<JSONRPCResponse>()
-
- client.post(serverPort, serverHost, "/")
+ val httpRequest = client.post(serverPort, serverHost, "/")
.putHeader("Content-Type", "application/json")
- .sendBuffer(Buffer.buffer(mapper.writeValueAsBytes(request))) { response ->
- if (response.failed()) {
- deferred.completeExceptionally(response.cause())
- } else {
- val jsonResponse = response.result().bodyAsJson(JSONRPCResponse::class.java)
- deferred.complete(jsonResponse)
- }
+
+ if (basicAuthenticationEnabled) {
+ httpRequest.putHeader("authorization", authorizationHeader)
+ }
+
+ httpRequest.sendBuffer(Buffer.buffer(mapper.writeValueAsBytes(request))) { response ->
+ if (response.failed()) {
+ deferred.completeExceptionally(response.cause())
+ } else {
+ val jsonResponse = response.result().bodyAsJson(JSONRPCResponse::class.java)
+ deferred.complete(jsonResponse)
}
+ }
return deferred
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org