gantry_protocol/
catalog.rs

1//! # Gantry catalog protocol
2//!
3//! This module contains data types and traits for use with Gantry's catalog
4//! functionality. Gantry supports the following catalog operations:
5//! * `put` - Adds a token to the catalog
6//! * `query` - Queries the catalog
7//! * `delete` - Takes an entity out of service from the catalog. This will mark the entity as removed/unavailable but will not erase the entry.
8//! * `get` - Obtain details on a given entity
9
10use std::fmt::Display;
11
12pub static SUBJECT_CATALOG_PUT_TOKEN: &str = "gantry.catalog.tokens.put";
13pub static SUBJECT_CATALOG_DELETE_TOKEN: &str = "gantry.catalog.tokens.delete";
14pub static SUBJECT_CATALOG_QUERY: &str = "gantry.catalog.tokens.query";
15pub static SUBJECT_CATALOG_GET: &str = "gantry.catalog.tokens.get";
16
17/// A token contains the raw string for a JWT signed with the ed25519 signature
18/// format. Actors, Accounts, Operators are all identified by tokens
19#[derive(Debug, PartialEq, Deserialize, Serialize)]
20pub struct Token {
21    pub raw_token: String,
22    pub decoded_token_json: String,
23    pub validation_result: Option<TokenValidation>,
24}
25
26/// A protocol-specific message version of the validation result that the wascap
27/// library provides
28#[derive(Debug, PartialEq, Deserialize, Serialize)]
29pub struct TokenValidation {
30    pub expired: bool,
31    pub expires_human: String,
32    pub not_before_human: String,
33    pub cannot_use_yet: bool,
34    pub signature_valid: bool,
35}
36
37#[derive(Debug, PartialEq, Deserialize, Serialize)]
38pub struct CatalogQuery {
39    pub query_type: QueryType,
40    pub issuer: Option<String>,
41}
42
43#[derive(Debug, PartialEq, Deserialize, Serialize)]
44pub struct CatalogQueryResults {
45    pub results: Vec<CatalogQueryResult>,
46}
47
48#[derive(Debug, PartialEq, Deserialize, Serialize)]
49pub struct CatalogQueryResult {
50    pub subject: String,
51    pub issuer: String,
52    pub issuer_name: String,
53    pub name: String,
54}
55
56#[derive(Debug, PartialEq, Deserialize, Serialize)]
57pub enum QueryType {
58    Actor,
59    Account,
60    Operator,
61}
62
63#[derive(Debug, PartialEq, Deserialize, Serialize)]
64pub enum PutTokenResponse {
65    Success {
66        subject: String,
67        issuer_name: String,
68        issuer_id: String,
69    },
70    Failure(String),
71}
72
73#[derive(Debug, PartialEq, Deserialize, Serialize)]
74pub enum TokenDetail {
75    Account {
76        name: String,
77        jwt: String,
78        issuer_id: String,
79        issuer_name: String,
80        signing_keys: Vec<String>,
81    },
82    Operator {
83        name: String,
84        jwt: String,
85        signing_keys: Vec<String>,
86    },
87    Actor {
88        issuer_id: String,
89        issuer_name: String,
90        name: String,
91        jwt: String,
92        revisions: Vec<ActorRevision>,
93    },
94}
95
96impl TokenDetail {
97    fn render_account(
98        f: &mut std::fmt::Formatter<'_>,
99        name: &str,
100        _jwt: &str,
101        issuer_id: &str,
102        issuer_name: &str,
103        signing_keys: &Vec<String>,
104    ) -> std::fmt::Result {
105        write!(
106            f,
107            "Account: {}\nIssuer: {} ({})\nSigning Keys: {}",
108            name,
109            issuer_id,
110            issuer_name,
111            signing_keys.join(",")
112        )
113    }
114
115    fn render_operator(
116        f: &mut std::fmt::Formatter<'_>,
117        name: &str,
118        _jwt: &str,
119        signing_keys: &Vec<String>,
120    ) -> std::fmt::Result {
121        write!(
122            f,
123            "Operator: {}\nSigning Keys: {}",
124            name,
125            signing_keys.join(",")
126        )
127    }
128
129    fn render_actor(
130        f: &mut std::fmt::Formatter<'_>,
131        issuer_id: &str,
132        issuer_name: &str,
133        name: &str,
134        _jwt: &str,
135        revisions: &Vec<ActorRevision>,
136    ) -> std::fmt::Result {
137        write!(
138            f,
139            "Actor: {}\nIssuer: {} ({})\nRevisions: {}",
140            name,
141            issuer_id,
142            issuer_name,
143            revisions
144                .iter()
145                .map(|r| format!("{} ({})", r.version, r.revision))
146                .collect::<Vec<_>>()
147                .join("\n")
148        )
149    }
150}
151
152impl Display for TokenDetail {
153    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154        match self {
155            TokenDetail::Account {
156                name,
157                jwt,
158                issuer_id,
159                issuer_name,
160                signing_keys,
161            } => Self::render_account(f, name, jwt, issuer_id, issuer_name, signing_keys),
162            TokenDetail::Operator {
163                name,
164                jwt,
165                signing_keys,
166            } => Self::render_operator(f, name, jwt, signing_keys),
167            TokenDetail::Actor {
168                issuer_id,
169                issuer_name,
170                name,
171                jwt,
172                revisions,
173            } => Self::render_actor(f, issuer_id, issuer_name, name, jwt, revisions),
174        }
175    }
176}
177
178#[derive(Debug, PartialEq, Deserialize, Serialize)]
179pub struct ActorRevision {
180    pub revision: u32,
181    pub version: String,
182}