Home

rs-spectre @f28602ec5e7ed32e0c78ab3401dcb47cbf705f44 - refs - log -
-
https://git.jolheiser.com/rs-spectre.git
Rust implementation for Spectre/Masterpassword
rs-spectre / spectre / src / scope.rs
- raw
 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
use crate::template;

#[derive(Debug)]
pub enum Scope {
    AUTHENTICATION,
    IDENTIFICATION,
    RECOVERY,
}

impl Scope {
    pub fn default_template(&self) -> template::Template {
        match self {
            Scope::AUTHENTICATION => template::Template::Long,
            Scope::IDENTIFICATION => template::Template::Name,
            Scope::RECOVERY => template::Template::Phrase,
        }
    }
}

#[derive(Debug)]
pub enum ParseScopeError {
    Invalid,
}

impl std::str::FromStr for Scope {
    type Err = ParseScopeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "AUTHENTICATION" => Ok(Self::AUTHENTICATION),
            "IDENTIFICATION" => Ok(Self::IDENTIFICATION),
            "RECOVERY" => Ok(Self::RECOVERY),
            _ => Err(ParseScopeError::Invalid),
        }
    }
}

pub trait Scoper: std::fmt::Debug {
    fn scope(&self, s: Scope) -> String;
}

#[derive(Debug)]
pub struct SimpleScoper {
    key: String,
}

impl SimpleScoper {
    pub fn new(key: String) -> Self {
        Self { key: key }
    }
}

impl Default for SimpleScoper {
    fn default() -> Self {
        Self {
            key: String::from("com.lyndir.masterpassword"),
        }
    }
}

impl Scoper for SimpleScoper {
    fn scope(&self, s: Scope) -> String {
        match s {
            Scope::AUTHENTICATION => self.key.clone(),
            Scope::IDENTIFICATION => format!("{}.login", self.key),
            Scope::RECOVERY => format!("{}.answer", self.key),
        }
    }
}