Messages/AuthenticationDaemon updated: simpler requesting API for authd.

master
Philippe Pittoli 2023-06-13 14:28:38 +02:00
parent 9f81166110
commit 82902c20b0
1 changed files with 140 additions and 99 deletions

View File

@ -31,7 +31,6 @@ import App.IPC as IPC
Maybe this could be changed in the future to match the actual possibilities of the API. Maybe this could be changed in the future to match the actual possibilities of the API.
Possible requests: Possible requests:
- 14 type EditProfile = { token :: String, new_profile :: Hash(String, JSON::Any) }
- 15 type EditProfileContent = { token :: Maybe String, shared_key :: Maybe String, user :: Int | String | Nil, new_profile :: Hash(String, JSON::Any) } - 15 type EditProfileContent = { token :: Maybe String, shared_key :: Maybe String, user :: Int | String | Nil, new_profile :: Hash(String, JSON::Any) }
-- Deletion can be triggered by either an admin or the user. -- Deletion can be triggered by either an admin or the user.
@ -53,80 +52,133 @@ type PasswordRecovered = { user :: UserPublic.UserPublic }
type Contacts = { user :: Int, email :: Maybe Email.Email, phone :: Maybe Phone.Phone } type Contacts = { user :: Int, email :: Maybe Email.Email, phone :: Maybe Phone.Phone }
type MatchingUsers = { users :: Array UserPublic.UserPublic } type MatchingUsers = { users :: Array UserPublic.UserPublic }
type Password = String type Password = String
{- UserID should be in a separate module with a dedicated codec. -}
type UserID = Int -- UserID is either a login or an uid number
{- 0 -}
type Login = { login :: String, password :: String } type Login = { login :: String, password :: String }
type AddUser = { shared_key :: String, login :: String, password :: String, email :: Maybe Email.Email, phone :: Maybe Phone.Phone } -- profile :: Maybe Hash(String, JSON::Any) codecLogin ∷ CA.JsonCodec Login
type ValidateUser = { login :: String, activation_key :: String } codecLogin = CA.object "Login" (CAR.record { login: CA.string, password: CA.string })
{- 1 -}
type Register = { login :: String
, password :: Password
, email :: Maybe Email.Email
{-, profile :: Maybe Hash(String, JSON::Any) -} }
codecRegister ∷ CA.JsonCodec Register
codecRegister
= CA.object "Register" (CAR.record
{ login: CA.string
, password: CA.string
, email: CAR.optional Email.codec })
{- 2 -}
type ValidateUser = { user :: UserID, activation_key :: String }
codecValidateUser ∷ CA.JsonCodec ValidateUser
codecValidateUser
= CA.object "ValidateUser" (CAR.record
{ user: CA.int
, activation_key: CA.string })
{- NOTE: "user" attribute for both PasswordRecovery and AskPasswordRecovery could be UserID,
but they'll be used as login since the user has to type it. -}
{- 3 -}
type AskPasswordRecovery = { user :: String }
codecAskPasswordRecovery ∷ CA.JsonCodec AskPasswordRecovery
codecAskPasswordRecovery = CA.object "AskPasswordRecovery" (CAR.record { user: CA.string })
{- 4 -}
type PasswordRecovery = { user :: String
, password_renew_key :: String
, new_password :: Password }
codecPasswordRecovery ∷ CA.JsonCodec PasswordRecovery
codecPasswordRecovery
= CA.object "PasswordRecovery" (CAR.record
{ user: CA.string
, password_renew_key: CA.string
, new_password: CA.string })
{- 5 -}
-- I'll split a message in two: either get a user by UID or by name. -- I'll split a message in two: either get a user by UID or by name.
-- TODO: change it for an Either Int String type. -- TODO: change it for an Either Int String type.
type GetUserByUID = { user :: Int } type GetUserByUID = { user :: Int }
type GetUserByName = { user :: String } type GetUserByName = { user :: String }
type GetUserByCredentials = { login :: String, password :: String }
type Register = { login :: String, password :: String, email :: Maybe Email.Email, phone :: Maybe Phone.Phone } -- profile :: Maybe Hash(String, JSON::Any)
type UpdatePassword = { login :: String, old_password :: String, new_password :: String }
type ListUsers = { token :: Maybe String, key :: Maybe String }
type CheckPermission = { shared_key :: Maybe String, token :: Maybe String, user :: String, service :: String, resource :: String }
type SetPermission = { shared_key :: String, user :: String, service :: String, resource :: String, permission :: PermissionLevel.PermissionLevel }
type PasswordRecovery = { user :: String, password_renew_key :: String, new_password :: String }
type AskPasswordRecovery = { user :: String, email :: Email.Email }
type SearchUser = { user :: String }
type EditContacts = { token :: String, email :: Maybe Email.Email, phone :: Maybe Phone.Phone }
type Delete = { shared_key :: Maybe String, login :: Maybe String, password :: Maybe String, user :: String }
type GetContacts = { token :: String }
-- Related JSON codecs.
codecLogin ∷ CA.JsonCodec Login
codecLogin = CA.object "Login" (CAR.record { login: CA.string, password: CA.string })
codecAddUser ∷ CA.JsonCodec AddUser
codecAddUser = CA.object "AddUser" (CAR.record { shared_key: CA.string
, login: CA.string
, password: CA.string
, email: CAR.optional Email.codec
, phone: CAR.optional Phone.codec })
codecValidateUser ∷ CA.JsonCodec ValidateUser
codecValidateUser = CA.object "ValidateUser" (CAR.record { login: CA.string, activation_key: CA.string })
codecGetUserByUID ∷ CA.JsonCodec GetUserByUID codecGetUserByUID ∷ CA.JsonCodec GetUserByUID
codecGetUserByUID = CA.object "GetUserByUID" (CAR.record { user: CA.int }) codecGetUserByUID = CA.object "GetUserByUID" (CAR.record { user: CA.int })
codecGetUserByName ∷ CA.JsonCodec GetUserByName codecGetUserByName ∷ CA.JsonCodec GetUserByName
codecGetUserByName = CA.object "GetUserByName" (CAR.record { user: CA.string }) codecGetUserByName = CA.object "GetUserByName" (CAR.record { user: CA.string })
codecGetUserByCredentials ∷ CA.JsonCodec GetUserByCredentials
codecGetUserByCredentials = CA.object "GetUserByCredentials" (CAR.record { login: CA.string, password: CA.string }) {- 6 -}
codecRegister ∷ CA.JsonCodec Register type ModUser = { user :: Maybe UserID
codecRegister = CA.object "Register" (CAR.record { login: CA.string , admin :: Maybe Boolean
, password :: Maybe Password
, email :: Maybe Email.Email }
codecModUser ∷ CA.JsonCodec ModUser
codecModUser
= CA.object "ModUser" (CAR.record
{ user: CAR.optional CA.int
, admin: CAR.optional CA.boolean
, password: CAR.optional CA.string
, email: CAR.optional Email.codec })
{- 7 -}
{- type EditProfileEntries = { user :: Maybe UserID
, new_profile_entries :: Hash(String, JSON::Any) } -}
{- 8 -}
type DeleteUser = { user :: Maybe UserID }
codecDeleteUser ∷ CA.JsonCodec DeleteUser
codecDeleteUser = CA.object "DeleteUser" (CAR.record { user: CAR.optional CA.int })
{- 9 -}
type AddUser = { login :: String
, password :: Password
, admin :: Boolean
, email :: Maybe Email.Email
{-, profile :: Maybe Hash(String, JSON::Any) -} }
codecAddUser ∷ CA.JsonCodec AddUser
codecAddUser
= CA.object "AddUser" (CAR.record
{ login: CA.string
, password: CA.string , password: CA.string
, admin: CA.boolean
, email: CAR.optional Email.codec , email: CAR.optional Email.codec
, phone: CAR.optional Phone.codec }) {-, profile :: Maybe Hash(String, JSON::Any) -} })
codecUpdatePassword ∷ CA.JsonCodec UpdatePassword
codecUpdatePassword = CA.object "UpdatePassword" (CAR.record { login: CA.string {- 10 -}
, old_password: CA.string type CheckPermission = { user :: UserID, service :: String, resource :: String }
, new_password: CA.string })
codecListUsers ∷ CA.JsonCodec ListUsers
codecListUsers = CA.object "ListUsers" (CAR.record { token: CAR.optional CA.string, key: CAR.optional CA.string })
codecCheckPermission ∷ CA.JsonCodec CheckPermission codecCheckPermission ∷ CA.JsonCodec CheckPermission
codecCheckPermission = CA.object "CheckPermission" (CAR.record { shared_key: CAR.optional CA.string codecCheckPermission
, token: CAR.optional CA.string = CA.object "CheckPermission" (CAR.record
, user: CA.string { user: CA.int
, service: CA.string , service: CA.string
, resource: CA.string }) , resource: CA.string })
{- 11 -}
type SetPermission = { user :: UserID
, service :: String
, resource :: String
, permission :: PermissionLevel.PermissionLevel }
codecSetPermission ∷ CA.JsonCodec SetPermission codecSetPermission ∷ CA.JsonCodec SetPermission
codecSetPermission = CA.object "SetPermission" (CAR.record { shared_key: CA.string codecSetPermission
, user: CA.string = CA.object "SetPermission" (CAR.record
{ user: CA.int
, service: CA.string , service: CA.string
, resource: CA.string , resource: CA.string
, permission: PermissionLevel.codec }) , permission: PermissionLevel.codec })
codecPasswordRecovery ∷ CA.JsonCodec PasswordRecovery
codecPasswordRecovery = CA.object "PasswordRecovery" (CAR.record { user: CA.string, password_renew_key: CA.string, new_password: CA.string })
codecAskPasswordRecovery ∷ CA.JsonCodec AskPasswordRecovery
codecAskPasswordRecovery = CA.object "AskPasswordRecovery" (CAR.record { user: CA.string, email: Email.codec })
codecSearchUser ∷ CA.JsonCodec SearchUser
codecSearchUser = CA.object "SearchUser" (CAR.record { user: CA.string })
codecEditContacts ∷ CA.JsonCodec EditContacts
codecEditContacts = CA.object "EditContacts" (CAR.record { token: CA.string, email: CAR.optional Email.codec, phone: CAR.optional Phone.codec })
codecDelete ∷ CA.JsonCodec Delete
codecDelete = CA.object "Delete" (CAR.record { shared_key: CAR.optional CA.string, login: CAR.optional CA.string, password: CAR.optional CA.string, user: CA.string })
codecGetContacts ∷ CA.JsonCodec GetContacts
codecGetContacts = CA.object "GetContacts" (CAR.record { token: CA.string })
{- 12 -}
type SearchUser = { regex :: Maybe String, offset :: Maybe Int }
codecSearchUser ∷ CA.JsonCodec SearchUser
codecSearchUser
= CA.object "SearchUser" (CAR.record
{ regex: CAR.optional CA.string
, offset: CAR.optional CA.int })
-- Related JSON codecs.
codecGotError ∷ CA.JsonCodec Error codecGotError ∷ CA.JsonCodec Error
codecGotError = CA.object "Error" (CAR.record { reason: CAR.optional CA.string }) codecGotError = CA.object "Error" (CAR.record { reason: CAR.optional CA.string })
codecGotToken ∷ CA.JsonCodec Token codecGotToken ∷ CA.JsonCodec Token
@ -165,24 +217,19 @@ codecGotMatchingUsers = CA.object "MatchingUsers" (CAR.record { users: CA.array
-- All possible requests. -- All possible requests.
data RequestMessage data RequestMessage
= MkLogin Login -- 0 = MkLogin Login -- 0
| MkAddUser AddUser -- 1 | MkRegister Register -- 1
| MkValidateUser ValidateUser -- 2 | MkValidateUser ValidateUser -- 2
| MkGetUserByUID GetUserByUID -- 3 | MkAskPasswordRecovery AskPasswordRecovery -- 3
| MkGetUserByName GetUserByName -- 3 (bis) | MkPasswordRecovery PasswordRecovery -- 4
| MkGetUserByCredentials GetUserByCredentials -- 4 | MkGetUserByUID GetUserByUID -- 5
| MkRegister Register -- 6 | MkGetUserByName GetUserByName -- 5 (bis)
| MkUpdatePassword UpdatePassword -- 7 | MkModUser ModUser -- 6
| MkListUsers ListUsers -- 8 --| MkEditProfileContent EditProfileContent -- 7
| MkCheckPermission CheckPermission -- 9 | MkDeleteUser DeleteUser -- 8
| MkSetPermission SetPermission -- 10 | MkAddUser AddUser -- 9
| MkPasswordRecovery PasswordRecovery -- 11 | MkCheckPermission CheckPermission -- 10
| MkAskPasswordRecovery AskPasswordRecovery -- 12 | MkSetPermission SetPermission -- 11
| MkSearchUser SearchUser -- 13 | MkSearchUser SearchUser -- 12
--| MkEditProfile EditProfile -- 14
--| MkEditProfileContent EditProfileContent -- 15
| MkEditContacts EditContacts -- 16
| MkDelete Delete -- 17
| MkGetContacts GetContacts -- 18
-- All possible answers from the authentication daemon (authd). -- All possible answers from the authentication daemon (authd).
data AnswerMessage data AnswerMessage
@ -203,26 +250,21 @@ data AnswerMessage
encode ∷ RequestMessage -> Tuple UInt String encode ∷ RequestMessage -> Tuple UInt String
encode m = case m of encode m = case m of
(MkLogin request) -> get_tuple 0 codecLogin request (MkLogin request) -> get_tuple 0 codecLogin request
(MkAddUser request) -> get_tuple 1 codecAddUser request (MkRegister request) -> get_tuple 1 codecRegister request
(MkValidateUser request) -> get_tuple 2 codecValidateUser request (MkValidateUser request) -> get_tuple 2 codecValidateUser request
(MkAskPasswordRecovery request) -> get_tuple 3 codecAskPasswordRecovery request
(MkPasswordRecovery request) -> get_tuple 4 codecPasswordRecovery request
-- Both messages are actually a single message type, so they have the same number. -- Both messages are actually a single message type, so they have the same number.
-- TODO: change the message codec for an Either Int String. -- TODO: change the message codec for an Either Int String.
(MkGetUserByUID request) -> get_tuple 3 codecGetUserByUID request (MkGetUserByUID request) -> get_tuple 5 codecGetUserByUID request
(MkGetUserByName request) -> get_tuple 3 codecGetUserByName request (MkGetUserByName request) -> get_tuple 5 codecGetUserByName request
(MkGetUserByCredentials request) -> get_tuple 4 codecGetUserByCredentials request (MkModUser request) -> get_tuple 6 codecModUser request
(MkRegister request) -> get_tuple 6 codecRegister request -- 7 MkEditProfileContent
(MkUpdatePassword request) -> get_tuple 7 codecUpdatePassword request (MkDeleteUser request) -> get_tuple 8 codecDeleteUser request
(MkListUsers request) -> get_tuple 8 codecListUsers request (MkAddUser request) -> get_tuple 9 codecAddUser request
(MkCheckPermission request) -> get_tuple 9 codecCheckPermission request (MkCheckPermission request) -> get_tuple 10 codecCheckPermission request
(MkSetPermission request) -> get_tuple 10 codecSetPermission request (MkSetPermission request) -> get_tuple 11 codecSetPermission request
(MkPasswordRecovery request) -> get_tuple 11 codecPasswordRecovery request (MkSearchUser request) -> get_tuple 12 codecSearchUser request
(MkAskPasswordRecovery request) -> get_tuple 12 codecAskPasswordRecovery request
(MkSearchUser request) -> get_tuple 13 codecSearchUser request
-- 14 MkEditProfile
-- 15 MkEditProfileContent
(MkEditContacts request) -> get_tuple 16 codecEditContacts request
(MkDelete request) -> get_tuple 17 codecDelete request
(MkGetContacts request) -> get_tuple 18 codecGetContacts request
where where
get_tuple :: forall a. Int -> CA.JsonCodec a -> a -> Tuple UInt String get_tuple :: forall a. Int -> CA.JsonCodec a -> a -> Tuple UInt String
get_tuple num codec request = Tuple (fromInt num) (J.stringify $ CA.encode codec request) get_tuple num codec request = Tuple (fromInt num) (J.stringify $ CA.encode codec request)
@ -262,7 +304,6 @@ parseDecodeJSON codec str = do
json <- JSONParser.jsonParser str json <- JSONParser.jsonParser str
lmap CA.printJsonDecodeError (CA.decode codec json) lmap CA.printJsonDecodeError (CA.decode codec json)
serialize :: RequestMessage -> Effect ArrayBuffer serialize :: RequestMessage -> Effect ArrayBuffer
serialize request serialize request
= case (encode request) of = case (encode request) of