idk what i did
This commit is contained in:
@@ -7,30 +7,9 @@ use crate::shared::models::app::{AuthAPI, CodeLoggedInSession, CodeLoginData, Ic
|
||||
use crate::shared::models::email::Email;
|
||||
use crate::shared::models::opaque::UserSecretKey;
|
||||
|
||||
struct KeyRegister;
|
||||
struct KeyLogin;
|
||||
struct KeyLoggedIn;
|
||||
struct CodeRegister;
|
||||
struct CodeLogIn;
|
||||
struct CodeLoggedIn;
|
||||
|
||||
|
||||
pub struct UserState<State, S: AuthAPI> {
|
||||
api: S,
|
||||
email: Email,
|
||||
user_secret_key: UserSecretKey,
|
||||
key_login: Option<KeyLoggedInSession>,
|
||||
icon_nonce: Option<Nonce>,
|
||||
icons: Option<Vec<Icon>>,
|
||||
keypad: Option<Keypad>,
|
||||
mask: Option<Vec<u64>>,
|
||||
cipher: Option<NKodeCipher>,
|
||||
_state: PhantomData<State>
|
||||
}
|
||||
|
||||
struct Login;
|
||||
|
||||
struct Register;
|
||||
struct KeyRegister;
|
||||
|
||||
pub struct UserStateKey<State, S: AuthAPI> {
|
||||
api: S,
|
||||
@@ -39,20 +18,20 @@ pub struct UserStateKey<State, S: AuthAPI> {
|
||||
_state: PhantomData<State>
|
||||
}
|
||||
|
||||
impl <S: AuthAPI> UserStateKey<Register,S> {
|
||||
pub async fn register(self) -> anyhow::Result<UserStateKey<Login, S>, String> {
|
||||
impl <S: AuthAPI> UserStateKey<KeyRegister,S> {
|
||||
pub async fn register(self) -> Result<UserStateKey<KeyLogin, S>, String> {
|
||||
self.api.register_key(&self.email, &self.user_secret_key).await?;
|
||||
Ok(UserStateKey {
|
||||
api: self.api,
|
||||
email: self.email,
|
||||
user_secret_key: self.user_secret_key,
|
||||
_state: PhantomData::<Login>,
|
||||
_state: PhantomData::<KeyLogin>,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl <S: AuthAPI> UserStateKey<Login,S> {
|
||||
pub async fn login(self) -> anyhow::Result<UserStateKeyLoggedIn<S>,String> {
|
||||
impl <S: AuthAPI> UserStateKey<KeyLogin,S> {
|
||||
pub async fn login(self) -> Result<UserStateKeyLoggedIn<S>,String> {
|
||||
let key_login = self.api.login_key(&self.email, &self.user_secret_key).await?;
|
||||
Ok(UserStateKeyLoggedIn {
|
||||
api: self.api,
|
||||
@@ -71,7 +50,7 @@ pub struct UserStateKeyLoggedIn<S: AuthAPI> {
|
||||
}
|
||||
|
||||
impl <S: AuthAPI> UserStateKeyLoggedIn<S> {
|
||||
pub async fn register_code(self) -> anyhow::Result<UserStateCodeRegister<S>, String> {
|
||||
pub async fn register_code(self) -> Result<UserStateCodeRegister<S>, String> {
|
||||
let icon_nonce = Nonce::new();
|
||||
let icons = self.get_icons(&icon_nonce).await?;
|
||||
let policy = self.api.get_policy().await?;
|
||||
@@ -86,7 +65,7 @@ impl <S: AuthAPI> UserStateKeyLoggedIn<S> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_icons(&self, icon_nonce: &Nonce) -> anyhow::Result<Vec<Icon>, String> {
|
||||
async fn get_icons(&self, icon_nonce: &Nonce) -> Result<Vec<Icon>, String> {
|
||||
let chacha20_key = self.user_secret_key.chacha20_secret_key();
|
||||
let mut chacha_cipher = nkode_rs::nkode_core::chacha20prng::ChaCha20PRNG::new(&chacha20_key, icon_nonce);
|
||||
let mut icons = self.api.get_new_icons(&self.key_login).await?;
|
||||
@@ -98,7 +77,7 @@ impl <S: AuthAPI> UserStateKeyLoggedIn<S> {
|
||||
Ok(icons)
|
||||
}
|
||||
|
||||
pub async fn login_code(self) -> anyhow::Result<UserStateCodeLogin<S>, String> {
|
||||
pub async fn login_code(self) -> Result<UserStateCodeLogin<S>, String> {
|
||||
let login_data = self.api.get_login_data(&self.key_login).await?;
|
||||
let icons = self.get_icons(&login_data.icon_nonce()).await?;
|
||||
let policy = self.api.get_policy().await?;
|
||||
@@ -128,7 +107,7 @@ pub struct UserStateCodeRegister<S: AuthAPI> {
|
||||
}
|
||||
|
||||
impl <S: AuthAPI> UserStateCodeRegister<S> {
|
||||
pub async fn register(self, selected_icons: Vec<IconID>) -> anyhow::Result<UserStateCodeLogin<S>, String> {
|
||||
pub async fn register(self, selected_icons: Vec<IconID>) -> Result<UserStateCodeLogin<S>, String> {
|
||||
let policy = self.api.get_policy().await?;
|
||||
let keypad = Keypad::new(policy.clone());
|
||||
let secret_key = self.user_secret_key.chacha20_secret_key();
|
||||
@@ -173,7 +152,7 @@ impl <S: AuthAPI> UserStateCodeLogin<S> {
|
||||
).unwrap().to_vec()
|
||||
}
|
||||
|
||||
pub async fn login(&self, selected_keys: &Vec<usize>) -> anyhow::Result<CodeLoggedInSession, String> {
|
||||
pub async fn login(&self, selected_keys: &Vec<usize>) -> Result<CodeLoggedInSession, String> {
|
||||
let passcode = self.cipher.decipher(selected_keys, self.keypad.indices(), &self.mask).map_err(|e| format!("invalid keys: {e}"))?;
|
||||
self.api.login_code(&self.email, &passcode).await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user