2020-07-15 13:40:48 +03:00
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
|
2020-05-28 19:18:26 +03:00
|
|
|
module Hasura.Server.AuthSpec (spec) where
|
|
|
|
|
2021-09-15 11:29:34 +03:00
|
|
|
import Control.Lens hiding ((.=))
|
2021-05-11 18:18:31 +03:00
|
|
|
import Control.Monad.Trans.Control
|
2021-09-15 11:29:34 +03:00
|
|
|
import Control.Monad.Trans.Managed (lowerManagedT)
|
|
|
|
import Crypto.JOSE.JWK qualified as Jose
|
|
|
|
import Crypto.JWT qualified as JWT
|
|
|
|
import Data.Aeson ((.=))
|
|
|
|
import Data.Aeson qualified as J
|
2022-06-08 18:31:28 +03:00
|
|
|
import Data.Aeson.Key qualified as K
|
|
|
|
import Data.Aeson.KeyMap qualified as KM
|
2021-09-15 11:29:34 +03:00
|
|
|
import Data.HashMap.Strict qualified as Map
|
2021-11-17 10:32:24 +03:00
|
|
|
import Data.HashSet qualified as Set
|
2021-05-11 18:18:31 +03:00
|
|
|
import Data.Parser.JSONPath
|
2022-07-05 18:52:40 +03:00
|
|
|
import Data.Text qualified as T
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-09-15 11:29:34 +03:00
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol (ReqsText)
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Logging
|
2020-05-28 19:18:26 +03:00
|
|
|
import Hasura.Prelude
|
2021-09-15 11:29:34 +03:00
|
|
|
import Hasura.Server.Auth hiding (getUserInfoWithExpTime, processJwt)
|
|
|
|
import Hasura.Server.Auth.JWT hiding (processJwt)
|
2020-05-28 19:18:26 +03:00
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Hasura.Session
|
2021-09-15 11:29:34 +03:00
|
|
|
import Hasura.Tracing qualified as Tracing
|
2022-02-16 10:08:51 +03:00
|
|
|
import Network.HTTP.Types qualified as HTTP
|
2020-05-28 19:18:26 +03:00
|
|
|
import Test.Hspec
|
2021-05-11 18:18:31 +03:00
|
|
|
|
2020-05-28 19:18:26 +03:00
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
getUserInfoWithExpTimeTests
|
|
|
|
setupAuthModeTests
|
2020-08-31 19:40:01 +03:00
|
|
|
parseClaimsMapTests
|
|
|
|
|
2022-06-08 18:31:28 +03:00
|
|
|
allowedRolesClaimText :: K.Key
|
|
|
|
allowedRolesClaimText = K.fromText $ sessionVariableToText allowedRolesClaim
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2022-06-08 18:31:28 +03:00
|
|
|
defaultRoleClaimText :: K.Key
|
|
|
|
defaultRoleClaimText = K.fromText $ sessionVariableToText defaultRoleClaim
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
-- Unit test the core of our authentication code. This doesn't test the details
|
|
|
|
-- of resolving roles from JWT or webhook.
|
2021-02-03 10:10:39 +03:00
|
|
|
-- TODO(swann): does this need to also test passing
|
2020-05-28 19:18:26 +03:00
|
|
|
getUserInfoWithExpTimeTests :: Spec
|
|
|
|
getUserInfoWithExpTimeTests = describe "getUserInfo" $ do
|
|
|
|
---- FUNCTION UNDER TEST:
|
2021-02-03 10:10:39 +03:00
|
|
|
let gqlUserInfoWithExpTime ::
|
2020-05-28 19:18:26 +03:00
|
|
|
J.Object ->
|
2022-02-16 10:08:51 +03:00
|
|
|
[HTTP.Header] ->
|
2021-02-03 10:10:39 +03:00
|
|
|
AuthMode ->
|
|
|
|
Maybe ReqsText ->
|
|
|
|
IO (Either Code RoleName)
|
|
|
|
gqlUserInfoWithExpTime claims rawHeaders authMode =
|
2020-07-15 13:40:48 +03:00
|
|
|
runExceptT
|
2020-05-28 19:18:26 +03:00
|
|
|
. withExceptT qeCode -- just look at Code for purposes of tests
|
|
|
|
. fmap _uiRole -- just look at RoleName for purposes of tests
|
2021-11-09 15:00:21 +03:00
|
|
|
. fmap (view _1) -- disregard Nothing expiration
|
2021-02-03 10:10:39 +03:00
|
|
|
. getUserInfoWithExpTime_ userInfoFromAuthHook processJwt () () rawHeaders authMode
|
2020-05-28 19:18:26 +03:00
|
|
|
where
|
|
|
|
-- mock authorization callbacks:
|
2021-02-03 10:10:39 +03:00
|
|
|
userInfoFromAuthHook _ _ _hook _reqHeaders _optionalReqs = do
|
2021-11-09 15:00:21 +03:00
|
|
|
(,Nothing,[]) <$> _UserInfo "hook"
|
2020-05-28 19:18:26 +03:00
|
|
|
where
|
|
|
|
-- we don't care about details here; we'll just check role name in tests:
|
2020-07-15 13:40:48 +03:00
|
|
|
_UserInfo nm =
|
2020-05-28 19:18:26 +03:00
|
|
|
mkUserInfo
|
|
|
|
(URBFromSessionVariablesFallback $ mkRoleNameE nm)
|
2020-07-15 13:40:48 +03:00
|
|
|
UAdminSecretNotSent
|
2020-08-31 19:40:01 +03:00
|
|
|
(mkSessionVariablesHeaders mempty)
|
2021-02-25 12:02:43 +03:00
|
|
|
|
|
|
|
processAuthZHeader _jwtCtx _authzHeader =
|
2022-06-08 18:31:28 +03:00
|
|
|
pure (Map.fromList $ map (first (mkSessionVariable . K.toText)) $ KM.toList claims, Nothing)
|
2021-02-25 12:02:43 +03:00
|
|
|
|
2022-02-14 02:33:49 +03:00
|
|
|
processJwt = processJwt_ processAuthZHeader tokenIssuer (const JHAuthorization)
|
2020-05-28 19:18:26 +03:00
|
|
|
|
2021-02-03 10:10:39 +03:00
|
|
|
let getUserInfoWithExpTime ::
|
|
|
|
J.Object ->
|
2022-02-16 10:08:51 +03:00
|
|
|
[HTTP.Header] ->
|
2021-02-03 10:10:39 +03:00
|
|
|
AuthMode ->
|
|
|
|
IO (Either Code RoleName)
|
|
|
|
getUserInfoWithExpTime o claims authMode = gqlUserInfoWithExpTime o claims authMode Nothing
|
|
|
|
|
2020-07-15 13:40:48 +03:00
|
|
|
let setupAuthMode'E a b c d =
|
2022-06-21 14:11:08 +03:00
|
|
|
either (const $ error "fixme") id
|
|
|
|
<$> setupAuthMode' a b c d
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
let ourUnauthRole = mkRoleNameE "an0nymous"
|
|
|
|
|
|
|
|
describe "started without admin secret" $ do
|
|
|
|
it "gives admin by default" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
mode <- setupAuthMode'E Nothing Nothing mempty Nothing
|
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "allows any requested role" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
mode <- setupAuthMode'E Nothing Nothing mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
describe "admin secret only" $ do
|
|
|
|
describe "unauth role NOT set" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
mode <- runIO $ setupAuthMode'E (Just $ Set.singleton $ hashAdminSecret "secret") Nothing mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
it "accepts when admin secret matches" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "accepts when admin secret matches, honoring role request" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), (userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
it "rejects when doesn't match" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (adminSecretHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
-- with deprecated header:
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (deprecatedAccessKeyHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
it "rejects when no secret sent, since no fallback unauth role" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t"), (userRoleHeader, "admin")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
describe "unauth role set" $ do
|
2020-07-15 13:40:48 +03:00
|
|
|
mode <-
|
|
|
|
runIO $
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode'E (Just $ Set.singleton $ hashAdminSecret "secret") Nothing mempty (Just ourUnauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
it "accepts when admin secret matches" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), ("heh", "heh")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "accepts when admin secret matches, honoring role request" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), (userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
it "rejects when doesn't match" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (adminSecretHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
-- with deprecated header:
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (deprecatedAccessKeyHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
it "accepts when no secret sent and unauth role defined" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right ourUnauthRole
|
|
|
|
getUserInfoWithExpTime mempty [("heh", "heh")] mode
|
|
|
|
`shouldReturn` Right ourUnauthRole
|
|
|
|
-- FIXME MAYBE (see NOTE (*))
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right ourUnauthRole
|
|
|
|
|
|
|
|
-- Unauthorized role is not supported for webhook
|
|
|
|
describe "webhook" $ do
|
2020-07-15 13:40:48 +03:00
|
|
|
mode <-
|
|
|
|
runIO $
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode'E (Just $ Set.singleton $ hashAdminSecret "secret") (Just fakeAuthHook) mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
it "accepts when admin secret matches" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "accepts when admin secret matches, honoring role request" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), (userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
it "rejects when admin secret doesn't match" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (adminSecretHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
-- with deprecated header:
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (deprecatedAccessKeyHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
it "authenticates with webhook when no admin secret sent" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (mkRoleNameE "hook")
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "hook")
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "hook")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "hook")
|
|
|
|
|
|
|
|
-- FIXME MAYBE (see NOTE (*))
|
|
|
|
it "ignores requested role, uses webhook role" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t"), (userRoleHeader, "admin")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "hook")
|
|
|
|
getUserInfoWithExpTime mempty [(userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "hook")
|
|
|
|
|
|
|
|
-- helper for generating mocked up verified JWT token claims, as though returned by 'processAuthZHeader':
|
|
|
|
let unObject l = case J.object l of
|
|
|
|
J.Object o -> o
|
2020-07-15 13:40:48 +03:00
|
|
|
_ -> error "impossible"
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
describe "JWT" $ do
|
|
|
|
describe "unauth role NOT set" $ do
|
2020-07-15 13:40:48 +03:00
|
|
|
mode <-
|
|
|
|
runIO $
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode'E (Just $ Set.singleton $ hashAdminSecret "secret") Nothing [fakeJWTConfig] Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
it "accepts when admin secret matches" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "accepts when admin secret matches, honoring role request" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), (userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
it "rejects when admin secret doesn't match" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (adminSecretHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
-- with deprecated header:
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (deprecatedAccessKeyHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
it "rejects when admin secret not sent and no 'Authorization' header" $ do
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah")] mode
|
|
|
|
`shouldReturn` Left InvalidHeaders
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left InvalidHeaders
|
|
|
|
|
|
|
|
describe "unauth role set" $ do
|
2020-07-15 13:40:48 +03:00
|
|
|
mode <-
|
|
|
|
runIO $
|
|
|
|
setupAuthMode'E
|
2021-11-17 10:32:24 +03:00
|
|
|
(Just $ Set.singleton $ hashAdminSecret "secret")
|
2020-07-15 13:40:48 +03:00
|
|
|
Nothing
|
2022-02-14 02:33:49 +03:00
|
|
|
[fakeJWTConfig]
|
2020-05-28 19:18:26 +03:00
|
|
|
(Just ourUnauthRole)
|
|
|
|
|
|
|
|
it "accepts when admin secret matches" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), ("heh", "heh")] mode
|
|
|
|
`shouldReturn` Right adminRoleName
|
|
|
|
it "accepts when admin secret matches, honoring role request" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "secret"), (userRoleHeader, "r00t")] mode
|
|
|
|
`shouldReturn` Right (mkRoleNameE "r00t")
|
|
|
|
|
|
|
|
it "rejects when admin secret doesn't match" $ do
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(adminSecretHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (adminSecretHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
-- with deprecated header:
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "bad secret")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [(deprecatedAccessKeyHeader, "")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah"), (deprecatedAccessKeyHeader, "blah")] mode
|
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
it "authorizes as unauth role when no 'Authorization' header" $ do
|
|
|
|
getUserInfoWithExpTime mempty [("blah", "blah")] mode
|
|
|
|
`shouldReturn` Right ourUnauthRole
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime mempty mempty mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right ourUnauthRole
|
|
|
|
|
|
|
|
describe "when Authorization header sent, and no admin secret" $ do
|
2020-07-15 13:40:48 +03:00
|
|
|
modeA <-
|
|
|
|
runIO $
|
|
|
|
setupAuthMode'E
|
2021-11-17 10:32:24 +03:00
|
|
|
(Just $ Set.singleton $ hashAdminSecret "secret")
|
2020-07-15 13:40:48 +03:00
|
|
|
Nothing
|
2022-02-14 02:33:49 +03:00
|
|
|
[fakeJWTConfig]
|
2020-05-28 19:18:26 +03:00
|
|
|
(Just ourUnauthRole)
|
2020-07-15 13:40:48 +03:00
|
|
|
modeB <-
|
|
|
|
runIO $
|
|
|
|
setupAuthMode'E
|
2021-11-17 10:32:24 +03:00
|
|
|
(Just $ Set.singleton $ hashAdminSecret "secret")
|
2020-05-28 19:18:26 +03:00
|
|
|
Nothing
|
2022-02-14 02:33:49 +03:00
|
|
|
[fakeJWTConfig]
|
2021-09-24 01:56:37 +03:00
|
|
|
Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
-- Here the unauth role does not come into play at all, so map same tests over both modes:
|
2020-07-15 13:40:48 +03:00
|
|
|
forM_ [(modeA, "with unauth role set"), (modeB, "with unauth role NOT set")] $
|
2020-05-28 19:18:26 +03:00
|
|
|
\(mode, modeMsg) -> describe modeMsg $ do
|
|
|
|
it "authorizes successfully with JWT when requested role allowed" $ do
|
2020-08-31 19:40:01 +03:00
|
|
|
let claim =
|
|
|
|
unObject
|
|
|
|
[ allowedRolesClaimText .= (["editor", "user", "mod"] :: [Text]),
|
|
|
|
defaultRoleClaimText .= ("user" :: Text)
|
2020-05-28 19:18:26 +03:00
|
|
|
]
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED"), (userRoleHeader, "editor")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (mkRoleNameE "editor")
|
2020-08-31 19:40:01 +03:00
|
|
|
-- Uses the defaultRoleClaimText:
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (mkRoleNameE "user")
|
|
|
|
|
|
|
|
it "rejects when requested role is not allowed" $ do
|
2020-08-31 19:40:01 +03:00
|
|
|
let claim =
|
|
|
|
unObject
|
|
|
|
[ allowedRolesClaimText .= (["editor", "user", "mod"] :: [Text]),
|
|
|
|
defaultRoleClaimText .= ("user" :: Text)
|
2020-05-28 19:18:26 +03:00
|
|
|
]
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED"), (userRoleHeader, "r00t")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED"), (userRoleHeader, "admin")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
|
|
|
-- A corner case, but the behavior seems desirable:
|
2020-08-31 19:40:01 +03:00
|
|
|
it "always rejects when token has empty allowedRolesClaimText" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
let claim = unObject [allowedRolesClaimText .= (mempty :: [Text]), defaultRoleClaimText .= ("user" :: Text)]
|
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED"), (userRoleHeader, "admin")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED"), (userRoleHeader, "user")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left AccessDenied
|
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
it "rejects when token doesn't have proper allowedRolesClaimText and defaultRoleClaimText" $ do
|
|
|
|
let claim0 = unObject [allowedRolesClaimText .= (["editor", "user", "mod"] :: [Text])]
|
|
|
|
claim1 = unObject [defaultRoleClaimText .= ("user" :: Text)]
|
2022-02-14 02:33:49 +03:00
|
|
|
claim2 = unObject mempty
|
2020-05-28 19:18:26 +03:00
|
|
|
for_ [claim0, claim1, claim2] $ \claim ->
|
2022-02-14 02:33:49 +03:00
|
|
|
getUserInfoWithExpTime claim [("Authorization", "Bearer IGNORED")] mode
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left JWTRoleClaimMissing
|
|
|
|
|
|
|
|
-- (*) FIXME NOTE (re above):
|
|
|
|
--
|
|
|
|
-- Ideally we should always return AccessDenied if the role we would
|
|
|
|
-- otherwise have returned does not match the requested role (from the
|
|
|
|
-- 'userRoleHeader').
|
|
|
|
--
|
|
|
|
-- This would harden a bit against bugs, makes the spec simpler, but
|
|
|
|
-- especially is better UX since in the current behavior the user can't be
|
|
|
|
-- sure which role their query is operating as (and in the worst case we
|
|
|
|
-- might e.g. delete rows the user didn't intend)
|
|
|
|
--
|
|
|
|
-- But this is a breaking change we need to think a little more about;
|
|
|
|
-- users might be relying on the behavior, e.g. just hardcoding a dev role
|
|
|
|
-- into clients.
|
|
|
|
|
|
|
|
-- Some very basic unit tests of AuthMode construction and error modes
|
|
|
|
setupAuthModeTests :: Spec
|
|
|
|
setupAuthModeTests = describe "setupAuthMode" $ do
|
2021-11-17 10:32:24 +03:00
|
|
|
let secret = Set.singleton $ hashAdminSecret "secret"
|
2020-05-28 19:18:26 +03:00
|
|
|
unauthRole = mkRoleNameE "anon"
|
|
|
|
|
|
|
|
-- These are all various error cases, except for the AMNoAuth mode:
|
|
|
|
it "with no admin secret provided" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing Nothing mempty Nothing
|
2020-10-28 19:40:33 +03:00
|
|
|
`shouldReturn` Right AMNoAuth
|
2020-05-28 19:18:26 +03:00
|
|
|
-- We insist on an admin secret in order to use webhook or JWT auth:
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing Nothing [fakeJWTConfig] Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing (Just fakeAuthHook) mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
|
|
|
-- ...and we can't have both:
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing (Just fakeAuthHook) [fakeJWTConfig] Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
|
|
|
-- If the unauthenticated role was set but would otherwise be ignored this
|
|
|
|
-- should be an error (for now), since users might expect all access to use
|
|
|
|
-- the specified role. This first case would be the real worrying one:
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing Nothing mempty (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing Nothing [fakeJWTConfig] (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing (Just fakeAuthHook) mempty (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' Nothing (Just fakeAuthHook) [fakeJWTConfig] (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
|
|
|
|
|
|
|
it "with admin secret provided" $ do
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) Nothing mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (AMAdminSecret secret Nothing)
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) Nothing mempty (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (AMAdminSecret secret $ Just unauthRole)
|
|
|
|
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) Nothing [fakeJWTConfig] Nothing >>= \case
|
2020-05-28 19:18:26 +03:00
|
|
|
Right (AMAdminSecretAndJWT s _ Nothing) -> do
|
|
|
|
s `shouldBe` secret
|
|
|
|
_ -> expectationFailure "AMAdminSecretAndJWT"
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) Nothing [fakeJWTConfig] (Just unauthRole) >>= \case
|
2020-05-28 19:18:26 +03:00
|
|
|
Right (AMAdminSecretAndJWT s _ ur) -> do
|
|
|
|
s `shouldBe` secret
|
|
|
|
ur `shouldBe` Just unauthRole
|
|
|
|
_ -> expectationFailure "AMAdminSecretAndJWT"
|
|
|
|
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) (Just fakeAuthHook) mempty Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Right (AMAdminSecretAndHook secret fakeAuthHook)
|
|
|
|
-- auth hook can't make use of unauthenticated role for now (no good UX):
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) (Just fakeAuthHook) mempty (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
|
|
|
-- we can't have both:
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) (Just fakeAuthHook) [fakeJWTConfig] Nothing
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' (Just secret) (Just fakeAuthHook) [fakeJWTConfig] (Just unauthRole)
|
2020-05-28 19:18:26 +03:00
|
|
|
`shouldReturn` Left ()
|
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
parseClaimsMapTests :: Spec
|
|
|
|
parseClaimsMapTests = describe "parseClaimMapTests" $ do
|
|
|
|
let parseClaimsMap_ ::
|
2021-01-21 19:49:46 +03:00
|
|
|
JWT.ClaimsSet ->
|
2020-08-31 19:40:01 +03:00
|
|
|
JWTClaims ->
|
|
|
|
IO (Either Code ClaimsMap)
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ claimsSet claims =
|
|
|
|
runExceptT $ withExceptT qeCode $ parseClaimsMap claimsSet claims
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
unObject l = case J.object l of
|
|
|
|
J.Object o -> o
|
|
|
|
_ -> error "Impossible!"
|
|
|
|
|
|
|
|
defaultClaimsMap =
|
|
|
|
Map.fromList
|
|
|
|
[ (allowedRolesClaim, J.toJSON (map mkRoleNameE ["user", "editor"])),
|
|
|
|
(defaultRoleClaim, J.toJSON (mkRoleNameE "user"))
|
2021-09-24 01:56:37 +03:00
|
|
|
]
|
2020-08-31 19:40:01 +03:00
|
|
|
|
|
|
|
describe "JWT configured with namespace" $ do
|
|
|
|
describe "JWT configured with namespace key, the key is a text value which is expected to be at the root of the JWT token" $ do
|
|
|
|
it "parses claims map from the JWT token with correct namespace " $ do
|
|
|
|
let claimsObj =
|
|
|
|
unObject $
|
|
|
|
[ "x-hasura-allowed-roles" .= (["user", "editor"] :: [Text]),
|
|
|
|
"x-hasura-default-role" .= ("user" :: Text)
|
|
|
|
]
|
2020-10-28 19:40:33 +03:00
|
|
|
let obj = unObject $ ["claims_map" .= claimsObj]
|
2021-01-21 19:49:46 +03:00
|
|
|
claimsSet = mkClaimsSetWithUnregisteredClaims obj
|
|
|
|
parseClaimsMap_ claimsSet (JCNamespace (ClaimNs "claims_map") defaultClaimsFormat)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Right defaultClaimsMap
|
|
|
|
|
|
|
|
it "doesn't parse claims map from the JWT token with wrong namespace " $ do
|
|
|
|
let claimsObj =
|
|
|
|
unObject $
|
|
|
|
[ "x-hasura-allowed-roles" .= (["user", "editor"] :: [Text]),
|
|
|
|
"x-hasura-default-role" .= ("user" :: Text)
|
|
|
|
]
|
2020-10-28 19:40:33 +03:00
|
|
|
let obj = unObject $ ["claims_map" .= claimsObj]
|
2021-01-21 19:49:46 +03:00
|
|
|
claimsSet = mkClaimsSetWithUnregisteredClaims obj
|
|
|
|
parseClaimsMap_ claimsSet (JCNamespace (ClaimNs "wrong_claims_map") defaultClaimsFormat)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Left JWTInvalidClaims
|
|
|
|
|
|
|
|
describe "JWT configured with namespace JSON path, JSON path to the claims map" $ do
|
|
|
|
it "parse claims map from the JWT token using claims namespace JSON Path" $ do
|
2021-01-21 19:49:46 +03:00
|
|
|
let unregisteredClaims =
|
|
|
|
unObject $
|
2020-08-31 19:40:01 +03:00
|
|
|
[ "x-hasura-allowed-roles" .= (["user", "editor"] :: [Text]),
|
|
|
|
"x-hasura-default-role" .= ("user" :: Text),
|
|
|
|
"sub" .= ("random" :: Text),
|
|
|
|
"exp" .= (1626420800 :: Int) -- we ignore these non session variables, in the response
|
|
|
|
]
|
2021-01-21 19:49:46 +03:00
|
|
|
claimsSetWithSub =
|
2022-06-08 18:31:28 +03:00
|
|
|
(JWT.emptyClaimsSet & JWT.claimSub .~ Just "random") & JWT.unregisteredClaims .~ KM.toMapText unregisteredClaims
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ claimsSetWithSub (JCNamespace (ClaimNsPath (mkJSONPathE "$")) defaultClaimsFormat)
|
2020-08-31 19:40:01 +03:00
|
|
|
-- "$" JSON path signifies the claims are to be found in the root of the JWT token
|
|
|
|
`shouldReturn` Right defaultClaimsMap
|
|
|
|
|
|
|
|
it "throws error while attempting to parse claims map from the JWT token with a wrong namespace JSON Path" $ do
|
|
|
|
let claimsObj =
|
|
|
|
unObject $
|
|
|
|
[ "x-hasura-allowed-roles" .= (["user", "editor"] :: [Text]),
|
|
|
|
"x-hasura-default-role" .= ("user" :: Text)
|
|
|
|
]
|
|
|
|
obj = unObject $ ["hasura_claims" .= claimsObj]
|
2021-01-21 19:49:46 +03:00
|
|
|
claimsSet = mkClaimsSetWithUnregisteredClaims obj
|
|
|
|
parseClaimsMap_ claimsSet (JCNamespace (ClaimNsPath (mkJSONPathE "$.claims")) defaultClaimsFormat)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Left JWTInvalidClaims
|
|
|
|
|
|
|
|
describe "JWT configured with custom JWT claims" $ do
|
|
|
|
let rolesObj =
|
|
|
|
unObject $
|
|
|
|
[ "allowed" .= (["user", "editor"] :: [Text]),
|
|
|
|
"default" .= ("user" :: Text)
|
|
|
|
]
|
|
|
|
userId = unObject ["id" .= ("1" :: Text)]
|
|
|
|
obj =
|
|
|
|
unObject $
|
|
|
|
[ "roles" .= rolesObj,
|
|
|
|
"user" .= userId
|
|
|
|
]
|
2021-01-21 19:49:46 +03:00
|
|
|
claimsSet = mkClaimsSetWithUnregisteredClaims obj
|
2020-08-31 19:40:01 +03:00
|
|
|
userIdClaim = mkSessionVariable "x-hasura-user-id"
|
|
|
|
|
|
|
|
describe "custom claims with JSON paths to the claim location in the JWT token" $ do
|
|
|
|
it "parse custom claims values, with correct values" $ do
|
|
|
|
let customDefRoleClaim = mkCustomDefaultRoleClaim (Just "$.roles.default") Nothing
|
|
|
|
customAllowedRolesClaim = mkCustomAllowedRoleClaim (Just "$.roles.allowed") Nothing
|
|
|
|
otherClaims =
|
|
|
|
Map.fromList
|
2020-10-28 19:40:33 +03:00
|
|
|
[(userIdClaim, mkCustomOtherClaim (Just "$.user.id") Nothing)]
|
2020-08-31 19:40:01 +03:00
|
|
|
customClaimsMap = JWTCustomClaimsMap customDefRoleClaim customAllowedRolesClaim otherClaims
|
|
|
|
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ claimsSet (JCMap customClaimsMap)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Right
|
|
|
|
( Map.fromList
|
|
|
|
[ (allowedRolesClaim, J.toJSON (map mkRoleNameE ["user", "editor"])),
|
|
|
|
(defaultRoleClaim, J.toJSON (mkRoleNameE "user")),
|
|
|
|
(userIdClaim, J.String "1")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-01-21 19:49:46 +03:00
|
|
|
it "parse custom claims values with session variable mapped to a standard JWT claim (sub)" $ do
|
|
|
|
let customDefRoleClaim = mkCustomDefaultRoleClaim (Just "$.roles.default") Nothing
|
|
|
|
customAllowedRolesClaim = mkCustomAllowedRoleClaim (Just "$.roles.allowed") Nothing
|
|
|
|
otherClaims =
|
|
|
|
Map.fromList
|
|
|
|
[(userIdClaim, mkCustomOtherClaim (Just "$.sub") Nothing)]
|
|
|
|
customClaimsMap = JWTCustomClaimsMap customDefRoleClaim customAllowedRolesClaim otherClaims
|
|
|
|
|
|
|
|
parseClaimsMap_ (claimsSet & JWT.claimSub .~ (Just "2")) (JCMap customClaimsMap)
|
|
|
|
`shouldReturn` Right
|
|
|
|
( Map.fromList
|
|
|
|
[ (allowedRolesClaim, J.toJSON (map mkRoleNameE ["user", "editor"])),
|
|
|
|
(defaultRoleClaim, J.toJSON (mkRoleNameE "user")),
|
|
|
|
(userIdClaim, J.String "2")
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
it "throws error when a specified custom claim value is missing" $ do
|
|
|
|
let customDefRoleClaim = mkCustomDefaultRoleClaim (Just "$.roles.wrong_default") Nothing -- wrong path provided
|
|
|
|
customAllowedRolesClaim = mkCustomAllowedRoleClaim (Just "$.roles.allowed") Nothing
|
|
|
|
customClaimsMap = JWTCustomClaimsMap customDefRoleClaim customAllowedRolesClaim mempty
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ claimsSet (JCMap customClaimsMap)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Left JWTRoleClaimMissing
|
|
|
|
|
|
|
|
it "doesn't throw an error when the specified custom claim is missing, but the default value is provided" $ do
|
|
|
|
let customDefRoleClaim = mkCustomDefaultRoleClaim (Just "$.roles.wrong_default") (Just "editor")
|
|
|
|
customAllowedRolesClaim = mkCustomAllowedRoleClaim (Just "$.roles.allowed") Nothing
|
|
|
|
customClaimsMap = JWTCustomClaimsMap customDefRoleClaim customAllowedRolesClaim mempty
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ claimsSet (JCMap customClaimsMap)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Right
|
|
|
|
( Map.fromList
|
|
|
|
[ (allowedRolesClaim, J.toJSON (map mkRoleNameE ["user", "editor"])),
|
|
|
|
(defaultRoleClaim, J.toJSON (mkRoleNameE "editor"))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
describe "custom claims with literal values" $ do
|
|
|
|
it "uses the literal custom claim value" $ do
|
|
|
|
let customDefRoleClaim = mkCustomDefaultRoleClaim Nothing (Just "editor")
|
|
|
|
customAllowedRolesClaim = mkCustomAllowedRoleClaim Nothing (Just ["user", "editor"])
|
|
|
|
customClaimsMap = JWTCustomClaimsMap customDefRoleClaim customAllowedRolesClaim mempty
|
2021-01-21 19:49:46 +03:00
|
|
|
parseClaimsMap_ JWT.emptyClaimsSet (JCMap customClaimsMap)
|
2020-08-31 19:40:01 +03:00
|
|
|
`shouldReturn` Right
|
|
|
|
( Map.fromList
|
|
|
|
[ (allowedRolesClaim, J.toJSON (map mkRoleNameE ["user", "editor"])),
|
|
|
|
(defaultRoleClaim, J.toJSON (mkRoleNameE "editor"))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-10-28 19:40:33 +03:00
|
|
|
mkCustomDefaultRoleClaim :: Maybe Text -> Maybe Text -> JWTCustomClaimsMapDefaultRole
|
2020-08-31 19:40:01 +03:00
|
|
|
mkCustomDefaultRoleClaim claimPath defVal =
|
|
|
|
-- check if claimPath is provided, if not then use the default value
|
|
|
|
-- as the literal value by removing the `Maybe` of defVal
|
|
|
|
case claimPath of
|
|
|
|
Just path -> JWTCustomClaimsMapJSONPath (mkJSONPathE path) $ defRoleName
|
2021-02-03 10:10:39 +03:00
|
|
|
Nothing -> JWTCustomClaimsMapStatic $ fromMaybe (mkRoleNameE "user") defRoleName
|
2020-08-31 19:40:01 +03:00
|
|
|
where
|
|
|
|
defRoleName = mkRoleNameE <$> defVal
|
|
|
|
|
2020-10-28 19:40:33 +03:00
|
|
|
mkCustomAllowedRoleClaim :: Maybe Text -> Maybe [Text] -> JWTCustomClaimsMapAllowedRoles
|
2020-08-31 19:40:01 +03:00
|
|
|
mkCustomAllowedRoleClaim claimPath defVal =
|
|
|
|
-- check if claimPath is provided, if not then use the default value
|
|
|
|
-- as the literal value by removing the `Maybe` of defVal
|
|
|
|
case claimPath of
|
|
|
|
Just path -> JWTCustomClaimsMapJSONPath (mkJSONPathE path) $ defAllowedRoles
|
|
|
|
Nothing ->
|
|
|
|
JWTCustomClaimsMapStatic $
|
2020-10-28 19:40:33 +03:00
|
|
|
fromMaybe (mkRoleNameE <$> ["user", "editor"]) defAllowedRoles
|
2020-08-31 19:40:01 +03:00
|
|
|
where
|
|
|
|
defAllowedRoles = fmap mkRoleNameE <$> defVal
|
|
|
|
|
|
|
|
-- use for claims other than `x-hasura-default-role` and `x-hasura-allowed-roles`
|
2020-10-28 19:40:33 +03:00
|
|
|
mkCustomOtherClaim :: Maybe Text -> Maybe Text -> JWTCustomClaimsMapValue
|
2020-08-31 19:40:01 +03:00
|
|
|
mkCustomOtherClaim claimPath defVal =
|
|
|
|
-- check if claimPath is provided, if not then use the default value
|
|
|
|
-- as the literal value by removing the `Maybe` of defVal
|
|
|
|
case claimPath of
|
|
|
|
Just path -> JWTCustomClaimsMapJSONPath (mkJSONPathE path) $ defVal
|
2021-02-03 10:10:39 +03:00
|
|
|
Nothing -> JWTCustomClaimsMapStatic $ fromMaybe "default claim value" defVal
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2021-12-14 17:28:50 +03:00
|
|
|
fakeJWTConfig :: JWTConfig
|
2020-07-15 13:40:48 +03:00
|
|
|
fakeJWTConfig =
|
2020-05-28 19:18:26 +03:00
|
|
|
let jcKeyOrUrl = Left (Jose.fromOctets [])
|
|
|
|
jcAudience = Nothing
|
|
|
|
jcIssuer = Nothing
|
2020-08-31 19:40:01 +03:00
|
|
|
jcClaims = JCNamespace (ClaimNs "") JCFJson
|
2021-01-13 11:38:13 +03:00
|
|
|
jcAllowedSkew = Nothing
|
2021-02-25 12:02:43 +03:00
|
|
|
jcHeader = Nothing
|
2021-12-14 17:28:50 +03:00
|
|
|
in JWTConfig {..}
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
fakeAuthHook :: AuthHook
|
2022-08-04 05:24:35 +03:00
|
|
|
fakeAuthHook = AuthHook "http://fake" AHTGet
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
mkRoleNameE :: Text -> RoleName
|
|
|
|
mkRoleNameE = fromMaybe (error "fixme") . mkRoleName
|
|
|
|
|
2022-07-06 10:54:57 +03:00
|
|
|
mkJSONPathE :: Text -> J.JSONPath
|
2022-07-05 18:52:40 +03:00
|
|
|
mkJSONPathE = either (error . T.unpack) id . parseJSONPath
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2020-07-15 13:40:48 +03:00
|
|
|
newtype NoReporter a = NoReporter {runNoReporter :: IO a}
|
|
|
|
deriving newtype (Functor, Applicative, Monad, MonadIO, MonadBase IO, MonadBaseControl IO)
|
|
|
|
|
|
|
|
instance Tracing.HasReporter NoReporter
|
|
|
|
|
|
|
|
setupAuthMode' ::
|
2021-11-17 10:32:24 +03:00
|
|
|
Maybe (HashSet AdminSecretHash) ->
|
2020-05-28 19:18:26 +03:00
|
|
|
Maybe AuthHook ->
|
2022-02-14 02:33:49 +03:00
|
|
|
[JWTConfig] ->
|
2020-05-28 19:18:26 +03:00
|
|
|
Maybe RoleName ->
|
|
|
|
IO (Either () AuthMode)
|
2022-02-14 02:33:49 +03:00
|
|
|
setupAuthMode' mAdminSecretHash mWebHook jwtSecrets mUnAuthRole =
|
2021-10-13 19:38:56 +03:00
|
|
|
-- just throw away the error message for ease of testing:
|
|
|
|
fmap (either (const $ Left ()) Right) $
|
|
|
|
runNoReporter $
|
|
|
|
lowerManagedT $
|
|
|
|
runExceptT $
|
|
|
|
setupAuthMode
|
2021-11-17 10:32:24 +03:00
|
|
|
(fromMaybe Set.empty mAdminSecretHash)
|
2021-10-13 19:38:56 +03:00
|
|
|
mWebHook
|
2022-02-14 02:33:49 +03:00
|
|
|
jwtSecrets
|
2021-10-13 19:38:56 +03:00
|
|
|
mUnAuthRole
|
|
|
|
-- NOTE: this won't do any http or launch threads if we don't specify JWT URL:
|
|
|
|
(error "H.Manager")
|
|
|
|
(Logger $ void . return)
|
2021-01-21 19:49:46 +03:00
|
|
|
|
2022-06-08 18:31:28 +03:00
|
|
|
mkClaimsSetWithUnregisteredClaims :: J.Object -> JWT.ClaimsSet
|
2021-01-21 19:49:46 +03:00
|
|
|
mkClaimsSetWithUnregisteredClaims unregisteredClaims =
|
2022-06-08 18:31:28 +03:00
|
|
|
JWT.emptyClaimsSet & JWT.unregisteredClaims .~ KM.toMapText unregisteredClaims
|