From ce442bf7d096ca9fb237f4eb402a8eb2ef443564 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:37:49 -0700 Subject: [PATCH 1/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 7108c755f038..3488504e4bdb 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1309,6 +1309,7 @@ impl RpcServerConfig { Ipv4Addr::LOCALHOST, DEFAULT_HTTP_RPC_PORT, ))); + let jwt_secret = self.jwt_secret.clone().unwrap(); let ws_socket_addr = self .ws_addr @@ -1355,6 +1356,7 @@ impl RpcServerConfig { http_local_addr: Some(addr), ws_local_addr: Some(addr), server: WsHttpServers::SamePort(server), + jwt_secret: Some(jwt_secret), }) } @@ -1397,6 +1399,7 @@ impl RpcServerConfig { http_local_addr, ws_local_addr, server: WsHttpServers::DifferentPort { http: http_server, ws: ws_server }, + jwt_secret: Some(jwt_secret), }) } @@ -1616,6 +1619,8 @@ struct WsHttpServer { ws_local_addr: Option, /// Configured ws,http servers server: WsHttpServers, + /// The jwt secret. + jwt_secret: Option, } /// Enum for holding the http and ws servers in all possible combinations. @@ -1790,6 +1795,10 @@ impl RpcServer { pub fn http_local_addr(&self) -> Option { self.ws_http.http_local_addr } + /// Return the JwtSecret of the the server + pub fn thing(&self) -> Option { + self.ws_http.jwt_secret.clone() + } /// Returns the [`SocketAddr`] of the ws server if started. pub fn ws_local_addr(&self) -> Option { @@ -1817,6 +1826,7 @@ impl RpcServer { ws: None, ipc_endpoint: None, ipc: None, + jwt_secret: None, }; let (http, ws) = ws_http.server.start(http, ws, &config).await?; @@ -1857,11 +1867,17 @@ pub struct RpcServerHandle { ws: Option, ipc_endpoint: Option, ipc: Option, + jwt_secret: Option, } // === impl RpcServerHandle === impl RpcServerHandle { + /// Configures the JWT secret for authentication. + pub fn with_jwt_secret(mut self, secret: Option) -> Self { + self.jwt_secret = secret; + self + } /// Returns the [`SocketAddr`] of the http server if started. pub fn http_local_addr(&self) -> Option { self.http_local_addr From 38874339a8a45f25ba6b0a3241223bb9c54cedf9 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:49:27 -0700 Subject: [PATCH 2/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 3488504e4bdb..d2e9d14a4b1d 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1619,7 +1619,7 @@ struct WsHttpServer { ws_local_addr: Option, /// Configured ws,http servers server: WsHttpServers, - /// The jwt secret. + /// The jwt secret jwt_secret: Option, } From 1b3747bfe42c664fc3fbf5849dc472ced94ec418 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:23:28 -0700 Subject: [PATCH 3/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index d2e9d14a4b1d..c69c708dc207 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1309,7 +1309,8 @@ impl RpcServerConfig { Ipv4Addr::LOCALHOST, DEFAULT_HTTP_RPC_PORT, ))); - let jwt_secret = self.jwt_secret.clone().unwrap(); + let jwt_secret = self.jwt_secret.clone().expect("Expected JWT."); + let ws_socket_addr = self .ws_addr From c5637e6678c4fc368cc990ce8dc4bb465afb32ac Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:29:20 -0700 Subject: [PATCH 4/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index c69c708dc207..bc048f648b85 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1309,8 +1309,11 @@ impl RpcServerConfig { Ipv4Addr::LOCALHOST, DEFAULT_HTTP_RPC_PORT, ))); - let jwt_secret = self.jwt_secret.clone().expect("Expected JWT."); - + // let jwt_secret = self.jwt_secret.clone().expect("Expected JWT secret to be set."); + let jwt_secret = self + .jwt_secret + .clone() + .ok_or_else(|| RpcError::Custom("JWT Secret is missing".into()))?; let ws_socket_addr = self .ws_addr @@ -1620,7 +1623,7 @@ struct WsHttpServer { ws_local_addr: Option, /// Configured ws,http servers server: WsHttpServers, - /// The jwt secret + /// The jwt secret. jwt_secret: Option, } From 02be11aa45ada0de1c2368814ee8f0cbda4e5b4d Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:44:56 -0700 Subject: [PATCH 5/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 51 ++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index bc048f648b85..20a11831a217 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -98,10 +98,10 @@ #![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)] #![deny(unused_must_use, rust_2018_idioms)] #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] - use crate::{auth::AuthRpcModule, error::WsHttpSamePortError, metrics::RpcServerMetrics}; use constants::*; use error::{RpcError, ServerKind}; +use hyper::{header::AUTHORIZATION, HeaderMap}; use jsonrpsee::{ server::{IdProvider, Server, ServerHandle}, Methods, RpcModule, @@ -117,8 +117,8 @@ use reth_rpc::{ cache::{cache_new_blocks_task, EthStateCache}, gas_oracle::GasPriceOracle, }, - AdminApi, AuthLayer, BlockingTaskGuard, BlockingTaskPool, DebugApi, EngineEthApi, EthApi, - EthFilter, EthPubSub, EthSubscriptionIdProvider, JwtAuthValidator, JwtSecret, NetApi, + AdminApi, AuthLayer, BlockingTaskGuard, BlockingTaskPool, Claims, DebugApi, EngineEthApi, + EthApi, EthFilter, EthPubSub, EthSubscriptionIdProvider, JwtAuthValidator, JwtSecret, NetApi, OtterscanApi, RPCApi, RethApi, TraceApi, TxPoolApi, Web3Api, }; use reth_rpc_api::{servers::*, EngineApiServer}; @@ -130,6 +130,7 @@ use std::{ fmt, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, str::FromStr, + time::{Duration, SystemTime, UNIX_EPOCH}, }; use strum::{AsRefStr, EnumString, EnumVariantNames, ParseError, VariantNames}; use tower::layer::util::{Identity, Stack}; @@ -1878,9 +1879,20 @@ pub struct RpcServerHandle { impl RpcServerHandle { /// Configures the JWT secret for authentication. - pub fn with_jwt_secret(mut self, secret: Option) -> Self { - self.jwt_secret = secret; - self + fn bearer_token(&self) -> Option { + self.jwt_secret.as_ref().map(|secret| { + format!( + "Bearer {}", + secret + .encode(&Claims { + iat: (SystemTime::now().duration_since(UNIX_EPOCH).unwrap() + + Duration::from_secs(60)) + .as_secs(), + exp: None, + }) + .unwrap() + ) + }) } /// Returns the [`SocketAddr`] of the http server if started. pub fn http_local_addr(&self) -> Option { @@ -1927,19 +1939,28 @@ impl RpcServerHandle { /// Returns a http client connected to the server. pub fn http_client(&self) -> Option { let url = self.http_url()?; - let client = jsonrpsee::http_client::HttpClientBuilder::default() - .build(url) - .expect("Failed to create http client"); - Some(client) - } + let client = if let Some(token) = self.bearer_token() { + jsonrpsee::http_client::HttpClientBuilder::default() + .set_headers(HeaderMap::from_iter([(AUTHORIZATION, token.parse().unwrap())])) + .build(url) + } else { + jsonrpsee::http_client::HttpClientBuilder::default().build(url) + }; + + client.expect("failed to create http client").into() + } /// Returns a ws client connected to the server. pub async fn ws_client(&self) -> Option { let url = self.ws_url()?; - let client = jsonrpsee::ws_client::WsClientBuilder::default() - .build(url) - .await - .expect("Failed to create ws client"); + let mut builder = jsonrpsee::ws_client::WsClientBuilder::default(); + + if let Some(token) = self.bearer_token() { + let headers = http::HeaderMap::from_iter([(AUTHORIZATION, token.parse().unwrap())]); + builder = builder.set_headers(headers); + } + + let client = builder.build(url).await.expect("failed to create ws client"); Some(client) } } From 9bc8811902579f580630072d52f42384819641bc Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:12:08 -0700 Subject: [PATCH 6/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 20a11831a217..6b4cfe62f16c 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1801,7 +1801,7 @@ impl RpcServer { self.ws_http.http_local_addr } /// Return the JwtSecret of the the server - pub fn thing(&self) -> Option { + pub fn jwt(&self) -> Option { self.ws_http.jwt_secret.clone() } From 0e7c165fed110222aab684693fc0f89d67656e59 Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:16:32 -0700 Subject: [PATCH 7/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 6b4cfe62f16c..74fd6a6675f9 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1956,7 +1956,7 @@ impl RpcServerHandle { let mut builder = jsonrpsee::ws_client::WsClientBuilder::default(); if let Some(token) = self.bearer_token() { - let headers = http::HeaderMap::from_iter([(AUTHORIZATION, token.parse().unwrap())]); + let headers = HeaderMap::from_iter([(AUTHORIZATION, token.parse().unwrap())]); builder = builder.set_headers(headers); } From c7d3a6521bf7076fd8989fc3853d8c921bdc3cbb Mon Sep 17 00:00:00 2001 From: DoTheBestToGetTheBest <146037313+DoTheBestToGetTheBest@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:29:24 -0700 Subject: [PATCH 8/8] Update lib.rs --- crates/rpc/rpc-builder/src/lib.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/rpc/rpc-builder/src/lib.rs b/crates/rpc/rpc-builder/src/lib.rs index 74fd6a6675f9..f97db9107a37 100644 --- a/crates/rpc/rpc-builder/src/lib.rs +++ b/crates/rpc/rpc-builder/src/lib.rs @@ -1310,11 +1310,7 @@ impl RpcServerConfig { Ipv4Addr::LOCALHOST, DEFAULT_HTTP_RPC_PORT, ))); - // let jwt_secret = self.jwt_secret.clone().expect("Expected JWT secret to be set."); - let jwt_secret = self - .jwt_secret - .clone() - .ok_or_else(|| RpcError::Custom("JWT Secret is missing".into()))?; + let jwt_secret = self.jwt_secret.clone(); let ws_socket_addr = self .ws_addr @@ -1361,7 +1357,7 @@ impl RpcServerConfig { http_local_addr: Some(addr), ws_local_addr: Some(addr), server: WsHttpServers::SamePort(server), - jwt_secret: Some(jwt_secret), + jwt_secret, }) } @@ -1404,7 +1400,7 @@ impl RpcServerConfig { http_local_addr, ws_local_addr, server: WsHttpServers::DifferentPort { http: http_server, ws: ws_server }, - jwt_secret: Some(jwt_secret), + jwt_secret, }) }