woo
parent: tbd commit: 2d48bc0
1 | use Error; |
2 | use ; |
3 | use |
4 | , |
5 | , |
6 | , | Sha256
7 | , |
8 | RsaPrivateKey, RsaPublicKey, |
9 | ; |
10 | use ; |
11 | use Debug; |
12 | |
13 | use crate:: |
14 | , | UserTokenMetadata
15 | , | HandshakeMessage
16 | , |
17 | ; |
18 | |
19 | use |
20 | , DiscoveryMessage, | AuthenticationMessage
21 | , | RepositoryMessage
22 | ; |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | Handshake, |
33 | Repository, |
34 | Authentication, |
35 | Discovery, |
36 | |
37 | |
38 | /// An authenticated message, where the instance is authenticating |
39 | /// a request it is making for itself. |
40 | |
41 | |
42 | message: T, |
43 | instance: Instance, |
44 | signature: , |
45 | |
46 | |
47 | |
48 | |
49 | T: PartialEq + Serialize, |
50 | |
51 | |
52 | self.message == other.message |
53 | && self.instance == other.instance |
54 | && self.signature == other.signature |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | T: Hash + Serialize, |
63 | |
64 | |
65 | self.message.hash; |
66 | self.instance.hash; |
67 | self.signature.hash; |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | T: Clone + Serialize, |
74 | |
75 | |
76 | Self |
77 | message: self.message.clone, |
78 | instance: self.instance.clone, |
79 | signature: self.signature.clone, |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | T: Debug + Serialize, |
87 | |
88 | |
89 | f.debug_struct |
90 | .field |
91 | .field |
92 | .field |
93 | .finish |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | let mut rng = thread_rng; |
100 | |
101 | let private_key = from_pkcs1_pem?; |
102 | let signing_key = new; |
103 | |
104 | let message_json = to_vec?; |
105 | |
106 | let signature = signing_key.sign_with_rng; |
107 | |
108 | Ok |
109 | message, |
110 | instance, |
111 | signature: signature.to_vec, |
112 | |
113 | |
114 | |
115 | pub async |
116 | &self.message |
117 | |
118 | |
119 | pub async |
120 | let public_key = public_key .await?; |
121 | let public_key = from_pkcs1_pem .unwrap; |
122 | |
123 | let verifying_key: = new; |
124 | |
125 | let message_json = to_vec .unwrap; |
126 | |
127 | verifying_key |
128 | .verify |
129 | &message_json, |
130 | & try_from .unwrap, |
131 | |
132 | .unwrap; |
133 | |
134 | Ok |
135 | |
136 | |
137 | |
138 | /// An authenticated message. |
139 | /// |
140 | /// Includes the message, with a digest generated with |
141 | /// our private key. |
142 | |
143 | |
144 | #[serde(flatten)] |
145 | message: T, |
146 | pub(crate) user: User, |
147 | |
148 | |
149 | |
150 | |
151 | T: Clone + Serialize, |
152 | |
153 | |
154 | Self |
155 | message: self.message.clone, |
156 | user: self.user.clone, |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | T: Debug + Serialize, |
164 | |
165 | |
166 | f.debug_struct |
167 | .field |
168 | .field |
169 | .finish |
170 | |
171 | |
172 | |
173 | |
174 | pub async |
175 | &self.message |
176 | |
177 | |
178 | pub async |
179 | &self.user |
180 | |
181 | |
182 | |
183 | /// An unvalidated authenticated message. |
184 | /// |
185 | /// Includes the message, with a digest generated with |
186 | /// our private key. |
187 | |
188 | |
189 | #[serde(flatten)] |
190 | message: T, |
191 | token: String, |
192 | digest: , |
193 | |
194 | |
195 | |
196 | |
197 | T: Clone + Serialize, |
198 | |
199 | |
200 | Self |
201 | message: self.message.clone, |
202 | token: self.token.clone, |
203 | digest: self.digest.clone, |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | T: Debug + Serialize, |
211 | |
212 | |
213 | f.debug_struct |
214 | .field |
215 | .field |
216 | .field |
217 | .finish |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | let mut rng = thread_rng; |
224 | |
225 | let private_key = from_pkcs1_pem?; |
226 | let signing_key = new; |
227 | |
228 | let message_json = to_vec?; |
229 | |
230 | let signature = signing_key.sign_with_rng; |
231 | |
232 | Ok |
233 | message, |
234 | token, |
235 | digest: signature.to_vec, |
236 | |
237 | |
238 | |
239 | pub async |
240 | &self.message |
241 | |
242 | |
243 | pub async |
244 | let instance = |
245 | let mut validation = new; |
246 | validation.insecure_disable_signature_validation; |
247 | |
248 | let value: = |
249 | decode .unwrap; |
250 | |
251 | value.claims.generated_for.clone |
252 | ; |
253 | |
254 | let public_key_raw = public_key .await?; |
255 | let public_key = from_pkcs1_pem .unwrap; |
256 | |
257 | let verifying_key: = new; |
258 | |
259 | let message_json = to_vec .unwrap; |
260 | |
261 | verifying_key |
262 | .verify |
263 | &message_json, |
264 | & try_from .unwrap, |
265 | |
266 | .unwrap; |
267 | |
268 | let verification_key = from_rsa_pem .unwrap; |
269 | |
270 | let data: = decode |
271 | &self.token, |
272 | &verification_key, |
273 | & new, |
274 | |
275 | .unwrap; |
276 | |
277 | assert_eq!; |
278 | |
279 | Ok |
280 | message: self.message, |
281 | user: data.claims.user, |
282 | |
283 | |
284 | |
285 | |
286 | async |
287 | let key = get |
288 | .await? |
289 | .text |
290 | .await?; |
291 | |
292 | Ok |
293 | |
294 |