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
use crate::Error;
use ring::signature;
use serde::Serialize;
#[derive(Serialize)]
pub(crate) struct Claims {
#[serde(rename = "iss")]
pub(crate) issuer: String,
#[serde(rename = "aud")]
pub(crate) audience: String,
#[serde(rename = "exp")]
pub(crate) expiration: i64,
#[serde(rename = "iat")]
pub(crate) issued_at: i64,
pub(crate) sub: Option<String>,
pub(crate) scope: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, serde::Deserialize)]
pub struct Header {
#[serde(skip_serializing_if = "Option::is_none")]
pub typ: Option<String>,
pub alg: Algorithm,
#[serde(skip_serializing_if = "Option::is_none")]
pub cty: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub jku: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x5u: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x5t: Option<String>,
}
impl Header {
pub fn new(algorithm: Algorithm) -> Header {
Header {
typ: Some("JWT".to_string()),
alg: algorithm,
cty: None,
jku: None,
kid: None,
x5u: None,
x5t: None,
}
}
}
impl Default for Header {
fn default() -> Self {
Header::new(Algorithm::default())
}
}
#[derive(Debug, PartialEq, Copy, Clone, Serialize, serde::Deserialize)]
pub enum Algorithm {
HS256,
HS384,
HS512,
ES256,
ES384,
RS256,
RS384,
RS512,
PS256,
PS384,
PS512,
}
impl Default for Algorithm {
fn default() -> Self {
Algorithm::HS256
}
}
pub enum Key<'a> {
Pkcs8(&'a [u8]),
}
pub fn to_jwt_part<T: Serialize>(input: &T) -> Result<String, Error> {
let encoded = serde_json::to_string(input)?;
Ok(base64::encode_config(
encoded.as_bytes(),
base64::URL_SAFE_NO_PAD,
))
}
fn sign_rsa(
alg: &'static dyn signature::RsaEncoding,
key: Key<'_>,
signing_input: &str,
) -> Result<String, Error> {
let key_pair = match key {
Key::Pkcs8(bytes) => {
signature::RsaKeyPair::from_pkcs8(bytes).map_err(|_| Error::InvalidRsaKey)?
}
};
let key_pair = std::sync::Arc::new(key_pair);
let mut signature = vec![0; key_pair.public_modulus_len()];
let rng = ring::rand::SystemRandom::new();
key_pair
.sign(alg, &rng, signing_input.as_bytes(), &mut signature)
.map_err(|_| Error::InvalidRsaKey)?;
Ok(base64::encode_config::<[u8]>(
&signature,
base64::URL_SAFE_NO_PAD,
))
}
pub fn sign(signing_input: &str, key: Key<'_>, algorithm: Algorithm) -> Result<String, Error> {
match algorithm {
Algorithm::RS256 => sign_rsa(&signature::RSA_PKCS1_SHA256, key, signing_input),
Algorithm::RS384 => sign_rsa(&signature::RSA_PKCS1_SHA384, key, signing_input),
Algorithm::RS512 => sign_rsa(&signature::RSA_PKCS1_SHA512, key, signing_input),
Algorithm::PS256 => sign_rsa(&signature::RSA_PSS_SHA256, key, signing_input),
Algorithm::PS384 => sign_rsa(&signature::RSA_PSS_SHA384, key, signing_input),
Algorithm::PS512 => sign_rsa(&signature::RSA_PSS_SHA512, key, signing_input),
_ => unimplemented!(),
}
}
pub fn encode<T: Serialize>(header: &Header, claims: &T, key: Key<'_>) -> Result<String, Error> {
let encoded_header = to_jwt_part(&header)?;
let encoded_claims = to_jwt_part(&claims)?;
let signing_input = [encoded_header.as_ref(), encoded_claims.as_ref()].join(".");
let signature = sign(&*signing_input, key, header.alg)?;
Ok([signing_input, signature].join("."))
}