disable registrations by default

This commit is contained in:
Sebastian Hugentobler 2024-07-10 15:26:35 +02:00
parent 23ed80aa4b
commit d9388ae56d
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
4 changed files with 16 additions and 1 deletions

View File

@ -30,6 +30,7 @@ Usage: hesinde-sync [OPTIONS]
Options: Options:
-a, --address <ADDRESS> Address to listen on [env: ADDRESS=] [default: localhost:3030] -a, --address <ADDRESS> Address to listen on [env: ADDRESS=] [default: localhost:3030]
-d, --db-connection <DB_CONNECTION> From which file to read the database connection string ("-" for stdin) [env: DB_CONNECTION=] [default: -] -d, --db-connection <DB_CONNECTION> From which file to read the database connection string ("-" for stdin) [env: DB_CONNECTION=] [default: -]
-a, --allow-registering Whether new users can register [env: ALLOW_REGISTERING=]
-h, --help Print help -h, --help Print help
-V, --version Print version -V, --version Print version
``` ```

View File

@ -78,6 +78,8 @@ pub struct Api;
impl Api { impl Api {
/// Register a new user. /// Register a new user.
/// ///
/// If registration is disabled return early.
///
/// If a user of that id already exist, return a conflict. /// If a user of that id already exist, return a conflict.
#[oai(path = "/users/create", method = "post")] #[oai(path = "/users/create", method = "post")]
async fn register( async fn register(
@ -85,7 +87,14 @@ impl Api {
req: Json<RegisterRequest>, req: Json<RegisterRequest>,
state: Data<&Arc<AppState>>, state: Data<&Arc<AppState>>,
) -> Result<payload::Response<payload::Json<UserCreated>>> { ) -> Result<payload::Response<payload::Json<UserCreated>>> {
let db = &state.0.db; if !state.config.allow_registration {
return Err(Error::from_string(
"Registration is disabled.",
StatusCode::UNAUTHORIZED,
));
}
let db = &state.db;
if db.get_user(&req.username).await?.is_some() { if db.get_user(&req.username).await?.is_some() {
Err(Error::from_status(StatusCode::CONFLICT)) Err(Error::from_status(StatusCode::CONFLICT))
} else { } else {

View File

@ -13,4 +13,8 @@ pub struct Config {
/// From which file to read the database connection string ("-" for stdin) /// From which file to read the database connection string ("-" for stdin)
#[arg(short, long, env, default_value = "-")] #[arg(short, long, env, default_value = "-")]
pub db_connection: String, pub db_connection: String,
/// Whether new users can register.
#[arg(short = 'r', long, env, default_value = "false")]
pub allow_registration: bool,
} }

View File

@ -15,6 +15,7 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let args = Config::parse(); let args = Config::parse();
println!("{}", args.allow_registration);
let db_url = read_db_url(&args.db_connection)?; let db_url = read_db_url(&args.db_connection)?;
hesinde_sync::run(&args, &db_url).await hesinde_sync::run(&args, &db_url).await