Lines
82.02 %
Functions
31.25 %
Branches
100 %
use argon2::{Argon2, PasswordHasher};
use askama::Template;
use axum::{Json, http::StatusCode, response::Html};
use rust_i18n::t;
use server::command::CmdError;
use server::command::user::{Account, NewUser};
use server::config::ConfigError;
use server::provision::Provisioned;
use crate::{model::RegisterUserSchema, response::WebError};
#[derive(Template)]
#[template(
source = r#"<div class="success-alert">User {{ email }} created</div>"#,
ext = "html"
)]
struct Created<'a> {
email: &'a str,
}
fn email_conflict() -> WebError {
WebError::new(
StatusCode::CONFLICT,
t!("User with that email already exists").to_string(),
)
fn insert_failure(error: CmdError) -> WebError {
match error {
CmdError::Config(ConfigError::EmailTaken) => email_conflict(),
CmdError::Config(ConfigError::EmailBlank) => {
WebError::bad_request(t!("Email must not be empty").to_string())
other => WebError::from(other),
fn hash_password(password: &str) -> Result<String, WebError> {
Argon2::default()
.hash_password(password.as_bytes())
.map(|hash| hash.to_string())
.map_err(|e| WebError::internal("Password hashing failed", e))
/// Drops a database this request provisioned; one it merely found belongs to someone's ledger.
async fn drop_orphan(provisioned: &Provisioned, user_id: uuid::Uuid) {
if !provisioned.created {
return;
if let Err(e) = server::provision::drop_user_database(user_id).await {
tracing::error!("{} {e}", t!("Failed to clean up provisioned database:"));
/// Creates the user row.
///
/// `verified` is set by `create_user`, not left to the column default:
/// registration is admin-gated, so the admin has already vouched for the
/// account, and nothing ever sets the flag afterwards.
async fn create_user(
body: &RegisterUserSchema,
user_id: uuid::Uuid,
provisioned: &Provisioned,
public_key: &str,
password_hash: &str,
) -> Result<Account, WebError> {
let insert = server::command::user::create_user(NewUser {
id: user_id,
name: &body.name,
email: &body.email,
password_hash,
database_name: &provisioned.database_name,
jwt_public_key: public_key,
})
.await;
match insert {
Ok(account) => Ok(account),
Err(e) => {
// A concurrent registration may have won the UNIQUE(email) race.
drop_orphan(provisioned, user_id).await;
Err(insert_failure(e))
pub async fn register_user_handler(
Json(body): Json<RegisterUserSchema>,
) -> Result<Html<String>, WebError> {
server::command::user::validate_email(&body.email).map_err(|e| insert_failure(e.into()))?;
let taken = server::command::user::email_taken(&body.email)
.await
.map_err(WebError::from)?;
if taken {
return Err(email_conflict());
let password_hash = hash_password(&body.password)?;
let user_id = uuid::Uuid::new_v4();
// The private half goes into the user's own database, the public half
// into the global users row.
let keypair = server::auth_keys::generate()
.map_err(|e| WebError::internal("Key generation failed", e))?;
let provisioned = server::provision::provision_user_database(user_id)
.map_err(|e| WebError::internal("Provisioning failed", e))?;
if let Err(e) = server::provision::store_user_private_key(
&provisioned.database_name,
&keypair.private_pem_b64,
{
drop_orphan(&provisioned, user_id).await;
return Err(WebError::internal("Storing the signing key failed", e));
let account = create_user(
&body,
user_id,
&provisioned,
&keypair.public_pem_b64,
&password_hash,
.await?;
created(&account.email)
fn created(email: &str) -> Result<Html<String>, WebError> {
Created { email }
.render()
.map(Html)
.map_err(|e| WebError::internal("Rendering the registration result failed", e))
#[cfg(test)]
#[path = "register_tests.rs"]
mod tests;