tinker tinker

This commit is contained in:
nek0 2019-05-16 04:07:20 +02:00
parent 887a4cae9e
commit 0ab20e57e9
3 changed files with 27 additions and 13 deletions

View file

@ -23,10 +23,11 @@ import Types
type UserAPI = type UserAPI =
"user" :> "user" :>
( "list" :> QueryParam "refine" Refine ( "list" :> AuthProtect "header-auth"
:> AuthProtect "header-auth" :> Get '[JSON] [User] :> QueryParam "refine" Refine :> Get '[JSON] [User]
:<|> "new" :> ReqBody '[JSON] UserSubmit :> Post '[JSON] Int :<|> "new" :> ReqBody '[JSON] UserSubmit :> Post '[JSON] Int
:<|> "update" :> ReqBody '[JSON] (Int, UserSubmit) :> Post '[JSON] () :<|> "update" :> AuthProtect "header-auth"
:> ReqBody '[JSON] (Int, UserSubmit) :> Post '[JSON] ()
) )
:<|> "auth" :> :<|> "auth" :>
( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo ( "get" :> ReqBody '[JSON] Int :> Post '[JSON] AuthInfo

View file

@ -98,8 +98,8 @@ users =
userNew :<|> userNew :<|>
userUpdate userUpdate
where where
userList :: Maybe Refine -> Maybe Int -> MateHandler [User] userList :: Maybe Int -> Maybe Refine -> MateHandler [User]
userList ref muid = do userList muid ref = do
conn <- rsConnection <$> ask conn <- rsConnection <$> ask
userSelect conn ref (isJust muid) userSelect conn ref (isJust muid)
@ -110,11 +110,20 @@ users =
conn <- rsConnection <$> ask conn <- rsConnection <$> ask
head <$> (liftIO $ runInsert_ conn (insertUser us (utctDay now) randSalt)) head <$> (liftIO $ runInsert_ conn (insertUser us (utctDay now) randSalt))
userUpdate :: (Int, UserSubmit) -> MateHandler () userUpdate :: Maybe Int -> (Int, UserSubmit) -> MateHandler ()
userUpdate (id, us) = do userUpdate Nothing _ =
throwError $ err403
{ errBody = "No Authorization present"
}
userUpdate (Just aid) (id, us) = if aid == id
then do
now <- liftIO $ getCurrentTime now <- liftIO $ getCurrentTime
conn <- rsConnection <$> ask conn <- rsConnection <$> ask
void $ liftIO $ runUpdate_ conn (updateUser id us (utctDay now)) void $ liftIO $ runUpdate_ conn (updateUser id us (utctDay now))
else
throwError $ err401
{ errBody = "Wrong Authorization present"
}
auth = auth =
authGet :<|> authGet :<|>

View file

@ -2,7 +2,7 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Model.Auth where module Model.Auth where
import Servant (Handler) import Servant
import GHC.Generics import GHC.Generics
@ -106,9 +106,13 @@ validateToken conn header = do
now <- liftIO $ getCurrentTime now <- liftIO $ getCurrentTime
if diffUTCTime stamp now > 0 if diffUTCTime stamp now > 0
then return $ Just uid then return $ Just uid
else return $ Nothing else throwError $ err401
{ errBody = "Your token expired!"
}
_ -> _ ->
return Nothing throwError $ err401
{ errBody = "No valid token found!"
}
generateToken generateToken
:: Ticket :: Ticket