You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-11-21 23:00:50 +03:00
Split the storage trait from the implementation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
// Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -12,31 +12,20 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use sqlx::{PgPool, Postgres, Transaction};
|
||||
|
||||
use crate::{
|
||||
compat::{
|
||||
CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionRepository,
|
||||
CompatSsoLoginRepository, PgCompatAccessTokenRepository, PgCompatRefreshTokenRepository,
|
||||
PgCompatSessionRepository, PgCompatSsoLoginRepository,
|
||||
CompatSsoLoginRepository,
|
||||
},
|
||||
oauth2::{
|
||||
OAuth2AccessTokenRepository, OAuth2AuthorizationGrantRepository, OAuth2ClientRepository,
|
||||
OAuth2RefreshTokenRepository, OAuth2SessionRepository, PgOAuth2AccessTokenRepository,
|
||||
PgOAuth2AuthorizationGrantRepository, PgOAuth2ClientRepository,
|
||||
PgOAuth2RefreshTokenRepository, PgOAuth2SessionRepository,
|
||||
OAuth2RefreshTokenRepository, OAuth2SessionRepository,
|
||||
},
|
||||
upstream_oauth2::{
|
||||
PgUpstreamOAuthLinkRepository, PgUpstreamOAuthProviderRepository,
|
||||
PgUpstreamOAuthSessionRepository, UpstreamOAuthLinkRepository,
|
||||
UpstreamOAuthProviderRepository, UpstreamOAuthSessionRepository,
|
||||
UpstreamOAuthLinkRepository, UpstreamOAuthProviderRepository,
|
||||
UpstreamOAuthSessionRepository,
|
||||
},
|
||||
user::{
|
||||
BrowserSessionRepository, PgBrowserSessionRepository, PgUserEmailRepository,
|
||||
PgUserPasswordRepository, PgUserRepository, UserEmailRepository, UserPasswordRepository,
|
||||
UserRepository,
|
||||
},
|
||||
DatabaseError,
|
||||
user::{BrowserSessionRepository, UserEmailRepository, UserPasswordRepository, UserRepository},
|
||||
};
|
||||
|
||||
pub trait Repository: Send {
|
||||
@@ -126,109 +115,3 @@ pub trait Repository: Send {
|
||||
fn compat_access_token(&mut self) -> Self::CompatAccessTokenRepository<'_>;
|
||||
fn compat_refresh_token(&mut self) -> Self::CompatRefreshTokenRepository<'_>;
|
||||
}
|
||||
|
||||
pub struct PgRepository {
|
||||
txn: Transaction<'static, Postgres>,
|
||||
}
|
||||
|
||||
impl PgRepository {
|
||||
pub async fn from_pool(pool: &PgPool) -> Result<Self, DatabaseError> {
|
||||
let txn = pool.begin().await?;
|
||||
Ok(PgRepository { txn })
|
||||
}
|
||||
|
||||
pub async fn save(self) -> Result<(), DatabaseError> {
|
||||
self.txn.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cancel(self) -> Result<(), DatabaseError> {
|
||||
self.txn.rollback().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Repository for PgRepository {
|
||||
type Error = DatabaseError;
|
||||
|
||||
type UpstreamOAuthLinkRepository<'c> = PgUpstreamOAuthLinkRepository<'c> where Self: 'c;
|
||||
type UpstreamOAuthProviderRepository<'c> = PgUpstreamOAuthProviderRepository<'c> where Self: 'c;
|
||||
type UpstreamOAuthSessionRepository<'c> = PgUpstreamOAuthSessionRepository<'c> where Self: 'c;
|
||||
type UserRepository<'c> = PgUserRepository<'c> where Self: 'c;
|
||||
type UserEmailRepository<'c> = PgUserEmailRepository<'c> where Self: 'c;
|
||||
type UserPasswordRepository<'c> = PgUserPasswordRepository<'c> where Self: 'c;
|
||||
type BrowserSessionRepository<'c> = PgBrowserSessionRepository<'c> where Self: 'c;
|
||||
type OAuth2ClientRepository<'c> = PgOAuth2ClientRepository<'c> where Self: 'c;
|
||||
type OAuth2AuthorizationGrantRepository<'c> = PgOAuth2AuthorizationGrantRepository<'c> where Self: 'c;
|
||||
type OAuth2SessionRepository<'c> = PgOAuth2SessionRepository<'c> where Self: 'c;
|
||||
type OAuth2AccessTokenRepository<'c> = PgOAuth2AccessTokenRepository<'c> where Self: 'c;
|
||||
type OAuth2RefreshTokenRepository<'c> = PgOAuth2RefreshTokenRepository<'c> where Self: 'c;
|
||||
type CompatSessionRepository<'c> = PgCompatSessionRepository<'c> where Self: 'c;
|
||||
type CompatSsoLoginRepository<'c> = PgCompatSsoLoginRepository<'c> where Self: 'c;
|
||||
type CompatAccessTokenRepository<'c> = PgCompatAccessTokenRepository<'c> where Self: 'c;
|
||||
type CompatRefreshTokenRepository<'c> = PgCompatRefreshTokenRepository<'c> where Self: 'c;
|
||||
|
||||
fn upstream_oauth_link(&mut self) -> Self::UpstreamOAuthLinkRepository<'_> {
|
||||
PgUpstreamOAuthLinkRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn upstream_oauth_provider(&mut self) -> Self::UpstreamOAuthProviderRepository<'_> {
|
||||
PgUpstreamOAuthProviderRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn upstream_oauth_session(&mut self) -> Self::UpstreamOAuthSessionRepository<'_> {
|
||||
PgUpstreamOAuthSessionRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn user(&mut self) -> Self::UserRepository<'_> {
|
||||
PgUserRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn user_email(&mut self) -> Self::UserEmailRepository<'_> {
|
||||
PgUserEmailRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn user_password(&mut self) -> Self::UserPasswordRepository<'_> {
|
||||
PgUserPasswordRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn browser_session(&mut self) -> Self::BrowserSessionRepository<'_> {
|
||||
PgBrowserSessionRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn oauth2_client(&mut self) -> Self::OAuth2ClientRepository<'_> {
|
||||
PgOAuth2ClientRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn oauth2_authorization_grant(&mut self) -> Self::OAuth2AuthorizationGrantRepository<'_> {
|
||||
PgOAuth2AuthorizationGrantRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn oauth2_session(&mut self) -> Self::OAuth2SessionRepository<'_> {
|
||||
PgOAuth2SessionRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn oauth2_access_token(&mut self) -> Self::OAuth2AccessTokenRepository<'_> {
|
||||
PgOAuth2AccessTokenRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn oauth2_refresh_token(&mut self) -> Self::OAuth2RefreshTokenRepository<'_> {
|
||||
PgOAuth2RefreshTokenRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn compat_session(&mut self) -> Self::CompatSessionRepository<'_> {
|
||||
PgCompatSessionRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn compat_sso_login(&mut self) -> Self::CompatSsoLoginRepository<'_> {
|
||||
PgCompatSsoLoginRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn compat_access_token(&mut self) -> Self::CompatAccessTokenRepository<'_> {
|
||||
PgCompatAccessTokenRepository::new(&mut self.txn)
|
||||
}
|
||||
|
||||
fn compat_refresh_token(&mut self) -> Self::CompatRefreshTokenRepository<'_> {
|
||||
PgCompatRefreshTokenRepository::new(&mut self.txn)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user