sylvia_iot_auth/models/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! Traits and implementations for accessing databases and caches.
//!
//! Currently we only provide pure MongoDB/SQLite implementation. Mixing implementation is
//! possible. For example, put users/clients in MongoDB and put tokens/codes in Redis. Then use a
//! model struct and impl to mix both databases.

use std::{error::Error as StdError, sync::Arc};

use async_trait::async_trait;

pub mod access_token;
pub mod authorization_code;
pub mod client;
pub mod login_session;
pub mod redis;
pub mod refresh_token;
pub mod user;

mod model_mongodb;
mod model_sqlite;
mod mongodb;
mod sqlite;

pub use self::{
    mongodb::conn::{self as mongodb_conn, Options as MongoDbOptions},
    redis::conn::{self as redis_conn, Options as RedisOptions},
    sqlite::conn::{self as sqlite_conn, Options as SqliteOptions},
};
pub use model_mongodb::Model as MongoDbModel;
pub use model_sqlite::Model as SqliteModel;

/// Database connection options for model implementation.
pub enum ConnOptions {
    // Pure MongoDB model implementation.
    MongoDB(MongoDbOptions),
    //MongoRedis(MongoDbOptions, RedisOptions),
    /// Pure SQLite model implementation.
    Sqlite(SqliteOptions),
}

/// The top level trait to get all models (tables/collections).
#[async_trait]
pub trait Model: Send + Sync {
    /// Close database connection.
    async fn close(&self) -> Result<(), Box<dyn StdError>>;

    /// To get the user model.
    fn user(&self) -> &dyn user::UserModel;

    /// To get the client model.
    fn client(&self) -> &dyn client::ClientModel;

    /// To get the login session model.
    fn login_session(&self) -> &dyn login_session::LoginSessionModel;

    /// To get the authorization code model.
    fn authorization_code(&self) -> &dyn authorization_code::AuthorizationCodeModel;

    /// To get the access token model.
    fn access_token(&self) -> &dyn access_token::AccessTokenModel;

    /// To get the refresh token model.
    fn refresh_token(&self) -> &dyn refresh_token::RefreshTokenModel;
}

/// To create the database model with the specified database implementation.
pub async fn new(opts: &ConnOptions) -> Result<Arc<dyn Model>, Box<dyn StdError>> {
    let model: Arc<dyn Model> = match opts {
        ConnOptions::MongoDB(opts) => Arc::new(MongoDbModel::new(opts).await?),
        ConnOptions::Sqlite(opts) => Arc::new(SqliteModel::new(opts).await?),
    };
    model.user().init().await?;
    model.client().init().await?;
    model.login_session().init().await?;
    model.authorization_code().init().await?;
    model.access_token().init().await?;
    model.refresh_token().init().await?;
    Ok(model)
}