rustbasic-passport 0.1.1

OAuth2 server and personal access tokens for Rust applications, a port of laravel/passport for the RustBasic ecosystem.
Documentation
use std::sync::Arc;

use async_trait::async_trait;

use crate::entity::{AccessToken, AuthCode, Client, DeviceCode, RefreshToken};
use crate::error::Error;

#[async_trait]
pub trait ClientStore: Send + Sync {
    async fn find_by_id(&self, id: &str) -> Result<Option<Client>, Error>;
    async fn find_personal_access_client(&self) -> Result<Option<Client>, Error>;
    async fn save(&self, client: &Client) -> Result<(), Error>;
}

#[async_trait]
pub trait AccessTokenStore: Send + Sync {
    async fn find_by_id(&self, id: &str) -> Result<Option<AccessToken>, Error>;
    async fn save(&self, token: &AccessToken) -> Result<(), Error>;
}

#[async_trait]
pub trait AuthCodeStore: Send + Sync {
    async fn find_by_id(&self, id: &str) -> Result<Option<AuthCode>, Error>;
    async fn save(&self, code: &AuthCode) -> Result<(), Error>;
    /// Atomically revoke an auth code if it has not been revoked yet.
    ///
    /// Implementasi harus menjalankan operasi atomic pembanding-dan-set
    /// (mis. `UPDATE ... SET revoked = 1 WHERE id = ? AND revoked = 0` dan
    /// kembalikan `true` hanya jika ada baris yang ter-update). Ini wajib
    /// agar auth code benar-benar sekali pakai, bahkan saat dua permintaan
    /// tukar tiba bersamaan.
    async fn revoke_if_unrevoked(&self, id: &str) -> Result<bool, Error>;
}

#[async_trait]
pub trait RefreshTokenStore: Send + Sync {
    async fn find_by_id(&self, id: &str) -> Result<Option<RefreshToken>, Error>;
    async fn save(&self, token: &RefreshToken) -> Result<(), Error>;
    /// Atomically revoke a refresh token if it has not been revoked yet.
    ///
    /// Sama seperti [`AuthCodeStore::revoke_if_unrevoked`]: wajib atomic
    /// (`UPDATE ... SET revoked = 1 WHERE id = ? AND revoked = 0`) agar
    /// rotasi refresh token aman terhadap penggunaan ganda secara konkuren.
    async fn revoke_if_unrevoked(&self, id: &str) -> Result<bool, Error>;
}

#[async_trait]
pub trait DeviceCodeStore: Send + Sync {
    async fn find_by_device_code(&self, id: &str) -> Result<Option<DeviceCode>, Error>;
    async fn find_by_user_code(&self, user_code: &str) -> Result<Option<DeviceCode>, Error>;
    async fn save(&self, code: &DeviceCode) -> Result<(), Error>;
}

#[async_trait]
pub trait UserProvider: Send + Sync {
    async fn find_by_credentials(
        &self,
        identifier: &str,
        password: &str,
    ) -> Result<Option<String>, Error>;
}

pub trait Stores: Send + Sync + 'static {
    fn clients(&self) -> &dyn ClientStore;
    fn tokens(&self) -> &dyn AccessTokenStore;
    fn auth_codes(&self) -> &dyn AuthCodeStore;
    fn refresh_tokens(&self) -> &dyn RefreshTokenStore;
    fn device_codes(&self) -> &dyn DeviceCodeStore;
    fn users(&self) -> &dyn UserProvider;
}

pub struct StoreBundle<C, T, A, R, D, U> {
    clients: C,
    tokens: T,
    auth_codes: A,
    refresh_tokens: R,
    device_codes: D,
    users: U,
}

impl<C, T, A, R, D, U> StoreBundle<C, T, A, R, D, U> {
    pub fn new(
        clients: C,
        tokens: T,
        auth_codes: A,
        refresh_tokens: R,
        device_codes: D,
        users: U,
    ) -> Self {
        Self {
            clients,
            tokens,
            auth_codes,
            refresh_tokens,
            device_codes,
            users,
        }
    }
}

impl<C, T, A, R, D, U> Stores for StoreBundle<C, T, A, R, D, U>
where
    C: ClientStore + 'static,
    T: AccessTokenStore + 'static,
    A: AuthCodeStore + 'static,
    R: RefreshTokenStore + 'static,
    D: DeviceCodeStore + 'static,
    U: UserProvider + 'static,
{
    fn clients(&self) -> &dyn ClientStore {
        &self.clients
    }

    fn tokens(&self) -> &dyn AccessTokenStore {
        &self.tokens
    }

    fn auth_codes(&self) -> &dyn AuthCodeStore {
        &self.auth_codes
    }

    fn refresh_tokens(&self) -> &dyn RefreshTokenStore {
        &self.refresh_tokens
    }

    fn device_codes(&self) -> &dyn DeviceCodeStore {
        &self.device_codes
    }

    fn users(&self) -> &dyn UserProvider {
        &self.users
    }
}

impl<S: Stores> Stores for Arc<S> {
    fn clients(&self) -> &dyn ClientStore {
        (**self).clients()
    }

    fn tokens(&self) -> &dyn AccessTokenStore {
        (**self).tokens()
    }

    fn auth_codes(&self) -> &dyn AuthCodeStore {
        (**self).auth_codes()
    }

    fn refresh_tokens(&self) -> &dyn RefreshTokenStore {
        (**self).refresh_tokens()
    }

    fn device_codes(&self) -> &dyn DeviceCodeStore {
        (**self).device_codes()
    }

    fn users(&self) -> &dyn UserProvider {
        (**self).users()
    }
}