1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-09 04:22:45 +03:00

Make the HomeserverConnection available in handlers

This commit is contained in:
Quentin Gliech
2024-02-28 09:58:27 +01:00
parent 20dd5ca311
commit 4aeb446061
8 changed files with 96 additions and 65 deletions

View File

@@ -17,6 +17,7 @@ use chrono::Duration;
use hyper::StatusCode;
use mas_axum_utils::sentry::SentryEventID;
use mas_data_model::{CompatSession, CompatSsoLoginState, Device, TokenType, User, UserAgent};
use mas_matrix::BoxHomeserverConnection;
use mas_storage::{
compat::{
CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionRepository,
@@ -32,7 +33,7 @@ use serde_with::{serde_as, skip_serializing_none, DurationMilliSeconds};
use thiserror::Error;
use zeroize::Zeroizing;
use super::{MatrixError, MatrixHomeserver};
use super::MatrixError;
use crate::{
impl_from_error_for_route, passwords::PasswordManager, site_config::SiteConfig,
BoundActivityTracker,
@@ -215,7 +216,7 @@ pub(crate) async fn post(
State(password_manager): State<PasswordManager>,
mut repo: BoxRepository,
activity_tracker: BoundActivityTracker,
State(homeserver): State<MatrixHomeserver>,
State(homeserver): State<BoxHomeserverConnection>,
State(site_config): State<SiteConfig>,
user_agent: Option<TypedHeader<headers::UserAgent>>,
Json(input): Json<RequestBody>,
@@ -254,7 +255,7 @@ pub(crate) async fn post(
.await?;
}
let user_id = format!("@{username}:{homeserver}", username = user.username);
let user_id = homeserver.mxid(&user.username);
// If the client asked for a refreshable token, make it expire
let expires_in = if input.refresh_token {

View File

@@ -22,22 +22,6 @@ pub(crate) mod login_sso_redirect;
pub(crate) mod logout;
pub(crate) mod refresh;
#[derive(Debug, Clone)]
pub struct MatrixHomeserver(String);
impl MatrixHomeserver {
#[must_use]
pub const fn new(hs: String) -> Self {
Self(hs)
}
}
impl std::fmt::Display for MatrixHomeserver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Serialize)]
struct MatrixError {
errcode: &'static str,

View File

@@ -43,6 +43,7 @@ use hyper::{
use mas_axum_utils::{cookies::CookieJar, FancyError};
use mas_http::CorsLayerExt;
use mas_keystore::{Encrypter, Keystore};
use mas_matrix::BoxHomeserverConnection;
use mas_policy::Policy;
use mas_router::{Route, UrlBuilder};
use mas_storage::{BoxClock, BoxRepository, BoxRng};
@@ -88,7 +89,6 @@ pub use mas_axum_utils::{
pub use self::{
activity_tracker::{ActivityTracker, Bound as BoundActivityTracker},
compat::MatrixHomeserver,
graphql::schema as graphql_schema,
preferred_language::PreferredLanguage,
site_config::SiteConfig,
@@ -253,7 +253,7 @@ where
S: Clone + Send + Sync + 'static,
UrlBuilder: FromRef<S>,
SiteConfig: FromRef<S>,
MatrixHomeserver: FromRef<S>,
BoxHomeserverConnection: FromRef<S>,
PasswordManager: FromRef<S>,
BoundActivityTracker: FromRequestParts<S>,
BoxRepository: FromRequestParts<S>,

View File

@@ -38,7 +38,7 @@ use mas_axum_utils::{
};
use mas_i18n::Translator;
use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey};
use mas_matrix::{HomeserverConnection, MockHomeserverConnection};
use mas_matrix::{BoxHomeserverConnection, HomeserverConnection, MockHomeserverConnection};
use mas_policy::{InstantiateError, Policy, PolicyFactory};
use mas_router::{SimpleRoute, UrlBuilder};
use mas_storage::{clock::MockClock, BoxClock, BoxRepository, BoxRng, Repository};
@@ -55,7 +55,7 @@ use crate::{
passwords::{Hasher, PasswordManager},
site_config::SiteConfig,
upstream_oauth2::cache::MetadataCache,
ActivityTracker, BoundActivityTracker, MatrixHomeserver,
ActivityTracker, BoundActivityTracker,
};
// This might fail if it's not the first time it's being called, which is fine,
@@ -99,7 +99,7 @@ pub(crate) struct TestState {
pub metadata_cache: MetadataCache,
pub encrypter: Encrypter,
pub url_builder: UrlBuilder,
pub homeserver: MatrixHomeserver,
pub homeserver_connection: Arc<MockHomeserverConnection>,
pub policy_factory: Arc<PolicyFactory>,
pub graphql_schema: mas_graphql::Schema,
pub http_client_factory: HttpClientFactory,
@@ -148,11 +148,9 @@ impl TestState {
let password_manager = PasswordManager::new([(1, Hasher::argon2id(None))])?;
let homeserver = MatrixHomeserver::new("example.com".to_owned());
let policy_factory = policy_factory(serde_json::json!({})).await?;
let homeserver_connection = MockHomeserverConnection::new("example.com");
let homeserver_connection = Arc::new(MockHomeserverConnection::new("example.com"));
let http_client_factory = HttpClientFactory::new().await?;
@@ -167,7 +165,7 @@ impl TestState {
let graphql_state = TestGraphQLState {
pool: pool.clone(),
policy_factory: Arc::clone(&policy_factory),
homeserver_connection,
homeserver_connection: Arc::clone(&homeserver_connection),
rng: Arc::clone(&rng),
clock: Arc::clone(&clock),
};
@@ -186,7 +184,7 @@ impl TestState {
metadata_cache,
encrypter,
url_builder,
homeserver,
homeserver_connection,
policy_factory,
graphql_schema,
http_client_factory,
@@ -281,7 +279,7 @@ impl TestState {
struct TestGraphQLState {
pool: PgPool,
homeserver_connection: MockHomeserverConnection,
homeserver_connection: Arc<MockHomeserverConnection>,
policy_factory: Arc<PolicyFactory>,
clock: Arc<MockClock>,
rng: Arc<Mutex<ChaChaRng>>,
@@ -360,12 +358,6 @@ impl FromRef<TestState> for UrlBuilder {
}
}
impl FromRef<TestState> for MatrixHomeserver {
fn from_ref(input: &TestState) -> Self {
input.homeserver.clone()
}
}
impl FromRef<TestState> for HttpClientFactory {
fn from_ref(input: &TestState) -> Self {
input.http_client_factory.clone()
@@ -396,6 +388,12 @@ impl FromRef<TestState> for SiteConfig {
}
}
impl FromRef<TestState> for BoxHomeserverConnection {
fn from_ref(input: &TestState) -> Self {
Box::new(input.homeserver_connection.clone())
}
}
#[async_trait]
impl FromRequestParts<TestState> for ActivityTracker {
type Rejection = Infallible;