stopping for the night
This commit is contained in:
@@ -1,27 +1,29 @@
|
||||
use std::marker::PhantomData;
|
||||
use nkode_rs::nkode_core::keypad::Keypad;
|
||||
use crate::models::app::{CodeLoginData, CodeLoginSession, Icon, IconID, KeyLoginSession, ClientAuthAPI, ICON_ID_SIZE};
|
||||
use crate::models::app::{OpaqueAPI, AuthAPI, CodeLoginData, CodeLoginSession, Icon, IconID, KeyLoginSession, ICON_ID_SIZE, LoginSession};
|
||||
use crate::models::email::Email;
|
||||
use crate::models::opaque::UserSecretKey;
|
||||
use crate::models::opaque::{OpaqueLoginSession, UserSecretKey};
|
||||
use anyhow::Result;
|
||||
use nkode_rs::nkode_core::nkode_cipher::NKodeCipher;
|
||||
use nkode_rs::from_bytes::FromBytes;
|
||||
use nkode_rs::nkode_core::chacha20prng::Nonce;
|
||||
use async_trait::async_trait;
|
||||
use nkode_rs::nkode_core::policy::{NKodePolicy, DEFAULT_POLICY};
|
||||
use crate::opaque::client::{AuthenticationData, OpaqueAuthLogin, OpaqueAuthRegister, ServerConnectionLogin, ServerConnectionRegister};
|
||||
|
||||
pub struct Login;
|
||||
pub struct Register;
|
||||
|
||||
|
||||
pub struct ClientAppKey<State, S: ClientAuthAPI> {
|
||||
pub struct ClientAppKey<State, S: OpaqueAPI> {
|
||||
api: S,
|
||||
email: Email,
|
||||
user_secret_key: UserSecretKey,
|
||||
_state: PhantomData<State>
|
||||
}
|
||||
|
||||
impl <S: ClientAuthAPI> ClientAppKey<Register,S> {
|
||||
impl <S: OpaqueAPI> ClientAppKey<Register,S> {
|
||||
pub async fn register(self) -> Result<ClientAppKey<Login, S>, String> {
|
||||
// self.repo.set_secret_key(&self.email, &self.user_secret_key).await?;
|
||||
self.api.register_key(&self.email, &self.user_secret_key).await?;
|
||||
Ok(ClientAppKey {
|
||||
api: self.api,
|
||||
@@ -32,7 +34,7 @@ impl <S: ClientAuthAPI> ClientAppKey<Register,S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl <S: ClientAuthAPI> ClientAppKey<Login,S> {
|
||||
impl <S: OpaqueAPI> ClientAppKey<Login,S> {
|
||||
pub async fn login(self) -> Result<ClientAppKeyLoggedIn<S>,String> {
|
||||
let key_login = self.api.login_key(&self.email, &self.user_secret_key).await?;
|
||||
Ok(ClientAppKeyLoggedIn{
|
||||
@@ -44,14 +46,14 @@ impl <S: ClientAuthAPI> ClientAppKey<Login,S> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientAppKeyLoggedIn<S: ClientAuthAPI> {
|
||||
pub struct ClientAppKeyLoggedIn<S: OpaqueAPI> {
|
||||
api: S,
|
||||
email: Email,
|
||||
user_secret_key: UserSecretKey,
|
||||
key_login: KeyLoginSession,
|
||||
}
|
||||
|
||||
impl <S: ClientAuthAPI> ClientAppKeyLoggedIn<S> {
|
||||
impl <S: OpaqueAPI + AuthAPI> ClientAppKeyLoggedIn<S> {
|
||||
pub async fn register_code(self) -> Result<ClientAppCodeRegister<S>, String> {
|
||||
let icon_nonce = Nonce::new();
|
||||
let icons = self.get_icons(&icon_nonce).await?;
|
||||
@@ -98,7 +100,7 @@ impl <S: ClientAuthAPI> ClientAppKeyLoggedIn<S> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientAppCodeRegister<S: ClientAuthAPI> {
|
||||
pub struct ClientAppCodeRegister<S: OpaqueAPI> {
|
||||
api: S,
|
||||
email: Email,
|
||||
user_secret_key: UserSecretKey,
|
||||
@@ -108,7 +110,7 @@ pub struct ClientAppCodeRegister<S: ClientAuthAPI> {
|
||||
keypad: Keypad,
|
||||
}
|
||||
|
||||
impl <S: ClientAuthAPI>ClientAppCodeRegister<S> {
|
||||
impl <S: OpaqueAPI + AuthAPI>ClientAppCodeRegister<S> {
|
||||
pub async fn register(self, selected_icons: Vec<IconID>) -> Result<ClientAppCodeLogin<S>, String> {
|
||||
let policy = self.api.get_policy().await?;
|
||||
let keypad = Keypad::new(policy.clone());
|
||||
@@ -137,7 +139,7 @@ impl <S: ClientAuthAPI>ClientAppCodeRegister<S> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientAppCodeLogin<S: ClientAuthAPI> {
|
||||
pub struct ClientAppCodeLogin<S: OpaqueAPI> {
|
||||
api: S,
|
||||
email: Email,
|
||||
mask: Vec<u64>,
|
||||
@@ -146,7 +148,7 @@ pub struct ClientAppCodeLogin<S: ClientAuthAPI> {
|
||||
cipher: NKodeCipher
|
||||
}
|
||||
|
||||
impl <S: ClientAuthAPI>ClientAppCodeLogin<S> {
|
||||
impl <S: OpaqueAPI>ClientAppCodeLogin<S> {
|
||||
pub fn sort_icons(&self) -> Vec<Icon> {
|
||||
nkode_rs::tensor::reorder(
|
||||
&self.icons,
|
||||
@@ -158,4 +160,90 @@ impl <S: ClientAuthAPI>ClientAppCodeLogin<S> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ClientAuth<
|
||||
R: ServerConnectionRegister + Clone,
|
||||
L: ServerConnectionLogin + Clone,
|
||||
K: AuthAPI
|
||||
> {
|
||||
opaque_key_register: OpaqueAuthRegister<R>,
|
||||
opaque_key_login: OpaqueAuthLogin<L>,
|
||||
opaque_code_register: OpaqueAuthRegister<R>,
|
||||
opaque_code_login: OpaqueAuthLogin<L>,
|
||||
nkode_api: K,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<R, L, K> OpaqueAPI for ClientAuth<R, L,K>
|
||||
where
|
||||
R: ServerConnectionRegister + Clone + Sync + Send,
|
||||
L: ServerConnectionLogin + Clone + Sync + Send,
|
||||
K: AuthAPI + Sync + Send,
|
||||
{
|
||||
async fn register_key(&self, email: &Email, secret_key: &UserSecretKey) -> Result<(), String> {
|
||||
let auth_data = AuthenticationData::from_secret_key(email.as_str(), secret_key.as_slice());
|
||||
self.opaque_key_register.register(&auth_data).await.map_err(|e| format!("error: {}", e))
|
||||
}
|
||||
|
||||
async fn register_code(&self, email: &Email, passcode: &[u64], key_login_session: &KeyLoginSession, data: &CodeLoginData) -> Result<(), String> {
|
||||
let auth_data = AuthenticationData::from_code(email.as_str(), passcode);
|
||||
self.opaque_code_register.register(&auth_data).await.map_err(|e| format!("error: {}", e))
|
||||
}
|
||||
|
||||
async fn login_key(&self, email: &Email, secret_key: &UserSecretKey) -> Result<KeyLoginSession, String> {
|
||||
let auth_data = AuthenticationData::from_secret_key(&email.as_str(), secret_key.as_slice());
|
||||
let session_key = self.opaque_key_login.login(&auth_data).await.map_err(|e| format!("error: {}", e))?;
|
||||
Ok(KeyLoginSession(
|
||||
LoginSession {
|
||||
email: email.clone(),
|
||||
session_key
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
async fn login_code(&self, email: &Email, passcode: &[u64]) -> Result<CodeLoginSession, String> {
|
||||
let auth_data = AuthenticationData::from_code(email.as_str(), passcode);
|
||||
let session_key = self.opaque_code_login.login(&auth_data).await.map_err(|e| format!("error: {}", e))?;
|
||||
Ok(CodeLoginSession(
|
||||
LoginSession {
|
||||
email: email.clone(),
|
||||
session_key
|
||||
}
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<R, L, K> AuthAPI for ClientAuth<R, L, K>
|
||||
where
|
||||
R: ServerConnectionRegister + Clone + Sync + Send,
|
||||
L: ServerConnectionLogin + Clone + Sync + Send,
|
||||
K: AuthAPI + Sync + Send,
|
||||
{
|
||||
async fn get_new_icons(
|
||||
&self,
|
||||
key_login_session: &KeyLoginSession,
|
||||
) -> Result<Vec<Icon>, String> {
|
||||
self.nkode_api
|
||||
.get_new_icons(key_login_session)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn get_login_data(
|
||||
&self,
|
||||
key_login_session: &KeyLoginSession,
|
||||
) -> Result<CodeLoginData, String> {
|
||||
self.nkode_api
|
||||
.get_login_data(key_login_session)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn is_code_registered(&self, key_login_session: &KeyLoginSession) -> Result<bool, String> {
|
||||
self.nkode_api.is_code_registered(key_login_session).await
|
||||
}
|
||||
|
||||
async fn get_policy(&self) -> Result<NKodePolicy, String> {
|
||||
Ok(DEFAULT_POLICY)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user