Skip to content

Commit bdc4b4d

Browse files
committed
refactor(sdk): implement RegisteredClaims for every token type with one macro
1 parent 0887b36 commit bdc4b4d

1 file changed

Lines changed: 40 additions & 58 deletions

File tree

‎crates/auth-sdk/src/token.rs‎

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -683,8 +683,10 @@ pub(crate) trait RegisteredClaims: DeserializeOwned {
683683
fn audience(&self) -> &Audience;
684684
/// `exp`.
685685
fn expires_at(&self) -> DateTime<Utc>;
686-
/// `nbf`, if any.
687-
fn not_before(&self) -> Option<DateTime<Utc>>;
686+
/// `nbf`, if the token type has one.
687+
fn not_before(&self) -> Option<DateTime<Utc>> {
688+
None
689+
}
688690
/// `iat`.
689691
fn issued_at(&self) -> DateTime<Utc>;
690692

@@ -727,63 +729,46 @@ pub(crate) trait RegisteredClaims: DeserializeOwned {
727729
}
728730
}
729731

730-
impl<X: DeserializeOwned> RegisteredClaims for AccessTokenClaims<X> {
731-
const EXCLUSIVE_AUDIENCE: bool = false;
732-
733-
fn issuer(&self) -> &str {
734-
&self.iss
735-
}
736-
fn audience(&self) -> &Audience {
737-
&self.aud
738-
}
739-
fn expires_at(&self) -> DateTime<Utc> {
740-
self.exp
741-
}
742-
fn not_before(&self) -> Option<DateTime<Utc>> {
743-
self.nbf
744-
}
745-
fn issued_at(&self) -> DateTime<Utc> {
746-
self.iat
747-
}
748-
}
749-
750-
impl RegisteredClaims for IdTokenClaims {
751-
const EXCLUSIVE_AUDIENCE: bool = true;
732+
/// Implements [`RegisteredClaims`] for claims types whose registered claims are their
733+
/// `iss`, `aud`, `exp` and `iat` fields, and `nbf` when the type has one.
734+
macro_rules! registered_claims {
735+
($(
736+
impl $([$($generics:tt)*])? for $claims:ty {
737+
exclusive_audience: $exclusive:literal $(, not_before: $nbf:ident)?
738+
}
739+
)+) => {$(
740+
impl$(<$($generics)*>)? RegisteredClaims for $claims {
741+
const EXCLUSIVE_AUDIENCE: bool = $exclusive;
752742

753-
fn issuer(&self) -> &str {
754-
&self.iss
755-
}
756-
fn audience(&self) -> &Audience {
757-
&self.aud
758-
}
759-
fn expires_at(&self) -> DateTime<Utc> {
760-
self.exp
761-
}
762-
fn not_before(&self) -> Option<DateTime<Utc>> {
763-
self.nbf
764-
}
765-
fn issued_at(&self) -> DateTime<Utc> {
766-
self.iat
767-
}
743+
fn issuer(&self) -> &str {
744+
&self.iss
745+
}
746+
fn audience(&self) -> &Audience {
747+
&self.aud
748+
}
749+
fn expires_at(&self) -> DateTime<Utc> {
750+
self.exp
751+
}
752+
$(fn not_before(&self) -> Option<DateTime<Utc>> {
753+
self.$nbf
754+
})?
755+
fn issued_at(&self) -> DateTime<Utc> {
756+
self.iat
757+
}
758+
}
759+
)+};
768760
}
769761

770-
impl RegisteredClaims for LogoutTokenClaims {
771-
const EXCLUSIVE_AUDIENCE: bool = true;
772-
773-
fn issuer(&self) -> &str {
774-
&self.iss
775-
}
776-
fn audience(&self) -> &Audience {
777-
&self.aud
762+
registered_claims! {
763+
impl [X: DeserializeOwned] for AccessTokenClaims<X> {
764+
exclusive_audience: false, not_before: nbf
778765
}
779-
fn expires_at(&self) -> DateTime<Utc> {
780-
self.exp
781-
}
782-
fn not_before(&self) -> Option<DateTime<Utc>> {
783-
None
766+
impl for IdTokenClaims {
767+
exclusive_audience: true, not_before: nbf
784768
}
785-
fn issued_at(&self) -> DateTime<Utc> {
786-
self.iat
769+
// `LogoutTokenClaims` has no `nbf`.
770+
impl for LogoutTokenClaims {
771+
exclusive_audience: true
787772
}
788773
}
789774

@@ -846,10 +831,7 @@ impl Issuer {
846831
.check(None, auth_time, self.now() - LEEWAY)
847832
.is_err()
848833
{
849-
return Err(TokenError::AuthenticationTooOld {
850-
auth_time: claims.authentication.auth_time,
851-
max_age,
852-
});
834+
return Err(TokenError::AuthenticationTooOld { auth_time, max_age });
853835
}
854836
}
855837
Ok(IdToken {

0 commit comments

Comments
 (0)