From 1d2a3e8501e41203313d4ad79bd4f0bbb80a22cc Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 3 Sep 2025 14:32:04 -0500 Subject: [PATCH] refactor: pass AuthResult to txVarQuery Saves one parameter to the function. --- src/PostgREST/Query.hs | 4 ++-- src/PostgREST/Query/PreQuery.hs | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/PostgREST/Query.hs b/src/PostgREST/Query.hs index 80f29b764..c698107c7 100644 --- a/src/PostgREST/Query.hs +++ b/src/PostgREST/Query.hs @@ -106,7 +106,7 @@ data ResultSet query :: AppConfig -> AuthResult -> ApiRequest -> ActionPlan -> SchemaCache -> Query query _ _ _ (NoDb x) _ = NoDbQuery $ NoDbResult x -query conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache = +query conf@AppConfig{..} auth@AuthResult{..} apiReq (Db plan) sCache = DbQuery isoLvl txMode dbHandler transaction mainSQLQuery where transaction = if configDbPreparedStatements then SQL.transaction else SQL.unpreparedTransaction @@ -115,7 +115,7 @@ query conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache = (mainActionQuery, mainSQLQuery) = actionQuery plan conf apiReq sCache dbHandler = do lift $ SQL.statement mempty $ SQL.dynamicallyParameterized - (PreQuery.txVarQuery plan conf authClaims authRole apiReq) + (PreQuery.txVarQuery plan conf auth apiReq) HD.noResult configDbPreparedStatements lift $ whenJust configDbPreRequest $ \prereq -> do SQL.statement mempty $ SQL.dynamicallyParameterized (PreQuery.preReqQuery prereq) HD.noResult configDbPreparedStatements diff --git a/src/PostgREST/Query/PreQuery.hs b/src/PostgREST/Query/PreQuery.hs index 271f9c811..c839068a6 100644 --- a/src/PostgREST/Query/PreQuery.hs +++ b/src/PostgREST/Query/PreQuery.hs @@ -10,14 +10,13 @@ module PostgREST.Query.PreQuery ) where import qualified Data.Aeson as JSON -import qualified Data.Aeson.KeyMap as KM -import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy.Char8 as LBS import qualified Data.HashMap.Strict as HM import qualified Hasql.DynamicStatements.Snippet as SQL hiding (sql) +import PostgREST.Auth.Types (AuthResult (..)) import PostgREST.ApiRequest (ApiRequest (..)) import PostgREST.ApiRequest.Preferences (PreferTimezone (..), Preferences (..)) @@ -35,8 +34,8 @@ import PostgREST.SchemaCache.Routine (Routine (..)) import Protolude hiding (Handler) -- sets transaction variables -txVarQuery :: DbActionPlan -> AppConfig -> KM.KeyMap JSON.Value -> BS.ByteString -> ApiRequest -> SQL.Snippet -txVarQuery dbActPlan AppConfig{..} claims role ApiRequest{..} = +txVarQuery :: DbActionPlan -> AppConfig -> AuthResult -> ApiRequest -> SQL.Snippet +txVarQuery dbActPlan AppConfig{..} AuthResult{..} ApiRequest{..} = -- To ensure `GRANT SET ON PARAMETER TO authenticator` works, the role settings must be set before the impersonated role. -- Otherwise the GRANT SET would have to be applied to the impersonated role. See https://github.com/PostgREST/postgrest/issues/3045 "select " <> intercalateSnippet ", " ( @@ -47,9 +46,9 @@ txVarQuery dbActPlan AppConfig{..} claims role ApiRequest{..} = pathSql = setConfigWithConstantName ("request.path", iPath) headersSql = setConfigWithConstantNameJSON "request.headers" iHeaders cookiesSql = setConfigWithConstantNameJSON "request.cookies" iCookies - claimsSql = [setConfigWithConstantName ("request.jwt.claims", LBS.toStrict $ JSON.encode claims)] - roleSql = [setConfigWithConstantName ("role", role)] - roleSettingsSql = setConfigWithDynamicName <$> HM.toList (fromMaybe mempty $ HM.lookup role configRoleSettings) + claimsSql = [setConfigWithConstantName ("request.jwt.claims", LBS.toStrict $ JSON.encode authClaims)] + roleSql = [setConfigWithConstantName ("role", authRole)] + roleSettingsSql = setConfigWithDynamicName <$> HM.toList (fromMaybe mempty $ HM.lookup authRole configRoleSettings) appSettingsSql = setConfigWithDynamicName . join bimap toUtf8 <$> configAppSettings timezoneSql = maybe mempty (\(PreferTimezone tz) -> [setConfigWithConstantName ("timezone", tz)]) $ preferTimezone iPreferences funcSettingsSql = setConfigWithDynamicName . join bimap toUtf8 <$> funcSettings