-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add custom claims to access_token * remove cargo-make * lint * test duplication * comment removed * remove useless complexity * Update src/model/claims.rs Co-authored-by: Simone Cottini <[email protected]> * use plain text string instead of reading from file * trailing space Co-authored-by: Simone Cottini <[email protected]>
- Loading branch information
1 parent
70f5b69
commit 300b905
Showing
7 changed files
with
230 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -160,11 +160,13 @@ fn new_token_response(app_data: &AppData, audience: &str, grant_type: GrantType) | |
.get_permissions(audience) | ||
.expect("Failed to get permissions"); | ||
|
||
let custom_claims = app_data.config().access_token().custom_claims().to_owned(); | ||
let claims: Claims = Claims::new( | ||
audience.to_string(), | ||
permissions, | ||
app_data.config().issuer().to_string(), | ||
grant_type, | ||
custom_claims, | ||
); | ||
|
||
let user_info: UserInfo = UserInfo::new(app_data.config(), audience.to_string()); | ||
|
@@ -175,3 +177,69 @@ fn new_token_response(app_data: &AppData, audience: &str, grant_type: GrantType) | |
|
||
TokenResponse::new(access_token, id_token, None) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{ | ||
config::Config, | ||
model::{AppData, GrantType}, | ||
}; | ||
|
||
use super::new_token_response; | ||
|
||
#[test] | ||
fn generate_access_token_with_custom_claims() { | ||
let config_string: &str = r#" | ||
issuer = "https://prima.localauth0.com/" | ||
[user_info] | ||
name = "Local" | ||
given_name = "Locie" | ||
family_name = "Auth0" | ||
gender = "none" | ||
birthdate = "2022-02-11" | ||
email = "[email protected]" | ||
picture = "https://github.com/primait/localauth0/blob/6f71c9318250219a9d03fb72afe4308b8824aef7/web/assets/static/media/localauth0.png" | ||
custom_fields = [ | ||
{ name = "address", value = { String = "github street" } }, | ||
{ name = "roles", value = { Vec = ["fake:auth"] } } | ||
] | ||
[[audience]] | ||
name = "audience1" | ||
permissions = ["audience1:permission1", "audience1:permission2"] | ||
[[audience]] | ||
name = "audience2" | ||
permissions = ["audience2:permission2"] | ||
[access_token] | ||
custom_claims = [ | ||
{ name = "at_custom_claims_str", value = { String = "str" } } | ||
] | ||
"#; | ||
let config: Config = toml::from_str(config_string).unwrap(); | ||
let app_data = AppData::new(config).unwrap(); | ||
let audience = "my-audience"; | ||
let grant_type = GrantType::AuthorizationCode; | ||
|
||
let token_response = new_token_response(&app_data, audience, grant_type); | ||
|
||
let access_token = token_response.access_token(); | ||
let jwks = app_data.jwks().get().unwrap(); | ||
let claims_json: serde_json::Value = jwks | ||
.parse(access_token, &[audience]) | ||
.expect("failed to parse access_token"); | ||
|
||
assert_eq!(claims_json.get("aud").unwrap(), "my-audience"); | ||
assert!(claims_json.get("iat").is_some()); | ||
assert!(claims_json.get("exp").is_some()); | ||
assert!(claims_json.get("scope").is_some()); | ||
assert_eq!(claims_json.get("iss").unwrap(), "https://prima.localauth0.com/"); | ||
assert_eq!(claims_json.get("gty").unwrap(), "authorization_code"); | ||
assert!(claims_json.get("permissions").is_some()); | ||
|
||
assert_eq!(claims_json.get("at_custom_claims_str").unwrap(), "str"); | ||
} | ||
} |
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