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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use simple_hyper_client::StatusCode;
#[cfg(feature = "native-tls")]
use tokio_native_tls::native_tls;
use serde::de::Error as DeserializeError;
use serde::ser::Error as SerializeError;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::time::SystemTime;
use std::{error, fmt, io};
use time::format_description::FormatItem;
use time::macros::format_description;
use time::{OffsetDateTime, PrimitiveDateTime};
use uuid::Uuid;
pub use crate::generated::*;
#[derive(Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Blob(Vec<u8>);
impl From<Vec<u8>> for Blob {
fn from(d: Vec<u8>) -> Self {
Blob(d)
}
}
impl From<String> for Blob {
fn from(s: String) -> Self {
Blob(s.into_bytes())
}
}
impl<'a> From<&'a str> for Blob {
fn from(s: &str) -> Self {
Blob(s.as_bytes().to_owned())
}
}
impl From<Blob> for Vec<u8> {
fn from(d: Blob) -> Self {
d.0
}
}
impl Deref for Blob {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Blob {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Serialize for Blob {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&base64::encode(&self.0))
}
}
impl<'de> Deserialize<'de> for Blob {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Visitor;
impl<'a> serde::de::Visitor<'a> for Visitor {
type Value = Blob;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "base64-encoded string")
}
fn visit_str<E: de::Error>(self, string: &str) -> Result<Blob, E> {
Ok(Blob(base64::decode(string).map_err(|_| {
de::Error::invalid_value(de::Unexpected::Str(string), &"base64 encoded string")
})?))
}
}
deserializer.deserialize_str(Visitor)
}
}
impl AsRef<[u8]> for Blob {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
pub type Name = String;
pub type Email = String;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Default)]
pub struct Time(pub u64);
static ISO_8601_FORMAT: &[FormatItem<'_>] = format_description!("[year][month][day]T[hour][minute][second]Z");
impl Serialize for Time {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let utc = self.to_utc_datetime().map_err(|e| S::Error::custom(e.to_string()))?;
let s = utc.format(ISO_8601_FORMAT).map_err(|e| S::Error::custom(e.to_string()))?;
serializer.serialize_str(&s)
}
}
impl<'de> Deserialize<'de> for Time {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s: String = Deserialize::deserialize(deserializer)?;
let t = PrimitiveDateTime::parse(&s, ISO_8601_FORMAT)
.map_err(|e| D::Error::custom(format!("expected date/time in ISO-8601 format: {}", e)))?;
Time::try_from(t.assume_utc()).map_err(|e| D::Error::custom(e))
}
}
impl Time {
pub fn now() -> Self {
let t = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
Self(t.as_secs())
}
pub fn to_utc_datetime(&self) -> Result<OffsetDateTime, TimeOutOfRange> {
if self.0 > i64::MAX as u64 {
return Err(TimeOutOfRange::TooLarge);
}
OffsetDateTime::from_unix_timestamp(self.0 as i64).map_err(|_| TimeOutOfRange::TooLarge)
}
}
impl TryFrom<OffsetDateTime> for Time {
type Error = TimeOutOfRange;
fn try_from(t: OffsetDateTime) -> Result<Self, Self::Error> {
if t.unix_timestamp() < 0 {
return Err(TimeOutOfRange::BeforeUnixEpoch);
}
Ok(Time(t.unix_timestamp() as u64))
}
}
#[derive(Debug)]
pub enum TimeOutOfRange {
BeforeUnixEpoch,
TooLarge,
}
impl fmt::Display for TimeOutOfRange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use TimeOutOfRange::*;
match *self {
BeforeUnixEpoch => write!(fmt, "date/times before Unix epoch (Jan. 1, 1970 00:00:00 UTC) cannot be stored as `Time`"),
TooLarge => write!(fmt, "`Time` value is out of range for `OffsetDateTime`"),
}
}
}
impl error::Error for TimeOutOfRange {}
#[derive(Debug)]
pub enum Error {
Unauthorized(String),
Forbidden(String),
BadRequest(String),
Conflict(String),
Locked(String),
NotFound(String),
StatusCode(String),
EncoderError(serde_json::error::Error),
IoError(io::Error),
NetworkError(simple_hyper_client::Error),
#[cfg(feature = "native-tls")]
TlsError(native_tls::Error),
}
impl error::Error for Error {
fn description(&self) -> &str {
"sdkms-client error"
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NotFound(ref msg) => write!(fmt, "{}", msg),
Error::Unauthorized(ref msg) => write!(fmt, "{}", msg),
Error::Forbidden(ref msg) => write!(fmt, "{}", msg),
Error::BadRequest(ref msg) => write!(fmt, "{}", msg),
Error::Conflict(ref msg) => write!(fmt, "{}", msg),
Error::Locked(ref msg) => write!(fmt, "{}", msg),
Error::EncoderError(ref err) => write!(fmt, "{}", err),
Error::IoError(ref err) => write!(fmt, "{}", err),
Error::NetworkError(ref err) => write!(fmt, "{}", err),
#[cfg(feature = "native-tls")]
Error::TlsError(ref err) => write!(fmt, "{}", err),
Error::StatusCode(ref msg) => write!(fmt, "unexpected status code: {}", msg),
}
}
}
impl Error {
pub fn from_status(status: StatusCode, msg: String) -> Self {
match status {
StatusCode::UNAUTHORIZED => Error::Unauthorized(msg),
StatusCode::FORBIDDEN => Error::Forbidden(msg),
StatusCode::BAD_REQUEST => Error::BadRequest(msg),
StatusCode::CONFLICT => Error::Conflict(msg),
StatusCode::LOCKED => Error::Locked(msg),
StatusCode::NOT_FOUND => Error::NotFound(msg),
_ => Error::StatusCode(format!("{}\n{}", status.to_string(), msg)),
}
}
}
impl From<serde_json::error::Error> for Error {
fn from(error: serde_json::error::Error) -> Error {
Error::EncoderError(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Error {
Error::IoError(error)
}
}
impl From<simple_hyper_client::Error> for Error {
fn from(error: simple_hyper_client::Error) -> Error {
Error::NetworkError(error)
}
}
#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(error: native_tls::Error) -> Error {
Error::TlsError(error)
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BatchEncryptRequestItem {
pub kid: Uuid,
pub request: EncryptRequest,
}
pub type BatchEncryptRequest = Vec<BatchEncryptRequestItem>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct BatchDecryptRequestItem {
pub kid: Uuid,
pub request: DecryptRequest,
}
pub type BatchDecryptRequest = Vec<BatchDecryptRequestItem>;
pub type BatchSignRequest = Vec<SignRequest>;
pub type BatchVerifyRequest = Vec<VerifyRequest>;
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum BatchResponseItem<T> {
Success { status: u16, body: T },
Error { status: u16, error: String },
}
impl<T> BatchResponseItem<T> {
pub fn status(&self) -> u16 {
match *self {
BatchResponseItem::Success { status, .. } | BatchResponseItem::Error { status, .. } => {
status
}
}
}
}
pub type BatchResponse<T> = Vec<BatchResponseItem<T>>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct AuthResponse {
pub token_type: String,
pub expires_in: u32,
pub access_token: String,
pub entity_id: Uuid,
pub challenge: Option<MfaChallengeResponse>,
}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct ApprovableResult {
pub status: u16,
pub body: serde_json::Value,
}
impl ApprovableResult {
pub fn is_ok(&self) -> bool {
200 <= self.status && self.status < 300
}
}
pub type PluginOutput = serde_json::Value;
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug)]
pub enum Order {
Ascending,
Descending,
}
impl FromStr for Order {
type Err = ();
fn from_str(order: &str) -> Result<Self, ()> {
match order {
"asc" => Ok(Order::Ascending),
"desc" => Ok(Order::Descending),
_ => Err(()),
}
}
}
impl fmt::Display for Order {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Order::Ascending => write!(f, "asc"),
Order::Descending => write!(f, "desc"),
}
}
}
impl Default for Order {
fn default() -> Self {
Order::Ascending
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppGroups(HashMap<Uuid, Option<AppPermissions>>);
impl Deref for AppGroups {
type Target = HashMap<Uuid, Option<AppPermissions>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for AppGroups {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<HashMap<Uuid, Option<AppPermissions>>> for AppGroups {
fn from(d: HashMap<Uuid, Option<AppPermissions>>) -> Self {
AppGroups(d)
}
}
impl From<AppGroups> for HashMap<Uuid, Option<AppPermissions>> {
fn from(d: AppGroups) -> Self {
d.0
}
}
impl Serialize for AppGroups {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0
.iter()
.map(|(id, perm)| (id, perm.unwrap_or(AppPermissions::empty())))
.collect::<HashMap<_, _>>()
.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for AppGroups {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(Deserialize)]
#[serde(untagged)]
enum S {
Modern(HashMap<Uuid, AppPermissions>),
Legacy(HashSet<Uuid>),
}
Ok(AppGroups(match S::deserialize(deserializer)? {
S::Modern(map) => map.into_iter().map(|(id, perm)| (id, Some(perm))).collect(),
S::Legacy(set) => set.into_iter().map(|id| (id, None)).collect(),
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn app_groups_modern() {
let id = Uuid::parse_str("34e03147-9f71-4be9-9a54-3feda0843393").unwrap();
let mut a = HashMap::new();
a.insert(id, Some(AppPermissions::ENCRYPT | AppPermissions::DECRYPT));
let a = AppGroups::from(a);
let json = r#"{"34e03147-9f71-4be9-9a54-3feda0843393":["ENCRYPT","DECRYPT"]}"#;
assert_eq!(serde_json::to_string(&a).unwrap(), json);
assert_eq!(a, serde_json::from_str(&json).unwrap());
let mut a = HashMap::new();
a.insert(id, Some(AppPermissions::empty()));
let a = AppGroups::from(a);
let json = r#"{"34e03147-9f71-4be9-9a54-3feda0843393":[]}"#;
assert_eq!(serde_json::to_string(&a).unwrap(), json);
assert_eq!(a, serde_json::from_str(&json).unwrap());
let a = AppGroups::from(HashMap::new());
let json = r#"{}"#;
assert_eq!(serde_json::to_string(&a).unwrap(), json);
assert_eq!(a, serde_json::from_str(&json).unwrap());
}
#[test]
fn app_groups_legacy() {
let id = Uuid::parse_str("34e03147-9f71-4be9-9a54-3feda0843393").unwrap();
let mut a = HashMap::new();
a.insert(id, None);
let a = AppGroups::from(a);
let json = r#"["34e03147-9f71-4be9-9a54-3feda0843393"]"#;
assert_eq!(a, serde_json::from_str(&json).unwrap());
let a = AppGroups::from(HashMap::new());
let json = r#"[]"#;
assert_eq!(a, serde_json::from_str(&json).unwrap());
}
#[test]
fn time() {
let t = Time::now();
t.to_utc_datetime().expect("in bounds");
serde_json::to_string(&t).expect("in bounds and correct format");
let t: Time = serde_json::from_str(r#""20200315T012345Z""#).expect("valid date/time");
assert_eq!(t.0, 1584235425);
let t: Time = serde_json::from_str(r#""19700101T000000Z""#).expect("valid date/time");
assert_eq!(t.0, 0);
let err = serde_json::from_str::<Time>(r#""20220119T024257""#).unwrap_err();
assert_eq!(err.to_string(), "expected date/time in ISO-8601 format: a character literal was not valid");
let err = serde_json::from_str::<Time>(r#""19670120T012345Z""#).unwrap_err();
assert_eq!(err.to_string(), "date/times before Unix epoch (Jan. 1, 1970 00:00:00 UTC) cannot be stored as `Time`");
let err = Time(i64::MAX as u64 + 10).to_utc_datetime().unwrap_err();
assert_eq!(err.to_string(), "`Time` value is out of range for `OffsetDateTime`");
let err = Time::try_from(OffsetDateTime::from_unix_timestamp(-1).unwrap()).unwrap_err();
assert_eq!(err.to_string(), "date/times before Unix epoch (Jan. 1, 1970 00:00:00 UTC) cannot be stored as `Time`");
}
}