From ce57643cecf29a18a77016a184166316ee479890 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 21 May 2023 18:08:41 +0200 Subject: [PATCH] Minor changes (mostly comments). --- spago.dhall | 1 + src/App/IPC.purs | 4 +- src/App/Messages/AuthenticationDaemon.purs | 82 ++++++++++++++-------- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/spago.dhall b/spago.dhall index 7c9d479..38df728 100644 --- a/spago.dhall +++ b/spago.dhall @@ -23,6 +23,7 @@ , "prelude" , "strings" , "transformers" + , "tuples" , "uint" , "web-encoding" , "web-events" diff --git a/src/App/IPC.purs b/src/App/IPC.purs index c8373f3..15aad7b 100644 --- a/src/App/IPC.purs +++ b/src/App/IPC.purs @@ -13,7 +13,7 @@ module App.IPC (toIPC, fromIPC, toTypedIPC, fromTypedIPC) where Actual message formats can be found in the App.Messages folder. -} -import Prelude (bind, (<$>), discard, ($), (>>>)) +import Prelude (bind, (<$>), discard, ($), (>>>), (+)) import Effect (Effect) import Effect.Class (liftEffect) @@ -64,7 +64,7 @@ toTypedIPC n s = Builder.execPutM do textEncoder <- liftEffect TextEncoder.new let stringbuf = Typed.buffer $ TextEncoder.encode s textEncoder -- Put a 32-bit big-endian length for the utf8 string, in bytes. - Builder.putUint32be $ fromInt $ ArrayBuffer.byteLength stringbuf + Builder.putUint32be $ fromInt $ (ArrayBuffer.byteLength stringbuf) + 1 -- 1 for message type Builder.putUint8 n Builder.putArrayBuffer stringbuf diff --git a/src/App/Messages/AuthenticationDaemon.purs b/src/App/Messages/AuthenticationDaemon.purs index 436db93..f0d9c60 100644 --- a/src/App/Messages/AuthenticationDaemon.purs +++ b/src/App/Messages/AuthenticationDaemon.purs @@ -1,4 +1,4 @@ -module App.Messages where +module App.Messages.AuthenticationDaemon where import Prelude @@ -11,34 +11,37 @@ import Data.Maybe import Data.Either import Data.Codec.Argonaut.Record as CAR +import App.IPC (toTypedIPC, fromTypedIPC) + {- TODO: - Possible requests: - - 0 type GetToken = { login :: String, password :: String } - - 1 type AddUser = { shared_key :: String, login :: String, password :: String, email :: String?, phone :: String?, profile :: Hash(String, JSON::Any)? } - - 2 type ValidateUser = { login :: String, activation_key :: String } - - 3 type GetUser = { user :: Int32 | String } - - 4 type GetUserByCredentials = { login :: String, password :: String } - - 6 type Register = { login :: String, password :: String, email :: String?, phone :: String? , profile :: Hash(String, JSON::Any)? } - - 7 type UpdatePassword = { login :: String, old_password :: String, new_password :: String } - - 8 type ListUsers = { token :: String?, key :: String? } - - 9 type CheckPermission = { shared_key :: String?, token :: String?, user :: Int32 | String, service :: String, resource :: String } - - 10 type SetPermission = { shared_key :: String, user :: Int32 | String, service :: String, resource :: String, permission :: ::AuthD::User::PermissionLevel } - - 11 type PasswordRecovery = { user :: Int32 | String, password_renew_key :: String, new_password :: String } - - 12 type AskPasswordRecovery = { user :: Int32 | String, email :: String } - - 13 type SearchUser = { user :: String } - - 14 type EditProfile = { token :: String, new_profile :: Hash(String, JSON::Any) } - - 15 type EditProfileContent = { token :: String?, shared_key :: String?, user :: Int32 | String | Nil, new_profile :: Hash(String, JSON::Any) } - - 16 type EditContacts = { token :: String, email :: String?, phone :: String? } - - 17 type Delete = { shared_key :: String?, login :: String?, password :: String?, user :: String | Int32 } - - 18 type GetContacts = { token :: String } + Possible requests: + - 0 type GetToken = { login :: String, password :: String } + - 1 type AddUser = { shared_key :: String, login :: String, password :: String, email :: Maybe String, phone :: Maybe String, profile :: Maybe Hash(String, JSON::Any) } + - 2 type ValidateUser = { login :: String, activation_key :: String } + - 3 type GetUser = { user :: Int32 | String } + - 4 type GetUserByCredentials = { login :: String, password :: String } + - 6 type Register = { login :: String, password :: String, email :: Maybe String, phone :: Maybe String , profile :: Maybe Hash(String, JSON::Any) } + - 7 type UpdatePassword = { login :: String, old_password :: String, new_password :: String } + - 8 type ListUsers = { token :: Maybe String, key :: Maybe String } + - 9 type CheckPermission = { shared_key :: Maybe String, token :: Maybe String, user :: Int32 | String, service :: String, resource :: String } + - 10 type SetPermission = { shared_key :: String, user :: Int32 | String, service :: String, resource :: String, permission :: ::AuthD::User::PermissionLevel } + - 11 type PasswordRecovery = { user :: Int32 | String, password_renew_key :: String, new_password :: String } + - 12 type AskPasswordRecovery = { user :: Int32 | String, email :: String } + - 13 type SearchUser = { user :: String } + - 14 type EditProfile = { token :: String, new_profile :: Hash(String, JSON::Any) } + - 15 type EditProfileContent = { token :: Maybe String, shared_key :: Maybe String, user :: Int32 | String | Nil, new_profile :: Hash(String, JSON::Any) } + - 16 type EditContacts = { token :: String, email :: Maybe String, phone :: Maybe String } + - 17 type Delete = { shared_key :: Maybe String, login :: Maybe String, password :: Maybe String, user :: String | Int32 } + - 18 type GetContacts = { token :: String } -- Deletion can be triggered by either an admin or the user. - Possible answers: - - PasswordRecoverySent and PasswordRecovered, - - PermissionCheck and PermissionSet, - - User, UserAdded, UserEdited, UserValidated, UsersList, MatchingUsers + Possible answers: + - PasswordRecoverySent and PasswordRecovered, + - PermissionCheck and PermissionSet, + - User, UserAdded, UserEdited, UserValidated, UsersList, MatchingUsers + -} -- Basic message types. @@ -48,11 +51,28 @@ type Contacts = { user :: Int, email :: Maybe String, phone :: Maybe String } type Email = String type Password = String -type Login = { email :: Email, password :: Password } +type GetToken = { login :: String, password :: String } -- All possible requests. data RequestMessage - = MessageLogin Login + = MkGetToken GetToken -- 0 GetToken + -- 1 AddUser + -- 2 ValidateUser + -- 3 GetUser + -- 4 GetUserByCredentials + -- 6 Register + -- 7 UpdatePassword + -- 8 ListUsers + -- 9 CheckPermission + -- 10 SetPermission + -- 11 PasswordRecovery + -- 12 AskPasswordRecovery + -- 13 SearchUser + -- 14 EditProfile + -- 15 EditProfileContent + -- 16 EditContacts + -- 17 Delete + -- 18 GetContacts -- All possible answers from the authentication daemon (authd). data AnswerMessage @@ -62,7 +82,7 @@ data AnswerMessage encode ∷ RequestMessage → J.Json encode m = case m of - (MessageLogin login) -> CA.encode codec_login login + (MkGetToken token) -> CA.encode codec_token token -- 0 GetToken -- 1 AddUser -- 2 ValidateUser @@ -82,6 +102,10 @@ encode m = case m of -- 17 Delete -- 18 GetContacts + where + codec_token ∷ CA.JsonCodec GetToken + codec_token = CA.object "GetToken" (CAR.record { login: CA.string, password: CA.string }) + data DecodeError = JSONERROR CA.JsonDecodeError | UnknownNumber @@ -109,9 +133,9 @@ decode number json codec_contacts ∷ CA.JsonCodec Contacts codec_contacts = CA.object "Contacts" (CAR.record { user: CA.int, email: CAR.optional CA.string, phone: CAR.optional CA.string }) --- login_serialize :: Login -> String +-- login_serialize :: GetToken -> String -- login_serialize = J.stringify <<< login_encode --- login_decode ∷ J.Json → Either CA.JsonDecodeError Login +-- login_decode ∷ J.Json → Either CA.JsonDecodeError GetToken -- login_decode = CA.decode codec_login -- -- example_login_deserialize :: J.Json -> Effect Unit