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
72
73
74
|
use crate::template;
/// The scope of the generated secret
#[derive(Debug)]
pub enum Scope {
AUTHENTICATION,
IDENTIFICATION,
RECOVERY,
}
impl Scope {
/// The default [`template::Template`] for the given 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,
}
}
}
/// Any error from parsing a Scope from a string
#[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),
}
}
}
/// A custom scoper simply needs to return an ideally unique string per-scope
pub trait Scoper: std::fmt::Debug {
fn scope(&self, s: Scope) -> String;
}
/// A generic [`Scoper`] that returns a key with suffix depending on the scope
#[derive(Debug)]
pub struct SimpleScoper {
key: String,
}
impl SimpleScoper {
/// A new SimpleScoper with a single key
pub fn new(key: impl Into<String>) -> Self {
Self { key: key.into() }
}
}
impl Default for SimpleScoper {
/// The default SimpleScoper uses the original masterpassword key
fn default() -> Self {
Self::new("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),
}
}
}
|