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
//! # Gantry catalog protocol
//!
//! This module contains data types and traits for use with Gantry's catalog
//! functionality. Gantry supports the following catalog operations:
//! * `put` - Adds a token to the catalog
//! * `query` - Queries the catalog
//! * `delete` - Takes an entity out of service from the catalog. This will mark the entity as removed/unavailable but will not erase the entry.
//! * `get` - Obtain details on a given entity

use std::fmt::Display;

pub static SUBJECT_CATALOG_PUT_TOKEN: &str = "gantry.catalog.tokens.put";
pub static SUBJECT_CATALOG_DELETE_TOKEN: &str = "gantry.catalog.tokens.delete";
pub static SUBJECT_CATALOG_QUERY: &str = "gantry.catalog.tokens.query";
pub static SUBJECT_CATALOG_GET: &str = "gantry.catalog.tokens.get";

/// A token contains the raw string for a JWT signed with the ed25519 signature
/// format. Actors, Accounts, Operators are all identified by tokens
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Token {
    pub raw_token: String,
    pub decoded_token_json: String,
    pub validation_result: Option<TokenValidation>,
}

/// A protocol-specific message version of the validation result that the wascap
/// library provides
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct TokenValidation {
    pub expired: bool,
    pub expires_human: String,
    pub not_before_human: String,
    pub cannot_use_yet: bool,
    pub signature_valid: bool,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CatalogQuery {
    pub query_type: QueryType,
    pub issuer: Option<String>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CatalogQueryResults {
    pub results: Vec<CatalogQueryResult>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CatalogQueryResult {
    pub subject: String,
    pub issuer: String,
    pub issuer_name: String,
    pub name: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum QueryType {
    Actor,
    Account,
    Operator,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum PutTokenResponse {
    Success {
        subject: String,
        issuer_name: String,
        issuer_id: String,
    },
    Failure(String),
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub enum TokenDetail {
    Account {
        name: String,
        jwt: String,
        issuer_id: String,
        issuer_name: String,
        signing_keys: Vec<String>,
    },
    Operator {
        name: String,
        jwt: String,
        signing_keys: Vec<String>,
    },
    Actor {
        issuer_id: String,
        issuer_name: String,
        name: String,
        jwt: String,
        revisions: Vec<ActorRevision>,
    },
}

impl TokenDetail {
    fn render_account(
        f: &mut std::fmt::Formatter<'_>,
        name: &str,
        _jwt: &str,
        issuer_id: &str,
        issuer_name: &str,
        signing_keys: &Vec<String>,
    ) -> std::fmt::Result {
        write!(
            f,
            "Account: {}\nIssuer: {} ({})\nSigning Keys: {}",
            name,
            issuer_id,
            issuer_name,
            signing_keys.join(",")
        )
    }

    fn render_operator(
        f: &mut std::fmt::Formatter<'_>,
        name: &str,
        _jwt: &str,
        signing_keys: &Vec<String>,
    ) -> std::fmt::Result {
        write!(
            f,
            "Operator: {}\nSigning Keys: {}",
            name,
            signing_keys.join(",")
        )
    }

    fn render_actor(
        f: &mut std::fmt::Formatter<'_>,
        issuer_id: &str,
        issuer_name: &str,
        name: &str,
        _jwt: &str,
        revisions: &Vec<ActorRevision>,
    ) -> std::fmt::Result {
        write!(
            f,
            "Actor: {}\nIssuer: {} ({})\nRevisions: {}",
            name,
            issuer_id,
            issuer_name,
            revisions
                .iter()
                .map(|r| format!("{} ({})", r.version, r.revision))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}

impl Display for TokenDetail {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TokenDetail::Account {
                name,
                jwt,
                issuer_id,
                issuer_name,
                signing_keys,
            } => Self::render_account(f, name, jwt, issuer_id, issuer_name, signing_keys),
            TokenDetail::Operator {
                name,
                jwt,
                signing_keys,
            } => Self::render_operator(f, name, jwt, signing_keys),
            TokenDetail::Actor {
                issuer_id,
                issuer_name,
                name,
                jwt,
                revisions,
            } => Self::render_actor(f, issuer_id, issuer_name, name, jwt, revisions),
        }
    }
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ActorRevision {
    pub revision: u32,
    pub version: String,
}