Skip to main content

hc_vault/token/
auth.rs

1use std::time::{Duration, Instant};
2
3use crate::Auth as AuthTrait;
4use crate::Error;
5
6/// The actual token-auth session that can be used to
7/// authenticate with vault
8pub struct Session {
9    token: String,
10    token_start: Instant,
11    token_duration: Duration,
12}
13
14impl AuthTrait for Session {
15    fn is_expired(&self) -> bool {
16        self.token_start.elapsed() >= self.token_duration
17    }
18    fn get_token(&self) -> String {
19        self.token.clone()
20    }
21    fn auth(&self, _vault_url: &str) -> Result<(), Error> {
22        Ok(())
23    }
24    fn is_renewable(&self) -> bool {
25        true
26    }
27    fn get_total_duration(&self) -> u64 {
28        0
29    }
30    fn renew(&self, _vault_url: &str) -> Result<(), Error> {
31        Ok(())
32    }
33}
34
35impl Session {
36    /// Used to obtain a new valid auth session that can
37    /// be used with the vault client to authenticate
38    pub fn new(token: String, token_duration: Duration) -> Result<Session, Error> {
39        Ok(Session {
40            token,
41            token_start: Instant::now(),
42            token_duration,
43        })
44    }
45}