diff --git a/giterated-daemon/src/backend/mod.rs b/giterated-daemon/src/backend/mod.rs index 71bd3e7..6fa9eb8 100644 --- a/giterated-daemon/src/backend/mod.rs +++ b/giterated-daemon/src/backend/mod.rs @@ -27,6 +27,7 @@ use giterated_models::{ }, }, model::{ + instance::Instance, repository::{Repository, RepositorySummary, RepositoryView}, user::User, }, @@ -83,6 +84,7 @@ pub trait AuthBackend { async fn login( &mut self, + source: &Instance, request: AuthenticationTokenRequest, ) -> Result; } diff --git a/giterated-daemon/src/backend/user.rs b/giterated-daemon/src/backend/user.rs index fb0af00..803c4e1 100644 --- a/giterated-daemon/src/backend/user.rs +++ b/giterated-daemon/src/backend/user.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use anyhow::Error; use aes_gcm::{aead::Aead, AeadCore, Aes256Gcm, Key, KeyInit}; -use argon2::{password_hash::SaltString, Argon2, PasswordHasher, PasswordHash, PasswordVerifier}; +use argon2::{password_hash::SaltString, Argon2, PasswordHash, PasswordHasher, PasswordVerifier}; use base64::{engine::general_purpose::STANDARD, Engine as _}; use giterated_models::{ messages::{ @@ -17,9 +17,10 @@ use giterated_models::{ }, }, model::{ + authenticated::UserAuthenticationToken, instance::Instance, settings::{Setting, UserBio, UserDisplayImage, UserDisplayName}, - user::User, authenticated::UserAuthenticationToken, + user::User, }, }; use rsa::{ @@ -221,13 +222,23 @@ impl AuthBackend for UserAuth { async fn login( &mut self, + source: &Instance, request: AuthenticationTokenRequest, ) -> Result { - let user = sqlx::query_as!(UserRow, r#"SELECT * FROM users WHERE username = $1"#, request.username).fetch_one(&self.pg_pool).await?; + let user = sqlx::query_as!( + UserRow, + r#"SELECT * FROM users WHERE username = $1"#, + request.username + ) + .fetch_one(&self.pg_pool) + .await?; let hash = PasswordHash::new(&user.password).unwrap(); - if !matches!(Argon2::default().verify_password(request.password.as_bytes(), &hash), Ok(())) { + if !matches!( + Argon2::default().verify_password(request.password.as_bytes(), &hash), + Ok(()) + ) { // Invalid password! return Err(Error::from(AuthenticationError::InvalidPassword)); } @@ -239,11 +250,13 @@ impl AuthBackend for UserAuth { username: user.username, instance: self.this_instance.clone(), }, - &request.issued_for.unwrap_or_else(|| self.this_instance.clone()), + &source, ) .await; - Ok(AuthenticationTokenResponse { token: UserAuthenticationToken::from(token) }) + Ok(AuthenticationTokenResponse { + token: UserAuthenticationToken::from(token), + }) } } @@ -260,5 +273,5 @@ struct UserRow { #[derive(Debug, thiserror::Error)] pub enum AuthenticationError { #[error("invalid password")] - InvalidPassword -} \ No newline at end of file + InvalidPassword, +} diff --git a/giterated-daemon/src/connection/authentication.rs b/giterated-daemon/src/connection/authentication.rs index b13c995..5a1c467 100644 --- a/giterated-daemon/src/connection/authentication.rs +++ b/giterated-daemon/src/connection/authentication.rs @@ -105,11 +105,6 @@ async fn authentication_token_request( private_key, }; - let request = AuthenticationTokenRequest { - issued_for: Some(issued_for), - ..request - }; - let response = giterated_api::request::request_local(request) .authenticate(authenticator) .execute_expect::(&connection) @@ -127,7 +122,10 @@ async fn authentication_token_request( let mut user_backend = connection_state.user_backend.lock().await; - let response = user_backend.login(request).await.map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?; + let response = user_backend + .login(instance.inner(), request) + .await + .map_err(|e| AuthenticationConnectionError::TokenIssuance(e))?; connection_state .send(response) diff --git a/giterated-daemon/src/main.rs b/giterated-daemon/src/main.rs index 0ef5557..24fbc2a 100644 --- a/giterated-daemon/src/main.rs +++ b/giterated-daemon/src/main.rs @@ -60,7 +60,8 @@ async fn main() -> Result<(), Error> { .as_str() .unwrap(), ), - instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(), + instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()) + .unwrap(), })); let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter { diff --git a/giterated-models/src/messages/authentication.rs b/giterated-models/src/messages/authentication.rs index 27f75d2..62305f4 100644 --- a/giterated-models/src/messages/authentication.rs +++ b/giterated-models/src/messages/authentication.rs @@ -41,7 +41,6 @@ pub struct RegisterAccountResponse { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AuthenticationTokenRequest { pub instance: Instance, - pub issued_for: Option, pub username: String, pub password: String, }