Add users authentication routes

This commit is contained in:
2025-11-03 22:17:29 +01:00
parent 830f47b61f
commit bc815a5cf1
21 changed files with 1417 additions and 451 deletions

View File

@@ -1,6 +1,7 @@
use crate::users::{APITokenID, UserEmail};
use crate::utils::crypt_utils::sha256str;
use clap::Parser;
use s3::creds::Credentials;
use s3::{Bucket, Region};
use std::path::{Path, PathBuf};
/// Matrix gateway backend API
#[derive(Parser, Debug, Clone)]
@@ -11,7 +12,7 @@ pub struct AppConfig {
pub listen_address: String,
/// Website origin
#[clap(short, long, env, default_value = "http://localhost:8000")]
#[clap(short, long, env, default_value = "http://localhost:5173")]
pub website_origin: String,
/// Proxy IP, might end with a star "*"
@@ -75,29 +76,9 @@ pub struct AppConfig {
#[arg(long, env, default_value = "APP_ORIGIN/oidc_cb")]
oidc_redirect_url: String,
/// S3 Bucket name
#[arg(long, env, default_value = "matrix-gw")]
s3_bucket_name: String,
/// S3 region (if not using Minio)
#[arg(long, env, default_value = "eu-central-1")]
s3_region: String,
/// S3 API endpoint
#[arg(long, env, default_value = "http://localhost:9000")]
s3_endpoint: String,
/// S3 access key
#[arg(long, env, default_value = "minioadmin")]
s3_access_key: String,
/// S3 secret key
#[arg(long, env, default_value = "minioadmin")]
s3_secret_key: String,
/// S3 skip auto create bucket if not existing
#[arg(long, env)]
pub s3_skip_auto_create_bucket: bool,
/// Application storage path
#[arg(long, env, default_value = "app_storage")]
storage_path: String,
}
lazy_static::lazy_static! {
@@ -113,10 +94,10 @@ impl AppConfig {
}
/// Get auto login email (if not empty)
pub fn unsecure_auto_login_email(&self) -> Option<&str> {
pub fn unsecure_auto_login_email(&self) -> Option<UserEmail> {
match self.unsecure_auto_login_email.as_deref() {
None | Some("") => None,
s => s,
Some(s) => Some(UserEmail(s.to_owned())),
}
}
@@ -165,28 +146,29 @@ impl AppConfig {
}
}
/// Get s3 bucket credentials
pub fn s3_credentials(&self) -> anyhow::Result<Credentials> {
Ok(Credentials::new(
Some(&self.s3_access_key),
Some(&self.s3_secret_key),
None,
None,
None,
)?)
/// Get storage path
pub fn storage_path(&self) -> &Path {
Path::new(self.storage_path.as_str())
}
/// Get S3 bucket
pub fn s3_bucket(&self) -> anyhow::Result<Box<Bucket>> {
Ok(Bucket::new(
&self.s3_bucket_name,
Region::Custom {
region: self.s3_region.to_string(),
endpoint: self.s3_endpoint.to_string(),
},
self.s3_credentials()?,
)?
.with_path_style())
/// User storage directory
pub fn user_directory(&self, mail: &UserEmail) -> PathBuf {
self.storage_path().join("users").join(sha256str(&mail.0))
}
/// User metadata file
pub fn user_metadata_file_path(&self, mail: &UserEmail) -> PathBuf {
self.user_directory(mail).join("metadata.json")
}
/// User API tokens directory
pub fn user_api_token_directory(&self, mail: &UserEmail) -> PathBuf {
self.user_directory(mail).join("api-tokens")
}
/// User API token metadata file
pub fn user_api_token_metadata_file(&self, mail: &UserEmail, id: &APITokenID) -> PathBuf {
self.user_api_token_directory(mail).join(id.0.to_string())
}
}