feat(gpodder): add create_user method to AuthStore

This commit is contained in:
Jef Roosens 2025-03-19 10:46:34 +01:00
parent 2a8917f21d
commit b44a47fefd
No known key found for this signature in database
GPG key ID: 21FD3D77D56BAF49
4 changed files with 40 additions and 7 deletions

View file

@ -1,8 +1,8 @@
use std::{collections::HashSet, sync::Arc};
use argon2::{Argon2, PasswordHash, PasswordVerifier};
use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use chrono::{DateTime, TimeDelta, Utc};
use rand::Rng;
use rand::{rngs::OsRng, Rng};
use crate::{
models,
@ -41,6 +41,17 @@ impl GpodderRepository {
self.store.get_user(username)?.ok_or(AuthErr::UnknownUser)
}
pub fn create_user(&self, username: &str, password: &str) -> Result<models::User, AuthErr> {
let salt = SaltString::generate(&mut OsRng);
let password_hash = Argon2::default()
.hash_password(password.as_bytes(), &salt)
.unwrap()
.to_string();
self.store.insert_user(username, &password_hash)
}
pub fn validate_credentials(
&self,
username: &str,

View file

@ -41,6 +41,9 @@ pub trait AuthStore {
/// Retrieve the user with the given username
fn get_user(&self, username: &str) -> Result<Option<User>, AuthErr>;
/// Insert a new user into the data store
fn insert_user(&self, username: &str, password_hash: &str) -> Result<User, AuthErr>;
/// Create a new session for a user with the given session ID
fn insert_session(&self, session: &Session) -> Result<(), AuthErr>;