diff options
author | Martin Fischer <martin@push-f.com> | 2021-04-09 13:11:57 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-04-09 13:11:57 +0200 |
commit | 61d8ccaddde9ce3f50d11daa706e0c93f70a3cd4 (patch) | |
tree | 6d5889e12c7567881104f3fed6bd4b73fc9299c9 /src/security.rs | |
parent | 21fdc62d9ee9fed72e70af740cda416d2bbafcb1 (diff) |
make decode_expiring_claim return slice
Diffstat (limited to 'src/security.rs')
-rw-r--r-- | src/security.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/security.rs b/src/security.rs index 51c2198..a270ee9 100644 --- a/src/security.rs +++ b/src/security.rs @@ -10,14 +10,14 @@ pub fn encode_expiring_claim(claim: &str, expiry_date: OffsetDateTime) -> String } /// Extract the string, failing if the expiry date is in the past. -pub fn decode_expiring_claim(value: String) -> Result<String,&'static str> { +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")?; if expiry_date > OffsetDateTime::now_utc().unix_timestamp() { - Ok(claim.to_string()) + Ok(claim) } else { Err("token is expired") } @@ -31,10 +31,10 @@ mod tests { 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)); - assert_eq!(super::decode_expiring_claim(encoded_claim).unwrap(), claim); + assert_eq!(super::decode_expiring_claim(&encoded_claim).unwrap(), claim); let encoded_claim = super::encode_expiring_claim(claim, OffsetDateTime::now_utc() - Duration::minutes(1)); - assert!(super::decode_expiring_claim(encoded_claim).is_err()); + assert!(super::decode_expiring_claim(&encoded_claim).is_err()); } assert!(super::decode_expiring_claim("test".into()).is_err()); } |