added scopes

This commit is contained in:
David Mosbach 2023-12-20 03:36:39 +01:00
parent a3e9764ca2
commit 7678af27c8
5 changed files with 40 additions and 18 deletions

View File

@ -5,4 +5,4 @@ import Server
import Network.Wai.Handler.Warp import Network.Wai.Handler.Warp
main :: IO () main :: IO ()
main = putStrLn "Try: http://localhost:8080/query?userID=1" >> run 8080 insecureOAuthMock main = putStrLn "Try: http://localhost:8080/auth?scopes=[ID,Profile]" >> run 8080 insecureOAuthMock

View File

@ -31,6 +31,7 @@ library
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, containers
, servant , servant
, servant-server , servant-server
, text , text
@ -49,6 +50,7 @@ executable oauth2-mock-server-exe
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, containers
, oauth2-mock-server , oauth2-mock-server
, servant , servant
, servant-server , servant-server
@ -69,6 +71,7 @@ test-suite oauth2-mock-server-test
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, containers
, oauth2-mock-server , oauth2-mock-server
, servant , servant
, servant-server , servant-server

View File

@ -26,6 +26,7 @@ dependencies:
- warp - warp
- aeson - aeson
- text - text
- containers
ghc-options: ghc-options:
- -Wall - -Wall

View File

@ -8,33 +8,42 @@ import User
import Control.Monad.IO.Class import Control.Monad.IO.Class
import Data.Aeson import Data.List (find)
import Data.List import Data.Text hiding (find, head, map)
import Data.Maybe
import Data.Text hiding (find) import qualified Data.Map.Strict as Map
import Servant import Servant
import Servant.API -- import Servant.API
testUsers :: [User] testUsers :: [User]
testUsers = testUsers =
[ User {name = "TestName", email = "foo@bar.com", uID = "1"}] [ User {name = "TestName", email = "foo@bar.com", uID = "1"}]
type Query = "query" :> QueryParam "userID" Text :> Get '[JSON] (Maybe User) type Auth = "auth" :> QueryParam "scopes" String :> Get '[JSON] (Maybe (Map.Map Text Text))
type Insert = "insert" :> Post '[JSON] User type Token = "token" :> Post '[JSON] Text -- TODO post jwt token
-- type Insert = "insert" :> Post '[JSON] User
queryServer :: Server Query authServer :: Server Auth
queryServer = handleQuery authServer = handleAuth
where where
handleQuery :: Maybe Text -> Handler (Maybe User) handleAuth :: Maybe String -> Handler (Maybe (Map.Map Text Text))
handleQuery Nothing = liftIO (putStrLn "no query param given") >> return Nothing handleAuth Nothing = liftIO (putStrLn "no query param given") >> return Nothing
handleQuery (Just x) = liftIO (putStrLn $ "query param: " ++ show x) >> (return $ find (\u -> uID u == x) testUsers) handleAuth (Just x) = do
let
scopes = read x :: [Scope]
userData = mconcat $ map (userScope $ head testUsers) scopes
liftIO (putStrLn $ "query param: " ++ show scopes)
return $ Just userData
queryAPI :: Proxy Query authAPI :: Proxy Auth
queryAPI = Proxy authAPI = Proxy
insecureOAuthMock :: Application insecureOAuthMock :: Application
insecureOAuthMock = queryAPI `serve` queryServer insecureOAuthMock = authAPI `serve` authServer
tokenEndpoint :: Server Token
tokenEndpoint = undefined

View File

@ -1,12 +1,15 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric, OverloadedStrings, RecordWildCards #-}
module User module User
( (
User (..) User (..)
, Scope (..)
, userScope
) where ) where
import Data.Aeson import Data.Aeson
import Data.Text import Data.Map.Strict
import Data.Text hiding (singleton)
import GHC.Generics import GHC.Generics
@ -18,3 +21,9 @@ data User = User
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic)
instance ToJSON User instance ToJSON User
data Scope = ID | Profile deriving (Show, Read, Eq)
userScope :: User -> Scope -> Map Text Text
userScope User{..} ID = singleton "id" uID
userScope User{..} Profile = fromList [("name", name), ("email", email)]