-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jwt authentication for RPC (#302)
**Motivation** Json RPC calls to the engine execution API should be authenticated by bearing a JWT token as specified https://github.com/ethereum/execution-apis/blob/main/src/engine/authentication.md This is to validate that authrpc calls are only issued by the consensus layer and prevents attack which would come from accidentally exposing the execution client to the internet. **Description** Introduces authentication.rs module which uses the jsonwebtoken crate to decode and validate tokens issued by the consensus layer. The tokens contain a "iat" claim which stands for "issued at timestamp", according to the spec, this unix timestamp must be at most 60 seconds from the time of validation. For this PR to pass the CI tests, this one needs to be merged first on lambdaclass/hive to enable the testing of the authentication by Hive: https://github.com/lambdaclass/hive/pull/2/files To enable authentication on our Kurtosis localnet this needs to be merged: https://github.com/lambdaclass/ethereum-package/pull/2/files Closes #13
- Loading branch information
1 parent
4754356
commit a6d2446
Showing
7 changed files
with
173 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use bytes::Bytes; | ||
use jsonwebtoken::{decode, Algorithm, DecodingKey, TokenData, Validation}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
pub enum AuthenticationError { | ||
InvalidIssuedAtClaim, | ||
TokenDecodingError, | ||
} | ||
|
||
// JWT claims struct | ||
#[derive(Debug, Serialize, Deserialize)] | ||
struct Claims { | ||
iat: usize, | ||
id: Option<String>, | ||
clv: Option<String>, | ||
} | ||
|
||
/// Authenticates bearer jwt to check that authrpc calls are sent by the consensus layer | ||
pub fn validate_jwt_authentication(token: &str, secret: Bytes) -> Result<(), AuthenticationError> { | ||
let decoding_key = DecodingKey::from_secret(&secret); | ||
let mut validation = Validation::new(Algorithm::HS256); | ||
validation.validate_exp = false; | ||
validation.set_required_spec_claims(&["iat"]); | ||
match decode::<Claims>(token, &decoding_key, &validation) { | ||
Ok(token_data) => { | ||
if invalid_issued_at_claim(token_data) { | ||
Err(AuthenticationError::InvalidIssuedAtClaim) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
Err(_) => Err(AuthenticationError::TokenDecodingError), | ||
} | ||
} | ||
|
||
/// Checks that the "iat" timestamp in the claim is less than 60 seconds from now | ||
fn invalid_issued_at_claim(token_data: TokenData<Claims>) -> bool { | ||
let now = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() as usize; | ||
(now as isize - token_data.claims.iat as isize).abs() > 60 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters