Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
cookie.rs 774 B
Newer Older
use actix_web::cookie::{time::Duration, Cookie, SameSite};
use rand::{distributions::Alphanumeric, Rng};

pub struct SecureCookie;
impl SecureCookie {
    pub fn new<'a>(name: &String, value: &String) -> Cookie<'a> {
        Cookie::build(name.to_owned(), value.to_owned())
            .path("/")
            .http_only(true)
            .max_age(Duration::days(7))
            .same_site(SameSite::Strict)
            .secure(true)
            .finish()
    }

    pub fn generate_token(size: usize) -> String {
        // Thanks to https://stackoverflow.com/questions/54275459
        rand::thread_rng()
            .sample_iter(&Alphanumeric)
            .take(size)
            .map(char::from)
            .collect::<String>()
            .to_ascii_lowercase()
    }
}