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
pub(crate) mod token {
use candid::Deserialize;
pub trait JwtClaims {
fn iat(&self) -> Option<u64>;
fn nonce(&self) -> Option<&str>;
}
#[derive(Clone, Deserialize)]
pub struct UnsafeClaims {
pub iss: Option<String>,
}
}
pub mod cert {
use candid::{CandidType, Deserialize};
use serde::Serialize;
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct Jwk {
// Key type, e.g. "RSA".
// https://tools.ietf.org/html/rfc7517#section-4.1
pub kty: JwkType,
// Algorithm, e.g. "RS256".
// https://tools.ietf.org/html/rfc7517#section-4.4
pub alg: Option<String>,
// Used to select which key in the JWKS to use.
// https://tools.ietf.org/html/rfc7517#section-4.5
pub kid: Option<String>,
// Type-Specific Key Properties.
// https://tools.ietf.org/html/rfc7517#section-4
pub params: JwkParams,
}
// Supported types for the JSON Web Key `kty` property.
// https://www.iana.org/assignments/jose/jose.xhtml#web-key-types
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub enum JwkType {
// Elliptic Curve.
#[serde(rename = "EC")]
Ec,
// RSA.
#[serde(rename = "RSA")]
Rsa,
// Octet sequence.
#[serde(rename = "oct")]
Oct,
// Octet string key pairs.
#[serde(rename = "OKP")]
Okp,
}
// Algorithm-specific parameters for JSON Web Keys.
// https://tools.ietf.org/html/rfc7518#section-6
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub enum JwkParams {
// Elliptic Curve parameters.
Ec(JwkParamsEc),
// RSA parameters.
Rsa(JwkParamsRsa),
// Octet Sequence parameters used to represent symmetric keys.
Oct(JwkParamsOct),
// Octet Key Pairs parameters.
Okp(JwkParamsOkp),
}
// Parameters for Elliptic Curve Keys.
// https://tools.ietf.org/html/rfc7518#section-6.2
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct JwkParamsEc {
// Identifies the cryptographic curve used with the key.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.1
pub crv: String, // Curve
// The `x` coordinate for the Elliptic Curve point as a base64url-encoded
// value.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.2
pub x: String, // X Coordinate
// The `y` coordinate for the Elliptic Curve point as a base64url-encoded
// value.
// https://tools.ietf.org/html/rfc7518#section-6.2.1.3
pub y: String, // Y Coordinate
// The Elliptic Curve private key as a base64url-encoded value.
// https://tools.ietf.org/html/rfc7518#section-6.2.2.1
// pub d: Option<String>, // ECC Private Key
// Unused in this implementation.
}
// Parameters for RSA Keys.
// https://tools.ietf.org/html/rfc7518#section-6.3
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct JwkParamsRsa {
// The modulus (part of the RSA public key).
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.1.1
pub n: String,
// The exponent (the other part of the RSA public key).
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.1.2
pub e: String,
// Other optional parameters describe private keys
// which are not used in this implementation.
// https://datatracker.ietf.org/doc/html/rfc7518#section-6.2.2
}
// Parameters for Symmetric Keys.
// https://tools.ietf.org/html/rfc7518#section-6.4
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct JwkParamsOct {
// The symmetric key as a base64url-encoded value.
// https://tools.ietf.org/html/rfc7518#section-6.4.1
pub k: String, // Key Value
}
// Parameters for Octet Key Pairs.
// https://tools.ietf.org/html/rfc8037#section-2
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct JwkParamsOkp {
// The subtype of the key pair.
// https://tools.ietf.org/html/rfc8037#section-2
pub crv: String, // Key SubType
// The public key as a base64url-encoded value.
// https://tools.ietf.org/html/rfc8037#section-2
pub x: String, // Public Key
// The private key as a base64url-encoded value.
// https://tools.ietf.org/html/rfc8037#section-2
// pub d: Option<String>,
// Unused in this implementation.
}
// JSON Web Key Set
#[derive(CandidType, Serialize, Deserialize, Clone)]
pub struct Jwks {
pub keys: Vec<Jwk>,
}
}
pub(crate) mod errors {
use candid::{CandidType, Deserialize};
use serde::Serialize;
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum JwtFindProviderError {
BadSig(String),
BadClaim(String),
NoMatchingProvider,
}
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum JwtFindKidError {
BadSig(String),
BadClaim(String),
MissingKid,
}
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum JwtVerifyError {
MissingKid,
NoKeyForKid,
WrongKeyType,
BadSig(String),
BadClaim(String),
}
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum JwtHeaderError {
BadSig(String),
BadClaim(String),
}
}
pub mod provider {
pub trait JwtIssuers {
fn issuers(&self) -> &[&'static str];
}
}