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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use crate::directory::Directory;
use crate::helpers::*;
use anyhow::Error;
use openssl::pkey::PKey;
use openssl::pkey::Private;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use tracing::field;
use tracing::instrument;
use tracing::Level;
use tracing::Span;
#[derive(Deserialize, Eq, PartialEq, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum AccountStatus {
Valid,
Deactivated,
Revoked,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Account {
#[serde(skip)]
pub(crate) directory: Option<Arc<Directory>>,
#[serde(skip)]
pub(crate) private_key: Option<PKey<Private>>,
#[serde(skip)]
pub(crate) private_key_id: String,
pub status: AccountStatus,
pub contact: Option<Vec<String>>,
pub terms_of_service_agreed: Option<bool>,
}
#[derive(Debug)]
pub struct AccountBuilder {
directory: Arc<Directory>,
private_key: Option<PKey<Private>>,
contact: Option<Vec<String>>,
terms_of_service_agreed: Option<bool>,
only_return_existing: Option<bool>,
}
impl AccountBuilder {
pub fn new(directory: Arc<Directory>) -> Self {
AccountBuilder {
directory,
private_key: None,
contact: None,
terms_of_service_agreed: None,
only_return_existing: None,
}
}
pub fn private_key(&mut self, private_key: PKey<Private>) -> &mut Self {
self.private_key = Some(private_key);
self
}
pub fn contact(&mut self, contact: Vec<String>) -> &mut Self {
self.contact = Some(contact);
self
}
pub fn terms_of_service_agreed(
&mut self,
terms_of_service_agreed: bool,
) -> &mut Self {
self.terms_of_service_agreed = Some(terms_of_service_agreed);
self
}
pub fn only_return_existing(
&mut self,
only_return_existing: bool,
) -> &mut Self {
self.only_return_existing = Some(only_return_existing);
self
}
#[instrument(level = Level::INFO, name = "acme2::AccountBuilder::build", err, skip(self), fields(contact = ?self.contact, terms_of_service_agreed = ?self.terms_of_service_agreed, only_return_existing = ?self.only_return_existing, private_key_id = field::Empty))]
pub async fn build(&mut self) -> Result<Arc<Account>, Error> {
let private_key = if let Some(private_key) = self.private_key.clone() {
private_key
} else {
gen_rsa_private_key(4096)?
};
let url = self.directory.new_account_url.clone();
let (res, headers) = self
.directory
.authenticated_request::<_, Account>(
&url,
json!({
"contact": self.contact,
"termsOfServiceAgreed": self.terms_of_service_agreed,
"onlyReturnExisting": self.only_return_existing
}),
private_key.clone(),
None,
)
.await?;
let res: Result<Account, Error> = res.into();
let mut acc = res?;
let private_key_id = headers
.get(reqwest::header::LOCATION)
.ok_or_else(|| {
anyhow::anyhow!("mandatory location header in newAccount not present")
})?
.to_str()?
.to_string();
Span::current().record("private_key_id", &field::display(&private_key_id));
acc.directory = Some(self.directory.clone());
acc.private_key = Some(private_key);
acc.private_key_id = private_key_id;
Ok(Arc::new(acc))
}
}
impl Account {
pub fn private_key(&self) -> PKey<Private> {
self.private_key.clone().unwrap()
}
}