diff options
author | Joris | 2017-11-08 23:47:26 +0100 |
---|---|---|
committer | Joris | 2017-11-08 23:47:26 +0100 |
commit | 27e11b20b06f2f2dbfb56c0998a63169b4b8abc4 (patch) | |
tree | 845f54d7fe876c9a3078036975ba85ec21d224a1 /server/src/Secure.hs | |
parent | a3601b5e6f5a3e41fa31752a2c704ccd3632790e (diff) |
Use a better project structure
Diffstat (limited to 'server/src/Secure.hs')
-rw-r--r-- | server/src/Secure.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/server/src/Secure.hs b/server/src/Secure.hs new file mode 100644 index 0000000..f427304 --- /dev/null +++ b/server/src/Secure.hs @@ -0,0 +1,47 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Secure + ( loggedAction + , getUserFromToken + ) where + +import Control.Monad.IO.Class (liftIO) +import Data.Text (Text) +import Data.Text.Lazy (fromStrict) +import Network.HTTP.Types.Status (forbidden403) +import Web.Scotty + +import qualified Common.Message as Message +import qualified Common.Message.Key as Key +import Common.Model (User) + +import Model.Query (Query) +import qualified LoginSession +import qualified Model.Query as Query +import qualified Model.SignIn as SignIn +import qualified Model.User as User + +loggedAction :: (User -> ActionM ()) -> ActionM () +loggedAction action = do + maybeToken <- LoginSession.get + case maybeToken of + Just token -> do + maybeUser <- liftIO . Query.run . getUserFromToken $ token + case maybeUser of + Just user -> + action user + Nothing -> do + status forbidden403 + html . fromStrict . Message.get $ Key.Secure_Unauthorized + Nothing -> do + status forbidden403 + html . fromStrict . Message.get $ Key.Secure_Forbidden + +getUserFromToken :: Text -> Query (Maybe User) +getUserFromToken token = do + mbSignIn <- SignIn.getSignIn token + case mbSignIn of + Just signIn -> + User.get (SignIn.email signIn) + Nothing -> + return Nothing |