You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
Convert many match/if expressions to let-else
This commit is contained in:
@ -560,8 +560,7 @@ mod tests {
|
||||
// Signed with client_secret = "client-secret"
|
||||
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjbGllbnQtaWQiLCJzdWIiOiJjbGllbnQtaWQiLCJhdWQiOiJodHRwczovL2V4YW1wbGUuY29tL29hdXRoMi9pbnRyb3NwZWN0IiwianRpIjoiYWFiYmNjIiwiZXhwIjoxNTE2MjM5MzIyLCJpYXQiOjE1MTYyMzkwMjJ9.XTaACG_Rww0GPecSZvkbem-AczNy9LLNBueCLCiQajU";
|
||||
let body = Bytes::from(format!(
|
||||
"client_assertion_type={}&client_assertion={}&foo=bar",
|
||||
JWT_BEARER_CLIENT_ASSERTION, jwt,
|
||||
"client_assertion_type={JWT_BEARER_CLIENT_ASSERTION}&client_assertion={jwt}&foo=bar",
|
||||
));
|
||||
|
||||
let req = Request::builder()
|
||||
@ -578,10 +577,7 @@ mod tests {
|
||||
.unwrap();
|
||||
assert_eq!(authz.form, Some(serde_json::json!({"foo": "bar"})));
|
||||
|
||||
let (client_id, jwt) =
|
||||
if let Credentials::ClientAssertionJwtBearer { client_id, jwt } = authz.credentials {
|
||||
(client_id, jwt)
|
||||
} else {
|
||||
let Credentials::ClientAssertionJwtBearer { client_id, jwt } = authz.credentials else {
|
||||
panic!("expected a JWT client_assertion");
|
||||
};
|
||||
|
||||
|
@ -47,9 +47,7 @@ impl SessionInfo {
|
||||
&self,
|
||||
repo: &mut impl RepositoryAccess<Error = E>,
|
||||
) -> Result<Option<BrowserSession>, E> {
|
||||
let session_id = if let Some(id) = self.current {
|
||||
id
|
||||
} else {
|
||||
let Some(session_id) = self.current else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
|
@ -89,9 +89,8 @@ impl<F: Send> UserAuthorization<F> {
|
||||
repo: &mut impl RepositoryAccess<Error = E>,
|
||||
clock: &impl Clock,
|
||||
) -> Result<(Session, F), AuthorizationVerificationError<E>> {
|
||||
let form = match self.form {
|
||||
Some(f) => f,
|
||||
None => return Err(AuthorizationVerificationError::MissingForm),
|
||||
let Some(form) = self.form else {
|
||||
return Err(AuthorizationVerificationError::MissingForm);
|
||||
};
|
||||
|
||||
let (token, session) = self.access_token.fetch(repo).await?;
|
||||
|
@ -71,9 +71,7 @@ pub async fn get(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
// If there is no session, redirect to the login or register screen
|
||||
let url = match params.action {
|
||||
Some(CompatLoginSsoAction::Register) => {
|
||||
@ -140,9 +138,7 @@ pub async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
// If there is no session, redirect to the login or register screen
|
||||
let url = match params.action {
|
||||
Some(CompatLoginSsoAction::Register) => {
|
||||
|
@ -107,9 +107,7 @@ pub(crate) async fn get(
|
||||
let callback_destination = CallbackDestination::try_from(&grant)?;
|
||||
let continue_grant = PostAuthAction::continue_grant(grant.id);
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
// If there is no session, redirect to the login screen, redirecting here after
|
||||
// logout
|
||||
return Ok((cookie_jar, mas_router::Login::and_then(continue_grant).go()).into_response());
|
||||
|
@ -166,9 +166,7 @@ pub(crate) async fn post(
|
||||
.ok_or(RouteError::GrantNotFound)?;
|
||||
let next = PostAuthAction::continue_grant(grant_id);
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::and_then(next);
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -154,9 +154,7 @@ pub(crate) async fn post(
|
||||
.verify(&http_client_factory, &encrypter, method, &client)
|
||||
.await?;
|
||||
|
||||
let form = if let Some(form) = client_authorization.form {
|
||||
form
|
||||
} else {
|
||||
let Some(form) = client_authorization.form else {
|
||||
return Err(RouteError::BadRequest);
|
||||
};
|
||||
|
||||
|
@ -49,9 +49,7 @@ pub(crate) async fn get(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
@ -80,9 +78,7 @@ pub(crate) async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -136,9 +136,7 @@ pub(crate) async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let mut session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(mut session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -56,9 +56,7 @@ pub(crate) async fn get(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
@ -104,9 +102,7 @@ pub(crate) async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -42,9 +42,7 @@ pub(crate) async fn get(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::default();
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -97,9 +97,7 @@ pub(crate) async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
let login = mas_router::Login::and_then(mas_router::PostAuthAction::ChangePassword);
|
||||
return Ok((cookie_jar, login.go()).into_response());
|
||||
};
|
||||
|
@ -54,9 +54,7 @@ pub(crate) async fn get(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
// If there is no session, redirect to the login screen, keeping the
|
||||
// PostAuthAction
|
||||
let login = mas_router::Login::from(query.post_auth_action);
|
||||
@ -93,9 +91,7 @@ pub(crate) async fn post(
|
||||
|
||||
let maybe_session = session_info.load_session(&mut repo).await?;
|
||||
|
||||
let session = if let Some(session) = maybe_session {
|
||||
session
|
||||
} else {
|
||||
let Some(session) = maybe_session else {
|
||||
// If there is no session, redirect to the login screen, keeping the
|
||||
// PostAuthAction
|
||||
let login = mas_router::Login::from(query.post_auth_action);
|
||||
|
@ -111,9 +111,7 @@ use serde_with::{{DeserializeFromStr, SerializeDisplay}};"#,
|
||||
)?;
|
||||
|
||||
for section in &self.sections {
|
||||
let list = if let Some(list) = self.items.get(section.key) {
|
||||
list
|
||||
} else {
|
||||
let Some(list) = self.items.get(section.key) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
|
@ -351,9 +351,8 @@ mod ec_impls {
|
||||
{
|
||||
fn from(key: &SecretKey<C>) -> Self {
|
||||
let point = key.public_key().to_encoded_point(false);
|
||||
let (x, y) = match point.coordinates() {
|
||||
Coordinates::Uncompressed { x, y } => (x, y),
|
||||
_ => unreachable!(),
|
||||
let Coordinates::Uncompressed { x, y } = point.coordinates() else {
|
||||
unreachable!()
|
||||
};
|
||||
let d = key.to_be_bytes();
|
||||
EcPrivateParameters {
|
||||
|
@ -306,9 +306,8 @@ mod ec_impls {
|
||||
{
|
||||
fn from(key: &PublicKey<C>) -> Self {
|
||||
let point = key.to_encoded_point(false);
|
||||
let (x, y) = match point.coordinates() {
|
||||
Coordinates::Uncompressed { x, y } => (x, y),
|
||||
_ => unreachable!(),
|
||||
let Coordinates::Uncompressed { x, y } = point.coordinates() else {
|
||||
unreachable!()
|
||||
};
|
||||
EcPublicParameters {
|
||||
crv: C::CRV,
|
||||
|
@ -235,17 +235,13 @@ impl<'a, T> Jwt<'a, T> {
|
||||
let candidates = constraints.filter(&**jwks);
|
||||
|
||||
for candidate in candidates {
|
||||
let key = match crate::jwa::AsymmetricVerifyingKey::from_jwk_and_alg(
|
||||
let Ok(key) = crate::jwa::AsymmetricVerifyingKey::from_jwk_and_alg(
|
||||
candidate.params(),
|
||||
self.header().alg(),
|
||||
) {
|
||||
Ok(v) => v,
|
||||
Err(_) => continue,
|
||||
};
|
||||
) else { continue };
|
||||
|
||||
match self.verify(&key) {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(_) => continue,
|
||||
if self.verify(&key).is_ok() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -74,14 +74,11 @@ impl ProxyProtocolV1Info {
|
||||
}
|
||||
|
||||
// Let's check in the first 108 bytes if we find a CRLF
|
||||
let crlf = if let Some(crlf) = buf
|
||||
let Some(crlf) = buf
|
||||
.as_ref()
|
||||
.windows(2)
|
||||
.take(108)
|
||||
.position(|needle| needle == [0x0D, 0x0A])
|
||||
{
|
||||
crlf
|
||||
} else {
|
||||
.position(|needle| needle == [0x0D, 0x0A]) else {
|
||||
// If not, it might be because we don't have enough bytes
|
||||
return if buf.remaining() < 108 {
|
||||
Err(E::NotEnoughBytes)
|
||||
|
@ -255,7 +255,7 @@ where
|
||||
// Then look for connections to accept
|
||||
res = accept_all.next(), if !accept_all.is_empty() => {
|
||||
// SAFETY: We shouldn't reach this branch if the unordered future set is empty
|
||||
let res = if let Some(res) = res { res } else { unreachable!() };
|
||||
let Some(res) = res else { unreachable!() };
|
||||
|
||||
// Spawn the connection in the set, so we don't have to wait for the handshake to
|
||||
// accept the next connection. This allows us to keep track of active connections
|
||||
|
@ -59,9 +59,8 @@ impl<T> Localized<T> {
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let map = match map.remove(field_name) {
|
||||
Some(map) => map,
|
||||
None => return Ok(None),
|
||||
let Some(map) = map.remove(field_name) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let mut non_localized = None;
|
||||
|
@ -190,12 +190,8 @@ pub fn verify_id_token<'a>(
|
||||
// Subject identifier must be present.
|
||||
let sub = claims::SUB.extract_required(&mut claims)?;
|
||||
|
||||
// No more checks if there is no previous ID token.
|
||||
let auth_id_token = match auth_id_token {
|
||||
Some(id_token) => id_token,
|
||||
None => return Ok(id_token),
|
||||
};
|
||||
|
||||
// More checks if there is a previous ID token.
|
||||
if let Some(auth_id_token) = auth_id_token {
|
||||
let mut auth_claims = auth_id_token.payload().clone();
|
||||
|
||||
// Subject identifier must always be the same.
|
||||
@ -212,6 +208,7 @@ pub fn verify_id_token<'a>(
|
||||
return Err(IdTokenError::WrongAuthTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(id_token)
|
||||
}
|
||||
|
@ -28,14 +28,8 @@ where
|
||||
}
|
||||
|
||||
pub fn http_all_error_status_codes() -> impl RangeBounds<StatusCode> {
|
||||
let client_errors_start_code = match StatusCode::from_u16(400) {
|
||||
Ok(code) => code,
|
||||
Err(_) => unreachable!(),
|
||||
};
|
||||
let server_errors_end_code = match StatusCode::from_u16(599) {
|
||||
Ok(code) => code,
|
||||
Err(_) => unreachable!(),
|
||||
};
|
||||
let Ok(client_errors_start_code) = StatusCode::from_u16(400) else { unreachable!() };
|
||||
let Ok(server_errors_end_code) = StatusCode::from_u16(599) else { unreachable!() };
|
||||
|
||||
client_errors_start_code..=server_errors_end_code
|
||||
}
|
||||
|
Reference in New Issue
Block a user