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
use crate::config::Config;
use crate::errors::*;
use rand::seq::SliceRandom;
use std::fs;
use std::path::{Path, PathBuf};

const VALID_CHARS: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

#[inline]
pub fn valid_token(t: &str) -> bool {
    t.chars().all(|c| VALID_CHARS.contains(c))
}

pub struct Challenge {
    path: PathBuf,
    written: Vec<PathBuf>,
}

impl Challenge {
    pub fn new(config: &Config) -> Challenge {
        let chall_dir = Path::new(&config.system.chall_dir);
        // TODO: consider creating the directory
        Challenge {
            path: chall_dir.join("challs"),
            written: Vec::new(),
        }
    }

    pub fn write(&mut self, token: &str, proof: &str) -> Result<()> {
        if !valid_token(token) {
            bail!("ACME server sent us malicious token")
        }

        let path = self.path.join(token);
        debug!("Writing challenge proof to {:?}", path);
        fs::write(&path, proof).context("Failed to write challenge proof")?;

        self.written.push(path);

        Ok(())
    }

    pub fn random(&mut self) -> Result<String> {
        const TOKEN_LEN: usize = 16;
        let mut rng = rand::thread_rng();

        let random = VALID_CHARS
            .as_bytes()
            .choose_multiple(&mut rng, TOKEN_LEN)
            .map(|b| *b as char)
            .collect::<String>();

        self.write(&random, &random)?;
        Ok(random)
    }

    pub fn cleanup(&mut self) -> Result<()> {
        for path in self.written.drain(..) {
            debug!("Deleting old challenge proof: {:?}", path);
            fs::remove_file(path)?;
        }
        Ok(())
    }
}