-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.rs
155 lines (133 loc) · 3.75 KB
/
config.rs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::fmt::Display;
use std::time::Duration;
static API1_HOST: &str = "https://api.yubico.com/wsapi/2.0/verify";
static API2_HOST: &str = "https://api2.yubico.com/wsapi/2.0/verify";
static API3_HOST: &str = "https://api3.yubico.com/wsapi/2.0/verify";
static API4_HOST: &str = "https://api4.yubico.com/wsapi/2.0/verify";
static API5_HOST: &str = "https://api5.yubico.com/wsapi/2.0/verify";
#[derive(Clone, Debug, PartialEq)]
pub enum Slot {
Slot1,
Slot2,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Mode {
Sha1,
Otp,
}
/// From the Validation Protocol documentation:
///
/// A value 0 to 100 indicating percentage of syncing required by client,
/// or strings "fast" or "secure" to use server-configured values; if
/// absent, let the server decide.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct SyncLevel(u8);
impl SyncLevel {
pub fn fast() -> SyncLevel {
SyncLevel(0)
}
pub fn secure() -> SyncLevel {
SyncLevel(100)
}
pub fn custom(level: u8) -> SyncLevel {
if level > 100 {
SyncLevel(100)
} else {
SyncLevel(level)
}
}
}
impl Display for SyncLevel {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Config {
pub client_id: String,
pub key: Vec<u8>,
pub api_hosts: Vec<String>,
pub user_agent: String,
pub sync_level: SyncLevel,
/// The timeout for HTTP requests.
pub request_timeout: Duration,
pub proxy_url: String,
pub proxy_username: String,
pub proxy_password: String,
}
#[allow(dead_code)]
impl Config {
pub fn default() -> Config {
Config {
client_id: String::new(),
key: Vec::new(),
api_hosts: build_hosts(),
user_agent: "github.com/wisespace-io/yubico-rs".to_string(),
sync_level: SyncLevel::secure(),
request_timeout: Duration::from_secs(30), // Value taken from the reqwest crate.
proxy_url: String::new(),
proxy_username: String::new(),
proxy_password: String::new(),
}
}
pub fn set_client_id<C>(mut self, client_id: C) -> Self
where
C: Into<String>,
{
self.client_id = client_id.into();
self
}
pub fn set_key<K>(mut self, key: K) -> Self
where
K: Into<String>,
{
self.key = key.into().into_bytes();
self
}
pub fn set_api_hosts(mut self, hosts: Vec<String>) -> Self {
self.api_hosts = hosts;
self
}
pub fn set_user_agent(mut self, user_agent: String) -> Self {
self.user_agent = user_agent;
self
}
pub fn set_sync_level(mut self, level: SyncLevel) -> Self {
self.sync_level = level;
self
}
pub fn set_request_timeout(mut self, timeout: Duration) -> Self {
self.request_timeout = timeout;
self
}
pub fn set_proxy_url<P>(mut self, proxy_url: P) -> Self
where
P: Into<String>,
{
self.proxy_url = proxy_url.into();
self
}
pub fn set_proxy_username<U>(mut self, proxy_username: U) -> Self
where
U: Into<String>,
{
self.proxy_username = proxy_username.into();
self
}
pub fn set_proxy_password<P>(mut self, proxy_password: P) -> Self
where
P: Into<String>,
{
self.proxy_password = proxy_password.into();
self
}
}
fn build_hosts() -> Vec<String> {
let mut hosts: Vec<String> = Vec::new();
hosts.push(API1_HOST.to_string());
hosts.push(API2_HOST.to_string());
hosts.push(API3_HOST.to_string());
hosts.push(API4_HOST.to_string());
hosts.push(API5_HOST.to_string());
hosts
}