-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ resolver = "2" | |
|
||
members = [ | ||
"auth", | ||
"framework", | ||
"framework", | ||
"lab", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
struct AuthApi { | ||
linkedin: LinkedInApi, | ||
Check warning on line 5 in auth/src/clients/wasm/api.rs
|
||
} | ||
|
||
#[wasm_bindgen] | ||
struct LinkedInApi; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod api; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub struct AuthConfig { | ||
Check warning on line 1 in auth/src/domain/auth_config.rs
|
||
pub jwt_secret: String, | ||
pub jwt_expiration: i64, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,34 @@ | ||
use super::{ | ||
auth_event::{AuthEvent, Credentials}, | ||
auth_scalar::{Password, PasswordHash, UserId}, | ||
auth_scalar::{PasswordHash, UserId}, | ||
}; | ||
|
||
pub struct AuthState<'a>(pub &'a [AuthEvent]); | ||
#[derive(Default)] | ||
pub struct AuthState<'a> { | ||
pub is_registered: bool, | ||
pub password_hash: Option<&'a PasswordHash>, | ||
pub user_id: Option<&'a UserId>, | ||
} | ||
|
||
impl<'a> AuthState<'a> { | ||
pub fn iter(&self) -> std::slice::Iter<AuthEvent> { | ||
self.0.iter() | ||
} | ||
|
||
pub fn is_registered(&self) -> bool { | ||
return self | ||
.iter() | ||
.any(|event| matches!(event, AuthEvent::Registered { .. })); | ||
} | ||
|
||
pub fn verify_password(&self, password: &Password) -> bool { | ||
let password_hash: &PasswordHash = &password.into(); | ||
let current_password_hash = self | ||
.iter() | ||
.filter_map(|event| match event { | ||
AuthEvent::Registered { credentials, .. } => Some(credentials), | ||
_ => None, | ||
}) | ||
.map(|credentials| match credentials { | ||
Credentials::EmailPassword { password, .. } => password_hash, | ||
}) | ||
.last(); | ||
|
||
match current_password_hash { | ||
Some(current_password_hash) => current_password_hash == password_hash, | ||
None => false, | ||
pub fn new(events: &'a [AuthEvent]) -> Self { | ||
let mut state = AuthState::default(); | ||
for event in events { | ||
match event { | ||
AuthEvent::Registered { | ||
credentials, | ||
user_id, | ||
.. | ||
} => { | ||
state.is_registered = true; | ||
state.password_hash = match credentials { | ||
Credentials::EmailPassword { password, .. } => Some(password), | ||
}; | ||
state.user_id = Some(user_id); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
pub fn user_id(&self) -> Option<&UserId> { | ||
self.iter() | ||
.filter_map(|event| match event { | ||
AuthEvent::Registered { user_id, .. } => Some(user_id), | ||
_ => None, | ||
}) | ||
.last() | ||
state | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod auth_config; | ||
pub mod auth_event; | ||
pub mod auth_message; | ||
pub mod auth_scalar; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "lab" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures = "0.3.30" | ||
tokio = { version = "1", features = ["full"] } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use std::{future::Future, pin::Pin}; | ||
|
||
pub struct Effect<R, T, E> { | ||
f: Box<dyn Fn(R) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + Sync>>>, | ||
} | ||
|
||
impl<R, T, E> Effect<R, T, E> { | ||
pub fn new<F, Fut>(f: F) -> Effect<R, T, E> | ||
where | ||
F: Fn(R) -> Fut + 'static, | ||
Fut: Future<Output = Result<T, E>> + Send + Sync + 'static, | ||
{ | ||
Effect { | ||
f: Box::new(move |runtime: R| Box::pin(f(runtime))), | ||
} | ||
} | ||
|
||
pub async fn run(&self, runtime: R) -> Result<T, E> { | ||
(self.f)(runtime).await | ||
} | ||
} | ||
|
||
pub trait ToEffect<R, T, E> { | ||
fn to_effect(self) -> Effect<R, T, E>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub struct Function<R, T, E> { | ||
Check failure on line 1 in lab/src/function.rs
|
||
f: Box<dyn Fn(R) -> Result<T, E>>, | ||
} | ||
|
||
impl<R, T, E> Function<R, T, E> { | ||
pub fn new<F>(f: F) -> Function<R, T, E> | ||
Check failure on line 6 in lab/src/function.rs
|
||
where | ||
F: Fn(R) -> Result<T, E> + 'static, | ||
{ | ||
Function { | ||
f: Box::new(move |runtime: R| f(runtime)), | ||
} | ||
} | ||
|
||
pub fn run(&self, runtime: R) -> Result<T, E> { | ||
(self.f)(runtime) | ||
} | ||
} |