Skip to content

Commit

Permalink
Serialize timestamps as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ramos committed Sep 8, 2019
1 parent 53210de commit fa50069
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/id_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ mod tests {
\"aud\":[\"s6BhdRkqt3\"],\
\"exp\":1311281970,\
\"iat\":1311280970,\
\"auth_time\":1311282970.5,\
\"auth_time\":1311282970,\
\"nonce\":\"Zm9vYmFy\",\
\"acr\":\"urn:mace:incommon:iap:silver\",\
\"amr\":[\"password\",\"totp\"],\
Expand Down Expand Up @@ -814,7 +814,7 @@ mod tests {
},
EmptyAdditionalClaims {},
)
.set_auth_time(Some(Utc.timestamp(1311282970, 500000000)))
.set_auth_time(Some(Utc.timestamp(1311282970, 0)))
.set_nonce(Some(Nonce::new("Zm9vYmFy".to_string())))
.set_auth_context_ref(Some(AuthenticationContextClass::new(
"urn:mace:incommon:iap:silver".to_string(),
Expand Down
14 changes: 4 additions & 10 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,17 +1144,11 @@ pub(crate) mod helpers {
Utc.timestamp_opt(secs, nsecs).single().ok_or(())
}

// The spec is ambiguous about whether seconds should be expressed as integers, or
// whether floating-point values are allowed. For compatibility with a wide range of
// clients, we round down to the nearest second.
pub(crate) fn utc_to_seconds(utc: &DateTime<Utc>) -> Seconds {
let (secs, nsecs) = (utc.timestamp(), utc.timestamp_subsec_nanos());
if nsecs == 0 {
Seconds::new(secs.into())
} else {
Seconds::new(
serde_json::Number::from_f64(secs as f64 + (f64::from(nsecs)) / 1_000_000_000.)
// This really shouldn't happen for a valid DateTime
.expect("Failed to convert timestamp to f64"),
)
}
Seconds::new(utc.timestamp().into())
}

pub mod serde_utc_seconds {
Expand Down

0 comments on commit fa50069

Please sign in to comment.