Authentication service
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "authentication"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
argon2 = { version = "0.5.3", features = ["std"] }
|
||||
|
||||
thiserror = "2.0.11"
|
||||
garde = { version = "0.22.0", features = ["email", "url", "derive"] }
|
||||
derive_more = { version = "1.0.0", features = ["deref", "deref_mut", "into"] }
|
||||
|
||||
[dependencies.database]
|
||||
path = "../../database"
|
||||
@@ -0,0 +1,4 @@
|
||||
// use database::port::user;
|
||||
|
||||
mod repository;
|
||||
mod service;
|
||||
@@ -0,0 +1,86 @@
|
||||
pub use database::port::user::*;
|
||||
|
||||
use derive_more::{Deref, DerefMut};
|
||||
|
||||
#[derive(Deref, DerefMut)]
|
||||
pub struct Authenticated(User);
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait AuthenticationRepository {
|
||||
async fn get_user(&self, get: Get) -> Result<Option<User>>;
|
||||
async fn create_user(&self, new: New) -> Result<User>;
|
||||
async fn start_session(&self, user: User) -> Result<Authenticated>;
|
||||
}
|
||||
|
||||
|
||||
pub enum Get {
|
||||
Name(Name),
|
||||
Email(Email),
|
||||
}
|
||||
|
||||
impl From<Get> for Unique {
|
||||
fn from(value: Get) -> Self {
|
||||
match value {
|
||||
Get::Name(s) => Self::Name(s),
|
||||
Get::Email(s) => Self::Email(s),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adapter
|
||||
|
||||
use database::connect::Connect;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct AuthenticationAdapter<D, C, UR>
|
||||
where
|
||||
D: Connect<Connection = C>,
|
||||
UR: UserRepository<C>,
|
||||
{
|
||||
driver: D,
|
||||
_user_repository: PhantomData<UR>,
|
||||
}
|
||||
|
||||
impl<D, C, UR> AuthenticationAdapter<D, C, UR>
|
||||
where
|
||||
D: Connect<Connection = C>,
|
||||
UR: UserRepository<C>,
|
||||
{
|
||||
pub const fn new(driver: D) -> Self {
|
||||
Self {
|
||||
driver,
|
||||
_user_repository: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, C, UR> AuthenticationRepository for AuthenticationAdapter<D, C, UR>
|
||||
where
|
||||
D: Connect<Connection = C>,
|
||||
UR: UserRepository<C>,
|
||||
{
|
||||
async fn get_user(&self, get: Get) -> Result<Option<User>> {
|
||||
let c = self.driver.open_connection().await?;
|
||||
let user = UR::read(&c, get.into()).await?;
|
||||
D::close_connection(c).await?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
async fn create_user(&self, new: New) -> Result<User> {
|
||||
let mut c = self.driver.open_connection().await?;
|
||||
let user = UR::create(&mut c, new).await?;
|
||||
D::close_connection(c).await?;
|
||||
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
async fn start_session(&self, mut user: User) -> Result<Authenticated> {
|
||||
let mut c = self.driver.open_connection().await?;
|
||||
UR::update(&mut c, &mut user, Field::LastUsed(Some(Utc::now()))).await?;
|
||||
D::close_connection(c).await?;
|
||||
|
||||
Ok(Authenticated(user))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
use crate::repository::{Authenticated, AuthenticationRepository};
|
||||
use database::port::user;
|
||||
|
||||
use derive_more::{Deref, Into};
|
||||
use garde::Validate;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
// Login
|
||||
#[error("login not found")]
|
||||
LoginNotFound,
|
||||
#[error("incorrect password")]
|
||||
IncorrectPassword,
|
||||
// Register
|
||||
#[error("username is taken")]
|
||||
NameExists,
|
||||
#[error("email is already in use")]
|
||||
EmailExists,
|
||||
// Shared
|
||||
#[error("invalid password: {0}")]
|
||||
InvalidPassword(Box<dyn std::error::Error>),
|
||||
#[error("data source error: {0}")]
|
||||
Repository(Box<dyn std::error::Error>),
|
||||
}
|
||||
|
||||
pub type Result<T = (), E = Error> = std::result::Result<T, E>;
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Name(user::Name);
|
||||
|
||||
impl TryFrom<String> for Name {
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
#[derive(Validate)]
|
||||
#[garde(transparent)]
|
||||
struct Username<'a>(#[garde(alphanumeric, length(chars, min = 2, max = 31))] &'a str);
|
||||
|
||||
Username(&value).validate()?;
|
||||
Ok(Self(user::Name::try_from(value)?))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Email(user::Email);
|
||||
impl TryFrom<String> for Email {
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
#[derive(Validate)]
|
||||
#[garde(transparent)]
|
||||
pub struct Email<'a>(#[garde(email, length(chars, max = 255))] &'a str);
|
||||
|
||||
Email(&value).validate()?;
|
||||
Ok(Self(user::Email::try_from(value)?))
|
||||
}
|
||||
}
|
||||
|
||||
// #[derive(Validate, Deref, From, Into)]
|
||||
// #[garde(transparent)]
|
||||
// pub struct Password(#[garde(length(chars, min = 8))] pub String);
|
||||
|
||||
pub enum LoginBy {
|
||||
Name(Name),
|
||||
Email(Email),
|
||||
}
|
||||
|
||||
pub struct LoginData {
|
||||
login: LoginBy,
|
||||
password: String,
|
||||
}
|
||||
|
||||
pub struct RegisterData {
|
||||
pub name: Name,
|
||||
pub email: Email,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
pub trait AuthenticationContract {
|
||||
async fn name_available(&self, name: Name) -> Result;
|
||||
async fn email_available(&self, email: Email) -> Result;
|
||||
|
||||
async fn login(&mut self, data: LoginData) -> Result<Authenticated>;
|
||||
async fn register(&mut self, data: RegisterData) -> Result<Authenticated>;
|
||||
}
|
||||
|
||||
// Service
|
||||
|
||||
use crate::repository::Get;
|
||||
|
||||
use argon2::{
|
||||
Argon2,
|
||||
password_hash::{
|
||||
self, PasswordHash, PasswordHasher, PasswordVerifier, SaltString, rand_core::OsRng,
|
||||
},
|
||||
};
|
||||
|
||||
impl From<password_hash::Error> for Error {
|
||||
fn from(error: password_hash::Error) -> Self {
|
||||
match error {
|
||||
password_hash::Error::Password => Self::IncorrectPassword,
|
||||
_ => Self::InvalidPassword(error.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AuthenticationService<R>
|
||||
where
|
||||
R: AuthenticationRepository,
|
||||
{
|
||||
repository: R,
|
||||
}
|
||||
|
||||
impl<R> AuthenticationService<R>
|
||||
where
|
||||
R: AuthenticationRepository,
|
||||
{
|
||||
pub const fn new(repository: R) -> Self {
|
||||
Self { repository }
|
||||
}
|
||||
}
|
||||
|
||||
impl<R> AuthenticationContract for AuthenticationService<R>
|
||||
where
|
||||
R: AuthenticationRepository,
|
||||
{
|
||||
async fn name_available(&self, name: Name) -> Result {
|
||||
if self
|
||||
.repository
|
||||
.get_user(Get::Name(name.0))
|
||||
.await
|
||||
.map_err(Error::Repository)?
|
||||
.is_some()
|
||||
{
|
||||
return Err(Error::NameExists);
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
async fn email_available(&self, email: Email) -> Result {
|
||||
if self
|
||||
.repository
|
||||
.get_user(Get::Email(email.0))
|
||||
.await
|
||||
.map_err(Error::Repository)?
|
||||
.is_some()
|
||||
{
|
||||
return Err(Error::EmailExists);
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn login(&mut self, data: LoginData) -> Result<Authenticated> {
|
||||
if data.password.chars().count() < 8 {
|
||||
return Err(Error::InvalidPassword(
|
||||
"password must be longer than 8 characters".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let user = match data.login {
|
||||
LoginBy::Name(name) => self.repository.get_user(Get::Name(name.0)),
|
||||
LoginBy::Email(email) => self.repository.get_user(Get::Email(email.0)),
|
||||
}
|
||||
.await
|
||||
.map_err(Error::Repository)?
|
||||
.ok_or(Error::LoginNotFound)?;
|
||||
|
||||
Argon2::default().verify_password(
|
||||
data.password.as_bytes(),
|
||||
&PasswordHash::new(user.password())?,
|
||||
)?;
|
||||
|
||||
self.repository
|
||||
.start_session(user)
|
||||
.await
|
||||
.map_err(Error::Repository)
|
||||
}
|
||||
|
||||
async fn register(&mut self, data: RegisterData) -> Result<Authenticated> {
|
||||
if data.password.chars().count() < 8 {
|
||||
return Err(Error::InvalidPassword(
|
||||
"password must be longer than 8 characters".into(),
|
||||
));
|
||||
}
|
||||
|
||||
self.name_available(data.name.clone()).await?;
|
||||
self.email_available(data.email.clone()).await?;
|
||||
|
||||
// Get PHC string ($argon2id$v=19$...)
|
||||
let password = Argon2::default()
|
||||
.hash_password(data.password.as_bytes(), &SaltString::generate(&mut OsRng))?
|
||||
.to_string()
|
||||
.try_into()
|
||||
.map_err(|e| Error::InvalidPassword(Box::from(e)))?;
|
||||
|
||||
let user = self
|
||||
.repository
|
||||
.create_user(user::New {
|
||||
name: data.name.0,
|
||||
email: data.email.0,
|
||||
password,
|
||||
last_used: None,
|
||||
})
|
||||
.await
|
||||
.map_err(Error::Repository)?;
|
||||
|
||||
self.repository
|
||||
.start_session(user)
|
||||
.await
|
||||
.map_err(Error::Repository)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user