add logout facility
This commit is contained in:
parent
aa740eef73
commit
e069bde130
3 changed files with 36 additions and 1 deletions
|
@ -39,4 +39,6 @@ type UserAPI =
|
||||||
:<|> "auth" :>
|
:<|> "auth" :>
|
||||||
( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo
|
( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo
|
||||||
:<|> "send" :> ReqBody '[JSON] AuthRequest :> Post '[JSON] AuthResult
|
:<|> "send" :> ReqBody '[JSON] AuthRequest :> Post '[JSON] AuthResult
|
||||||
|
:<|> "logout" :> AuthProtect "header-auth" :> ReqBody '[JSON] Int
|
||||||
|
:> Post '[JSON] ()
|
||||||
)
|
)
|
||||||
|
|
17
src/Main.hs
17
src/Main.hs
|
@ -162,7 +162,8 @@ beverages =
|
||||||
|
|
||||||
auth =
|
auth =
|
||||||
authGet :<|>
|
authGet :<|>
|
||||||
authSend
|
authSend :<|>
|
||||||
|
authLogout
|
||||||
where
|
where
|
||||||
authGet :: Int -> MateHandler AuthInfo
|
authGet :: Int -> MateHandler AuthInfo
|
||||||
authGet id =
|
authGet id =
|
||||||
|
@ -170,3 +171,17 @@ auth =
|
||||||
|
|
||||||
authSend :: AuthRequest -> MateHandler AuthResult
|
authSend :: AuthRequest -> MateHandler AuthResult
|
||||||
authSend = processAuthRequest
|
authSend = processAuthRequest
|
||||||
|
|
||||||
|
authLogout :: Maybe Int -> Int -> MateHandler ()
|
||||||
|
authLogout (Just muid) luid = do
|
||||||
|
if muid == luid
|
||||||
|
then
|
||||||
|
processLogout luid
|
||||||
|
else
|
||||||
|
throwError $ err403
|
||||||
|
{ errBody = "Forbidden"
|
||||||
|
}
|
||||||
|
authLogout Nothing _ = do
|
||||||
|
throwError $ err403
|
||||||
|
{ errBody = "Forbidden"
|
||||||
|
}
|
||||||
|
|
|
@ -189,6 +189,17 @@ deleteToken tstr = Delete
|
||||||
, dReturning = rCount
|
, dReturning = rCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
deleteTokenByUserId
|
||||||
|
:: Int
|
||||||
|
-> Opaleye.Delete Int64
|
||||||
|
deleteTokenByUserId uid = Delete
|
||||||
|
{ dTable = tokenTable
|
||||||
|
, dWhere = (\(_, rid, _) -> rid .== C.constant uid)
|
||||||
|
, dReturning = rCount
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
newTicket :: Int -> MateHandler AuthTicket
|
newTicket :: Int -> MateHandler AuthTicket
|
||||||
newTicket ident = do
|
newTicket ident = do
|
||||||
store <- rsTicketStore <$> ask
|
store <- rsTicketStore <$> ask
|
||||||
|
@ -232,3 +243,10 @@ processAuthRequest (AuthRequest aticket hash) = do
|
||||||
#else
|
#else
|
||||||
return Denied
|
return Denied
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
processLogout
|
||||||
|
:: Int
|
||||||
|
-> MateHandler ()
|
||||||
|
processLogout uid = do
|
||||||
|
conn <- rsConnection <$> ask
|
||||||
|
liftIO $ void $ runDelete_ conn (deleteTokenByUserId uid)
|
||||||
|
|
Loading…
Reference in a new issue