feat: implement authentication module with JWT support and user management
This commit is contained in:
51
apps/api/src/configs/auth.rs
Normal file
51
apps/api/src/configs/auth.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use config::{Config, ConfigError};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::configs::key::{
|
||||
AUTH_DEFAULT_ADMIN_PASSWORD_KEY, AUTH_DEFAULT_ADMIN_USERNAME_KEY, AUTH_JWT_SECRET_KEY,
|
||||
};
|
||||
|
||||
use super::FromConfig;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AuthSettings {
|
||||
pub jwt_secret: Option<String>,
|
||||
pub default_admin_username: Option<String>,
|
||||
pub default_admin_password: Option<String>,
|
||||
}
|
||||
|
||||
impl FromConfig for AuthSettings {
|
||||
fn from_config(_config: &Config) -> Result<Self, String> {
|
||||
Ok(AuthSettings {
|
||||
jwt_secret: _config
|
||||
.get_string(AUTH_JWT_SECRET_KEY)
|
||||
.inspect_err(|err| {
|
||||
match err {
|
||||
ConfigError::NotFound(_) => {
|
||||
warn!(
|
||||
"{} not found in configuration, A random secret will be generated at runtime.",
|
||||
AUTH_JWT_SECRET_KEY
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
warn!(
|
||||
"Failed to read {} from configuration, A random secret will be generated at runtime: {}",
|
||||
AUTH_JWT_SECRET_KEY, err
|
||||
);
|
||||
}
|
||||
};
|
||||
})
|
||||
.ok(),
|
||||
default_admin_username: _config
|
||||
.get_string(AUTH_DEFAULT_ADMIN_USERNAME_KEY)
|
||||
.ok(),
|
||||
default_admin_password: _config
|
||||
.get_string(AUTH_DEFAULT_ADMIN_PASSWORD_KEY)
|
||||
.ok(),
|
||||
})
|
||||
}
|
||||
|
||||
fn validate(&self) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,7 @@ pub(crate) const SERVER_PORT_KEY: &str = "SERVER.PORT";
|
||||
pub(crate) const DATABASE_URL_KEY: &str = "DATABASE.URL";
|
||||
pub(crate) const DATABASE_MAX_CONNECTIONS_KEY: &str = "DATABASE.MAX_CONNECTIONS";
|
||||
pub(crate) const DATABASE_MIGRATE_ON_STARTUP_KEY: &str = "DATABASE.MIGRATION.MIGRATE_ON_STARTUP";
|
||||
//
|
||||
pub(crate) const AUTH_JWT_SECRET_KEY: &str = "AUTH.JWT_SECRET";
|
||||
pub(crate) const AUTH_DEFAULT_ADMIN_USERNAME_KEY: &str = "AUTH.DEFAULT_ADMIN_USERNAME";
|
||||
pub(crate) const AUTH_DEFAULT_ADMIN_PASSWORD_KEY: &str = "AUTH.DEFAULT_ADMIN_PASSWORD";
|
||||
|
||||
Reference in New Issue
Block a user