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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
use std::convert::{TryFrom, TryInto};
use std::ops::Add;

use async_trait::async_trait;
use openssl::x509::X509;
use tokio_postgres::{Row, Transaction};
use url::Url;

use super::{Postgres, Record, RecordList};
use crate::acme::challenge::ChallengeType;
use crate::acme::ACMEIdentifier;
use crate::{
    acme::{dns::DNSName, handlers::order::OrderStatus},
    errors::db::{LoadError, SaveError},
    util::make_nonce,
};

#[derive(Debug, Clone, PartialEq)]
pub struct Order {
    id: Option<i32>,
    pub order_id: String,
    pub error: Option<crate::errors::Error>,
    pub status: OrderStatus,
    pub created_at: chrono::DateTime<chrono::Local>,
    pub authorizations: Option<Vec<Authorization>>,
    pub not_before: Option<chrono::DateTime<chrono::Local>>,
    pub not_after: Option<chrono::DateTime<chrono::Local>>,
    expires: Option<chrono::DateTime<chrono::Local>>,
    finalized: bool,
    deleted_at: Option<chrono::DateTime<chrono::Local>>,
}

impl Default for Order {
    fn default() -> Self {
        Self {
            id: None,
            order_id: make_nonce(super::NONCE_KEY_SIZE),
            finalized: false,
            expires: None,
            not_before: None,
            not_after: None,
            error: None,
            status: OrderStatus::Pending,
            authorizations: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }
}

impl Order {
    pub(crate) fn new(
        not_before: Option<chrono::DateTime<chrono::Local>>,
        not_after: Option<chrono::DateTime<chrono::Local>>,
    ) -> Order {
        Order {
            not_before,
            not_after,
            ..Default::default()
        }
    }

    pub(crate) async fn find_by_reference(
        order_id: String,
        db: Postgres,
    ) -> Result<Self, LoadError> {
        let mut client = db.clone().client().await?;
        let tx = client.transaction().await?;
        let res = tx
            .query_one("select id from orders where order_id = $1", &[&order_id])
            .await;

        match res {
            Ok(row) => {
                let id = row.get(0);
                drop(tx);
                Self::find(id, db).await
            }
            Err(_) => Err(LoadError::NotFound),
        }
    }

    pub(crate) fn into_handler_order(
        self,
        url: Url,
    ) -> Result<crate::acme::handlers::order::Order, LoadError> {
        let auths = self.authorizations;

        let mut dt_expires: Option<chrono::DateTime<chrono::Local>> = None;
        let mut dt_notbefore: Option<chrono::DateTime<chrono::Local>> = None;
        let mut dt_notafter: Option<chrono::DateTime<chrono::Local>> = None;

        if let Some(expires) = self.expires {
            dt_expires = Some(expires.into())
        }

        if let Some(notbefore) = self.not_before {
            dt_notbefore = Some(notbefore.into())
        }

        if let Some(notafter) = self.not_after {
            dt_notafter = Some(notafter.into())
        }

        let o = crate::acme::handlers::order::Order {
            status: Some(self.status.clone()),
            expires: dt_expires,
            identifiers: if auths.clone().is_some() {
                auths
                    .clone()
                    .unwrap()
                    .iter()
                    // FIXME remove these unwraps
                    .map(|a| {
                        ACMEIdentifier::DNS(
                            DNSName::from_str(&a.identifier.clone().unwrap()).unwrap(),
                        )
                    })
                    .collect::<Vec<ACMEIdentifier>>()
            } else {
                Vec::new()
            },
            not_after: dt_notafter,
            not_before: dt_notbefore,
            error: self.error,
            authorizations: if auths.clone().is_some() {
                Some(
                    auths
                        .clone()
                        .unwrap()
                        .iter()
                        .map(|x: &Authorization| x.into_url(url.clone()))
                        .collect::<Vec<url::Url>>(),
                )
            } else {
                None
            },
            finalize: Some(
                url.join(&format!("/order/{}/finalize", self.order_id))
                    .unwrap(),
            ),
            // FIXME this needs to be at a unique location, not related to the order id
            certificate: Some(
                url.join(&format!("/order/{}/certificate", self.order_id))
                    .unwrap(),
            ),
        };

        Ok(o)
    }

    // FIXME this is only used in tests rn
    #[cfg(test)]
    pub(crate) async fn challenges(
        &self,
        tx: &Transaction<'_>,
    ) -> Result<Vec<Challenge>, LoadError> {
        Challenge::collect(self.order_id.clone(), tx).await
    }

    pub(crate) async fn record_certificate(
        &self,
        certificate: X509,
        db: Postgres,
    ) -> Result<i32, SaveError> {
        let mut cert = Certificate::default();
        cert.order_id = self.order_id.clone();
        let pem = match certificate.to_pem() {
            Ok(pem) => pem,
            Err(e) => return Err(SaveError::Generic(e.to_string())),
        };

        cert.certificate = pem;
        cert.create(db).await
    }

    pub(crate) async fn certificate(&self, db: Postgres) -> Result<Certificate, LoadError> {
        Certificate::find_by_order_id(self.order_id.clone(), db).await
    }
}

#[async_trait]
impl Record<i32> for Order {
    async fn new_from_row(_row: &Row, _tx: &Transaction<'_>) -> Result<Self, LoadError> {
        Err(LoadError::Generic("unimplemented".to_string()))
    }

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

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

        let order_id: String = order_row.get("order_id");
        let mut status = OrderStatus::Pending;
        let authorizations = Authorization::collect(order_id.clone(), &tx).await?;

        // ensure all at least one challenge has passed for each identifier (carried in the
        // authorization)
        //
        // FIXME test the shit out of this later
        let mut valid = false;
        for authz in &authorizations {
            if authz.identifier.is_none() {
                // FIXME this should never happen and we should do something here
                break;
            }

            valid = false;

            // any invalids breaks it.
            for chall in authz.challenges(&tx).await? {
                if chall.status == OrderStatus::Invalid {
                    status = OrderStatus::Invalid;
                    break;
                } else if chall.status == OrderStatus::Valid {
                    valid = true
                }
            }

            // escape hatch for invalid status
            if status == OrderStatus::Invalid || !valid {
                break;
            }
        }

        if valid {
            status = OrderStatus::Valid;
        }

        let error: Option<String> = order_row.get("error");

        Ok(Order {
            id: order_row.get("id"),
            order_id: order_row.get("order_id"),
            expires: order_row.get("expires"),
            not_before: order_row.get("not_before"),
            not_after: order_row.get("not_after"),
            error: if error.is_some() {
                serde_json::from_str(&error.unwrap())?
            } else {
                None
            },
            finalized: order_row.get("finalized"),
            deleted_at: order_row.get("deleted_at"),
            created_at: order_row.get("created_at"),
            status: status.into(),
            authorizations: Some(authorizations),
        })
    }

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

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

        let mut error = None;

        if self.error.is_some() {
            error = Some(serde_json::to_string(&self.error)?)
        }

        let res = tx
            .query_one(
                "
            insert into orders
                (order_id, expires, not_before, not_after, error, finalized)
            values 
                ($1, $2, $3, $4, $5, $6)
            returning 
                id, created_at
        ",
                &[
                    &self.order_id,
                    &self.expires,
                    &self
                        .not_before
                        .unwrap_or(chrono::DateTime::<chrono::Local>::from(
                            std::time::SystemTime::now(),
                        )),
                    &self.clone().not_after.unwrap_or(
                        chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now())
                            .add(chrono::Duration::days(365)),
                    ),
                    &error,
                    &self.finalized,
                ],
            )
            .await?;

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

        tx.commit().await?;

        return Ok(id);
    }

    async fn update(&self, db: super::Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "record was not saved and updates were requested".to_string(),
            ));
        }

        let mut client = db.client().await?;
        let tx = client.transaction().await?;

        let mut error = None;

        if self.error.is_some() {
            error = Some(serde_json::to_string(&self.error)?)
        }

        let res = tx
            .execute(
                "update orders set deleted_at=$1, expires=$2, error=$3 where id=$4 and deleted_at is null",
                &[&self.deleted_at, &self.expires, &error, &self.id.unwrap()],
            )
            .await?;

        if res != 1 {
            return Err(SaveError::Generic("row could not be deleted".to_string()));
        }

        // FIXME update authz and certs
        Ok(tx.commit().await?)
    }

    async fn delete(&self, db: super::Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "record was not saved and deletion was requested".to_string(),
            ));
        }

        let mut client = db.client().await?;
        let tx = client.transaction().await?;

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

        if res != 1 {
            return Err(SaveError::Generic("row could not be deleted".to_string()));
        }

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

#[derive(Debug, Clone, PartialEq)]
pub struct Challenge {
    pub id: Option<i32>,
    pub order_id: String,
    pub challenge_type: ChallengeType,
    pub identifier: String,
    pub token: String,
    pub reference: String,
    pub status: OrderStatus,
    pub validated: Option<chrono::DateTime<chrono::Local>>,
    pub created_at: chrono::DateTime<chrono::Local>,
    pub deleted_at: Option<chrono::DateTime<chrono::Local>>,
    pub authorization_id: String,
}

impl Challenge {
    pub fn new(
        order_id: String,
        authorization_id: String,
        challenge_type: ChallengeType,
        identifier: String,
        status: OrderStatus,
    ) -> Self {
        Self {
            id: None,
            order_id,
            authorization_id,
            challenge_type,
            identifier,
            token: make_nonce(None),
            reference: make_nonce(None),
            status,
            validated: None,
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }

    pub(crate) async fn find_by_reference(
        challenge_id: String,
        tx: &Transaction<'_>,
    ) -> Result<Self, LoadError> {
        let row = tx
            .query_one(
                "select * from orders_challenges where reference=$1",
                &[&challenge_id],
            )
            .await?;

        Self::new_from_row(&row)
    }

    pub(crate) async fn find_by_authorization(
        authorization: String,
        tx: &Transaction<'_>,
    ) -> Result<Vec<Self>, LoadError> {
        let rows = tx
            .query(
                "select * from orders_challenges where authorization_id = $1 order by created_at DESC",
                &[&authorization],
            )
            .await?;

        let mut ret = Vec::new();

        for row in rows {
            ret.push(Self::new_from_row(&row)?)
        }

        Ok(ret)
    }

    pub(crate) async fn authorization(
        &self,
        tx: &Transaction<'_>,
    ) -> Result<Authorization, LoadError> {
        Authorization::find_by_reference(&self.authorization_id, tx).await
    }

    pub(crate) fn into_url(&self, url: url::Url) -> url::Url {
        url.join(&format!("/chall/{}", self.reference)).unwrap()
    }

    fn new_from_row(result: &Row) -> Result<Self, LoadError> {
        let ct: ChallengeType = result.get::<_, &str>("challenge_type").try_into()?;
        let id = result.get::<_, &str>("identifier");

        Ok(Self {
            id: result.get("id"),
            order_id: result.get("order_id"),
            authorization_id: result.get("authorization_id"),
            challenge_type: ct.clone(),
            identifier: id.to_string(),
            validated: result.get("validated"),
            reference: result.get("reference"),
            token: result.get("token"),
            status: OrderStatus::try_from(result.get::<_, String>("status"))?,
            created_at: result.get("created_at"),
            deleted_at: result.get("deleted_at"),
        })
    }

    pub async fn create(&mut self, db: Postgres) -> Result<i32, SaveError> {
        let mut client = db.client().await?;
        let tx = client.transaction().await?;
        let res = tx.query_one(
            "insert into orders_challenges (order_id, authorization_id, challenge_type, identifier, token, reference, status, created_at, deleted_at) values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning id",
            &[&self.order_id.clone(), &self.authorization_id.clone(), &self.challenge_type.clone().to_string(), &self.identifier.clone().to_string(), &self.token.clone(), &self.reference.clone(), &self.status.clone().to_string(), &self.created_at, &self.deleted_at],
            ).await?;

        let id = res.get("id");
        tx.commit().await?;

        self.id = Some(id);

        Ok(id)
    }

    pub async fn persist_status(&mut self, tx: &Transaction<'_>) -> Result<(), SaveError> {
        match self.id {
            None => return Err(SaveError::Generic("save this record first".to_string())),
            _ => {}
        }

        if self.status == OrderStatus::Valid {
            self.validated = Some(chrono::DateTime::<chrono::Local>::from(
                std::time::SystemTime::now(),
            ))
        }

        tx.execute(
            "update orders_challenges set status=$1, validated=$2 where authorization_id=$3 and id=$4",
            &[
                &self.status.clone().to_string(),
                &self.validated,
                &self.authorization_id.clone(),
                &self.id.unwrap(),
            ],
        )
        .await?;
        Ok(())
    }
}

#[async_trait]
impl RecordList<String> for Challenge {
    async fn collect(order_id: String, tx: &Transaction<'_>) -> Result<Vec<Self>, LoadError> {
        let mut ret = Vec::new();

        let results = tx
            .query(
                "select * from orders_challenges where order_id = $1 order by created_at ASC",
                &[&order_id],
            )
            .await?;

        for result in results.iter() {
            ret.push(Self::new_from_row(result)?);
        }

        Ok(ret)
    }

    async fn latest(_order_id: String, _tx: &Transaction<'_>) -> Result<Self, LoadError> {
        return Err(LoadError::Generic("unimplemented".to_string()));
    }

    async fn append(&self, order_id: String, tx: &Transaction<'_>) -> Result<Vec<Self>, SaveError> {
        tx.execute(
            "insert into orders_challenges (order_id, authorization_id, challenge_type, token, reference, status, created_at, deleted_at) values ($1, $2, $3, $4, $5, $6, $7, $8) returning id",
            &[&order_id, &self.authorization_id.clone(), &self.challenge_type.clone().to_string(), &self.token.clone(), &self.reference.clone(), &self.status.clone().to_string(), &self.created_at, &self.deleted_at],
            ).await?;
        Ok(Self::collect(order_id, tx).await?)
    }

    async fn remove(&self, _: String, _tx: &Transaction<'_>) -> Result<(), SaveError> {
        return Err(SaveError::Generic("unimplemented".to_string()));
    }

    async fn exists(&self, _order_id: String, _tx: &Transaction<'_>) -> Result<bool, LoadError> {
        return Err(LoadError::Generic("unimplemented".to_string()));
    }
}

// XXX this and authorizations are awful similar and it drives me nuts
#[derive(Debug, Clone, PartialEq)]
pub struct Certificate {
    id: Option<i32>,
    order_id: String,
    reference: String,
    pub certificate: Vec<u8>,
    created_at: chrono::DateTime<chrono::Local>,
    deleted_at: Option<chrono::DateTime<chrono::Local>>,
}

impl Default for Certificate {
    fn default() -> Self {
        Self {
            id: None,
            order_id: "".to_string(),
            reference: make_nonce(None),
            certificate: Vec::new(),
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }
}

impl Into<String> for Certificate {
    fn into(self) -> String {
        self.reference
    }
}

impl Certificate {
    pub(crate) async fn find_by_order_id(
        order_id: String,
        db: Postgres,
    ) -> Result<Self, LoadError> {
        let mut client = db.client().await?;
        let tx = client.transaction().await?;

        let result = tx
            .query_one(
                "select * from orders_certificate where order_id = $1 and deleted_at is null limit 1",
                &[&order_id],
            )
            .await?;

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

#[async_trait]
impl Record<i32> for Certificate {
    async fn new_from_row(row: &Row, _tx: &Transaction<'_>) -> Result<Self, LoadError> {
        Ok(Self {
            id: row.get("id"),
            order_id: row.get("order_id"),
            reference: row.get("reference"),
            certificate: row.get("certificate"),
            created_at: row.get("created_at"),
            deleted_at: row.get("deleted_at"),
        })
    }

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

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

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

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

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

        let ret = tx.query_one(
            "insert into orders_certificate (order_id, reference, certificate) values ($1, $2, $3) returning id, created_at",
            &[&self.order_id, &self.reference, &self.certificate]
        ).await?;

        self.id = Some(ret.get("id"));
        self.created_at = ret.get("created_at");

        tx.commit().await?;

        Ok(self.id.unwrap())
    }

    async fn delete(&self, db: super::Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "record was not saved and deletion was requested".to_string(),
            ));
        }

        let mut client = db.client().await?;
        let tx = client.transaction().await?;

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

        if res != 1 {
            return Err(SaveError::Generic("row could not be deleted".to_string()));
        }

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

    async fn update(&self, _db: super::Postgres) -> Result<(), SaveError> {
        Err(SaveError::Generic(
            "update is not implemented for order certificates".to_string(),
        ))
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Authorization {
    id: Option<i32>,
    // FIXME make this Option<> to guard against writing an empty string
    pub order_id: String,
    pub reference: String,
    pub expires: chrono::DateTime<chrono::Local>,
    pub identifier: Option<String>,
    created_at: chrono::DateTime<chrono::Local>,
    pub deleted_at: Option<chrono::DateTime<chrono::Local>>,
}

impl Default for Authorization {
    fn default() -> Self {
        Self {
            id: None,
            order_id: "".to_string(),
            identifier: None,
            expires: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            reference: make_nonce(None),
            created_at: chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now()),
            deleted_at: None,
        }
    }
}

impl ToString for Authorization {
    fn to_string(&self) -> String {
        self.reference.clone()
    }
}

impl Authorization {
    pub(crate) async fn find_by_reference(
        reference: &str,
        tx: &Transaction<'_>,
    ) -> Result<Self, LoadError> {
        let res = tx
            .query_one(
                "select * from orders_authorizations where reference = $1",
                &[&reference],
            )
            .await?;

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

    pub(crate) async fn challenges(
        &self,
        tx: &Transaction<'_>,
    ) -> Result<Vec<Challenge>, LoadError> {
        Challenge::find_by_authorization(self.reference.clone(), tx).await
    }

    pub fn into_url(&self, baseurl: Url) -> Url {
        baseurl.join(&format!("/authz/{}", self.reference)).unwrap()
    }
}

#[async_trait]
impl RecordList<String> for Authorization {
    async fn collect(order_id: String, tx: &Transaction<'_>) -> Result<Vec<Self>, LoadError> {
        let mut ret = Vec::new();

        let results = tx
            .query(
                "select * from orders_authorizations where order_id = $1 order by created_at ASC",
                &[&order_id],
            )
            .await?;

        for result in results.iter() {
            ret.push(Self::new_from_row(result, tx).await?);
        }

        Ok(ret)
    }

    async fn latest(order_id: String, tx: &Transaction<'_>) -> Result<Self, LoadError> {
        let row = tx
            .query_one(
                "select * from orders_authorizations where order_id = $1 order by created_at ASC limit 1",
                &[&order_id],
            )
            .await?;

        Ok(Self::new_from_row(&row, &tx).await?)
    }

    async fn append(&self, order_id: String, tx: &Transaction<'_>) -> Result<Vec<Self>, SaveError> {
        if self.identifier.is_none() {
            return Err(SaveError::Generic(
                "cannot insert an authorization without an identifier".to_string(),
            ));
        }

        tx.execute("insert into orders_authorizations (order_id, expires, identifier, reference, created_at, deleted_at) values ($1, $2, $3, $4, $5, $6)", &[&order_id, &self.expires, &self.identifier.clone().unwrap(),&self.reference, &self.created_at, &self.deleted_at]).await?;
        Ok(Self::collect(order_id, tx).await?)
    }

    async fn remove(&self, order_id: String, tx: &Transaction<'_>) -> Result<(), SaveError> {
        tx.execute(
            "delete from orders_authorizations where id=$1 and order_id=$2",
            &[&self.id()?, &order_id],
        )
        .await?;
        Ok(())
    }

    async fn exists(&self, order_id: String, tx: &Transaction<'_>) -> Result<bool, LoadError> {
        let res = tx
            .query_one(
                "select count(*)::integer as count from orders_authorizations where id=$1 and order_id=$2",
                &[&self.id()?, &order_id],
            )
            .await?;

        Ok(res.get::<_, i32>(0) == 1)
    }
}

#[async_trait]
impl Record<i32> for Authorization {
    async fn new_from_row(row: &Row, _tx: &Transaction<'_>) -> Result<Self, LoadError> {
        Ok(Self {
            id: row.get("id"),
            order_id: row.get("order_id"),
            identifier: Some(row.get::<_, String>("identifier")),
            reference: row.get("reference"),
            expires: row.get("expires"),
            created_at: row.get("created_at"),
            deleted_at: row.get("deleted_at"),
        })
    }

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

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

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

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

    async fn create(&mut self, db: super::Postgres) -> Result<i32, SaveError> {
        if self.identifier.is_none() {
            return Err(SaveError::Generic(
                "cannot insert an authorization without an identifier".to_string(),
            ));
        }

        let mut client = db.client().await?;
        let tx = client.transaction().await?;

        let ret = tx.query_one("insert into orders_authorizations (order_id, expires, reference, identifier) values ($1, $2, $3, $4) returning id, created_at", &[&self.order_id, &self.expires, &self.reference, &self.identifier]).await?;

        self.id = Some(ret.get("id"));
        self.created_at = ret.get("created_at");

        tx.commit().await?;

        Ok(self.id.unwrap())
    }

    async fn delete(&self, db: super::Postgres) -> Result<(), SaveError> {
        if self.id.is_none() {
            return Err(SaveError::Generic(
                "record was not saved and deletion was requested".to_string(),
            ));
        }

        let mut client = db.client().await?;
        let tx = client.transaction().await?;

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

        if res != 1 {
            return Err(SaveError::Generic("row could not be deleted".to_string()));
        }

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

    async fn update(&self, _db: super::Postgres) -> Result<(), SaveError> {
        Err(SaveError::Generic(
            "update is not implemented for order authorizations".to_string(),
        ))
    }
}

mod tests {
    #[tokio::test(flavor = "multi_thread")]
    async fn test_order_certificate() {
        use super::Certificate;
        use crate::models::Record;
        use crate::test::PGTest;
        use crate::util::make_nonce;
        use spectral::prelude::*;

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

        let good = vec![Certificate {
            order_id: make_nonce(None),
            ..Default::default()
        }];

        for mut item in good {
            assert_that!(item.create(pg.db()).await).is_ok();
            assert_that!(item.id()).is_ok();
            assert_that!(item.id().unwrap()).is_some();
            assert_that!(item.id().unwrap().unwrap()).is_not_equal_to(0);

            assert_that!(item.order_id).is_not_equal_to("".to_string());
            assert_that!(item.reference).is_not_equal_to("".to_string());

            let s: String = item.clone().into();
            assert_that!(&s).is_equal_to(&item.reference);

            let new = Certificate::find(item.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(new).is_ok();

            let new = new.unwrap();

            assert_that!(&new).is_equal_to(&item);

            assert_that!(item.update(pg.db()).await).is_err();

            assert_that!(item.delete(pg.db()).await).is_ok();

            let new = Certificate::find(item.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(new).is_err();
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_order_authorization() {
        use super::Authorization;
        use crate::models::{Record, RecordList};
        use crate::test::PGTest;
        use crate::util::make_nonce;

        use spectral::prelude::*;

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

        let mut bad = Authorization::default();

        assert_that!(bad.create(pg.db()).await).is_err();
        bad.order_id = make_nonce(None);
        assert_that!(bad.create(pg.db()).await).is_err();
        bad.identifier = Some("example.com".to_string());
        assert_that!(bad.create(pg.db()).await).is_ok();

        let good = vec![Authorization {
            order_id: make_nonce(None),
            identifier: Some("example.com".to_string()),
            ..Default::default()
        }];

        for mut item in good {
            assert_that!(item.create(pg.db()).await).is_ok();
            assert_that!(item.id()).is_ok();
            assert_that!(item.id().unwrap()).is_some();
            assert_that!(item.id().unwrap().unwrap()).is_not_equal_to(0);

            assert_that!(item.order_id).is_not_equal_to("".to_string());
            assert_that!(item.reference).is_not_equal_to("".to_string());

            let s: String = item.to_string();
            assert_that!(&s).is_equal_to(&item.reference);

            let new = Authorization::find(item.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(new).is_ok();

            let mut new = new.unwrap();
            new.expires = chrono::DateTime::<chrono::Local>::from(std::time::SystemTime::now());
            item.expires = new.expires;

            assert_that!(&new).is_equal_to(&item);

            assert_that!(item.update(pg.db()).await).is_err();

            assert_that!(item.delete(pg.db()).await).is_ok();

            let new = Authorization::find(item.id().unwrap().unwrap(), pg.db()).await;
            assert_that!(new).is_err();
        }

        for _ in 0..10 {
            let mut obj = Authorization {
                order_id: "special".to_string(),
                identifier: Some("example.com".to_string()),
                ..Default::default()
            };

            assert_that!(obj.create(pg.db()).await).is_ok();
        }

        let auths = Authorization::collect(
            "special".to_string(),
            &pg.db().client().await.unwrap().transaction().await.unwrap(),
        )
        .await;
        assert_that!(auths).is_ok();
        let auths = auths.unwrap();

        assert_that!(auths.len()).is_equal_to(10);

        let mut bad2 = Authorization::default();

        let good2 = Authorization {
            order_id: "special".to_string(),
            identifier: Some("example.com".to_string()),
            ..Default::default()
        };

        let db = pg.db();
        let mut lockeddb = db.client().await.unwrap();
        let tx = lockeddb.transaction().await.unwrap();
        assert_that!(bad2.append("special".to_string(), &tx).await).is_err();

        // these drops are important because the errors will abort the tx
        drop(tx);
        bad2.order_id = make_nonce(None);

        let tx = lockeddb.transaction().await.unwrap();
        assert_that!(bad2.append("special".to_string(), &tx).await).is_err();

        drop(tx);
        bad2.identifier = Some("example.com".to_string());

        let tx = lockeddb.transaction().await.unwrap();
        assert_that!(bad2.append("special".to_string(), &tx).await).is_ok();

        let auths = good2.append("special".to_string(), &tx).await.unwrap();
        tx.commit().await.unwrap();

        assert_that!(auths.len()).is_equal_to(12);

        for auth in &auths {
            assert_that!(auth
                .exists(
                    "special".to_string(),
                    &lockeddb.transaction().await.unwrap(),
                )
                .await
                .unwrap())
            .is_true();
        }

        assert_that!(auths[11]
            .exists(
                "special".to_string(),
                &lockeddb.transaction().await.unwrap(),
            )
            .await
            .unwrap())
        .is_true();

        let tx = lockeddb.transaction().await.unwrap();

        assert_that!(auths[11].remove("special".to_string(), &tx).await).is_ok();

        tx.commit().await.unwrap();

        let auths_new = Authorization::collect(
            "special".to_string(),
            &lockeddb.transaction().await.unwrap(),
        )
        .await;
        assert_that!(auths_new).is_ok();
        let auths_new = auths_new.unwrap();

        assert_that!(auths[11]
            .exists(
                "special".to_string(),
                &lockeddb.transaction().await.unwrap(),
            )
            .await
            .unwrap())
        .is_false();

        assert_that!(auths_new.len()).is_equal_to(11);
    }
}