mellon 0.1.0

Library for adding contemporary authentication to rust-based websites.
Documentation
use poem_openapi::{
    ApiResponse, Object,
    payload::{Json, PlainText},
};

#[derive(ApiResponse)]
pub enum CreateUser {
    /// A new user was successfully created
    #[oai(status = 200)]
    Bearer(PlainText<String>),
    /// The name is too complex (longer than 32 characters and contains non-alphanumeric characters)
    #[oai(status = 400)]
    NameIsTooComplex,
    /// The name is already taken
    #[oai(status = 409)]
    NameAlreadyTaken,
    /// Other error
    #[oai(status = 500)]
    Other,
}

#[derive(Default, Object)]
pub struct LoginChallenges {
    pub password: bool,
    pub totp: bool,
    pub webauthn: Option<serde_json::Value>,
}

/// Request login challenge
#[derive(ApiResponse)]
pub enum PrepareLogin {
    /// Information on what data to provide to proceed with login
    #[oai(status = 200)]
    Challenges(Json<LoginChallenges>),
    /// The name is too complex (longer than 32 characters and contains non-alphanumeric characters)
    #[oai(status = 400)]
    NameIsTooComplex,
    /// Username not found
    #[oai(status = 404)]
    UsernameNotFound,
    /// Other error. Details might be deliberately omitted
    #[oai(status = 500)]
    Other,
}

#[derive(ApiResponse)]
pub enum FinishLogin {
    /// Login successful
    #[oai(status = 200)]
    Bearer(PlainText<String>),
    #[oai(status = 400)]
    InvalidDataFormat,
    #[oai(status = 401)]
    WrongUsername,
    #[oai(status = 403)]
    Unauthorized,
    #[oai(status = 500)]
    Other,
}

#[derive(Object)]
pub struct CurrentCredentialsList {
    pub password: bool,
    pub totp: bool,
    pub webauthn_keys: usize,
}

#[derive(ApiResponse)]
pub enum CurrentCredentials {
    #[oai(status = 200)]
    List(Json<CurrentCredentialsList>),
    #[oai(status = 500)]
    Other,
}

#[derive(ApiResponse)]
pub enum CurrentWebauthnCredentials {
    #[oai(status = 200)]
    List(Json<Vec<WebAuthnCredential>>),
    #[oai(status = 500)]
    Other,
}

#[derive(Object)]
pub struct WebAuthnCredential {
    pub name: String,
}

#[derive(ApiResponse)]
pub enum SimpleTask {
    #[oai(status = 204)]
    Done,
    #[oai(status = 400)]
    BadInput,
    #[oai(status = 403)]
    InsufficientPermissions,
    #[oai(status = 500)]
    Failed,
}

#[derive(Object)]
pub struct NewTotpSetup {
    pub seed_base32: String,
    pub authenticator_url: String,
}

#[derive(ApiResponse)]
pub enum NewTotp {
    #[oai(status = 200)]
    Setup(Json<NewTotpSetup>),
    #[oai(status = 403)]
    InsufficientPermissions,
    #[oai(status = 500)]
    Other,
}

#[derive(ApiResponse)]
pub enum NewWebauthnCredential {
    /// Type-erased value of `webauthn_rs::prelude::CreationChallengeResponse`
    #[oai(status = 200)]
    Setup(Json<serde_json::Value>),
    #[oai(status = 403)]
    InsufficientPermissions,
    #[oai(status = 500)]
    Other,
}

#[derive(ApiResponse)]
pub enum ConfirmNewWebauthnCredential {
    /// The new webauthn credential has been successfully installed
    #[oai(status = 204)]
    Done,
    /// The name for the key uses characters that are not allowed or it is longer than 32 characters
    #[oai(status = 400)]
    KeyNameIsTooComplex,
    /// Authentication parameters are not correct
    #[oai(status = 401)]
    AuthFailed,
    /// Authentication token does not have the `upd` priviledge
    #[oai(status = 403)]
    InsufficientPermissions,
    /// Data does not confirm to schema
    #[oai(status = 422)]
    BadWebauthnPayload,
    /// Something else went wrong
    #[oai(status = 500)]
    Other,
}