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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use serde_json::{Map, Number, Value};
use std::{collections::HashMap, time::Duration};
use crate::{
config::configuration_options::ConfigurationOptions,
errors::{OidcReturn, OpenIdError},
helpers::{base64_encode, generate_random, unix_timestamp, url_encoded},
jwk::{Jwk, JwkType},
types::{
http_client::{ClientCertificate, HttpRequest, RequestBody},
AuthMethods, Header, IssuerMetadata, OpenIdCrypto, Payload,
},
};
/// HS256 algorithm value
pub const DEFAULT_HS256_ALGORITHM: &str = "HS256";
/// RS256 algorithm value
pub const DEFAULT_RS256_ALGORITHM: &str = "RS256";
/// Default JWT assertion type
pub const DEFAULT_JWT_ASSERTION_TYPE: &str =
"urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
/// Options to configure the JWT assertion.
#[derive(Default, Debug, Clone)]
pub struct JwtAssertionOptions {
/// Optional duration (in seconds) for which the assertion is valid. (exp) claim is derived from this value.
pub assertion_duration: Option<Duration>,
/// Optional additional claims to include in the JWT payload.
pub custom_claims: Option<HashMap<String, Value>>,
/// Optional additional claims to include in the JWT header.
pub custom_header_claims: Option<HashMap<String, Value>>,
/// Optional signing algorithm to use for the JWT. Default is HS256 for client_secret_jwt and RS256 for private_key_jwt.
pub signing_algorithm: Option<String>,
/// Optional assertion type. The default is urn:ietf:params:oauth:client-assertion-type:jwt-bearer.
pub assertion_type: Option<String>,
}
/// Represents the different client authentication methods in OpenID Connect.
#[derive(Debug, Clone)]
pub enum ClientAuth {
/// Auth using client ID and optional secret in HTTP Basic Authorization header.
ClientSecretBasic {
/// Client secret
client_secret: String,
},
/// Auth by sending client ID and optional secret in POST form body.
ClientSecretPost {
/// Client secret
client_secret: String,
},
/// Auth using a client secret JWT with optional custom claims.
ClientSecretJwt {
/// The client secret used to sign the JWT.
client_secret: String,
/// Options to configure the JWT assertion.
options: JwtAssertionOptions,
},
/// Auth with a private key JWT and optional custom claims.
PrivateKeyJwt {
/// The client secret used to sign the JWT.
jwk: Jwk,
/// Options to configure the JWT assertion.
options: JwtAssertionOptions,
},
/// Auth using a self-signed TLS client certificate.
SelfSignedTls(ClientCertificate),
/// Auth using a TLS client certificate.
Tls(ClientCertificate),
/// No client authentication.
None,
}
impl ClientAuth {
/// Creates a client authentication method representing no authentication.
pub fn none() -> Self {
ClientAuth::None
}
/// Creates a `ClientSecretBasic` authentication method.
pub fn client_secret_basic(client_secret: impl Into<String>) -> Self {
ClientAuth::ClientSecretBasic {
client_secret: client_secret.into(),
}
}
/// Creates a `ClientSecretPost` authentication method.
pub fn client_secret_post(client_secret: impl Into<String>) -> Self {
ClientAuth::ClientSecretPost {
client_secret: client_secret.into(),
}
}
/// Creates a `ClientSecretJwt` authentication method.
pub fn client_secret_jwt(client_secret: impl Into<String>) -> Self {
ClientAuth::ClientSecretJwt {
client_secret: client_secret.into(),
options: JwtAssertionOptions::default(),
}
}
/// Creates a `PrivateKeyJwt` authentication method.
pub fn private_key_jwt(jwk: Jwk) -> Self {
ClientAuth::PrivateKeyJwt {
jwk,
options: JwtAssertionOptions::default(),
}
}
/// Creates a `SelfSignedTls` authentication method.
pub fn self_signed_tls(certificate: ClientCertificate) -> Self {
ClientAuth::SelfSignedTls(certificate)
}
/// Creates a `Tls` authentication method.
pub fn tls(certificate: ClientCertificate) -> Self {
ClientAuth::Tls(certificate)
}
}
impl ClientAuth {
/// Adds client authentication data to the HTTP request according to the auth method.
///
/// - For `ClientSecretBasic`, sets the `Authorization` header with Basic auth.
/// - For `ClientSecretPost`, adds client credentials to the form body.
/// - For `ClientSecretJwt` and `PrivateKeyJwt`, creates an assertion and adds to the body.
/// - For `Tls` and `SelfSignedTls`, attaches the client certificate.
/// - For `None`, does nothing.
pub fn authenticate<C: OpenIdCrypto>(
&self,
client_id: impl AsRef<str>,
options: &ConfigurationOptions,
issuer: &IssuerMetadata,
request: &mut HttpRequest,
crypto: &C,
) -> OidcReturn<()> {
match &self {
ClientAuth::ClientSecretBasic { client_secret } => {
let client_id_ref = client_id.as_ref();
let encoded = format!(
"{}:{}",
url_encoded(client_id_ref.as_bytes()),
url_encoded(client_secret.as_bytes())
);
let header = format!("Basic {}", base64_encode(encoded));
request
.headers
.insert("Authorization".to_owned(), vec![header]);
}
ClientAuth::ClientSecretPost { client_secret } => match &mut request.body {
Some(RequestBody::Form(form)) => {
form.insert("client_id".to_owned(), client_id.as_ref().to_owned());
form.insert("client_secret".to_owned(), client_secret.to_string());
}
_ => {
return Err(OpenIdError::new_error(
"ClientSecretPost requires a form body but received another type",
))
}
},
ClientAuth::ClientSecretJwt {
client_secret,
options:
JwtAssertionOptions {
custom_header_claims,
signing_algorithm,
assertion_type,
..
},
} => {
self.create_assertion(
issuer,
client_id.as_ref(),
request,
&Jwk::from_symmetric_key(client_secret.as_bytes()),
signing_algorithm
.as_deref()
.unwrap_or(DEFAULT_HS256_ALGORITHM),
assertion_type.as_deref(),
custom_header_claims,
crypto,
)?;
}
ClientAuth::PrivateKeyJwt {
jwk,
options:
JwtAssertionOptions {
custom_header_claims,
signing_algorithm,
assertion_type,
..
},
..
} => {
let key_type = jwk
.key_type()
.ok_or(OpenIdError::new_error("Unknown key type"))?;
if key_type == JwkType::OCT {
return Err(OpenIdError::new_error(
"Cannot use oct key to sign using private_key_jwt",
));
}
self.create_assertion(
issuer,
client_id.as_ref(),
request,
jwk,
signing_algorithm
.as_deref()
.unwrap_or(DEFAULT_RS256_ALGORITHM),
assertion_type.as_deref(),
custom_header_claims,
crypto,
)?;
}
ClientAuth::Tls(certificate) | ClientAuth::SelfSignedTls(certificate) => {
match &mut request.body {
Some(RequestBody::Form(form)) => {
form.insert("client_id".to_owned(), client_id.as_ref().to_owned());
}
_ => {
return Err(OpenIdError::new_error(
"mTLS auth requires a form body but received another type",
))
}
}
request.client_certificate = Some(certificate.clone());
}
ClientAuth::None => {
if options.add_client_id_to_request {
let client_id = client_id.as_ref().to_owned();
match &mut request.body {
Some(RequestBody::Form(form)) => {
form.insert("client_id".to_owned(), client_id);
}
_ => {
if request
.url
.query_pairs()
.find(|q| q.0 == "client_id")
.is_none()
{
request
.url
.query_pairs_mut()
.append_pair("client_id", &client_id);
}
}
}
}
}
};
Ok(())
}
/// Sets custom JWT assertion claims for JWT-based client authentication.
///
/// Has an effect only on `ClientSecretJwt` and `PrivateKeyJwt` variants.
pub fn set_custom_assertion_claims(
&mut self,
custom_claims: impl IntoIterator<Item = (String, Value)>,
) {
if let Some(options) = self.get_jwt_options_mut() {
options.custom_claims = Some(custom_claims.into_iter().collect());
}
}
/// Sets custom JWT assertion header claims for JWT-based client authentication.
///
/// Has an effect only on `ClientSecretJwt` and `PrivateKeyJwt` variants.
pub fn set_custom_header_claims(
&mut self,
custom_claims: impl IntoIterator<Item = (String, Value)>,
) {
if let Some(options) = self.get_jwt_options_mut() {
options.custom_header_claims = Some(custom_claims.into_iter().collect());
}
}
/// Sets assertion type for JWT-based client authentication.
///
/// Has effect only on `ClientSecretJwt` and `PrivateKeyJwt` variants.
pub fn set_assertion_type(&mut self, assertion_type: impl Into<String>) {
if let Some(options) = self.get_jwt_options_mut() {
options.assertion_type = Some(assertion_type.into());
}
}
/// Creates the payload of the assertion
pub fn create_assertion_payload(
&self,
issuer: &IssuerMetadata,
client_id: &str,
) -> OidcReturn<Payload> {
let mut payload = Payload {
params: serde_json::Map::new(),
};
// The 'aud' (audience) claim of the JWT assertion is set to include both the issuer
// and the token_endpoint as per the OpenID Connect specification, to ensure
// the assertion is only accepted by the intended audience.
let mut audience = vec![Value::String(issuer.issuer.to_owned())];
if let Some(token_endpoint) = &issuer.token_endpoint {
audience.push(Value::String(token_endpoint.to_owned()));
}
payload.params.insert("aud".into(), Value::Array(audience));
let client_id_string = client_id.to_owned();
payload
.params
.insert("iss".into(), Value::String(client_id_string.clone()));
payload
.params
.insert("sub".into(), Value::String(client_id_string));
payload
.params
.insert("jti".into(), Value::String(generate_random(None)));
let now = unix_timestamp();
payload
.params
.insert("iat".into(), Value::Number(Number::from(now)));
payload
.params
.insert("nbf".into(), Value::Number(Number::from(now)));
let (assertion_duration, custom_claims) = match self {
ClientAuth::PrivateKeyJwt {
options:
JwtAssertionOptions {
assertion_duration,
custom_claims,
..
},
..
}
| ClientAuth::ClientSecretJwt {
options:
JwtAssertionOptions {
assertion_duration,
custom_claims,
..
},
..
} => (*assertion_duration, custom_claims.as_ref()),
_ => (None, None),
};
let exp_duration = assertion_duration.map(|d| d.as_secs()).unwrap_or(300);
let exp = now + exp_duration;
payload
.params
.insert("exp".into(), Value::Number(Number::from(exp)));
if let Some(claims) = custom_claims {
for (k, v) in claims {
payload.params.insert(k.to_owned(), v.to_owned());
}
}
Ok(payload)
}
/// Retruns the configured client authentication method
pub fn get_auth_method(&self) -> AuthMethods {
match self {
ClientAuth::ClientSecretBasic { .. } => AuthMethods::ClientSecretBasic,
ClientAuth::ClientSecretPost { .. } => AuthMethods::ClientSecretPost,
ClientAuth::ClientSecretJwt { .. } => AuthMethods::ClientSecretJwt,
ClientAuth::PrivateKeyJwt { .. } => AuthMethods::PrivateKeyJwt,
ClientAuth::SelfSignedTls(_) => AuthMethods::SelfSignedTlsClientAuth,
ClientAuth::Tls(_) => AuthMethods::TlsClientAuth,
ClientAuth::None => AuthMethods::None,
}
}
#[allow(clippy::too_many_arguments)]
fn create_assertion<C: OpenIdCrypto>(
&self,
issuer: &IssuerMetadata,
client_id: &str,
request: &mut HttpRequest,
jwk: &Jwk,
alg: &str,
assertion_type: Option<&str>,
custom_header_claims: &Option<HashMap<String, Value>>,
crypto: &C,
) -> OidcReturn<()> {
let payload = self.create_assertion_payload(issuer, client_id)?;
let mut params = Map::new();
params.insert("alg".to_string(), Value::String(alg.to_owned()));
if let Some(kid) = jwk
.get_param("kid")
.and_then(|v| v.as_str().map(|k| Value::String(k.to_owned())))
{
params.insert("kid".to_string(), kid);
}
params.insert("typ".to_string(), Value::String("JWT".to_owned()));
let mut header = Header { params };
if let Some(custom_header_claims) = custom_header_claims {
for (k, v) in custom_header_claims {
header.params.insert(k.to_owned(), v.to_owned());
}
}
let assertion = crypto
.jws_serialize(payload, header, jwk)
.map_err(OpenIdError::new_error)?;
let assertion_type = assertion_type.unwrap_or(DEFAULT_JWT_ASSERTION_TYPE);
match request.body {
Some(RequestBody::Form(ref mut form)) => {
form.insert("client_id".to_owned(), client_id.to_owned());
form.insert("client_assertion_type".to_owned(), assertion_type.to_owned());
form.insert("client_assertion".to_owned(), assertion);
Ok(())
}
_ => Err(OpenIdError::new_error("Body is not form; JWT client authentication requires application/x-www-form-urlencoded on token endpoint")),
}
}
fn get_jwt_options_mut(&mut self) -> Option<&mut JwtAssertionOptions> {
match self {
ClientAuth::ClientSecretJwt { options, .. }
| ClientAuth::PrivateKeyJwt { options, .. } => Some(options),
_ => None,
}
}
}