-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move claims-related code into module
Co-authored-by: Trong Huu Nguyen <[email protected]> Co-authored-by: Tommy Trøen <[email protected]>
- Loading branch information
1 parent
2a32ed2
commit 28ba1cd
Showing
4 changed files
with
93 additions
and
86 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,78 @@ | ||
use serde::Serialize; | ||
use jsonwebtoken as jwt; | ||
|
||
pub trait Assertion { | ||
fn new(token_endpoint: String, client_id: String, target: String) -> Self; | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct ClientAssertion { | ||
exp: usize, | ||
iat: usize, | ||
nbf: usize, | ||
jti: String, | ||
sub: String, | ||
iss: String, | ||
aud: String, | ||
} | ||
|
||
#[derive(Serialize)] | ||
pub struct JWTBearerAssertion { | ||
exp: usize, | ||
iat: usize, | ||
nbf: usize, | ||
jti: String, | ||
scope: String, | ||
iss: String, | ||
aud: String, | ||
} | ||
|
||
impl Assertion for JWTBearerAssertion { | ||
fn new(token_endpoint: String, client_id: String, target: String) -> Self { | ||
let now = epoch_now_secs(); | ||
let jti = uuid::Uuid::new_v4(); | ||
|
||
Self { | ||
exp: (now + 30) as usize, | ||
iat: now as usize, | ||
nbf: now as usize, | ||
jti: jti.to_string(), | ||
iss: client_id, // issuer of the token is the client itself | ||
aud: token_endpoint, // audience of the token is the issuer | ||
scope: target, | ||
} | ||
} | ||
} | ||
|
||
impl Assertion for ClientAssertion { | ||
fn new(token_endpoint: String, client_id: String, _target: String) -> Self { | ||
let now = epoch_now_secs(); | ||
let jti = uuid::Uuid::new_v4(); | ||
|
||
Self { | ||
exp: (now + 30) as usize, | ||
iat: now as usize, | ||
nbf: now as usize, | ||
jti: jti.to_string(), | ||
iss: client_id.clone(), // issuer of the token is the client itself | ||
aud: token_endpoint, // audience of the token is the issuer | ||
sub: client_id, | ||
} | ||
} | ||
} | ||
|
||
fn epoch_now_secs() -> u64 { | ||
std::time::SystemTime::now() | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.unwrap() | ||
.as_secs() | ||
} | ||
|
||
pub fn serialize<T: Serialize + Assertion>( | ||
claims: T, | ||
client_assertion_header: &jwt::Header, | ||
key: &jwt::EncodingKey, | ||
) -> Result<String, jsonwebtoken::errors::Error> { | ||
jwt::encode(client_assertion_header, &claims, key) | ||
} | ||
|
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