summaryrefslogtreecommitdiff
path: root/rust/src/token.rs
blob: 8b4ab5b5be4a1a3db9cc9a3689420097e045c120 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
///! Token generation
use std::iter::*;

use crypto::bcrypt::bcrypt;

pub struct TokenGenerator {
    /// Salt for bcrypt
    salt: Vec<u8>,
    /// bcrypt cost factor, defaults to 10
    bcrypt_cost: u32,
    // length of a tokens valid time in seconds
    pub valid_duration_secs: i64,
}

impl TokenGenerator {
    pub fn new(valid_duration_secs: i64, salt: Vec<u8>) -> TokenGenerator {
        TokenGenerator {
            salt: salt,
            bcrypt_cost: 10,
            valid_duration_secs: valid_duration_secs
        }
    }

    pub fn generate_token(&self, username: &str, at_time: i64) -> (i64, String) {
        let timeslot = at_time - (at_time % self.valid_duration_secs);
        let input: String = format!("{}{}", username, timeslot);
        return (timeslot + self.valid_duration_secs, self.make_hash_token(&input.as_bytes()))
    }

    pub fn generate_token_norm(&self, username: &str, at_time: i64) -> (i64, String) {
        let (valid, tok) = self.generate_token(username, at_time);
        return (valid, normalize_token(tok.as_str()));
    }

    fn make_hash_token(&self, input: &[u8]) -> String {
        let mut out = [0u8; 24];
        bcrypt(self.bcrypt_cost, &self.salt, input, &mut out);
        let fold_func = { |acc, &e| acc ^ e };
        return format!("{:02X}-{:02X}-{:02X}",
                       out[0..7].into_iter().fold(0xff, &fold_func),
                       out[8..15].into_iter().fold(0xff, &fold_func),
                       out[16..23].into_iter().fold(0xff, &fold_func))
    }
}


pub fn normalize_token(token: &str) -> String {
    token.to_lowercase().chars().filter(|c| c.is_digit(16)).collect::<String>()
}

#[cfg(test)]
mod tests {
    use super::*;


    #[test]
    fn test_normalize_token() {
        println!("{}", normalize_token(&"7A-74-F4".to_string()));
        assert!(normalize_token(&"7A-74-F4".to_string()) == "7a74f4");
    }

    #[test]
    fn test_generate_token() {
        use time;
        let tg = TokenGenerator::new(time::Duration::hours(2).num_seconds(),
                                     vec!(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16));
        let (valid_until, result) = tg.generate_token("a", 99999999);
        assert!( valid_until == 100000800);
        assert!( result  == "7A-74-F4");
    }
}