Removes 'Prefer: return=jwt' header and chooses jwt return based on function type
This commit is contained in:
@@ -175,9 +175,10 @@ app dbstructure conf reqBody req =
|
|||||||
let call = B.Stmt "select " V.empty True <>
|
let call = B.Stmt "select " V.empty True <>
|
||||||
asJson (callProc qi $ fromMaybe M.empty (decode reqBody))
|
asJson (callProc qi $ fromMaybe M.empty (decode reqBody))
|
||||||
bodyJson :: Maybe (Identity Value) <- H.maybeEx call
|
bodyJson :: Maybe (Identity Value) <- H.maybeEx call
|
||||||
|
returnJWT <- doesProcReturnJWT schema proc
|
||||||
return $ responseLBS status200 [jsonH]
|
return $ responseLBS status200 [jsonH]
|
||||||
(let body = fromMaybe emptyArray $ runIdentity <$> bodyJson in
|
(let body = fromMaybe emptyArray $ runIdentity <$> bodyJson in
|
||||||
if hasPrefer "return=jwt"
|
if returnJWT
|
||||||
then "{\"token\":\"" <> cs (tokenJWT jwtSecret body) <> "\"}"
|
then "{\"token\":\"" <> cs (tokenJWT jwtSecret body) <> "\"}"
|
||||||
else cs $ encode body)
|
else cs $ encode body)
|
||||||
else return $ responseLBS status404 [] ""
|
else return $ responseLBS status404 [] ""
|
||||||
|
|||||||
@@ -13,25 +13,39 @@ import Data.Monoid
|
|||||||
import Data.Text (Text, split)
|
import Data.Text (Text, split)
|
||||||
import qualified Hasql as H
|
import qualified Hasql as H
|
||||||
import qualified Hasql.Postgres as P
|
import qualified Hasql.Postgres as P
|
||||||
|
import qualified Hasql.Backend as B
|
||||||
import PostgREST.PgQuery ()
|
import PostgREST.PgQuery ()
|
||||||
import PostgREST.Types
|
import PostgREST.Types
|
||||||
|
|
||||||
import GHC.Exts (groupWith)
|
import GHC.Exts (groupWith)
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
doesProc :: forall c s. B.CxValue c Int =>
|
||||||
|
(Text -> Text -> B.Stmt c) -> Text -> Text -> H.Tx c s Bool
|
||||||
|
doesProc stmt schema proc = do
|
||||||
|
row :: Maybe (Identity Int) <- H.maybeEx $ stmt schema proc
|
||||||
|
return $ isJust row
|
||||||
|
|
||||||
doesProcExist :: Text -> Text -> H.Tx P.Postgres s Bool
|
doesProcExist :: Text -> Text -> H.Tx P.Postgres s Bool
|
||||||
doesProcExist schema proc = do
|
doesProcExist = doesProc [H.stmt|
|
||||||
row :: Maybe (Identity Int) <- H.maybeEx $ [H.stmt|
|
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM pg_catalog.pg_namespace n
|
FROM pg_catalog.pg_namespace n
|
||||||
JOIN pg_catalog.pg_proc p
|
JOIN pg_catalog.pg_proc p
|
||||||
ON pronamespace = n.oid
|
ON pronamespace = n.oid
|
||||||
WHERE nspname = ?
|
WHERE nspname = ?
|
||||||
AND proname = ?
|
AND proname = ?
|
||||||
|] schema proc
|
|]
|
||||||
return $ isJust row
|
|
||||||
|
|
||||||
|
doesProcReturnJWT :: Text -> Text -> H.Tx P.Postgres s Bool
|
||||||
|
doesProcReturnJWT = doesProc [H.stmt|
|
||||||
|
SELECT 1
|
||||||
|
FROM pg_catalog.pg_namespace n
|
||||||
|
JOIN pg_catalog.pg_proc p
|
||||||
|
ON pronamespace = n.oid
|
||||||
|
WHERE nspname = ?
|
||||||
|
AND proname = ?
|
||||||
|
AND pg_catalog.pg_get_function_result(p.oid) = 'jwt'
|
||||||
|
|]
|
||||||
|
|
||||||
tableFromRow :: (Text, Text, Bool, Maybe Text) -> Table
|
tableFromRow :: (Text, Text, Bool, Maybe Text) -> Table
|
||||||
tableFromRow (s, n, i, a) = Table s n i (parseAcl a)
|
tableFromRow (s, n, i, a) = Table s n i (parseAcl a)
|
||||||
|
|||||||
Reference in New Issue
Block a user