Skip to content

Commit

Permalink
Return DateTime claims as owned values since the type implements Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ramos committed Nov 30, 2019
1 parent 6b31f88 commit 3dd217c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/id_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ mod tests {
*claims.audiences(),
vec![Audience::new("s6BhdRkqt3".to_string())]
);
assert_eq!(*claims.expiration(), Utc.timestamp(1311281970, 0));
assert_eq!(*claims.issue_time(), Utc.timestamp(1311280970, 0));
assert_eq!(claims.expiration(), Utc.timestamp(1311281970, 0));
assert_eq!(claims.issue_time(), Utc.timestamp(1311280970, 0));
assert_eq!(
*claims.subject(),
SubjectIdentifier::new("24400320".to_string())
Expand Down Expand Up @@ -490,8 +490,8 @@ mod tests {
*claims.audiences(),
vec![Audience::new("s6BhdRkqt3".to_string())]
);
assert_eq!(*claims.expiration(), Utc.timestamp(1311281970, 0));
assert_eq!(*claims.issue_time(), Utc.timestamp(1311280970, 0));
assert_eq!(claims.expiration(), Utc.timestamp(1311281970, 0));
assert_eq!(claims.issue_time(), Utc.timestamp(1311280970, 0));
assert_eq!(
*claims.subject(),
SubjectIdentifier::new("24400320".to_string())
Expand Down
38 changes: 37 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,19 @@ macro_rules! field_getters {
$($body)+
}
};
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident Option <$type:ty >) => {
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident Option < DateTime < Utc >>) => {
#[doc = $doc]
$vis fn $field(&$self) -> Option<DateTime<Utc>> {
$zero.$field
}
};
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident Option < DateTime < Utc >> { $($body:tt)+ }) => {
#[doc = $doc]
$vis fn $field(&$self) -> Option<DateTime<Utc>> {
$($body)+
}
};
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident Option < $type:ty >) => {
#[doc = $doc]
$vis fn $field(&$self) -> Option<&$type> {
$zero.$field.as_ref()
Expand All @@ -653,6 +665,12 @@ macro_rules! field_getters {
$($body)+
}
};
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident DateTime < Utc >) => {
#[doc = $doc]
$vis fn $field(&$self) -> DateTime<Utc> {
$zero.$field
}
};
(@case [$doc:expr] $vis:vis $self:ident [$zero:expr] $field:ident $type:ty) => {
#[doc = $doc]
$vis fn $field(&$self) -> &$type {
Expand All @@ -677,6 +695,18 @@ macro_rules! field_getters {
$($body)+
}
};
(@case [$doc:expr] $self:ident [$zero:expr] $field:ident() Option < DateTime < Utc >>) => {
#[doc = $doc]
fn $field(&$self) -> Option<DateTime<Utc>> {
$zero.$field()
}
};
(@case [$doc:expr] $self:ident [$zero:expr] $field:ident() Option < DateTime < Utc >> { $($body:tt)+ }) => {
#[doc = $doc]
fn $field(&$self) -> Option<DateTime<Utc>> {
$($body)+
}
};
(@case [$doc:expr] $self:ident [$zero:expr] $field:ident() Option < $type:ty >) => {
#[doc = $doc]
fn $field(&$self) -> Option<&$type> {
Expand All @@ -689,6 +719,12 @@ macro_rules! field_getters {
$($body)+
}
};
(@case [$doc:expr] $self:ident [$zero:expr] $field:ident() DateTime < Utc >) => {
#[doc = $doc]
fn $field(&$self) -> DateTime<Utc> {
$zero.$field()
}
};
(@case [$doc:expr] $self:ident [$zero:expr] $field:ident() $type:ty) => {
#[doc = $doc]
fn $field(&$self) -> &$type {
Expand Down
4 changes: 2 additions & 2 deletions src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,11 +1274,11 @@ mod tests {
ClientConfigUrl::new("https://example-provider.com/registration".to_string()).unwrap()
);
assert_eq!(
*registration_response.client_id_issued_at().unwrap(),
registration_response.client_id_issued_at().unwrap(),
Utc.timestamp(1523953306, 0)
);
assert_eq!(
*registration_response.client_secret_expires_at().unwrap(),
registration_response.client_secret_expires_at().unwrap(),
Utc.timestamp(1526545306, 0)
);
assert_eq!(
Expand Down
12 changes: 6 additions & 6 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,8 @@ where
{
acr_verifier_fn: Rc<dyn Fn(Option<&AuthenticationContextClass>) -> Result<(), String> + 'a>,
#[allow(clippy::type_complexity)]
auth_time_verifier_fn: Rc<dyn Fn(Option<&DateTime<Utc>>) -> Result<(), String> + 'a>,
iat_verifier_fn: Rc<dyn Fn(&DateTime<Utc>) -> Result<(), String> + 'a>,
auth_time_verifier_fn: Rc<dyn Fn(Option<DateTime<Utc>>) -> Result<(), String> + 'a>,
iat_verifier_fn: Rc<dyn Fn(DateTime<Utc>) -> Result<(), String> + 'a>,
jwt_verifier: JwtClaimsVerifier<'a, JS, JT, JU, K>,
time_fn: Rc<dyn Fn() -> DateTime<Utc> + 'a>,
}
Expand Down Expand Up @@ -607,7 +607,7 @@ where
///
pub fn set_auth_time_verifier_fn<T>(mut self, auth_time_verifier_fn: T) -> Self
where
T: Fn(Option<&DateTime<Utc>>) -> Result<(), String> + 'a,
T: Fn(Option<DateTime<Utc>>) -> Result<(), String> + 'a,
{
self.auth_time_verifier_fn = Rc::new(auth_time_verifier_fn);
self
Expand Down Expand Up @@ -675,7 +675,7 @@ where
///
pub fn set_issue_time_verifier_fn<T>(mut self, iat_verifier_fn: T) -> Self
where
T: Fn(&DateTime<Utc>) -> Result<(), String> + 'a,
T: Fn(DateTime<Utc>) -> Result<(), String> + 'a,
{
self.iat_verifier_fn = Rc::new(iat_verifier_fn);
self
Expand Down Expand Up @@ -787,7 +787,7 @@ where

// 9. The current time MUST be before the time represented by the exp Claim.
let cur_time = (*self.time_fn)();
if cur_time >= *partially_verified_claims.expiration() {
if cur_time >= partially_verified_claims.expiration() {
return Err(ClaimsVerificationError::Expired(format!(
"ID token expired at {} (current time is {})",
partially_verified_claims.expiration(),
Expand Down Expand Up @@ -1680,7 +1680,7 @@ mod tests {
.clone()
.set_auth_time_verifier_fn(|auth_time| {
assert_eq!(
*auth_time.unwrap(),
auth_time.unwrap(),
seconds_to_utc(&Seconds::new(1544928548.into())).unwrap(),
);
Err("Invalid auth_time claim".to_string())
Expand Down

0 comments on commit 3dd217c

Please sign in to comment.