-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add jwt_signature_pk_urls
to state
#866
Open
esaminu
wants to merge
5
commits into
develop
Choose a base branch
from
multiple-jwks-urls
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d95dfd
feat: add jwt_signature_pk_urls to state
esaminu 5966538
fix: integration tests value
esaminu 2c2b585
feat: change `jwt_signature_pk_urls` to HashMap
esaminu 53585bd
fix: integration test variable
esaminu 0f072c0
chore: cargo fmt
esaminu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
use anyhow::{Context, Result}; | ||
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _}; | ||
use jsonwebtoken::{Algorithm, DecodingKey}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
|
||
use crate::firewall::allowed::OidcProviderList; | ||
|
@@ -13,15 +16,21 @@ pub async fn verify_oidc_token( | |
token: &OidcToken, | ||
oidc_providers: Option<&OidcProviderList>, | ||
client: &reqwest::Client, | ||
jwt_signature_pk_url: &str, | ||
jwt_signature_pk_urls: &HashMap<String, String>, | ||
) -> anyhow::Result<IdTokenClaims> { | ||
let public_keys = get_pagoda_firebase_public_keys(client, jwt_signature_pk_url) | ||
let (_, claims, _) = token.decode_unverified()?; | ||
let issuer = &claims.iss; | ||
|
||
let jwks_url = jwt_signature_pk_urls.get(issuer) | ||
.ok_or_else(|| anyhow::anyhow!("No JWKS URL found for issuer: {}", issuer))?; | ||
|
||
let public_keys = get_public_keys(client, jwks_url) | ||
.await | ||
.map_err(|e| anyhow::anyhow!("failed to get Firebase public key: {e}"))?; | ||
tracing::info!("verify_oidc_token firebase public keys: {public_keys:?}"); | ||
.map_err(|e| anyhow::anyhow!("failed to get public keys: {e}"))?; | ||
tracing::info!("verify_oidc_token public keys: {public_keys:?}"); | ||
|
||
let mut last_occured_error = | ||
anyhow::anyhow!("Unexpected error. Firebase public keys not found"); | ||
anyhow::anyhow!("Unexpected error. Public keys not found"); | ||
for public_key in public_keys { | ||
match validate_jwt(token, public_key.as_bytes(), oidc_providers) { | ||
Ok(claims) => { | ||
|
@@ -99,13 +108,49 @@ impl IdTokenClaims { | |
} | ||
} | ||
|
||
pub async fn get_pagoda_firebase_public_keys( | ||
client: &reqwest::Client, | ||
jwt_signature_pk_url: &str, | ||
) -> anyhow::Result<Vec<String>> { | ||
let response = client.get(jwt_signature_pk_url).send().await?; | ||
let json: HashMap<String, String> = response.json().await?; | ||
Ok(json.into_values().collect()) | ||
pub async fn get_public_keys(client: &reqwest::Client, jwks_url: &str) -> Result<Vec<String>> { | ||
let response = client | ||
.get(jwks_url) | ||
.send() | ||
.await | ||
.context("Failed to send request")?; | ||
|
||
let json: Value = response.json().await.context("Failed to parse JSON")?; | ||
|
||
match json { | ||
Value::Object(obj) if obj.contains_key("keys") => parse_jwks_format(&obj), | ||
Value::Object(obj) => parse_firebase_format(&obj), | ||
_ => { | ||
tracing::warn!("Unexpected response format from {}", jwks_url); | ||
Ok(vec![]) | ||
} | ||
} | ||
} | ||
|
||
fn parse_jwks_format(obj: &serde_json::Map<String, Value>) -> Result<Vec<String>> { | ||
obj["keys"] | ||
.as_array() | ||
.context("'keys' is not an array")? | ||
.iter() | ||
.filter_map(|key| match (key["n"].as_str(), key["e"].as_str()) { | ||
(Some(n), Some(e)) => Some(format_rsa_key(n, e)), | ||
_ => None, | ||
}) | ||
.collect::<Result<Vec<_>>>() | ||
} | ||
|
||
fn parse_firebase_format(obj: &serde_json::Map<String, Value>) -> Result<Vec<String>> { | ||
Ok(obj | ||
.values() | ||
.filter_map(|value| value.as_str().map(String::from)) | ||
.collect()) | ||
} | ||
|
||
fn format_rsa_key(n: &str, e: &str) -> Result<String> { | ||
Ok(format!( | ||
"-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----", | ||
BASE64.encode(format!("{}:{}", n, e)) | ||
)) | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -124,7 +169,8 @@ mod tests { | |
let url = | ||
"https://www.googleapis.com/robot/v1/metadata/x509/[email protected]"; | ||
let client = reqwest::Client::new(); | ||
let pk = get_pagoda_firebase_public_keys(&client, url).await.unwrap(); | ||
let pk = get_public_keys(&client, url).await.unwrap(); | ||
|
||
assert!(!pk.is_empty()); | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's test this