2020-10-27 16:53:49 +03:00
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
2020-04-03 03:00:13 +03:00
|
|
|
|
2018-07-20 10:22:46 +03:00
|
|
|
module Hasura.Server.Auth
|
|
|
|
( getUserInfo
|
2019-05-14 09:24:46 +03:00
|
|
|
, getUserInfoWithExpTime
|
2020-04-03 03:00:13 +03:00
|
|
|
, AuthMode (..)
|
2020-05-19 17:48:49 +03:00
|
|
|
, setupAuthMode
|
|
|
|
, AdminSecretHash
|
|
|
|
, hashAdminSecret
|
|
|
|
-- * WebHook related
|
2020-04-03 03:00:13 +03:00
|
|
|
, AuthHookType (..)
|
2018-12-19 14:38:33 +03:00
|
|
|
, AuthHookG (..)
|
|
|
|
, AuthHook
|
2020-05-19 17:48:49 +03:00
|
|
|
-- * JWT related
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
, RawJWT
|
|
|
|
, JWTConfig (..)
|
2018-09-27 14:22:49 +03:00
|
|
|
, JWTCtx (..)
|
|
|
|
, JWKSet (..)
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
, processJwt
|
2018-09-27 14:22:49 +03:00
|
|
|
, updateJwkRef
|
2019-11-26 15:14:21 +03:00
|
|
|
, UserAuthentication (..)
|
2020-05-28 19:18:26 +03:00
|
|
|
|
|
|
|
-- * Exposed for testing
|
|
|
|
, getUserInfoWithExpTime_
|
2018-07-20 10:22:46 +03:00
|
|
|
) where
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
import qualified Control.Concurrent.Async.Lifted.Safe as LA
|
2020-10-27 16:53:49 +03:00
|
|
|
import qualified Crypto.Hash as Crypto
|
|
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import qualified Network.HTTP.Client as H
|
|
|
|
import qualified Network.HTTP.Types as N
|
|
|
|
|
2020-07-14 22:00:58 +03:00
|
|
|
import Control.Concurrent.Extended (forkImmortal)
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
|
|
|
import Data.IORef (newIORef)
|
|
|
|
import Data.Time.Clock (UTCTime)
|
2020-03-20 09:46:45 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import qualified Hasura.Tracing as Tracing
|
2018-10-25 21:16:25 +03:00
|
|
|
|
|
|
|
import Hasura.Logging
|
2018-07-20 10:22:46 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-07-14 22:00:58 +03:00
|
|
|
import Hasura.Server.Auth.JWT hiding (processJwt_)
|
2020-04-03 03:00:13 +03:00
|
|
|
import Hasura.Server.Auth.WebHook
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
import Hasura.Server.Utils
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2020-04-24 12:10:53 +03:00
|
|
|
import Hasura.Session
|
2020-10-27 16:53:49 +03:00
|
|
|
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
-- | Typeclass representing the @UserInfo@ authorization and resolving effect
|
|
|
|
class (Monad m) => UserAuthentication m where
|
|
|
|
resolveUserInfo
|
2020-04-03 03:00:13 +03:00
|
|
|
:: HasVersion
|
2020-01-23 00:55:55 +03:00
|
|
|
=> Logger Hasura
|
2019-11-26 15:14:21 +03:00
|
|
|
-> H.Manager
|
|
|
|
-> [N.Header]
|
|
|
|
-- ^ request headers
|
|
|
|
-> AuthMode
|
2019-12-11 04:04:49 +03:00
|
|
|
-> m (Either QErr (UserInfo, Maybe UTCTime))
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-05-19 17:48:49 +03:00
|
|
|
-- | The hashed admin password. 'hashAdminSecret' is our public interface for
|
2020-07-14 22:00:58 +03:00
|
|
|
-- constructing the secret.
|
2020-05-19 17:48:49 +03:00
|
|
|
--
|
|
|
|
-- To prevent misuse and leaking we keep this opaque and don't provide
|
|
|
|
-- instances that could leak information. Likewise for 'AuthMode'.
|
|
|
|
--
|
|
|
|
-- Although this exists only in memory we store only a hash of the admin secret
|
|
|
|
-- primarily in order to:
|
2020-07-14 22:00:58 +03:00
|
|
|
--
|
2020-05-28 19:18:26 +03:00
|
|
|
-- - prevent theoretical timing attacks from a naive `==` check
|
2020-05-19 17:48:49 +03:00
|
|
|
-- - prevent misuse or inadvertent leaking of the secret
|
|
|
|
--
|
|
|
|
newtype AdminSecretHash = AdminSecretHash (Crypto.Digest Crypto.SHA512)
|
|
|
|
deriving (Ord, Eq)
|
|
|
|
|
2020-05-28 19:18:26 +03:00
|
|
|
-- We don't want to be able to leak the secret hash. This is a dummy instance
|
|
|
|
-- to support 'Show AuthMode' which we want for testing.
|
|
|
|
instance Show AdminSecretHash where
|
|
|
|
show _ = "(error \"AdminSecretHash hidden\")"
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
hashAdminSecret :: Text -> AdminSecretHash
|
2020-05-19 17:48:49 +03:00
|
|
|
hashAdminSecret = AdminSecretHash . Crypto.hash . T.encodeUtf8
|
|
|
|
|
|
|
|
-- | The methods we'll use to derive roles for authenticating requests.
|
|
|
|
--
|
|
|
|
-- @Maybe RoleName@ below is the optionally-defined role for the
|
2020-07-14 22:00:58 +03:00
|
|
|
-- unauthenticated (anonymous) user.
|
2020-05-19 17:48:49 +03:00
|
|
|
--
|
2020-07-14 22:00:58 +03:00
|
|
|
-- See: https://hasura.io/docs/1.0/graphql/manual/auth/authentication/unauthenticated-access.html
|
|
|
|
|
2018-07-20 10:22:46 +03:00
|
|
|
data AuthMode
|
|
|
|
= AMNoAuth
|
2020-05-19 17:48:49 +03:00
|
|
|
| AMAdminSecret !AdminSecretHash !(Maybe RoleName)
|
|
|
|
| AMAdminSecretAndHook !AdminSecretHash !AuthHook
|
|
|
|
| AMAdminSecretAndJWT !AdminSecretHash !JWTCtx !(Maybe RoleName)
|
2020-05-28 19:18:26 +03:00
|
|
|
deriving (Show, Eq)
|
2020-05-19 17:48:49 +03:00
|
|
|
|
|
|
|
-- | Validate the user's requested authentication configuration, launching any
|
|
|
|
-- required maintenance threads for JWT etc.
|
|
|
|
--
|
|
|
|
-- This must only be run once, on launch.
|
|
|
|
setupAuthMode
|
2020-01-23 00:55:55 +03:00
|
|
|
:: ( HasVersion
|
|
|
|
, MonadIO m
|
2020-07-14 22:00:58 +03:00
|
|
|
, MonadBaseControl IO m
|
|
|
|
, LA.Forall (LA.Pure m)
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.HasReporter m
|
2018-10-25 21:16:25 +03:00
|
|
|
)
|
2020-05-19 17:48:49 +03:00
|
|
|
=> Maybe AdminSecretHash
|
2018-12-03 14:19:08 +03:00
|
|
|
-> Maybe AuthHook
|
2019-07-11 08:37:06 +03:00
|
|
|
-> Maybe JWTConfig
|
2018-10-25 21:16:25 +03:00
|
|
|
-> Maybe RoleName
|
|
|
|
-> H.Manager
|
2019-11-28 12:03:14 +03:00
|
|
|
-> Logger Hasura
|
2020-07-14 22:00:58 +03:00
|
|
|
-> ExceptT Text m AuthMode
|
2020-05-19 17:48:49 +03:00
|
|
|
setupAuthMode mAdminSecretHash mWebHook mJwtSecret mUnAuthRole httpManager logger =
|
|
|
|
case (mAdminSecretHash, mWebHook, mJwtSecret) of
|
|
|
|
(Just hash, Nothing, Nothing) -> return $ AMAdminSecret hash mUnAuthRole
|
|
|
|
(Just hash, Nothing, Just jwtConf) -> do
|
|
|
|
jwtCtx <- mkJwtCtx jwtConf
|
|
|
|
return $ AMAdminSecretAndJWT hash jwtCtx mUnAuthRole
|
2020-05-28 19:18:26 +03:00
|
|
|
-- Nothing below this case uses unauth role. Throw a fatal error if we would otherwise ignore
|
|
|
|
-- that parameter, lest users misunderstand their auth configuration:
|
2020-07-14 22:00:58 +03:00
|
|
|
_ | isJust mUnAuthRole -> throwError $
|
|
|
|
"Fatal Error: --unauthorized-role (HASURA_GRAPHQL_UNAUTHORIZED_ROLE)"
|
2020-05-28 19:18:26 +03:00
|
|
|
<> requiresAdminScrtMsg
|
|
|
|
<> " and is not allowed when --auth-hook (HASURA_GRAPHQL_AUTH_HOOK) is set"
|
|
|
|
|
|
|
|
(Nothing, Nothing, Nothing) -> return AMNoAuth
|
|
|
|
(Just hash, Just hook, Nothing) -> return $ AMAdminSecretAndHook hash hook
|
2018-10-25 21:16:25 +03:00
|
|
|
|
2020-04-03 03:00:13 +03:00
|
|
|
(Nothing, Just _, Nothing) -> throwError $
|
2019-02-14 12:37:47 +03:00
|
|
|
"Fatal Error : --auth-hook (HASURA_GRAPHQL_AUTH_HOOK)" <> requiresAdminScrtMsg
|
2020-04-03 03:00:13 +03:00
|
|
|
(Nothing, Nothing, Just _) -> throwError $
|
2019-02-14 12:37:47 +03:00
|
|
|
"Fatal Error : --jwt-secret (HASURA_GRAPHQL_JWT_SECRET)" <> requiresAdminScrtMsg
|
2020-04-03 03:00:13 +03:00
|
|
|
(Nothing, Just _, Just _) -> throwError
|
2018-10-25 21:16:25 +03:00
|
|
|
"Fatal Error: Both webhook and JWT mode cannot be enabled at the same time"
|
2020-04-03 03:00:13 +03:00
|
|
|
(Just _, Just _, Just _) -> throwError
|
2018-10-25 21:16:25 +03:00
|
|
|
"Fatal Error: Both webhook and JWT mode cannot be enabled at the same time"
|
|
|
|
where
|
2019-05-14 09:24:46 +03:00
|
|
|
requiresAdminScrtMsg =
|
|
|
|
" requires --admin-secret (HASURA_GRAPHQL_ADMIN_SECRET) or "
|
|
|
|
<> " --access-key (HASURA_GRAPHQL_ACCESS_KEY) to be set"
|
2018-10-25 21:16:25 +03:00
|
|
|
|
2020-05-19 17:48:49 +03:00
|
|
|
-- | Given the 'JWTConfig' (the user input of JWT configuration), create
|
|
|
|
-- the 'JWTCtx' (the runtime JWT config used)
|
2020-07-14 22:00:58 +03:00
|
|
|
-- mkJwtCtx :: HasVersion => JWTConfig -> m JWTCtx
|
|
|
|
mkJwtCtx
|
|
|
|
:: ( HasVersion
|
|
|
|
, MonadIO m
|
|
|
|
, MonadBaseControl IO m
|
|
|
|
, LA.Forall (LA.Pure m)
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.HasReporter m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> JWTConfig
|
2020-10-27 16:53:49 +03:00
|
|
|
-> ExceptT Text m JWTCtx
|
2020-05-19 17:48:49 +03:00
|
|
|
mkJwtCtx JWTConfig{..} = do
|
|
|
|
jwkRef <- case jcKeyOrUrl of
|
|
|
|
Left jwk -> liftIO $ newIORef (JWKSet [jwk])
|
|
|
|
Right url -> getJwkFromUrl url
|
2020-08-31 19:40:01 +03:00
|
|
|
return $ JWTCtx jwkRef jcAudience jcIssuer jcClaims
|
2020-05-19 17:48:49 +03:00
|
|
|
where
|
|
|
|
-- if we can't find any expiry time for the JWK (either in @Expires@ header or @Cache-Control@
|
|
|
|
-- header), do not start a background thread for refreshing the JWK
|
|
|
|
getJwkFromUrl url = do
|
|
|
|
ref <- liftIO $ newIORef $ JWKSet []
|
2020-07-15 13:40:48 +03:00
|
|
|
maybeExpiry <- withJwkError $ Tracing.runTraceT "jwk init" $ updateJwkRef logger httpManager url ref
|
2020-05-19 17:48:49 +03:00
|
|
|
case maybeExpiry of
|
|
|
|
Nothing -> return ref
|
|
|
|
Just time -> do
|
2020-07-14 22:00:58 +03:00
|
|
|
void . lift $ forkImmortal "jwkRefreshCtrl" logger $
|
2020-05-19 17:48:49 +03:00
|
|
|
jwkRefreshCtrl logger httpManager url ref (convertDuration time)
|
|
|
|
return ref
|
|
|
|
|
|
|
|
withJwkError act = do
|
|
|
|
res <- runExceptT act
|
|
|
|
case res of
|
|
|
|
Right r -> return r
|
|
|
|
Left err -> case err of
|
|
|
|
-- when fetching JWK initially, except expiry parsing error, all errors are critical
|
|
|
|
JFEHttpException _ msg -> throwError msg
|
|
|
|
JFEHttpError _ _ _ e -> throwError e
|
|
|
|
JFEJwkParseError _ e -> throwError e
|
|
|
|
JFEExpiryParseError _ _ -> return Nothing
|
2020-02-05 10:07:31 +03:00
|
|
|
|
2018-07-20 10:22:46 +03:00
|
|
|
getUserInfo
|
2020-07-15 13:40:48 +03:00
|
|
|
:: (HasVersion, MonadIO m, MonadBaseControl IO m, MonadError QErr m, Tracing.MonadTrace m)
|
2019-11-26 15:14:21 +03:00
|
|
|
=> Logger Hasura
|
2018-08-03 11:43:35 +03:00
|
|
|
-> H.Manager
|
2018-07-20 10:22:46 +03:00
|
|
|
-> [N.Header]
|
|
|
|
-> AuthMode
|
|
|
|
-> m UserInfo
|
2019-05-14 09:24:46 +03:00
|
|
|
getUserInfo l m r a = fst <$> getUserInfoWithExpTime l m r a
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-05-19 17:48:49 +03:00
|
|
|
-- | Authenticate the request using the headers and the configured 'AuthMode'.
|
2019-05-14 09:24:46 +03:00
|
|
|
getUserInfoWithExpTime
|
2020-07-15 13:40:48 +03:00
|
|
|
:: forall m. (HasVersion, MonadIO m, MonadBaseControl IO m, MonadError QErr m, Tracing.MonadTrace m)
|
2019-11-26 15:14:21 +03:00
|
|
|
=> Logger Hasura
|
2019-05-14 09:24:46 +03:00
|
|
|
-> H.Manager
|
|
|
|
-> [N.Header]
|
|
|
|
-> AuthMode
|
|
|
|
-> m (UserInfo, Maybe UTCTime)
|
2020-05-28 19:18:26 +03:00
|
|
|
getUserInfoWithExpTime = getUserInfoWithExpTime_ userInfoFromAuthHook processJwt
|
|
|
|
|
|
|
|
-- Broken out for testing with mocks:
|
|
|
|
getUserInfoWithExpTime_
|
|
|
|
:: forall m _Manager _Logger_Hasura. (MonadIO m, MonadError QErr m)
|
|
|
|
=> (_Logger_Hasura -> _Manager -> AuthHook -> [N.Header] -> m (UserInfo, Maybe UTCTime))
|
|
|
|
-- ^ mock 'userInfoFromAuthHook'
|
|
|
|
-> (JWTCtx -> [N.Header] -> Maybe RoleName -> m (UserInfo, Maybe UTCTime))
|
|
|
|
-- ^ mock 'processJwt'
|
|
|
|
-> _Logger_Hasura
|
|
|
|
-> _Manager
|
|
|
|
-> [N.Header]
|
|
|
|
-> AuthMode
|
|
|
|
-> m (UserInfo, Maybe UTCTime)
|
|
|
|
getUserInfoWithExpTime_ userInfoFromAuthHook_ processJwt_ logger manager rawHeaders = \case
|
2019-05-14 09:24:46 +03:00
|
|
|
|
2020-05-05 22:57:17 +03:00
|
|
|
AMNoAuth -> withNoExpTime $ mkUserInfoFallbackAdminRole UAuthNotSet
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-05-28 19:18:26 +03:00
|
|
|
-- If hasura was started with an admin secret we:
|
|
|
|
-- - check if a secret was sent in the request
|
|
|
|
-- - if so, check it and authorize as admin else fail
|
|
|
|
-- - if not proceed with either webhook or JWT auth if configured
|
2020-05-19 17:48:49 +03:00
|
|
|
AMAdminSecret realAdminSecretHash maybeUnauthRole ->
|
2020-05-28 19:18:26 +03:00
|
|
|
checkingSecretIfSent realAdminSecretHash $ withNoExpTime $
|
2020-05-05 22:57:17 +03:00
|
|
|
-- Consider unauthorized role, if not found raise admin secret header required exception
|
|
|
|
case maybeUnauthRole of
|
|
|
|
Nothing -> throw401 $ adminSecretHeader <> "/"
|
|
|
|
<> deprecatedAccessKeyHeader <> " required, but not found"
|
|
|
|
Just unAuthRole ->
|
|
|
|
mkUserInfo (URBPreDetermined unAuthRole) UAdminSecretNotSent sessionVariables
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-05-19 17:48:49 +03:00
|
|
|
AMAdminSecretAndHook realAdminSecretHash hook ->
|
2020-05-28 19:18:26 +03:00
|
|
|
checkingSecretIfSent realAdminSecretHash $ userInfoFromAuthHook_ logger manager hook rawHeaders
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
2020-05-19 17:48:49 +03:00
|
|
|
AMAdminSecretAndJWT realAdminSecretHash jwtSecret unAuthRole ->
|
2020-05-28 19:18:26 +03:00
|
|
|
checkingSecretIfSent realAdminSecretHash $ processJwt_ jwtSecret rawHeaders unAuthRole
|
2018-07-20 10:22:46 +03:00
|
|
|
|
|
|
|
where
|
2020-05-28 19:18:26 +03:00
|
|
|
-- CAREFUL!:
|
2020-05-05 22:57:17 +03:00
|
|
|
mkUserInfoFallbackAdminRole adminSecretState =
|
|
|
|
mkUserInfo (URBFromSessionVariablesFallback adminRoleName)
|
|
|
|
adminSecretState sessionVariables
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
sessionVariables = mkSessionVariablesHeaders rawHeaders
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-05-28 19:18:26 +03:00
|
|
|
checkingSecretIfSent
|
2020-05-19 17:48:49 +03:00
|
|
|
:: AdminSecretHash -> m (UserInfo, Maybe UTCTime) -> m (UserInfo, Maybe UTCTime)
|
2020-05-28 19:18:26 +03:00
|
|
|
checkingSecretIfSent realAdminSecretHash actionIfNoAdminSecret = do
|
2020-05-05 22:57:17 +03:00
|
|
|
let maybeRequestAdminSecret =
|
|
|
|
foldl1 (<|>) $ map (`getSessionVariableValue` sessionVariables)
|
|
|
|
[adminSecretHeader, deprecatedAccessKeyHeader]
|
|
|
|
|
|
|
|
-- when admin secret is absent, run the action to retrieve UserInfo
|
|
|
|
case maybeRequestAdminSecret of
|
|
|
|
Nothing -> actionIfNoAdminSecret
|
|
|
|
Just requestAdminSecret -> do
|
2020-05-19 17:48:49 +03:00
|
|
|
when (hashAdminSecret requestAdminSecret /= realAdminSecretHash) $ throw401 $
|
2020-05-05 22:57:17 +03:00
|
|
|
"invalid " <> adminSecretHeader <> "/" <> deprecatedAccessKeyHeader
|
|
|
|
withNoExpTime $ mkUserInfoFallbackAdminRole UAdminSecretSent
|
2019-05-14 09:24:46 +03:00
|
|
|
|
|
|
|
withNoExpTime a = (, Nothing) <$> a
|