-
Notifications
You must be signed in to change notification settings - Fork 2
Adding support for jwt token authorization authentication with a central Jwks #131
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
Open
dawid-nowak
wants to merge
7
commits into
main
Choose a base branch
from
dawid.nowak/adding_jwt_token_authorization_authentication
base: main
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.
+1,203
−627
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bc255d
Adding JWKS based AuthN and AuthZ
dawid-nowak 9b804aa
Adding JWKS based AuthN and AuthZ
dawid-nowak 9f6bb4f
Adding JWKS based AuthN and AuthZ
dawid-nowak 1169a52
Extracting principal from claims
dawid-nowak 3cad283
Fixing build problems
dawid-nowak 47e8a3d
Fixing build and test problems
dawid-nowak 4f3566a
Fixing the missign verification key
dawid-nowak 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
182 changes: 182 additions & 0 deletions
182
crates/contextforge-data-plane-lib/src/authorization/jwks/jwks.rs
This file contains hidden or 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,182 @@ | ||
| use std::time::Duration; | ||
|
|
||
| use futures::StreamExt as _; | ||
| use jsonwebtoken::{ | ||
| Algorithm, AlgorithmFamily, DecodingKey, Header, Validation, decode, | ||
| jwk::{Jwk, JwkSet, KeyOperations, PublicKeyUse}, | ||
| }; | ||
| use lru_time_cache::LruCache; | ||
|
|
||
| use reqwest::Url; | ||
| use serde_json::Value; | ||
| use tokio::sync::RwLock; | ||
| use tracing::debug; | ||
| use typed_builder::TypedBuilder; | ||
|
|
||
| use crate::authorization::{ | ||
| AuthorizationClaims, AuthorizationError, | ||
| jwks::principal::{DefaultPrincipalExtractor, PrincipalExtractor}, | ||
| }; | ||
|
|
||
| pub const JWKS_CACHE_TTL: Duration = Duration::from_mins(5); | ||
| pub const JWKS_CACHE_KEY: &str = "jwks"; | ||
|
|
||
| const JWKS_MAX_RESPONSE_BYTES: usize = 1024 * 1024; | ||
|
|
||
| #[derive(TypedBuilder)] | ||
| pub(super) struct Jwks<T> | ||
| where | ||
| T: PrincipalExtractor, | ||
| { | ||
| client: reqwest::Client, | ||
| url: Url, | ||
| #[builder(default = RwLock::new(LruCache::with_expiry_duration(JWKS_CACHE_TTL)))] | ||
| cache: RwLock<LruCache<String, Vec<VerificationKey>>>, | ||
| #[builder(default = false)] | ||
| validate_audience: bool, | ||
| #[builder(default = true)] | ||
| validate_expiry: bool, | ||
| #[builder(default = true)] | ||
| validate_not_before: bool, | ||
| principal_extractor: T, | ||
| } | ||
|
|
||
| impl Jwks<DefaultPrincipalExtractor> { | ||
| fn validation(&self, alg: Algorithm) -> Validation { | ||
| let mut validation = Validation::new(alg); | ||
| validation.required_spec_claims.clear(); | ||
| validation.validate_aud = self.validate_audience; | ||
| validation.validate_exp = self.validate_expiry; | ||
| validation.validate_nbf = self.validate_not_before; | ||
| validation | ||
| } | ||
|
|
||
| pub async fn validate(&self, token: &str, header: &Header) -> Option<AuthorizationClaims> { | ||
| { | ||
| let cache = self.cache.read().await; | ||
| if let Some(keys) = cache.peek(JWKS_CACHE_KEY) | ||
| && keys.iter().any(|key| key.matches(header)) | ||
| { | ||
| return self.validate_with_keys(keys, token, header, &self.validation(header.alg)); | ||
| } | ||
| } | ||
|
|
||
| match fetch_jwks(&self.client, &self.url).await { | ||
| Ok(keys) => { | ||
| let key_count = keys.len(); | ||
| let claims = self.validate_with_keys(&keys, token, header, &self.validation(header.alg)); | ||
| self.cache.write().await.insert(JWKS_CACHE_KEY.to_owned(), keys); | ||
| tracing::info!("validate: SaaS JWKS cache refreshed {key_count}"); | ||
|
|
||
| claims | ||
| }, | ||
| Err(error) => { | ||
| tracing::info!("validate: unable to refresh SaaS JWKS {error:?}"); | ||
| None | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn validate_with_keys( | ||
| &self, | ||
| keys: &[VerificationKey], | ||
| token: &str, | ||
| header: &Header, | ||
| validation: &Validation, | ||
| ) -> Option<AuthorizationClaims> { | ||
| keys.iter() | ||
| .filter(|key| key.matches(header)) | ||
| .find_map(|key| self.validate_and_decode_claims(token, &key.decoding_key, validation)) | ||
| } | ||
|
|
||
| fn validate_and_decode_claims( | ||
| &self, | ||
| token: &str, | ||
| key: &DecodingKey, | ||
| validation: &Validation, | ||
| ) -> Option<AuthorizationClaims> { | ||
| let claims = decode::<Value>(token, key, validation) | ||
| .inspect_err(|e| { | ||
| debug!("validate_and_decode_claims: problem {e:?}"); | ||
| }) | ||
| .ok()? | ||
| .claims; | ||
| let claims = claims.as_object()?; | ||
| let user_id = self.principal_extractor.user_id(claims)?; | ||
| let tenant_id = self.principal_extractor.tenant_id(claims)?; | ||
|
|
||
| Some(AuthorizationClaims::new(user_id, tenant_id)) | ||
| } | ||
| } | ||
|
|
||
| pub struct VerificationKey { | ||
| pub(crate) key_id: Option<String>, | ||
| pub(crate) decoding_key: DecodingKey, | ||
| } | ||
|
|
||
| impl VerificationKey { | ||
| fn from_jwk(jwk: Jwk) -> Result<Option<Self>, AuthorizationError> { | ||
| if jwk.common.public_key_use.as_ref().is_some_and(|key_use| key_use != &PublicKeyUse::Signature) | ||
| || jwk.common.key_operations.as_ref().is_some_and(|operations| !operations.contains(&KeyOperations::Verify)) | ||
| { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let decoding_key = DecodingKey::from_jwk(&jwk).map_err(AuthorizationError::InvalidKey)?; | ||
| if !matches!(decoding_key.family(), AlgorithmFamily::Rsa | AlgorithmFamily::Ec) { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| Ok(Some(Self { key_id: jwk.common.key_id, decoding_key })) | ||
| } | ||
|
|
||
| pub(super) fn matches(&self, header: &Header) -> bool { | ||
| self.decoding_key.family() == header.alg.family() | ||
| && header | ||
| .kid | ||
| .as_ref() | ||
| .is_none_or(|header_key_id| self.key_id.as_ref().is_none_or(|key_id| key_id == header_key_id)) | ||
| } | ||
| } | ||
|
lucarlig marked this conversation as resolved.
|
||
|
|
||
| async fn fetch_jwks(client: &reqwest::Client, url: &Url) -> Result<Vec<VerificationKey>, AuthorizationError> { | ||
| let response = client | ||
| .get(url.clone()) | ||
| .send() | ||
| .await | ||
| .and_then(reqwest::Response::error_for_status) | ||
| .map_err(AuthorizationError::JwksRequest)?; | ||
| if response | ||
| .content_length() | ||
| .is_some_and(|length| length > u64::try_from(JWKS_MAX_RESPONSE_BYTES).unwrap_or(u64::MAX)) | ||
| { | ||
| return Err(AuthorizationError::JwksResponseTooLarge); | ||
| } | ||
| let mut body = Vec::new(); | ||
| let mut chunks = response.bytes_stream(); | ||
| while let Some(chunk) = chunks.next().await { | ||
| let chunk = chunk.map_err(AuthorizationError::JwksRequest)?; | ||
| if chunk.len() > JWKS_MAX_RESPONSE_BYTES.saturating_sub(body.len()) { | ||
| return Err(AuthorizationError::JwksResponseTooLarge); | ||
| } | ||
| body.extend_from_slice(&chunk); | ||
| } | ||
| let jwks = serde_json::from_slice::<JwkSet>(&body).map_err(AuthorizationError::InvalidJson)?; | ||
| if jwks.keys.is_empty() { Ok(Vec::new()) } else { validated_json_web_keys(jwks.keys) } | ||
| } | ||
|
|
||
| pub(super) fn validated_json_web_keys( | ||
| jwks: impl IntoIterator<Item = Jwk>, | ||
| ) -> Result<Vec<VerificationKey>, AuthorizationError> { | ||
| let mut keys = Vec::new(); | ||
| for jwk in jwks { | ||
| if let Some(key) = VerificationKey::from_jwk(jwk)? { | ||
| keys.push(key); | ||
| } | ||
| } | ||
|
|
||
| if keys.is_empty() { | ||
| return Err(AuthorizationError::NoSupportedKeys); | ||
| } | ||
| Ok(keys) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.