-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-cedarling): improve error handling for JWKS responses (#9982)
feat(jans-cedarling): add graceful error handling for unsupported algorithms in JWKs - added logic to skip over JWKs that use unsupported algorithms without breaking the initialization process - updated error handling to avoid stopping service initialization when encountering unknown algorithm variants in JWKs Signed-off-by: rmarinn <[email protected]> refactor(jans-cedarling): extract repeated code into a method for better code reusability Signed-off-by: rmarinn <[email protected]> chore(jans-cedarling): fix misspellings Signed-off-by: rmarinn <[email protected]> fix(jans-cedarling): add error handling to update_jwks_for_iss Signed-off-by: rmarinn <[email protected]> test(jans-cedarling): improve test assertions and messages Signed-off-by: rmarinn <[email protected]> test(jans-cedarling): improve panic message in test utils Signed-off-by: rmarinn <[email protected]> test(jans-cedarling): Make generate_token_using_claims return a Result - Make gernerate_token_using_claims return a Result instead of panicking for improved error management in tests. Signed-off-by: rmarinn <[email protected]> feat(jans-cedarling): add retry mechanism for KeyService HTTP requests Signed-off-by: rmarinn <[email protected]> chore(jans-cedarling): add missing license header Signed-off-by: rmarinn <[email protected]> chore(jans-cedarling): resolve clippy issues Signed-off-by: rmarinn <[email protected]> chore(jans-cedarling): update example for authorize_with_jwt_validation.rs Signed-off-by: rmarinn <[email protected]> refactor(jans-cedarling): remove `exp` and `nbf` requirement for userinfo_token Signed-off-by: rmarinn <[email protected]>
- Loading branch information
Showing
16 changed files
with
547 additions
and
333 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tokens.json |
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 |
---|---|---|
|
@@ -9,21 +9,37 @@ use cedarling::{ | |
BootstrapConfig, Cedarling, JwtConfig, LogConfig, LogTypeConfig, PolicyStoreConfig, | ||
PolicyStoreSource, Request, ResourceData, | ||
}; | ||
use serde::Deserialize; | ||
use std::collections::HashMap; | ||
|
||
// Load a JSON policy store file, containing policies and trusted issuers, at compile time. | ||
// This file defines access control policies for different resources and actions. | ||
static POLICY_STORE_RAW: &str = | ||
include_str!("../../test_files/policy-store_with_trusted_issuers_ok.json"); | ||
|
||
// Load example tokens from a JSON file, also at compile time. | ||
// NOTE: `tokens.json` is ignored in version control for security reasons. | ||
// To run this example, create a `tokens.json` file based on `tokens.example.json`. | ||
static TOKENS: &str = include_str!("./tokens.json"); | ||
|
||
#[derive(Deserialize)] | ||
struct Tokens { | ||
access_token: String, | ||
userinfo_token: String, | ||
id_token: String, | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Configure the JwtService to validate signatures using the specified algorithms: | ||
// `HS256` and `RS256`. | ||
// | ||
// Tokens signed with an algorithm not in `signature_algorithms` | ||
// will be automatically marked as invalid. | ||
// Configure JWT validation settings. Enable the JwtService to validate JWT tokens | ||
// using specific algorithms: `HS256` and `RS256`. Only tokens signed with these algorithms | ||
// will be accepted; others will be marked as invalid during validation. | ||
let jwt_config = JwtConfig::Enabled { | ||
signature_algorithms: vec!["HS256".to_string(), "RS256".to_string()], | ||
}; | ||
|
||
// Initialize the main Cedarling instance, responsible for policy-based authorization. | ||
// This setup includes basic application information, logging configuration, and | ||
// policy store configuration. | ||
let cedarling = Cedarling::new(BootstrapConfig { | ||
application_name: "test_app".to_string(), | ||
log_config: LogConfig { | ||
|
@@ -35,48 +51,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |
jwt_config, | ||
})?; | ||
|
||
// access_token claims: | ||
// { | ||
// "iss": "https://admin-ui-test.gluu.org", | ||
// "aud": "some_audience", | ||
// "sub": "some_subject", | ||
// "exp": 2724945978, -> May 8, 2056 01:26:18 GMT+0800 | ||
// "iat": 1624832259 -> June 28, 2021 06:17:39 GMT+0800 | ||
// } | ||
let access_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FkbWluLXVpLXRlc3QuZ2x1dS5vcmciLCJhdWQiOiJzb21lX2F1ZGllbmNlIiwic3ViIjoic29tZV9zdWJqZWN0IiwiZXhwIjoyNzI0OTQ1OTc4LCJpYXQiOjE2MjQ4MzIyNTl9.oZKCdPvtvA8yJ5BQhP5725TYf0CAzcOZEhPQmom7cOc".to_string(); | ||
|
||
// id_token claims: | ||
// { | ||
// "iss": "https://admin-ui-test.gluu.org", | ||
// "aud": "some_audience", | ||
// "sub": "some_subject", | ||
// "exp": 2724945978, -> May 8, 2056 01:26:18 GMT+0800 | ||
// "iat": 1624832259 -> June 28, 2021 06:17:39 GMT+0800 | ||
// "nonce": "123123123", | ||
// "name": "Mr. admin", | ||
// "email": "[email protected]" | ||
// } | ||
let id_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FkbWluLXVpLXRlc3QuZ2x1dS5vcmciLCJhdWQiOiJzb21lX2F1ZGllbmNlIiwic3ViIjoic29tZV9zdWJqZWN0IiwiZXhwIjoyNzI0OTQ1OTc4LCJpYXQiOjE2MjQ4MzIyNTksIm5vbmNlIjoiMTIzMTIzMTIzIiwibmFtZSI6Ik1yLiBhZG1pbiIsImVtYWlsIjoiYWRtaW5AZ2x1dS5vcmcifQ.Zzx3gz3d3YK2geb0aCPLyiOEvFviuMsGbf1urNnmPDU".to_string(); | ||
|
||
// userinfo_token claims: | ||
// { | ||
// "iss": "https://admin-ui-test.gluu.org", | ||
// "aud": "some_audience", | ||
// "sub": "some_subject", | ||
// "exp": 2724945978, -> May 8, 2056 01:26:18 GMT+0800 | ||
// "iat": 1624832259 -> June 28, 2021 06:17:39 GMT+0800 | ||
// "nonce": "123123123", | ||
// "name": "Mr. admin", | ||
// "email": "[email protected]", | ||
// "email_verified": true, | ||
// "locale": "en_US", | ||
// } | ||
let userinfo_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2FkbWluLXVpLXRlc3QuZ2x1dS5vcmciLCJhdWQiOiJzb21lX2F1ZGllbmNlIiwic3ViIjoic29tZV9zdWJqZWN0IiwiZXhwIjoyNzI0OTQ1OTc4LCJpYXQiOjE2MjQ4MzIyNTksIm5vbmNlIjoiMTIzMTIzMTIzIiwibmFtZSI6Ik1yLiBhZG1pbiIsImVtYWlsIjoiYWRtaW5AZ2x1dS5vcmciLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwibG9jYWxlIjoiZW5fVVMifQ.HvX2s_ZWUfyvRHLUl5CWaSPOIp9zVpwP2LbF5U6tNGA".to_string(); | ||
// Parse the tokens from the JSON string loaded from `tokens.json`. | ||
// This will create a `Tokens` struct populated with `access_token`, `userinfo_token`, and `id_token`. | ||
let tokens = serde_json::from_str::<Tokens>(TOKENS).expect("should deserialize tokens"); | ||
|
||
// Perform an authorization request to Cedarling. | ||
// This request checks if the provided tokens have sufficient permission to perform an action | ||
// on a specific resource. Each token (access, ID, and userinfo) is required for the | ||
// authorization process, alongside resource and action details. | ||
let result = cedarling.authorize(Request { | ||
access_token, | ||
id_token, | ||
userinfo_token, | ||
access_token: tokens.access_token, | ||
id_token: tokens.id_token, | ||
userinfo_token: tokens.userinfo_token, | ||
action: "Jans::Action::\"Update\"".to_string(), | ||
context: serde_json::json!({}), | ||
resource: ResourceData { | ||
|
@@ -88,6 +74,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |
)]), | ||
}, | ||
}); | ||
|
||
// Handle authorization result. If there's an error, print it. | ||
if let Err(ref e) = &result { | ||
eprintln!("Error while authorizing: {:?}\n\n", e) | ||
} | ||
|
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,5 @@ | ||
{ | ||
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...", | ||
"id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE2...", | ||
"userinfo_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." | ||
} |
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
Oops, something went wrong.