Skip to content

Commit 59a5c8f

Browse files
committed
Address comments
1 parent 7c4f1de commit 59a5c8f

File tree

17 files changed

+87
-93
lines changed

17 files changed

+87
-93
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/config/src/sections/upstream_oauth2.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,25 @@ fn is_default_true(value: &bool) -> bool {
399399
}
400400

401401
#[allow(clippy::ref_option)]
402-
fn is_signed_response_alg_default(signed_response_alg: &Option<JsonWebSignatureAlg>) -> bool {
402+
fn is_signed_response_alg_default(signed_response_alg: &JsonWebSignatureAlg) -> bool {
403403
*signed_response_alg == signed_response_alg_default()
404404
}
405405

406+
#[allow(clippy::ref_option)]
407+
fn is_optional_signed_response_alg_default(
408+
optional_signed_response_alg: &Option<JsonWebSignatureAlg>,
409+
) -> bool {
410+
*optional_signed_response_alg == optional_signed_response_alg_default()
411+
}
412+
406413
#[allow(clippy::unnecessary_wraps)]
407-
fn signed_response_alg_default() -> Option<JsonWebSignatureAlg> {
408-
Some(JsonWebSignatureAlg::Rs256)
414+
fn signed_response_alg_default() -> JsonWebSignatureAlg {
415+
JsonWebSignatureAlg::Rs256
416+
}
417+
418+
#[allow(clippy::unnecessary_wraps)]
419+
fn optional_signed_response_alg_default() -> Option<JsonWebSignatureAlg> {
420+
Some(signed_response_alg_default())
409421
}
410422

411423
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
@@ -485,13 +497,12 @@ pub struct Provider {
485497
/// Expected signature for the JWT payload returned by the token
486498
/// authentication endpoint.
487499
///
488-
/// If null, the response is expected to be an unsigned JSON payload.
489500
/// Defaults to `RS256`.
490501
#[serde(
491502
default = "signed_response_alg_default",
492503
skip_serializing_if = "is_signed_response_alg_default"
493504
)]
494-
pub id_token_signed_response_alg: Option<JsonWebSignatureAlg>,
505+
pub id_token_signed_response_alg: JsonWebSignatureAlg,
495506

496507
/// The scopes to request from the provider
497508
pub scope: String,
@@ -524,8 +535,8 @@ pub struct Provider {
524535
/// If null, the response is expected to be an unsigned JSON payload.
525536
/// Defaults to `RS256`.
526537
#[serde(
527-
default = "signed_response_alg_default",
528-
skip_serializing_if = "is_signed_response_alg_default"
538+
default = "optional_signed_response_alg_default",
539+
skip_serializing_if = "is_optional_signed_response_alg_default"
529540
)]
530541
pub userinfo_signed_response_alg: Option<JsonWebSignatureAlg>,
531542

crates/data-model/src/upstream_oauth2/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub struct UpstreamOAuthProvider {
235235
pub encrypted_client_secret: Option<String>,
236236
pub token_endpoint_signing_alg: Option<JsonWebSignatureAlg>,
237237
pub token_endpoint_auth_method: TokenAuthMethod,
238-
pub id_token_signed_response_alg: Option<JsonWebSignatureAlg>,
238+
pub id_token_signed_response_alg: JsonWebSignatureAlg,
239239
pub response_mode: ResponseMode,
240240
pub created_at: DateTime<Utc>,
241241
pub disabled_at: Option<DateTime<Utc>>,

crates/handlers/src/upstream_oauth2/cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ mod tests {
289289
use mas_data_model::{
290290
UpstreamOAuthProviderClaimsImports, UpstreamOAuthProviderTokenAuthMethod,
291291
};
292+
use mas_iana::jose::JsonWebSignatureAlg;
292293
use mas_storage::{clock::MockClock, Clock};
293294
use oauth2_types::scope::{Scope, OPENID};
294295
use ulid::Ulid;
@@ -411,7 +412,7 @@ mod tests {
411412
token_endpoint_signing_alg: None,
412413
token_endpoint_auth_method: UpstreamOAuthProviderTokenAuthMethod::None,
413414
response_mode: mas_data_model::UpstreamOAuthProviderResponseMode::Query,
414-
id_token_signed_response_alg: None,
415+
id_token_signed_response_alg: JsonWebSignatureAlg::Rs256,
415416
created_at: clock.now(),
416417
disabled_at: None,
417418
claims_imports: UpstreamOAuthProviderClaimsImports::default(),

crates/handlers/src/upstream_oauth2/callback.rs

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -278,62 +278,53 @@ pub(crate) async fn handler(
278278

279279
let mut context = AttributeMappingContext::new();
280280
if let Some(id_token) = token_response.id_token.as_ref() {
281-
if let Some(signed_response_alg) = &provider.id_token_signed_response_alg {
282-
jwks = Some(
283-
mas_oidc_client::requests::jose::fetch_jwks(
284-
&client,
285-
lazy_metadata.jwks_uri().await?,
286-
)
281+
jwks = Some(
282+
mas_oidc_client::requests::jose::fetch_jwks(&client, lazy_metadata.jwks_uri().await?)
287283
.await?,
288-
);
289-
290-
let id_token_verification_data = JwtVerificationData {
291-
issuer: &provider.issuer,
292-
jwks: &jwks.clone().unwrap(),
293-
signing_algorithm: signed_response_alg,
294-
client_id: &provider.client_id,
295-
};
296-
297-
// Decode and verify the ID token
298-
let id_token = mas_oidc_client::requests::jose::verify_id_token(
299-
id_token,
300-
id_token_verification_data,
301-
None,
302-
clock.now(),
303-
)?;
304-
305-
let (_headers, mut claims) = id_token.into_parts();
306-
307-
// Access token hash must match.
308-
mas_jose::claims::AT_HASH
309-
.extract_optional_with_options(
310-
&mut claims,
311-
TokenHash::new(
312-
id_token_verification_data.signing_algorithm,
313-
&token_response.access_token,
314-
),
315-
)
316-
.map_err(mas_oidc_client::error::IdTokenError::from)?;
284+
);
317285

318-
// Code hash must match.
319-
mas_jose::claims::C_HASH
320-
.extract_optional_with_options(
321-
&mut claims,
322-
TokenHash::new(id_token_verification_data.signing_algorithm, &code),
323-
)
324-
.map_err(mas_oidc_client::error::IdTokenError::from)?;
325-
326-
// Nonce must match.
327-
mas_jose::claims::NONCE
328-
.extract_required_with_options(&mut claims, session.nonce.as_str())
329-
.map_err(mas_oidc_client::error::IdTokenError::from)?;
330-
331-
context = context.with_id_token_claims(claims);
332-
} else {
333-
let claims = serde_json::from_str(id_token)
334-
.map_err(mas_oidc_client::error::IdTokenError::from)?;
335-
context = context.with_id_token_claims(claims);
336-
}
286+
let id_token_verification_data = JwtVerificationData {
287+
issuer: &provider.issuer,
288+
jwks: &jwks.clone().unwrap(),
289+
signing_algorithm: &provider.id_token_signed_response_alg,
290+
client_id: &provider.client_id,
291+
};
292+
293+
// Decode and verify the ID token
294+
let id_token = mas_oidc_client::requests::jose::verify_id_token(
295+
id_token,
296+
id_token_verification_data,
297+
None,
298+
clock.now(),
299+
)?;
300+
301+
let (_headers, mut claims) = id_token.into_parts();
302+
303+
// Access token hash must match.
304+
mas_jose::claims::AT_HASH
305+
.extract_optional_with_options(
306+
&mut claims,
307+
TokenHash::new(
308+
id_token_verification_data.signing_algorithm,
309+
&token_response.access_token,
310+
),
311+
)
312+
.map_err(mas_oidc_client::error::IdTokenError::from)?;
313+
314+
// Code hash must match.
315+
mas_jose::claims::C_HASH
316+
.extract_optional_with_options(
317+
&mut claims,
318+
TokenHash::new(id_token_verification_data.signing_algorithm, &code),
319+
)
320+
.map_err(mas_oidc_client::error::IdTokenError::from)?;
321+
322+
// Nonce must match.
323+
mas_jose::claims::NONCE
324+
.extract_required_with_options(&mut claims, session.nonce.as_str())
325+
.map_err(mas_oidc_client::error::IdTokenError::from)?;
326+
327+
context = context.with_id_token_claims(claims);
337328
}
338329

339330
if let Some(extra_callback_parameters) = extra_callback_parameters.clone() {

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ mod tests {
922922
scope: Scope::from_iter([OPENID]),
923923
token_endpoint_auth_method: UpstreamOAuthProviderTokenAuthMethod::None,
924924
token_endpoint_signing_alg: None,
925-
id_token_signed_response_alg: None,
925+
id_token_signed_response_alg: JsonWebSignatureAlg::Rs256,
926926
client_id: "client".to_owned(),
927927
encrypted_client_secret: None,
928928
claims_imports,

crates/handlers/src/views/login.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ mod test {
346346
use mas_data_model::{
347347
UpstreamOAuthProviderClaimsImports, UpstreamOAuthProviderTokenAuthMethod,
348348
};
349+
use mas_iana::jose::JsonWebSignatureAlg;
349350
use mas_router::Route;
350351
use mas_storage::{
351352
upstream_oauth2::{UpstreamOAuthProviderParams, UpstreamOAuthProviderRepository},
@@ -403,7 +404,7 @@ mod test {
403404
scope: [OPENID].into_iter().collect(),
404405
token_endpoint_auth_method: UpstreamOAuthProviderTokenAuthMethod::None,
405406
token_endpoint_signing_alg: None,
406-
id_token_signed_response_alg: None,
407+
id_token_signed_response_alg: JsonWebSignatureAlg::Rs256,
407408
fetch_userinfo: false,
408409
userinfo_signed_response_alg: None,
409410
client_id: "client".to_owned(),
@@ -443,7 +444,7 @@ mod test {
443444
scope: [OPENID].into_iter().collect(),
444445
token_endpoint_auth_method: UpstreamOAuthProviderTokenAuthMethod::None,
445446
token_endpoint_signing_alg: None,
446-
id_token_signed_response_alg: None,
447+
id_token_signed_response_alg: JsonWebSignatureAlg::Rs256,
447448
fetch_userinfo: false,
448449
userinfo_signed_response_alg: None,
449450
client_id: "client".to_owned(),

crates/oidc-client/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ pub enum IdTokenError {
200200
/// one we got before.
201201
#[error("wrong authentication time")]
202202
WrongAuthTime,
203-
204-
#[error(transparent)]
205-
/// TODO
206-
Deserialize(#[from] serde_json::Error),
207203
}
208204

209205
/// All errors that can occur when adding client credentials to the request.

crates/storage-pg/.sqlx/query-1d758df58ccfead4cb39ee8f88f60b382b7881e9c4ead31ff257ff5ff4414b6e.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/storage-pg/.sqlx/query-27d6f228a9a608b5d03d30cb4074be94dc893df9107e982583aa954b5067dfd1.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)