From c8835c1d4564cd712b0df3dc2423629cd4a476bf Mon Sep 17 00:00:00 2001 From: David Mosbach Date: Wed, 10 Jan 2024 22:13:00 +0100 Subject: [PATCH] return user data at user end point based on token id --- src/Server.hs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Server.hs b/src/Server.hs index eb8dbe9..9adf940 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -14,7 +14,7 @@ import Control.Concurrent import Control.Concurrent.STM (atomically) import Control.Concurrent.STM.TVar (newTVarIO, readTVar, modifyTVar) import Control.Exception (bracket) -import Control.Monad (unless) +import Control.Monad (unless, (>=>)) import Control.Monad.IO.Class import Control.Monad.Trans.Reader @@ -261,12 +261,11 @@ userEndpoint = handleUserData let jwt = fromJust . decode @JWT $ fromStrict body -- TODO check if token grants access, then read logged in user from cookie liftIO $ print jwt - return mempty - -- let - -- scopes' = map (readScope @user @userData) $ words scopes - -- uData = mconcat $ map (userScope @user @userData u) scopes' - -- liftIO . putStrLn $ "user: " ++ show u ++ " | scopes: " ++ show (map (showScope @user @userData) scopes') - -- return uData + mUser <- ask >>= liftIO . (atomically . readTVar >=> return . Map.lookup (jti jwt) . activeTokens) + case mUser of + Just (u, scopes) -> return . mconcat $ map (userScope @user @userData u) scopes + Nothing -> throwError $ err500 { errBody = "Unknown token" } + decodeToken :: forall user userData . UserData user userData => Text -> AuthState user -> IO (Either JwtError JwtContent) decodeToken token state = do