trust_graph/
trust_relation.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::revoke::Revocation;
18use crate::trust::Trust;
19use failure::_core::time::Duration;
20use fluence_keypair::public_key::PublicKey;
21use fluence_keypair::Signature;
22use serde::{Deserialize, Serialize};
23
24/// Represents who give a trust
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct Auth {
27    /// proof of this authorization
28    pub trust: Trust,
29    /// the issuer of this authorization
30    pub issued_by: PublicKey,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
34pub enum TrustRelation {
35    Auth(Auth),
36    Revocation(Revocation),
37}
38
39impl TrustRelation {
40    /// Returns timestamp of when this relation was created
41    pub fn issued_at(&self) -> Duration {
42        match self {
43            TrustRelation::Auth(auth) => auth.trust.issued_at,
44            TrustRelation::Revocation(r) => r.revoked_at,
45        }
46    }
47
48    /// Returns public key of the creator of this relation
49    pub fn issued_by(&self) -> &PublicKey {
50        match self {
51            TrustRelation::Auth(auth) => &auth.issued_by,
52            TrustRelation::Revocation(r) => &r.revoked_by,
53        }
54    }
55
56    pub fn issued_for(&self) -> &PublicKey {
57        match self {
58            TrustRelation::Auth(auth) => &auth.trust.issued_for,
59            TrustRelation::Revocation(r) => &r.pk,
60        }
61    }
62
63    pub fn expires_at(&self) -> Duration {
64        match self {
65            TrustRelation::Auth(auth) => auth.trust.expires_at,
66            // revocations never expire
67            TrustRelation::Revocation(_) => Duration::from_secs(0),
68        }
69    }
70
71    pub fn signature(&self) -> &Signature {
72        match self {
73            TrustRelation::Auth(auth) => &auth.trust.signature,
74            TrustRelation::Revocation(r) => &r.signature,
75        }
76    }
77}