aboutsummaryrefslogtreecommitdiff
path: root/src/security.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-04-09 15:42:53 +0200
committerMartin Fischer <martin@push-f.com>2021-04-10 19:33:43 +0200
commit0296cab7787b9d993ba2d932a0b261e6a9b77108 (patch)
treeb07815ae56a9b0974f0e03fdbbf662dc64b0c628 /src/security.rs
parentf755eb02b4be1a2d97941f15c776d2391420ecad (diff)
drop dependency on cookie & time crates
Diffstat (limited to 'src/security.rs')
-rw-r--r--src/security.rs17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/security.rs b/src/security.rs
index abe114e..cd9d7bd 100644
--- a/src/security.rs
+++ b/src/security.rs
@@ -1,14 +1,13 @@
//! Provides [`Key`] and functions to encode & decode expiring claims.
-use time::OffsetDateTime;
-
pub use signed::Key;
+pub use std::time::{SystemTime, UNIX_EPOCH};
mod signed;
/// Join a string and an expiry date together into a string.
-pub fn encode_expiring_claim(claim: &str, expiry_date: OffsetDateTime) -> String {
- format!("{}:{}", claim, expiry_date.unix_timestamp())
+pub fn encode_expiring_claim(claim: &str, expiry_date: SystemTime) -> String {
+ format!("{}:{}", claim, expiry_date.duration_since(UNIX_EPOCH).unwrap().as_secs())
}
/// Extract the string, failing if the expiry date is in the past.
@@ -16,9 +15,9 @@ pub fn decode_expiring_claim(value: &str) -> Result<&str, &'static str> {
let mut parts = value.rsplitn(2, ':');
let expiry_date = parts.next().expect("first .rsplitn().next() is expected to return Some");
let claim = parts.next().ok_or("expected colon")?;
- let expiry_date: i64 = expiry_date.parse().map_err(|_| "failed to parse timestamp")?;
+ let expiry_date: u64 = expiry_date.parse().map_err(|_| "failed to parse timestamp")?;
- if expiry_date > OffsetDateTime::now_utc().unix_timestamp() {
+ if expiry_date > SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() {
Ok(claim)
} else {
Err("token is expired")
@@ -27,15 +26,15 @@ pub fn decode_expiring_claim(value: &str) -> Result<&str, &'static str> {
#[cfg(test)]
mod tests {
- use time::{OffsetDateTime, Duration};
+ use std::time::{SystemTime, Duration};
#[test]
fn test_expiring_claim() {
for claim in vec!["test", "", "foo:bar"] {
- let encoded_claim = super::encode_expiring_claim(claim, OffsetDateTime::now_utc() + Duration::minutes(1));
+ let encoded_claim = super::encode_expiring_claim(claim, SystemTime::now() + Duration::from_secs(60));
assert_eq!(super::decode_expiring_claim(&encoded_claim).unwrap(), claim);
- let encoded_claim = super::encode_expiring_claim(claim, OffsetDateTime::now_utc() - Duration::minutes(1));
+ let encoded_claim = super::encode_expiring_claim(claim, SystemTime::now() - Duration::from_secs(60));
assert!(super::decode_expiring_claim(&encoded_claim).is_err());
}
assert!(super::decode_expiring_claim("test".into()).is_err());