JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Add authentication operations

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c27b4f3

Showing ⁨⁨4⁩ changed files⁩ with ⁨⁨47⁩ insertions⁩ and ⁨⁨8⁩ deletions⁩

giterated-daemon/src/database_backend/handler.rs

View file
@@ -2,7 +2,9 @@ use std::error::Error;
2 2
3 3 use futures_util::{future::BoxFuture, FutureExt};
4 4 use giterated_models::{
5 error::{GetValueError, OperationError, RepositoryError, UserError},
5 authenticated::UserAuthenticationToken,
6 error::{GetValueError, InstanceError, OperationError, RepositoryError, UserError},
7 instance::{AuthenticationTokenRequest, Instance, RegisterAccountRequest},
6 8 object_backend::ObjectBackend,
7 9 repository::{
8 10 Commit, DefaultBranch, Description, LatestCommit, Repository,
@@ -290,3 +292,36 @@ pub fn repository_set_setting(
290 292 }
291 293 .boxed()
292 294 }
295
296 pub fn instance_authentication_request(
297 object: &Instance,
298 operation: AuthenticationTokenRequest,
299 state: DatabaseBackend,
300 ) -> BoxFuture<'static, Result<UserAuthenticationToken, OperationError<InstanceError>>> {
301 let object = object.clone();
302 async move {
303 let mut backend = state.user_backend.lock().await;
304
305 backend
306 .login(&object, operation)
307 .await
308 .map_err(|e| OperationError::Internal(e.to_string()))
309 }
310 .boxed()
311 }
312
313 pub fn instance_registration_request(
314 _object: &Instance,
315 operation: RegisterAccountRequest,
316 state: DatabaseBackend,
317 ) -> BoxFuture<'static, Result<UserAuthenticationToken, OperationError<InstanceError>>> {
318 async move {
319 let mut backend = state.user_backend.lock().await;
320
321 backend
322 .register(operation)
323 .await
324 .map_err(|e| OperationError::Internal(e.to_string()))
325 }
326 .boxed()
327 }

giterated-daemon/src/database_backend/mod.rs

View file
@@ -17,9 +17,10 @@ use tokio::sync::Mutex;
17 17 use crate::backend::{RepositoryBackend, UserBackend};
18 18
19 19 use self::handler::{
20 repository_commit_before, repository_diff, repository_file_from_id, repository_get_setting,
21 repository_get_value, repository_info, repository_set_setting, user_get_repositories,
22 user_get_setting, user_get_value, user_set_setting,
20 instance_authentication_request, instance_registration_request, repository_commit_before,
21 repository_diff, repository_file_from_id, repository_get_setting, repository_get_value,
22 repository_info, repository_set_setting, user_get_repositories, user_get_setting,
23 user_get_value, user_set_setting,
23 24 };
24 25
25 26 #[derive(Clone, Debug)]
@@ -83,6 +84,8 @@ impl DatabaseBackend {
83 84 .insert(repository_get_value)
84 85 .insert(repository_get_setting)
85 86 .insert(repository_set_setting)
87 .insert(instance_registration_request)
88 .insert(instance_authentication_request)
86 89 .register_object::<Instance>()
87 90 .register_object::<Repository>()
88 91 .register_object::<User>();

giterated-models/src/instance/operations.rs

View file
@@ -31,9 +31,7 @@ impl GiteratedOperation<Instance> for RegisterAccountRequest {
31 31 }
32 32
33 33 #[derive(Clone, Debug, Serialize, Deserialize)]
34 pub struct RegisterAccountResponse {
35 pub token: String,
36 }
34 pub struct RegisterAccountResponse(pub UserAuthenticationToken);
37 35
38 36 /// An authentication token request.
39 37 ///

giterated-models/src/repository/mod.rs

View file
@@ -243,7 +243,10 @@ impl From<git2::Commit<'_>> for Commit {
243 243 .to_string(),
244 244 summary: commit.summary().map(|summary| summary.to_string()),
245 245 body: commit.body().map(|body| body.to_string()),
246 parents: commit.parents().map(|parent| parent.id().to_string()).collect::<Vec<String>>(),
246 parents: commit
247 .parents()
248 .map(|parent| parent.id().to_string())
249 .collect::<Vec<String>>(),
247 250 author: commit.author().into(),
248 251 committer: commit.committer().into(),
249 252 time: chrono::NaiveDateTime::from_timestamp_opt(commit.time().seconds(), 0).unwrap(),