1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-31 09:24:31 +03:00

storage: start unifying database errors

This commit is contained in:
Quentin Gliech
2022-12-07 16:04:46 +01:00
parent 12ce2a3d04
commit 1ddc05ff01
13 changed files with 143 additions and 62 deletions

View File

@ -22,7 +22,7 @@ use mas_axum_utils::http_client_factory::HttpClientFactory;
use mas_keystore::Encrypter;
use mas_oidc_client::requests::authorization_code::AuthorizationRequestData;
use mas_router::UrlBuilder;
use mas_storage::{upstream_oauth2::lookup_provider, LookupResultExt};
use mas_storage::upstream_oauth2::lookup_provider;
use sqlx::PgPool;
use thiserror::Error;
use ulid::Ulid;
@ -46,7 +46,7 @@ impl_from_error_for_route!(sqlx::Error);
impl_from_error_for_route!(mas_http::ClientInitError);
impl_from_error_for_route!(mas_oidc_client::error::DiscoveryError);
impl_from_error_for_route!(mas_oidc_client::error::AuthorizationError);
impl_from_error_for_route!(mas_storage::upstream_oauth2::ProviderLookupError);
impl_from_error_for_route!(mas_storage::DatabaseError);
impl IntoResponse for RouteError {
fn into_response(self) -> axum::response::Response {
@ -75,8 +75,7 @@ pub(crate) async fn get(
let mut txn = pool.begin().await?;
let provider = lookup_provider(&mut txn, provider_id)
.await
.to_option()?
.await?
.ok_or(RouteError::ProviderNotFound)?;
let http_service = http_client_factory

View File

@ -96,6 +96,7 @@ pub(crate) enum RouteError {
Anyhow(#[from] anyhow::Error),
}
impl_from_error_for_route!(mas_storage::DatabaseError);
impl_from_error_for_route!(mas_storage::GenericLookupError);
impl_from_error_for_route!(mas_storage::upstream_oauth2::SessionLookupError);
impl_from_error_for_route!(mas_http::ClientInitError);
@ -242,9 +243,7 @@ pub(crate) async fn get(
let subject = mas_jose::claims::SUB.extract_required(&mut id_token)?;
// Look for an existing link
let maybe_link = lookup_link_by_subject(&mut txn, &provider, &subject)
.await
.to_option()?;
let maybe_link = lookup_link_by_subject(&mut txn, &provider, &subject).await?;
let link = if let Some(link) = maybe_link {
link

View File

@ -79,6 +79,7 @@ impl_from_error_for_route!(mas_storage::user::ActiveSessionLookupError);
impl_from_error_for_route!(mas_storage::user::UserLookupError);
impl_from_error_for_route!(mas_axum_utils::csrf::CsrfError);
impl_from_error_for_route!(super::cookie::UpstreamSessionNotFound);
impl_from_error_for_route!(mas_storage::DatabaseError);
impl IntoResponse for RouteError {
fn into_response(self) -> axum::response::Response {
@ -118,8 +119,7 @@ pub(crate) async fn get(
.map_err(|_| RouteError::MissingCookie)?;
let link = lookup_link(&mut txn, link_id)
.await
.to_option()?
.await?
.ok_or(RouteError::LinkNotFound)?;
// This checks that we're in a browser session which is allowed to consume this
@ -221,8 +221,7 @@ pub(crate) async fn post(
};
let link = lookup_link(&mut txn, link_id)
.await
.to_option()?
.await?
.ok_or(RouteError::LinkNotFound)?;
// This checks that we're in a browser session which is allowed to consume this

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::Context;
use mas_router::{PostAuthAction, Route};
use mas_storage::{
compat::get_compat_sso_login_by_id, oauth2::authorization_grant::get_grant_by_id,
@ -58,11 +59,14 @@ impl OptionalPostAuthAction {
PostAuthAction::ChangePassword => PostAuthContextInner::ChangePassword,
PostAuthAction::LinkUpstream { id } => {
let link = mas_storage::upstream_oauth2::lookup_link(&mut *conn, id).await?;
let link = mas_storage::upstream_oauth2::lookup_link(&mut *conn, id)
.await?
.context("Failed to load upstream OAuth 2.0 link")?;
let provider =
mas_storage::upstream_oauth2::lookup_provider(&mut *conn, link.provider_id)
.await?;
.await?
.context("Failed to load upstream OAuth 2.0 provider")?;
let provider = Box::new(provider);
let link = Box::new(link);