coyote 0.1.1

Embeddable ACME server with programmable challenges and storage
Documentation
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use std::convert::{TryFrom, TryInto};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use tokio_postgres::{Row, Transaction};
use url::Url;

use crate::{
    acme::{handlers::account::NewAccount, jose},
    errors::acme::JWSError,
    util::make_nonce,
};

use super::{LoadError, Postgres, Record, SaveError};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Account {
    pub id: Option<i32>,
    jwk_id: i32,
    orders_nonce: String,
    contacts: Vec<String>,
    created_at: chrono::DateTime<chrono::Local>,
    deleted_at: Option<chrono::DateTime<chrono::Local>>,
}

pub(crate) fn new_accounts(
    account: NewAccount,
    jwk: JWK,
    _db: Postgres,
) -> Result<Account, LoadError> {
    let jwk_id = jwk.id()?;

    if jwk_id.is_none() {
        return Err(LoadError::Generic(
            "missing JWK in account save".to_string(),
        ));
    }

    let contacts = account.contacts();
    let contacts = contacts.unwrap();
    let jwk_id = jwk_id.unwrap();

    Ok(Account::new(
        jwk_id,
        contacts
            .iter()
            .map(|c| c.to_owned().into())
            .collect::<Vec<String>>(),
    ))
}

pub async fn get_contacts_for_account(
    id: i32,
    tx: &Transaction<'_>,
) -> Result<Vec<String>, LoadError> {
    let mut contacts = Vec::new();

    let rows = tx
        .query("select contact from contacts where account_id=$1", &[&id])
        .await?;
    for row in rows {
        contacts.push(row.get("contact"));
    }

    Ok(contacts)
}

impl Account {
    pub fn new(jwk_id: i32, contacts: Vec<String>) -> Self {
        Self {
            jwk_id,
            contacts,
            orders_nonce: make_nonce(super::NONCE_KEY_SIZE),
            id: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }

    pub async fn find_by_kid(jwk_id: i32, db: Postgres) -> Result<Self, LoadError> {
        let mut lockeddb = db.client().await?;
        let tx = lockeddb.transaction().await?;

        let res = tx
            .query_one("select * from accounts where jwk_id=$1", &[&jwk_id])
            .await?;

        Self::new_from_row(&res, &tx).await
    }

    pub async fn find_deleted(id: i32, db: Postgres) -> Result<Self, LoadError> {
        let mut lockeddb = db.client().await?;
        let tx = lockeddb.transaction().await?;

        let res = tx
            .query_one("select * from accounts where id=$1", &[&id])
            .await?;

        Self::new_from_row(&res, &tx).await
    }
}

#[async_trait]
impl Record<i32> for Account {
    async fn new_from_row(row: &Row, tx: &Transaction<'_>) -> Result<Self, LoadError> {
        Ok(Self {
            id: Some(row.get("id")),
            jwk_id: row.get("jwk_id"),
            orders_nonce: row.get("orders_nonce"),
            contacts: get_contacts_for_account(row.get("id"), tx).await?,
            created_at: row.get("created_at"),
            deleted_at: row.get("deleted_at"),
        })
    }

    async fn find(id: i32, db: Postgres) -> Result<Self, LoadError> {
        let mut lockeddb = db.client().await?;
        let tx = lockeddb.transaction().await?;

        let res = tx
            .query_one(
                "select * from accounts where id=$1 and deleted_at is null",
                &[&id],
            )
            .await?;

        Self::new_from_row(&res, &tx).await
    }

    fn id(&self) -> Result<Option<i32>, LoadError> {
        Ok(self.id)
    }

    async fn create(&mut self, db: Postgres) -> Result<i32, SaveError> {
        let mut db = db.client().await?;
        let tx = db.transaction().await?;

        let res = tx
            .query_one(
                "
                    insert into accounts (jwk_id, orders_nonce) values ($1, $2)
                    returning id, created_at
                ",
                &[&self.jwk_id, &self.orders_nonce],
            )
            .await?;

        let id = res.get("id");
        let created_at = res.get("created_at");

        self.id = Some(id);
        self.created_at = created_at;

        for contact in &self.contacts {
            tx.query_one(
                "
                        insert into contacts (account_id, contact) values ($1, $2)
                        returning id, created_at
                    ",
                &[&id, &contact],
            )
            .await?;
        }

        tx.commit().await?;

        return Ok(id);
    }

    async fn delete(&self, db: Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "this JWK record was never saved".to_string(),
            ));
        }

        let mut db = db.client().await?;
        let tx = db.transaction().await?;
        let res = tx
            .execute(
                "update accounts set deleted_at=CURRENT_TIMESTAMP where id=$1 and deleted_at is null",
                &[&self.id.unwrap()],
            )
            .await?;

        if res == 0 {
            // FIXME this should probably be a log warning later
            return Err(SaveError::Generic(
                "db did not delete primary key; was already removed".to_string(),
            ));
        }

        Ok(tx.commit().await?)
    }

    async fn update(&self, _db: Postgres) -> Result<(), SaveError> {
        Err(SaveError::Generic(
            "accounts may not be updated".to_string(),
        ))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JWK {
    pub id: Option<i32>,
    pub nonce_key: String,
    pub alg: String,
    pub n: Option<String>,
    pub e: Option<String>,
    pub x: Option<String>,
    pub y: Option<String>,
    pub created_at: chrono::DateTime<chrono::Local>,
    pub deleted_at: Option<chrono::DateTime<chrono::Local>>,
}

impl JWK {
    pub fn new_rs256(n: String, e: String) -> Self {
        Self {
            id: None,
            nonce_key: make_nonce(super::NONCE_KEY_SIZE),
            n: Some(n),
            e: Some(e),
            alg: "RS256".into(),
            x: None,
            y: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }

    pub fn new_es256(x: String, y: String) -> Self {
        Self {
            id: None,
            nonce_key: make_nonce(super::NONCE_KEY_SIZE),
            x: Some(x),
            y: Some(y),
            alg: "ES256".into(),
            e: None,
            n: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }

    pub async fn find_deleted(id: i32, db: Postgres) -> Result<Self, LoadError> {
        let mut db = db.client().await?;
        let tx = db.transaction().await?;

        let res = tx
            .query_one("select * from jwks where id=$1", &[&id])
            .await?;

        Self::new_from_row(&res, &tx).await
    }

    pub async fn find_by_nonce(nonce_key: String, db: Postgres) -> Result<Self, LoadError> {
        let res = db
            .clone()
            .client()
            .await?
            .query_one(
                "select id from jwks where nonce_key=$1 and deleted_at is null",
                &[&nonce_key],
            )
            .await;

        match res {
            Ok(row) => {
                let id: i32 = row.get(0);
                Self::find(id, db).await
            }

            Err(_) => Err(LoadError::NotFound),
        }
    }

    pub async fn find_by_kid(url: Url, db: Postgres) -> Result<Self, LoadError> {
        if let None = url.path_segments() {
            return Err(LoadError::NotFound);
        }

        if let None = url.path_segments().unwrap().last() {
            return Err(LoadError::NotFound);
        }

        Self::find_by_nonce(url.path_segments().unwrap().last().unwrap().to_string(), db).await
    }

    pub fn nonce_key(&self) -> String {
        self.nonce_key.clone()
    }
}

impl TryFrom<&mut jose::JWK> for JWK {
    type Error = JWSError;

    fn try_from(jwk: &mut jose::JWK) -> Result<Self, Self::Error> {
        let (alg, n, e, x, y) = jwk.params();

        let alg = match alg {
            Some(alg) => alg,
            None => return Err(JWSError::InvalidPublicKey),
        };

        Ok(JWK {
            nonce_key: make_nonce(super::NONCE_KEY_SIZE),
            n,
            e,
            x,
            y,
            alg,
            id: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        })
    }
}

impl TryInto<jose::JWK> for JWK {
    type Error = JWSError;

    fn try_into(self) -> Result<jose::JWK, Self::Error> {
        let mut crv = None;

        if self.x.is_some() && self.y.is_some() {
            // NOTE once more algos are supported this will need to change
            crv = Some("P-256".to_string())
        }

        Ok(jose::JWK {
            _use: None,
            kty: match self.alg.as_str() {
                "ES256" => "ECDSA",
                "RS256" => "RSA",
                _ => "you should really be validating this field",
            }
            .to_string(),
            crv,
            n: self.n,
            e: self.e,
            x: self.x,
            y: self.y,
            alg: Some(self.alg),
        })
    }
}

#[async_trait]
impl Record<i32> for JWK {
    async fn new_from_row(row: &Row, _tx: &Transaction<'_>) -> Result<Self, LoadError> {
        Ok(Self {
            id: Some(row.get("id")),
            nonce_key: row.get("nonce_key"),
            n: row.get("n"),
            e: row.get("e"),
            alg: row.get("alg"),
            x: row.get("x"),
            y: row.get("y"),
            created_at: row.get("created_at"),
            deleted_at: row.get("deleted_at"),
        })
    }

    async fn find(id: i32, db: Postgres) -> Result<Self, LoadError> {
        let mut db = db.client().await?;
        let tx = db.transaction().await?;

        let res = tx
            .query_one(
                "select * from jwks where id=$1 and deleted_at is null",
                &[&id],
            )
            .await?;

        Self::new_from_row(&res, &tx).await
    }

    fn id(&self) -> Result<Option<i32>, LoadError> {
        Ok(self.id)
    }

    async fn create(&mut self, db: Postgres) -> Result<i32, SaveError> {
        let mut db = db.client().await?;
        let tx = db.transaction().await?;

        let res = tx
            .query_one(
                "
        insert into jwks (nonce_key, n, e, alg, x, y) values ($1, $2, $3, $4, $5, $6)
        returning id, created_at
        ",
                &[
                    &self.nonce_key,
                    &self.n,
                    &self.e,
                    &self.alg,
                    &self.x,
                    &self.y,
                ],
            )
            .await?;

        tx.commit().await?;

        let id = res.get("id");
        let created_at = res.get("created_at");
        self.id = Some(id);
        self.created_at = created_at;

        return Ok(id);
    }

    async fn delete(&self, db: Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "this JWK record was never saved".to_string(),
            ));
        }

        let mut db = db.client().await?;
        let tx = db.transaction().await?;
        let res = tx
            .execute(
                "update jwks set deleted_at=CURRENT_TIMESTAMP where id=$1 and deleted_at is null",
                &[&self.id.unwrap()],
            )
            .await?;

        if res == 0 {
            // FIXME this should probably be a log warning later
            return Err(SaveError::Generic(
                "db did not delete primary key; was already removed".to_string(),
            ));
        }

        Ok(tx.commit().await?)
    }

    async fn update(&self, _db: Postgres) -> Result<(), SaveError> {
        Err(SaveError::Generic("JWKs may not be updated".to_string()))
    }
}

mod tests {
    #[tokio::test(flavor = "multi_thread")]
    async fn account_crud_single_contact() {
        use spectral::prelude::*;

        use super::{Account, JWK};
        use crate::acme::handlers::account::NewAccount;
        use crate::models::Record;
        use crate::test::PGTest;
        use std::convert::TryInto;

        let pg = PGTest::new("account_crud_single_contact").await.unwrap();

        let mut acct = NewAccount::default();
        acct.contact = Some(vec!["mailto:erik@hollensbe.org".try_into().unwrap()]);

        let mut jwk = JWK::new_es256("x".to_string(), "y".to_string());
        jwk.create(pg.db()).await.unwrap();
        let acct = super::new_accounts(acct, jwk, pg.db());
        assert_that!(acct).is_ok();
        let mut acct = acct.unwrap();
        assert_that!(acct.create(pg.db()).await).is_ok();
        let id = acct.id();
        assert_that!(id).is_ok();
        let id = id.unwrap();
        assert_that!(id).is_some();
        assert_that!(id.unwrap()).is_not_equal_to(0);
        let id = id.unwrap();

        let newacct = Account::find(id, pg.db()).await.unwrap();
        assert_that!(acct).is_equal_to(newacct);

        assert_that!(acct.delete(pg.db()).await).is_ok();
        assert_that!(acct.delete(pg.db()).await).is_err();

        let oldacct = Account::find_deleted(id, pg.db()).await.unwrap();

        acct.deleted_at = oldacct.deleted_at;

        assert_that!(acct).is_equal_to(oldacct);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn jwk_check_constraint() {
        use spectral::prelude::*;

        // this test ensures that n & e and x & y are sticky to each other. other validation is performed outside
        // the db, but this is good for keeping the db hygenic.
        use crate::test::PGTest;
        use tokio_postgres::types::ToSql;

        let pg = PGTest::new("jwk_check_constraint").await.unwrap();

        let bad: &[(&str, &[&(dyn ToSql + Sync)])] = &[
            (
                "insert into jwks (nonce_key, n, x, alg) values ($1, $2, $3, $4)",
                &[
                    &"firstbad".to_string(),
                    &"aaaa".to_string(),
                    &"bbbb".to_string(),
                    &"alg".to_string(),
                ],
            ),
            (
                "insert into jwks (nonce_key, e, y, alg) values ($1, $2, $3, $4)",
                &[
                    &"secondbad".to_string(),
                    &"aaaa".to_string(),
                    &"bbbb".to_string(),
                    &"alg".to_string(),
                ],
            ),
        ];

        for args in bad.iter() {
            let res = pg
                .db()
                .client()
                .await
                .unwrap()
                .execute(args.0, args.1)
                .await;
            assert_that!(res).is_err();
        }

        let good: &[(&str, &[&(dyn ToSql + Sync)])] = &[
            (
                "insert into jwks (nonce_key, n, e, alg) values ($1, $2, $3, $4)",
                &[
                    &"firstgood".to_string(),
                    &"aaaa".to_string(),
                    &"bbbb".to_string(),
                    &"alg".to_string(),
                ],
            ),
            (
                "insert into jwks (nonce_key, x, y, alg) values ($1, $2, $3, $4)",
                &[
                    &"secondgood".to_string(),
                    &"aaaa".to_string(),
                    &"bbbb".to_string(),
                    &"alg".to_string(),
                ],
            ),
        ];

        for args in good.iter() {
            let res = pg
                .db()
                .client()
                .await
                .unwrap()
                .execute(args.0, args.1)
                .await;
            assert_that!(res).is_ok();
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn jwk_find_nonexistent_nonce() {
        use spectral::prelude::*;

        use super::JWK;
        use crate::errors::db::LoadError;
        use crate::test::PGTest;

        let pg = PGTest::new("jwk_find_nonexistent_nonce").await.unwrap();

        let res = JWK::find_by_nonce("abcdef".to_string(), pg.db()).await;
        assert_that!(res).is_err();
        assert_that!(res.unwrap_err()).matches(|e| match e {
            // ugh!
            &LoadError::NotFound => true,
            _ => false,
        });
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn jwk_create_delete_find() {
        use spectral::prelude::*;

        use super::JWK;
        use crate::models::Record;
        use crate::test::PGTest;

        let pg = PGTest::new("jwk_create_delete_find").await.unwrap();

        let jwks = &mut [
            JWK::new_rs256("aaaaaa".to_string(), "bbbb".to_string()),
            JWK::new_es256("aaaaaa".to_string(), "bbbb".to_string()),
        ];

        for origjwk in jwks.iter_mut() {
            let res = origjwk.create(pg.db()).await;
            assert_that!(res).is_ok();

            let res = JWK::find(origjwk.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(res).is_ok();

            let jwk = res.unwrap();

            assert_that!(jwk).is_equal_to(origjwk.clone());

            let res = JWK::find_by_nonce(origjwk.nonce_key(), pg.db()).await;
            assert_that!(res).is_ok();

            let jwk = res.unwrap();

            assert_that!(jwk).is_equal_to(origjwk.clone());

            let res = origjwk.delete(pg.db()).await;
            assert_that!(res).is_ok();

            let res = origjwk.delete(pg.db()).await;
            assert_that!(res).is_err();

            let res = JWK::find(origjwk.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(res).is_err();

            let res = JWK::find_by_nonce(origjwk.nonce_key(), pg.db()).await;
            assert_that!(res).is_err();

            let res = JWK::find_deleted(origjwk.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(res).is_ok();

            let jwk = res.unwrap();
            origjwk.deleted_at = jwk.deleted_at;

            assert_that!(jwk).is_equal_to(origjwk.clone());
        }
    }
}