1use crate::signer::HarnessSigner;
4use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
5use nkeys::KeyPair;
6use serde_json::{json, Value};
7use sha2::{Digest, Sha256};
8use std::time::{SystemTime, UNIX_EPOCH};
9
10pub fn unix_now() -> i64 {
11 SystemTime::now()
12 .duration_since(UNIX_EPOCH)
13 .expect("clock after epoch")
14 .as_secs() as i64
15}
16
17pub fn root_id(provider: &str) -> String {
19 format!("signing:{provider}:1")
20}
21
22pub fn box_root_id() -> String {
23 root_id("ck-bus-account")
24}
25
26pub fn system_root_id() -> String {
27 root_id("ck-bus-sysaccount")
28}
29
30pub fn signer_root_id() -> String {
31 root_id("ck-bus-operator-signer")
32}
33
34pub(crate) fn encode_jwt(
35 signer: &KeyPair,
36 subject: &str,
37 name: &str,
38 iat: i64,
39 nats: Value,
40) -> String {
41 let mut claims = json!({
42 "jti": "",
43 "iat": iat,
44 "iss": signer.public_key(),
45 "name": name,
46 "sub": subject,
47 "nats": nats,
48 });
49 let digest = Sha256::digest(claims.to_string().as_bytes());
50 claims["jti"] = Value::String(super::signer::hex(&digest).to_uppercase());
51 let input = format!(
52 "{}.{}",
53 URL_SAFE_NO_PAD.encode(br#"{"alg":"ed25519-nkey","typ":"JWT"}"#),
54 URL_SAFE_NO_PAD.encode(claims.to_string().as_bytes())
55 );
56 let signature = signer
57 .sign(input.as_bytes())
58 .expect("fixture keys hold seeds");
59 format!("{input}.{}", URL_SAFE_NO_PAD.encode(signature))
60}
61
62pub fn claims(jwt: &str) -> Value {
64 let part = jwt.trim().split('.').nth(1).expect("a JWT has claims");
65 serde_json::from_slice(&URL_SAFE_NO_PAD.decode(part).expect("claims are base64url"))
66 .expect("claims are JSON")
67}
68
69#[derive(Clone)]
71pub struct TrustChain {
72 pub signer: HarnessSigner,
73 pub(crate) operator_root: KeyPair,
74 pub system_account: String,
77}
78
79impl TrustChain {
80 pub fn generate() -> Self {
81 let signer = HarnessSigner::from_pairs([
82 (signer_root_id(), KeyPair::new_operator()),
83 (system_root_id(), KeyPair::new_account()),
84 (box_root_id(), KeyPair::new_account()),
85 ]);
86 Self {
87 signer,
88 operator_root: KeyPair::new_operator(),
89 system_account: KeyPair::new_account().public_key(),
90 }
91 }
92
93 pub fn signer_public(&self) -> String {
95 self.signer.root(&signer_root_id()).account_public()
96 }
97
98 pub fn box_root_public(&self) -> String {
101 self.signer.root(&box_root_id()).account_public()
102 }
103
104 pub fn operator_jwt(&self) -> String {
105 encode_jwt(
106 &self.operator_root,
107 &self.operator_root.public_key(),
108 "ckbus-harness-operator",
109 unix_now() - 120,
110 json!({
111 "type": "operator",
112 "version": 2,
113 "system_account": self.system_account,
114 "signing_keys": [self.signer_public()],
115 "strict_signing_key_usage": false,
116 }),
117 )
118 }
119
120 pub fn system_account_jwt(&self) -> String {
121 encode_jwt(
122 &self.operator_root,
123 &self.system_account,
124 "SYS",
125 unix_now() - 120,
126 json!({
127 "type": "account",
128 "version": 2,
129 "limits": {
130 "subs": -1, "data": -1, "payload": -1, "imports": -1, "exports": -1,
131 "wildcards": true, "conn": -1, "leaf": -1,
132 },
133 "signing_keys": [self.signer.root(&system_root_id()).account_public()],
134 "default_permissions": {"pub": {}, "sub": {}},
135 }),
136 )
137 }
138
139 pub fn user_jwt(
142 &self,
143 root_id: &str,
144 account_public: &str,
145 user: &KeyPair,
146 iat: i64,
147 ) -> String {
148 self.user_jwt_for_subject(root_id, account_public, &user.public_key(), iat)
149 }
150
151 pub fn user_jwt_for_subject(
153 &self,
154 root_id: &str,
155 account_public: &str,
156 subject: &str,
157 iat: i64,
158 ) -> String {
159 encode_jwt(
160 &self.signer.root(root_id).pair,
161 subject,
162 "harness-user",
163 iat,
164 json!({
165 "type": "user", "version": 2, "issuer_account": account_public,
166 "pub": {}, "sub": {}, "subs": -1, "data": -1, "payload": -1,
167 }),
168 )
169 }
170}