diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/cookie.rs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/utils/cookie.rs b/src/utils/cookie.rs index c716936..826efa9 100644 --- a/src/utils/cookie.rs +++ b/src/utils/cookie.rs @@ -1,10 +1,13 @@ -use uuid::Uuid; +use hex; +use rand_core::{OsRng, RngCore}; use crate::crypto::signed; use crate::model::config::Config; -pub fn login(config: &Config, token: Uuid) -> Result<String, String> { - let signed_token = signed::sign(&config.auth_secret, &token.to_string())?; +const TOKEN_BYTES: usize = 20; + +pub fn login(config: &Config, token: &str) -> Result<String, String> { + let signed_token = signed::sign(&config.auth_secret, token)?; Ok(cookie(config, &signed_token, 24 * 60 * 60)) } @@ -19,6 +22,12 @@ pub fn extract_token(config: &Config, cookie: &str) -> Result<String, String> { signed::verify(&config.auth_secret, signed_cookie) } +pub fn generate_token() -> String { + let mut token = [0u8; TOKEN_BYTES]; + OsRng.fill_bytes(&mut token); + hex::encode(token) +} + fn cookie(config: &Config, token: &str, max_age_seconds: i32) -> String { let mut xs = vec![ format!("TOKEN={token}"), |