1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-07-29 22:01:14 +03:00

Box the repository everywhere

This commit is contained in:
Quentin Gliech
2023-01-20 17:49:16 +01:00
parent f4c64c2171
commit a9facab131
49 changed files with 296 additions and 296 deletions

View File

@ -10,7 +10,7 @@ anyhow = "1.0.68"
async-graphql = { version = "5.0.4", features = ["chrono", "url"] }
chrono = "0.4.23"
serde = { version = "1.0.152", features = ["derive"] }
sqlx = { version = "0.6.2", features = ["runtime-tokio-rustls", "postgres"] }
tokio = { version = "1.23.0", features = ["sync"] }
thiserror = "1.0.38"
tracing = "0.1.37"
ulid = "1.0.0"
@ -19,7 +19,6 @@ url = "2.3.1"
oauth2-types = { path = "../oauth2-types" }
mas-data-model = { path = "../data-model" }
mas-storage = { path = "../storage" }
mas-storage-pg = { path = "../storage-pg" }
[[bin]]
name = "schema"

View File

@ -34,11 +34,10 @@ use mas_storage::{
oauth2::OAuth2ClientRepository,
upstream_oauth2::{UpstreamOAuthLinkRepository, UpstreamOAuthProviderRepository},
user::{BrowserSessionRepository, UserEmailRepository},
Pagination, Repository,
BoxRepository, Pagination,
};
use mas_storage_pg::PgRepository;
use model::CreationEvent;
use sqlx::PgPool;
use tokio::sync::Mutex;
use self::model::{
BrowserSession, Cursor, Node, NodeCursor, NodeType, OAuth2Client, UpstreamOAuth2Link,
@ -94,7 +93,7 @@ impl RootQuery {
id: ID,
) -> Result<Option<OAuth2Client>, async_graphql::Error> {
let id = NodeType::OAuth2Client.extract_ulid(&id)?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let client = repo.oauth2_client().lookup(id).await?;
@ -124,7 +123,7 @@ impl RootQuery {
) -> Result<Option<BrowserSession>, async_graphql::Error> {
let id = NodeType::BrowserSession.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
@ -150,7 +149,7 @@ impl RootQuery {
) -> Result<Option<UserEmail>, async_graphql::Error> {
let id = NodeType::UserEmail.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
@ -172,7 +171,7 @@ impl RootQuery {
) -> Result<Option<UpstreamOAuth2Link>, async_graphql::Error> {
let id = NodeType::UpstreamOAuth2Link.extract_ulid(&id)?;
let session = ctx.data_opt::<mas_data_model::BrowserSession>().cloned();
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let Some(session) = session else { return Ok(None) };
let current_user = session.user;
@ -192,7 +191,7 @@ impl RootQuery {
id: ID,
) -> Result<Option<UpstreamOAuth2Provider>, async_graphql::Error> {
let id = NodeType::UpstreamOAuth2Provider.extract_ulid(&id)?;
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let provider = repo.upstream_oauth_provider().lookup(id).await?;
@ -211,7 +210,7 @@ impl RootQuery {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, UpstreamOAuth2Provider>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,

View File

@ -15,9 +15,8 @@
use anyhow::Context as _;
use async_graphql::{Context, Description, Object, ID};
use chrono::{DateTime, Utc};
use mas_storage::{compat::CompatSessionRepository, user::UserRepository, Repository};
use mas_storage_pg::PgRepository;
use sqlx::PgPool;
use mas_storage::{compat::CompatSessionRepository, user::UserRepository, BoxRepository};
use tokio::sync::Mutex;
use url::Url;
use super::{NodeType, User};
@ -36,7 +35,7 @@ impl CompatSession {
/// The user authorized for this session.
async fn user(&self, ctx: &Context<'_>) -> Result<User, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let user = repo
.user()
.lookup(self.0.user_id)
@ -101,7 +100,7 @@ impl CompatSsoLogin {
) -> Result<Option<CompatSession>, async_graphql::Error> {
let Some(session_id) = self.0.session_id() else { return Ok(None) };
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let session = repo
.compat_session()
.lookup(session_id)

View File

@ -14,10 +14,9 @@
use anyhow::Context as _;
use async_graphql::{Context, Description, Object, ID};
use mas_storage::{oauth2::OAuth2ClientRepository, user::BrowserSessionRepository, Repository};
use mas_storage_pg::PgRepository;
use mas_storage::{oauth2::OAuth2ClientRepository, user::BrowserSessionRepository, BoxRepository};
use oauth2_types::scope::Scope;
use sqlx::PgPool;
use tokio::sync::Mutex;
use ulid::Ulid;
use url::Url;
@ -37,7 +36,7 @@ impl OAuth2Session {
/// OAuth 2.0 client used by this session.
pub async fn client(&self, ctx: &Context<'_>) -> Result<OAuth2Client, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let client = repo
.oauth2_client()
.lookup(self.0.client_id)
@ -57,7 +56,7 @@ impl OAuth2Session {
&self,
ctx: &Context<'_>,
) -> Result<BrowserSession, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let browser_session = repo
.browser_session()
.lookup(self.0.user_session_id)
@ -69,7 +68,7 @@ impl OAuth2Session {
/// User authorized for this session.
pub async fn user(&self, ctx: &Context<'_>) -> Result<User, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let browser_session = repo
.browser_session()
.lookup(self.0.user_session_id)
@ -139,7 +138,7 @@ impl OAuth2Consent {
/// OAuth 2.0 client for which the user granted access.
pub async fn client(&self, ctx: &Context<'_>) -> Result<OAuth2Client, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let client = repo
.oauth2_client()
.lookup(self.client_id)

View File

@ -16,10 +16,9 @@ use anyhow::Context as _;
use async_graphql::{Context, Object, ID};
use chrono::{DateTime, Utc};
use mas_storage::{
upstream_oauth2::UpstreamOAuthProviderRepository, user::UserRepository, Repository,
upstream_oauth2::UpstreamOAuthProviderRepository, user::UserRepository, BoxRepository,
};
use mas_storage_pg::PgRepository;
use sqlx::PgPool;
use tokio::sync::Mutex;
use super::{NodeType, User};
@ -103,7 +102,7 @@ impl UpstreamOAuth2Link {
provider.clone()
} else {
// Fetch on-the-fly
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let provider = repo
.upstream_oauth_provider()
.lookup(self.link.provider_id)
@ -122,7 +121,7 @@ impl UpstreamOAuth2Link {
user.clone()
} else if let Some(user_id) = &self.link.user_id {
// Fetch on-the-fly
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let user = repo
.user()
.lookup(*user_id)

View File

@ -22,10 +22,9 @@ use mas_storage::{
oauth2::OAuth2SessionRepository,
upstream_oauth2::UpstreamOAuthLinkRepository,
user::{BrowserSessionRepository, UserEmailRepository},
Pagination, Repository,
BoxRepository, Pagination,
};
use mas_storage_pg::PgRepository;
use sqlx::PgPool;
use tokio::sync::Mutex;
use super::{
compat_sessions::CompatSsoLogin, BrowserSession, Cursor, NodeCursor, NodeType, OAuth2Session,
@ -65,10 +64,9 @@ impl User {
&self,
ctx: &Context<'_>,
) -> Result<Option<UserEmail>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let mut user_email_repo = repo.user_email();
Ok(user_email_repo.get_primary(&self.0).await?.map(UserEmail))
}
@ -84,7 +82,7 @@ impl User {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, CompatSsoLogin>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,
@ -131,7 +129,7 @@ impl User {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, BrowserSession>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,
@ -178,7 +176,7 @@ impl User {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, UserEmail, UserEmailsPagination>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,
@ -229,7 +227,7 @@ impl User {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, OAuth2Session>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,
@ -276,7 +274,7 @@ impl User {
#[graphql(desc = "Returns the first *n* elements from the list.")] first: Option<i32>,
#[graphql(desc = "Returns the last *n* elements from the list.")] last: Option<i32>,
) -> Result<Connection<Cursor, UpstreamOAuth2Link>, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
query(
after,
@ -350,7 +348,7 @@ pub struct UserEmailsPagination(mas_data_model::User);
impl UserEmailsPagination {
/// Identifies the total count of items in the connection.
async fn total_count(&self, ctx: &Context<'_>) -> Result<usize, async_graphql::Error> {
let mut repo = PgRepository::from_pool(ctx.data::<PgPool>()?).await?;
let mut repo = ctx.data::<Mutex<BoxRepository>>()?.lock().await;
let count = repo.user_email().count(&self.0).await?;
Ok(count)
}