refactoring to allow more generic use for different user & scope models
This commit is contained in:
parent
7678af27c8
commit
24229be508
@ -1,4 +1,4 @@
|
|||||||
{-# LANGUAGE DataKinds, TypeOperators, OverloadedStrings #-}
|
{-# LANGUAGE DataKinds, TypeOperators, OverloadedStrings, ScopedTypeVariables, TypeApplications #-}
|
||||||
|
|
||||||
module Server
|
module Server
|
||||||
( insecureOAuthMock
|
( insecureOAuthMock
|
||||||
@ -8,6 +8,7 @@ import User
|
|||||||
|
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
|
|
||||||
|
import Data.Aeson
|
||||||
import Data.List (find)
|
import Data.List (find)
|
||||||
import Data.Text hiding (find, head, map)
|
import Data.Text hiding (find, head, map)
|
||||||
|
|
||||||
@ -22,27 +23,30 @@ testUsers =
|
|||||||
[ User {name = "TestName", email = "foo@bar.com", uID = "1"}]
|
[ User {name = "TestName", email = "foo@bar.com", uID = "1"}]
|
||||||
|
|
||||||
|
|
||||||
type Auth = "auth" :> QueryParam "scopes" String :> Get '[JSON] (Maybe (Map.Map Text Text))
|
type Auth userData = "auth" :> QueryParam "scopes" String :> Get '[JSON] (Maybe userData) -- TODO also parametrise "auth" to (auth :: Symbol)
|
||||||
type Token = "token" :> Post '[JSON] Text -- TODO post jwt token
|
type Token = "token" :> Post '[JSON] Text -- TODO post jwt token
|
||||||
-- type Insert = "insert" :> Post '[JSON] User
|
-- type Insert = "insert" :> Post '[JSON] User
|
||||||
|
|
||||||
authServer :: Server Auth
|
authServer :: forall user userData . UserData user userData => [user] -> Server (Auth userData)
|
||||||
authServer = handleAuth
|
authServer testUsers = handleAuth
|
||||||
where
|
where
|
||||||
handleAuth :: Maybe String -> Handler (Maybe (Map.Map Text Text))
|
handleAuth :: Maybe String -> Handler (Maybe userData)
|
||||||
handleAuth Nothing = liftIO (putStrLn "no query param given") >> return Nothing
|
handleAuth Nothing = liftIO (putStrLn "no query param given") >> return Nothing
|
||||||
handleAuth (Just x) = do
|
handleAuth (Just x) = do
|
||||||
let
|
let
|
||||||
scopes = read x :: [Scope]
|
scopes = readScopes @user @userData x
|
||||||
userData = mconcat $ map (userScope $ head testUsers) scopes
|
ud = mconcat $ map (userScope @user @userData $ head testUsers) scopes
|
||||||
liftIO (putStrLn $ "query param: " ++ show scopes)
|
liftIO (putStrLn $ "query param: " ++ showScopes @user @userData scopes)
|
||||||
return $ Just userData
|
return $ Just ud
|
||||||
|
|
||||||
authAPI :: Proxy Auth
|
exampleAuthServer :: Server (Auth (Map.Map Text Text))
|
||||||
|
exampleAuthServer = authServer testUsers
|
||||||
|
|
||||||
|
authAPI :: Proxy (Auth (Map.Map Text Text))
|
||||||
authAPI = Proxy
|
authAPI = Proxy
|
||||||
|
|
||||||
insecureOAuthMock :: Application
|
insecureOAuthMock :: Application
|
||||||
insecureOAuthMock = authAPI `serve` authServer
|
insecureOAuthMock = authAPI `serve` exampleAuthServer
|
||||||
|
|
||||||
tokenEndpoint :: Server Token
|
tokenEndpoint :: Server Token
|
||||||
tokenEndpoint = undefined
|
tokenEndpoint = undefined
|
||||||
|
|||||||
36
src/User.hs
36
src/User.hs
@ -1,10 +1,8 @@
|
|||||||
{-# LANGUAGE DeriveGeneric, OverloadedStrings, RecordWildCards #-}
|
{-# LANGUAGE OverloadedStrings, RecordWildCards, TypeFamilies, MultiParamTypeClasses, FlexibleInstances, AllowAmbiguousTypes #-}
|
||||||
|
|
||||||
module User
|
module User
|
||||||
(
|
( UserData(..)
|
||||||
User (..)
|
, User (..)
|
||||||
, Scope (..)
|
|
||||||
, userScope
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Aeson
|
import Data.Aeson
|
||||||
@ -14,16 +12,24 @@ import Data.Text hiding (singleton)
|
|||||||
import GHC.Generics
|
import GHC.Generics
|
||||||
|
|
||||||
|
|
||||||
|
class (Eq u, ToJSON a, Monoid a) => UserData u a where -- TODO Eq maybe not necessary, but currently needed for TypeApplications
|
||||||
|
data Scope u
|
||||||
|
readScopes :: String -> [Scope u]
|
||||||
|
showScopes :: [Scope u] -> String
|
||||||
|
userScope :: u -> Scope u -> a
|
||||||
|
|
||||||
|
|
||||||
data User = User
|
data User = User
|
||||||
{ name :: Text
|
{ name :: Text
|
||||||
, email :: Text
|
, email :: Text
|
||||||
, uID :: Text
|
, password :: Text
|
||||||
} deriving (Eq, Show, Generic)
|
, uID :: Text
|
||||||
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
instance ToJSON User
|
|
||||||
|
|
||||||
data Scope = ID | Profile deriving (Show, Read, Eq)
|
instance UserData User (Map Text Text) where
|
||||||
|
data Scope User = ID | Profile deriving (Show, Read, Eq)
|
||||||
userScope :: User -> Scope -> Map Text Text
|
readScopes = read
|
||||||
userScope User{..} ID = singleton "id" uID
|
showScopes = show
|
||||||
userScope User{..} Profile = fromList [("name", name), ("email", email)]
|
userScope User{..} ID = singleton "id" uID
|
||||||
|
userScope User{..} Profile = fromList [("name", name), ("email", email)]
|
||||||
Loading…
Reference in New Issue
Block a user