-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtraits.rs
More file actions
32 lines (30 loc) · 1.26 KB
/
Copy pathtraits.rs
File metadata and controls
32 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use base64::DecodeError;
use base64::prelude::*;
use subtle::ConstantTimeEq;
use std::fmt::Display;
//marker traits
/// Used by marker traits to determine at compile time which PASETO version the user is attempting to use
pub trait VersionTrait: Display + Default + AsRef<str> {}
/// Used by marker traits to determine at compile time which PASETO purpose the user is attempting to use
pub trait PurposeTrait: Display + Default + AsRef<str> {}
pub trait V1orV3: VersionTrait {}
/// A marker trait used to determine if the PASETO token version is capable of using an implicit
/// assertion. Currently this applies only to V3/V4 PASETO tokens
pub trait ImplicitAssertionCapable: VersionTrait {}
pub trait V2orV4: VersionTrait {}
/// Enable a type to encode/decode to/from base64 and compare itself to another implementer using
/// constant time comparision
pub(crate) trait Base64Encodable<T: ?Sized + AsRef<[u8]>>: Display + AsRef<T> {
fn encode(&self) -> String {
BASE64_URL_SAFE_NO_PAD.encode(self.as_ref())
}
fn decode(&self) -> Result<Vec<u8>, DecodeError> {
BASE64_URL_SAFE_NO_PAD.decode(self.as_ref())
}
fn constant_time_equals<B>(&self, other: B) -> bool
where
B: AsRef<str>,
{
self.encode().as_bytes().ct_eq(other.as_ref().as_bytes()).into()
}
}