unilim-cas 1.0.1

A purrfect CAS authentication wrapper for Unilim.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use super::{Error, Result};
use totp_rs::{Algorithm, Secret, TOTP};
use web_time::{SystemTime, UNIX_EPOCH};

pub fn generate(key: &str) -> Result<String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(|error| Error::Parse(error.to_string()))?
        .as_secs();
    at(key, now)
}

fn at(key: &str, unix_seconds: u64) -> Result<String> {
    let secret = Secret::Encoded(key.to_owned())
        .to_bytes()
        .map_err(|error| Error::Parse(format!("invalid totp secret: {error:?}")))?;
    Ok(TOTP::new_unchecked(Algorithm::SHA1, 6, 1, 30, secret).generate(unix_seconds))
}