-
Notifications
You must be signed in to change notification settings - Fork 90
test(jans-cedarling): add tests and fix bugs caught in testing #9999
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
Conversation
…able types - replace token structs in test utils with generic serializable types for greater test flexibility Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the access_token has an invalid signature. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the id_token has an invalid signature. - Add test for when the id_token has a different iss with access_token. - Add test for when the id_token has a different aud with access_token. - Add test for when the id_token is expired. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the userinfo_token has an invalid signature. - Add test for when the userinfo_token has a different iss with the access_token. - Add test for when the userinfo_token has a different aud with the access_token. - Add test for when the userinfo_token has a different sub with the id_token. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- fixed a bug where the validation for the `aud` and `iss` of the userinfo_token is mixed up Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
…ecodingArgs` - This change consolidates the parameters for the `decode` function into a single `DecodingArgs` struct, for easier code readability and maintainability. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
…validation Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
… variant - renamed decoding_strategy::Error::JwkMissingKid to decoding_strategy::Error::JwtMissingKeyId Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- add test expecting to error when using access_token before nbf - add test expecting to error when using id_token before nbf - add test expecting to error when using userinfo_token nbf Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
DryRun Security SummaryThe pull request focuses on improving the security and robustness of the JWT (JSON Web Token) handling in the Expand for full summarySummary: The code changes in this pull request focus on improving the security and robustness of the JWT (JSON Web Token) handling in the
These changes demonstrate a strong focus on application security, as they help mitigate potential vulnerabilities related to the misuse or tampering of JWT tokens. The attention to detail in the token validation process and the robust error handling mechanisms contribute to the overall security and reliability of the Files Changed:
Code AnalysisWe ran Riskiness🟢 Risk threshold not exceeded. |
- references to `JwtService::decode_claims` updated to `JwtService::decode_tokens` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- add test that should error when a key with a given `kid` that should be used for validating a token can't be found. - add a test that panics when the openid configuration cannot be fetched at JwtService's initialization. the openid configuration cannot be fetched - add a test that panics when the JWKS cannot be fetched at JwtService's initialization. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
…_jwt_validation.rs Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- moved `can_update_local_jwks` from `with_validation.rs` to `key_service.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- updated docstrings on some test files to more accurately indicate what they contain. - remove unnecessary "unexpected" data checks on tests and just have it on one. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- improve code readability in tests by returning a List<EncodingKey> instead of a List<(String, jwt::EncodingKey)> when generating keys Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
- Modified `invalidate_token` to handle cases where the first two characters in the signature are identical and swapping them won't invalidate the token. This change introduces a loop to assign a distinct character to the first position if characters match, ensuring the token is reliably invalidated without unintended duplication. - Moved `invalidate_token` to `utils.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
5329133
to
f2012ad
Compare
refactor(jans-cedarling): replace token structs with generic serializable types - replace token structs in test utils with generic serializable types for greater test flexibility Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add negative tests for access_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the access_token has an invalid signature. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add negative tests for id_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the id_token has an invalid signature. - Add test for when the id_token has a different iss with access_token. - Add test for when the id_token has a different aud with access_token. - Add test for when the id_token is expired. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add test for checking access_token's expiration Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add negative tests for userinfo_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the userinfo_token has an invalid signature. - Add test for when the userinfo_token has a different iss with the access_token. - Add test for when the userinfo_token has a different aud with the access_token. - Add test for when the userinfo_token has a different sub with the id_token. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): move files around for better organization Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> fix(jans-cedarling): fix userinfo_token validation bug - fixed a bug where the validation for the `aud` and `iss` of the userinfo_token is mixed up Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> refactor(jans-cedarling): replace parameters in `decode(...)` with `DecodingArgs` - This change consolidates the parameters for the `decode` function into a single `DecodingArgs` struct, for easier code readability and maintainability. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> refactor(jans-cedarling): remove requirment for `iat` claim in token validation Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> fix(jans-cedarling): fix incorrect test fixture Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add detailed assertions for improved test accuracy Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> refactor(jans-cedarling): improve Error organization in jwt module Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): update outdated docstrings and rename an Error variant - renamed decoding_strategy::Error::JwkMissingKid to decoding_strategy::Error::JwtMissingKeyId Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add unit tests for validating `nbf` - add test expecting to error when using access_token before nbf - add test expecting to error when using id_token before nbf - add test expecting to error when using userinfo_token nbf Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): resolve clippy warnings Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): update incorrect docstrings - references to `JwtService::decode_claims` updated to `JwtService::decode_tokens` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): add tests relating to KeyService - add test that should error when a key with a given `kid` that should be used for validating a token can't be found. - add a test that panics when the openid configuration cannot be fetched at JwtService's initialization. the openid configuration cannot be fetched - add a test that panics when the JWKS cannot be fetched at JwtService's initialization. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> test(jans-cedarling): increase specificity of asserts on errors Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): update token claims in examples/authroize_with_jwt_validation.rs Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> refactor(jans-cedarling): move test into a different file - moved `can_update_local_jwks` from `with_validation.rs` to `key_service.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> chore(jans-cedarling): update docstrings and remove unnecessary checks - updated docstrings on some test files to more accurately indicate what they contain. - remove unnecessary "unexpected" data checks on tests and just have it on one. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> refactor(jans-cedarling): improve code readability in tests - improve code readability in tests by returning a List<EncodingKey> instead of a List<(String, jwt::EncodingKey)> when generating keys Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> fix(jans-cedarling): improve token invalidation robustness in tests - Modified `invalidate_token` to handle cases where the first two characters in the signature are identical and swapping them won't invalidate the token. This change introduces a loop to assign a distinct character to the first position if characters match, ensuring the token is reliably invalidated without unintended duplication. - Moved `invalidate_token` to `utils.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
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.
I don`t like when function panic. But it should be refactored in further issue. As I know..
jans-cedarling/cedarling/src/jwt/test/with_validation/key_service.rs
Outdated
Show resolved
Hide resolved
Also For me not really understandable renaming of errors. And usage of same name of error in different places confuse me. |
- renamed decoding_strategy::Error to decoding_strategy::DecodingError - renamed key_service::Error to key_service::KeyServiceError Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
7887aa5
to
8adf70e
Compare
}); | ||
|
||
// generate the signed token strings | ||
let access_token = generate_token_using_claims(&access_token_claims, &encoding_keys[0]); |
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.
Like the way the token is generated using claims and then used in test cases. Now there should be no issue in understanding the token content.
/// and processing of JWTs, including issues with the key service and unsupported | ||
/// algorithms. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum DecodingError { |
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.
Do you think TokenDecodingError or JWTDecodingError would be a better name? It should clearly indicate the types of errors represented within the enum.
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.
renamed jwt::decoding_error::DecodingError
to jwt::decoding_error::JwtDecodingError
here: f4f20be
#[allow(clippy::enum_variant_names)] | ||
pub enum JwtDecodingError { | ||
pub enum Error { |
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.
Can we use a more specific name instead of just Error? Error is too generic. Since it’s related to JWT, the enum name should reflect that.
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.
renamed jwt::Error
to jwt::JwtServiceError
here: f4f20be
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.
Please see inline comments.
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.
Looks good to me.
wrt the Error naming - my opinion is that something like keyservice::Error
is fine, provided that the qualified name is used in the code, eg
fn turn_key() -> Result<(),keyservice::Error>
tells me exactly what's going on, but
use keyservice::Error;
.
.
.
fn turn_key() -> Result<(),Error>
is much less clear.
- rename `jwt::Error` to `jwt::JwtServiceError` - rename `decoding_strategy::DecodingError` to `decoding_strategy::toJwtDecodingError` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com>
* refactor(jans-cedarling): replace token structs with generic serializable types - replace token structs in test utils with generic serializable types for greater test flexibility Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add negative tests for access_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the access_token has an invalid signature. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add negative tests for id_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the id_token has an invalid signature. - Add test for when the id_token has a different iss with access_token. - Add test for when the id_token has a different aud with access_token. - Add test for when the id_token is expired. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add test for checking access_token's expiration Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add negative tests for userinfo_token validation - Implement tests to verify error handling when required claims are missing (iss, aud, sub, iat, exp). - Add test for when the userinfo_token has an invalid signature. - Add test for when the userinfo_token has a different iss with the access_token. - Add test for when the userinfo_token has a different aud with the access_token. - Add test for when the userinfo_token has a different sub with the id_token. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): move files around for better organization Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * fix(jans-cedarling): fix userinfo_token validation bug - fixed a bug where the validation for the `aud` and `iss` of the userinfo_token is mixed up Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * refactor(jans-cedarling): replace parameters in `decode(...)` with `DecodingArgs` - This change consolidates the parameters for the `decode` function into a single `DecodingArgs` struct, for easier code readability and maintainability. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * refactor(jans-cedarling): remove requirment for `iat` claim in token validation Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * fix(jans-cedarling): fix incorrect test fixture Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add detailed assertions for improved test accuracy Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * refactor(jans-cedarling): improve Error organization in jwt module Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): update outdated docstrings and rename an Error variant - renamed decoding_strategy::Error::JwkMissingKid to decoding_strategy::Error::JwtMissingKeyId Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add unit tests for validating `nbf` - add test expecting to error when using access_token before nbf - add test expecting to error when using id_token before nbf - add test expecting to error when using userinfo_token nbf Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): resolve clippy warnings Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): update incorrect docstrings - references to `JwtService::decode_claims` updated to `JwtService::decode_tokens` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): add tests relating to KeyService - add test that should error when a key with a given `kid` that should be used for validating a token can't be found. - add a test that panics when the openid configuration cannot be fetched at JwtService's initialization. the openid configuration cannot be fetched - add a test that panics when the JWKS cannot be fetched at JwtService's initialization. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): increase specificity of asserts on errors Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): update token claims in examples/authroize_with_jwt_validation.rs Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * refactor(jans-cedarling): move test into a different file - moved `can_update_local_jwks` from `with_validation.rs` to `key_service.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): update docstrings and remove unnecessary checks - updated docstrings on some test files to more accurately indicate what they contain. - remove unnecessary "unexpected" data checks on tests and just have it on one. Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * refactor(jans-cedarling): improve code readability in tests - improve code readability in tests by returning a List<EncodingKey> instead of a List<(String, jwt::EncodingKey)> when generating keys Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * fix(jans-cedarling): improve token invalidation robustness in tests - Modified `invalidate_token` to handle cases where the first two characters in the signature are identical and swapping them won't invalidate the token. This change introduces a loop to assign a distinct character to the first position if characters match, ensuring the token is reliably invalidated without unintended duplication. - Moved `invalidate_token` to `utils.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): change error naming convention - renamed decoding_strategy::Error to decoding_strategy::DecodingError - renamed key_service::Error to key_service::KeyServiceError Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * test(jans-cedarling): remove tests that expects to panic Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): change error naming in JWT module - rename `jwt::Error` to `jwt::JwtServiceError` - rename `decoding_strategy::DecodingError` to `decoding_strategy::toJwtDecodingError` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> * chore(jans-cedarling): move `test/mod.rs` to `test.rs` Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> --------- Signed-off-by: rmarinn <34529290+rmarinn@users.noreply.github.com> Former-commit-id: 32c21ea
Prepare
Description
This PR adds tests for
JwtService
to improve test coverage. Please see the target issue for the tests covered.Target issue
target issue: #9995
closes #9995
Implementation Details
Test and Document the changes
Please check the below before submitting your PR. The PR will not be merged if there are no commits that start with
docs:
to indicate documentation changes or if the below checklist is not selected.