New authd API on request functions: complete. Still subject to changes, though.

master
Philippe Pittoli 2023-06-13 19:49:38 +02:00
parent 82902c20b0
commit 597243a9f5
3 changed files with 24 additions and 34 deletions

View File

@ -141,8 +141,7 @@ type Query = Const Void
type Input = String type Input = String
data AddUserInput data AddUserInput
= ADDUSER_INP_secret String = ADDUSER_INP_login String
| ADDUSER_INP_login String
| ADDUSER_INP_email String | ADDUSER_INP_email String
| ADDUSER_INP_pass String | ADDUSER_INP_pass String
@ -157,9 +156,7 @@ data Action
-- | Finalize -- | Finalize
| HandleWebSocket (WebSocketEvent WebSocketMessageType) | HandleWebSocket (WebSocketEvent WebSocketMessageType)
type StateAuthenticationForm = { login :: String, pass :: String } type StateAddUserForm = { login :: String, email :: String, pass :: String }
type StateRegistrationForm = { login :: String, email :: String, pass :: String }
type StateAddUserForm = { secretKey :: String, login :: String, email :: String, pass :: String }
type State = type State =
{ messages :: Array String { messages :: Array String
@ -190,7 +187,7 @@ initialState input =
{ messages: [] { messages: []
, messageHistoryLength: 10 , messageHistoryLength: 10
, addUserForm: { secretKey: "", login: "", email: "", pass: "" } , addUserForm: { login: "", email: "", pass: "" }
-- TODO: put network stuff in a record. -- TODO: put network stuff in a record.
, wsUrl: input , wsUrl: input
@ -221,12 +218,7 @@ render {
render_adduser_form = HH.form render_adduser_form = HH.form
[ HE.onSubmit AddUserAttempt ] [ HE.onSubmit AddUserAttempt ]
[ Bulma.box_input "Secret" "shared secret with authd" -- title, placeholder [ Bulma.box_input "User login" "login" -- title, placeholder
(HandleAddUserInput <<< ADDUSER_INP_secret) -- action
addUserForm.secretKey -- value
true -- validity (TODO)
should_be_disabled -- condition
, Bulma.box_input "User login" "login" -- title, placeholder
(HandleAddUserInput <<< ADDUSER_INP_login) -- action (HandleAddUserInput <<< ADDUSER_INP_login) -- action
addUserForm.login -- value addUserForm.login -- value
true -- validity (TODO) true -- validity (TODO)
@ -298,7 +290,6 @@ handleAction = case _ of
HandleAddUserInput adduserinp -> do HandleAddUserInput adduserinp -> do
case adduserinp of case adduserinp of
ADDUSER_INP_secret v -> H.modify_ _ { addUserForm { secretKey = v } }
ADDUSER_INP_login v -> H.modify_ _ { addUserForm { login = v } } ADDUSER_INP_login v -> H.modify_ _ { addUserForm { login = v } }
ADDUSER_INP_email v -> H.modify_ _ { addUserForm { email = v } } ADDUSER_INP_email v -> H.modify_ _ { addUserForm { email = v } }
ADDUSER_INP_pass v -> H.modify_ _ { addUserForm { pass = v } } ADDUSER_INP_pass v -> H.modify_ _ { addUserForm { pass = v } }
@ -307,28 +298,24 @@ handleAction = case _ of
H.liftEffect $ Event.preventDefault ev H.liftEffect $ Event.preventDefault ev
{ wsConnection, addUserForm } <- H.get { wsConnection, addUserForm } <- H.get
let secret = addUserForm.secretKey let login = addUserForm.login
login = addUserForm.login
email = addUserForm.email email = addUserForm.email
pass = addUserForm.pass pass = addUserForm.pass
case wsConnection, secret, login, email, pass of case wsConnection, login, email, pass of
Nothing, _, _, _, _ -> Nothing, _, _, _ ->
unableToSend "Not connected to server." unableToSend "Not connected to server."
Just _, "", _, _, _ -> Just _, "", _, _ ->
unableToSend "Write the secret key with authd!"
Just _, _, "", _, _ ->
unableToSend "Write the user's login!" unableToSend "Write the user's login!"
Just _, _, _, "", _ -> Just _, _, "", _ ->
unableToSend "Write the user's email!" unableToSend "Write the user's email!"
Just _, _, _, _, "" -> Just _, _, _, "" ->
unableToSend "Write the user's password!" unableToSend "Write the user's password!"
Just webSocket, _, _, _, _ -> do Just webSocket, _, _, _ -> do
H.liftEffect (WS.readyState webSocket) >>= case _ of H.liftEffect (WS.readyState webSocket) >>= case _ of
Connecting -> Connecting ->
unableToSend "Still connecting to server." unableToSend "Still connecting to server."
@ -344,11 +331,10 @@ handleAction = case _ of
Open -> do Open -> do
H.liftEffect $ do H.liftEffect $ do
ab <- AuthD.serialize $ AuthD.MkAddUser { shared_key: secret ab <- AuthD.serialize $ AuthD.MkAddUser { login: login
, login: login , admin: false
, email: Just (Email.Email email) , email: Just (Email.Email email)
, password: pass , password: pass }
, phone: Nothing}
sendArrayBuffer webSocket ab sendArrayBuffer webSocket ab
appendMessageReset "[😇] Trying to add a user" appendMessageReset "[😇] Trying to add a user"

View File

@ -365,8 +365,7 @@ handleAction = case _ of
H.liftEffect $ do H.liftEffect $ do
ab <- AuthD.serialize $ AuthD.MkRegister { login: login ab <- AuthD.serialize $ AuthD.MkRegister { login: login
, email: Just (Email.Email email) , email: Just (Email.Email email)
, password: pass , password: pass }
, phone: Nothing}
sendArrayBuffer webSocket ab sendArrayBuffer webSocket ab
appendMessageReset "[😇] Trying to register" appendMessageReset "[😇] Trying to register"
@ -429,8 +428,6 @@ handleAction = case _ of
(AuthD.GotToken msg) -> do (AuthD.GotToken msg) -> do
appendMessage $ "[😈] Success! user " <> (show msg.uid) <> " has token: " <> msg.token appendMessage $ "[😈] Success! user " <> (show msg.uid) <> " has token: " <> msg.token
H.raise $ AuthToken (Tuple msg.uid msg.token) H.raise $ AuthToken (Tuple msg.uid msg.token)
(AuthD.GotUserAdded msg) -> do
appendMessage $ "[😈] Success! Server added user: " <> show msg.user
-- WTH?! -- WTH?!
_ -> do _ -> do
appendMessage $ "[😈] Failed! Authentication server didn't send a valid message." appendMessage $ "[😈] Failed! Authentication server didn't send a valid message."

View File

@ -58,6 +58,13 @@ type Password = String
{- UserID should be in a separate module with a dedicated codec. -} {- UserID should be in a separate module with a dedicated codec. -}
type UserID = Int -- UserID is either a login or an uid number type UserID = Int -- UserID is either a login or an uid number
{-
REQUESTS
General notes:
- when the "UserID" is optional, the server will work on the requesting user
-}
{- 0 -} {- 0 -}
type Login = { login :: String, password :: String } type Login = { login :: String, password :: String }
codecLogin ∷ CA.JsonCodec Login codecLogin ∷ CA.JsonCodec Login
@ -149,11 +156,11 @@ codecAddUser
{-, profile :: Maybe Hash(String, JSON::Any) -} }) {-, profile :: Maybe Hash(String, JSON::Any) -} })
{- 10 -} {- 10 -}
type CheckPermission = { user :: UserID, service :: String, resource :: String } type CheckPermission = { user :: Maybe UserID, service :: String, resource :: String }
codecCheckPermission ∷ CA.JsonCodec CheckPermission codecCheckPermission ∷ CA.JsonCodec CheckPermission
codecCheckPermission codecCheckPermission
= CA.object "CheckPermission" (CAR.record = CA.object "CheckPermission" (CAR.record
{ user: CA.int { user: CAR.optional CA.int
, service: CA.string , service: CA.string
, resource: CA.string }) , resource: CA.string })