diff --git a/giterated-daemon/src/database_backend/handler.rs b/giterated-daemon/src/database_backend/handler.rs index 0b2e180..038b95d 100644 --- a/giterated-daemon/src/database_backend/handler.rs +++ b/giterated-daemon/src/database_backend/handler.rs @@ -2,7 +2,9 @@ use std::error::Error; use futures_util::{future::BoxFuture, FutureExt}; use giterated_models::{ - error::{GetValueError, OperationError, RepositoryError, UserError}, + authenticated::UserAuthenticationToken, + error::{GetValueError, InstanceError, OperationError, RepositoryError, UserError}, + instance::{AuthenticationTokenRequest, Instance, RegisterAccountRequest}, object_backend::ObjectBackend, repository::{ Commit, DefaultBranch, Description, LatestCommit, Repository, @@ -290,3 +292,36 @@ pub fn repository_set_setting( } .boxed() } + +pub fn instance_authentication_request( + object: &Instance, + operation: AuthenticationTokenRequest, + state: DatabaseBackend, +) -> BoxFuture<'static, Result>> { + let object = object.clone(); + async move { + let mut backend = state.user_backend.lock().await; + + backend + .login(&object, operation) + .await + .map_err(|e| OperationError::Internal(e.to_string())) + } + .boxed() +} + +pub fn instance_registration_request( + _object: &Instance, + operation: RegisterAccountRequest, + state: DatabaseBackend, +) -> BoxFuture<'static, Result>> { + async move { + let mut backend = state.user_backend.lock().await; + + backend + .register(operation) + .await + .map_err(|e| OperationError::Internal(e.to_string())) + } + .boxed() +} diff --git a/giterated-daemon/src/database_backend/mod.rs b/giterated-daemon/src/database_backend/mod.rs index d32f1e3..e472297 100644 --- a/giterated-daemon/src/database_backend/mod.rs +++ b/giterated-daemon/src/database_backend/mod.rs @@ -17,9 +17,10 @@ use tokio::sync::Mutex; use crate::backend::{RepositoryBackend, UserBackend}; use self::handler::{ - repository_commit_before, repository_diff, repository_file_from_id, repository_get_setting, - repository_get_value, repository_info, repository_set_setting, user_get_repositories, - user_get_setting, user_get_value, user_set_setting, + instance_authentication_request, instance_registration_request, repository_commit_before, + repository_diff, repository_file_from_id, repository_get_setting, repository_get_value, + repository_info, repository_set_setting, user_get_repositories, user_get_setting, + user_get_value, user_set_setting, }; #[derive(Clone, Debug)] @@ -83,6 +84,8 @@ impl DatabaseBackend { .insert(repository_get_value) .insert(repository_get_setting) .insert(repository_set_setting) + .insert(instance_registration_request) + .insert(instance_authentication_request) .register_object::() .register_object::() .register_object::(); diff --git a/giterated-models/src/instance/operations.rs b/giterated-models/src/instance/operations.rs index c9404a6..b9e9536 100644 --- a/giterated-models/src/instance/operations.rs +++ b/giterated-models/src/instance/operations.rs @@ -31,9 +31,7 @@ impl GiteratedOperation for RegisterAccountRequest { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct RegisterAccountResponse { - pub token: String, -} +pub struct RegisterAccountResponse(pub UserAuthenticationToken); /// An authentication token request. /// diff --git a/giterated-models/src/repository/mod.rs b/giterated-models/src/repository/mod.rs index 57fd47c..47b0649 100644 --- a/giterated-models/src/repository/mod.rs +++ b/giterated-models/src/repository/mod.rs @@ -243,7 +243,10 @@ impl From> for Commit { .to_string(), summary: commit.summary().map(|summary| summary.to_string()), body: commit.body().map(|body| body.to_string()), - parents: commit.parents().map(|parent| parent.id().to_string()).collect::>(), + parents: commit + .parents() + .map(|parent| parent.id().to_string()) + .collect::>(), author: commit.author().into(), committer: commit.committer().into(), time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),