2020-02-05 10:07:31 +03:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
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 (..)
|
2018-10-25 21:16:25 +03:00
|
|
|
, mkAuthMode
|
2019-02-14 12:37:47 +03:00
|
|
|
, AdminSecret (..)
|
2020-04-03 03:00:13 +03:00
|
|
|
-- WebHook related
|
|
|
|
, AuthHookType (..)
|
2018-12-19 14:38:33 +03:00
|
|
|
, AuthHookG (..)
|
|
|
|
, AuthHook
|
2018-09-27 14:22: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 (..)
|
2018-07-20 10:22:46 +03:00
|
|
|
) where
|
|
|
|
|
2020-03-05 20:59:26 +03:00
|
|
|
import Control.Concurrent.Extended (forkImmortal)
|
2020-03-20 09:46:45 +03:00
|
|
|
import Data.IORef (newIORef)
|
|
|
|
import Data.Time.Clock (UTCTime)
|
|
|
|
import Hasura.Server.Version (HasVersion)
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Network.HTTP.Client as H
|
|
|
|
import qualified Network.HTTP.Types as N
|
2018-10-25 21:16:25 +03:00
|
|
|
|
|
|
|
import Hasura.Logging
|
2018-07-20 10:22:46 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types
|
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.Auth.JWT
|
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-04-24 12:10:53 +03:00
|
|
|
import Hasura.Session
|
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
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
newtype AdminSecret
|
|
|
|
= AdminSecret { getAdminSecret :: T.Text }
|
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
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
|
2018-07-20 10:22:46 +03:00
|
|
|
data AuthMode
|
|
|
|
= AMNoAuth
|
2019-02-14 12:37:47 +03:00
|
|
|
| AMAdminSecret !AdminSecret !(Maybe RoleName)
|
|
|
|
| AMAdminSecretAndHook !AdminSecret !AuthHook
|
|
|
|
| AMAdminSecretAndJWT !AdminSecret !JWTCtx !(Maybe RoleName)
|
2018-07-20 10:22:46 +03:00
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2018-10-25 21:16:25 +03:00
|
|
|
mkAuthMode
|
2020-01-23 00:55:55 +03:00
|
|
|
:: ( HasVersion
|
|
|
|
, MonadIO m
|
2018-10-25 21:16:25 +03:00
|
|
|
, MonadError T.Text m
|
|
|
|
)
|
2019-02-14 12:37:47 +03:00
|
|
|
=> Maybe AdminSecret
|
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
|
2018-10-25 21:16:25 +03:00
|
|
|
-> m AuthMode
|
2019-11-28 12:03:14 +03:00
|
|
|
mkAuthMode mAdminSecret mWebHook mJwtSecret mUnAuthRole httpManager logger =
|
2019-02-14 12:37:47 +03:00
|
|
|
case (mAdminSecret, mWebHook, mJwtSecret) of
|
2018-10-25 21:16:25 +03:00
|
|
|
(Nothing, Nothing, Nothing) -> return AMNoAuth
|
2019-02-14 12:37:47 +03:00
|
|
|
(Just key, Nothing, Nothing) -> return $ AMAdminSecret key mUnAuthRole
|
2018-10-25 21:16:25 +03:00
|
|
|
(Just key, Just hook, Nothing) -> unAuthRoleNotReqForWebHook >>
|
2019-02-14 12:37:47 +03:00
|
|
|
return (AMAdminSecretAndHook key hook)
|
2018-10-25 21:16:25 +03:00
|
|
|
(Just key, Nothing, Just jwtConf) -> do
|
2019-11-28 12:03:14 +03:00
|
|
|
jwtCtx <- mkJwtCtx jwtConf httpManager logger
|
2019-02-14 12:37:47 +03:00
|
|
|
return $ AMAdminSecretAndJWT key jwtCtx mUnAuthRole
|
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
|
|
|
unAuthRoleNotReqForWebHook =
|
2019-05-14 09:24:46 +03:00
|
|
|
when (isJust mUnAuthRole) $ throwError $
|
|
|
|
"Fatal Error: --unauthorized-role (HASURA_GRAPHQL_UNAUTHORIZED_ROLE) is not allowed"
|
|
|
|
<> " when --auth-hook (HASURA_GRAPHQL_AUTH_HOOK) is set"
|
2018-10-25 21:16:25 +03:00
|
|
|
|
2020-02-05 10:07:31 +03:00
|
|
|
-- | Given the 'JWTConfig' (the user input of JWT configuration), create the 'JWTCtx' (the runtime JWT config used)
|
2018-10-25 21:16:25 +03:00
|
|
|
mkJwtCtx
|
2020-01-23 00:55:55 +03:00
|
|
|
:: ( HasVersion
|
|
|
|
, MonadIO m
|
2018-10-25 21:16:25 +03:00
|
|
|
, MonadError T.Text m
|
|
|
|
)
|
2019-07-11 08:37:06 +03:00
|
|
|
=> JWTConfig
|
2018-10-25 21:16:25 +03:00
|
|
|
-> H.Manager
|
2019-11-28 12:03:14 +03:00
|
|
|
-> Logger Hasura
|
2018-10-25 21:16:25 +03:00
|
|
|
-> m JWTCtx
|
2020-02-05 10:07:31 +03:00
|
|
|
mkJwtCtx JWTConfig{..} httpManager logger = do
|
|
|
|
jwkRef <- case jcKeyOrUrl of
|
2018-10-25 21:16:25 +03:00
|
|
|
Left jwk -> liftIO $ newIORef (JWKSet [jwk])
|
2020-02-05 10:07:31 +03:00
|
|
|
Right url -> getJwkFromUrl url
|
|
|
|
let claimsFmt = fromMaybe JCFJson jcClaimsFormat
|
|
|
|
return $ JWTCtx jwkRef jcClaimNs jcAudience claimsFmt jcIssuer
|
|
|
|
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
|
2018-10-25 21:16:25 +03:00
|
|
|
ref <- liftIO $ newIORef $ JWKSet []
|
2020-02-05 10:07:31 +03:00
|
|
|
maybeExpiry <- withJwkError $ updateJwkRef logger httpManager url ref
|
|
|
|
case maybeExpiry of
|
|
|
|
Nothing -> return ref
|
|
|
|
Just time -> do
|
2020-03-05 20:59:26 +03:00
|
|
|
void $ liftIO $ forkImmortal "jwkRefreshCtrl" logger $
|
|
|
|
jwkRefreshCtrl logger httpManager url ref (fromUnits time)
|
2018-10-25 21:16:25 +03:00
|
|
|
return ref
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-02-05 10:07:31 +03:00
|
|
|
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
|
|
|
|
|
2018-07-20 10:22:46 +03:00
|
|
|
getUserInfo
|
2020-01-23 00:55:55 +03:00
|
|
|
:: (HasVersion, MonadIO m, MonadError QErr 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
|
|
|
|
2019-05-14 09:24:46 +03:00
|
|
|
getUserInfoWithExpTime
|
2020-01-23 00:55:55 +03:00
|
|
|
:: (HasVersion, MonadIO m, MonadError QErr 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)
|
|
|
|
getUserInfoWithExpTime logger manager rawHeaders = \case
|
|
|
|
|
2020-04-24 12:10:53 +03:00
|
|
|
AMNoAuth -> (, Nothing) <$> userInfoFromHeaders UAuthNotSet
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
AMAdminSecret adminScrt unAuthRole ->
|
|
|
|
case adminSecretM of
|
2019-05-14 09:24:46 +03:00
|
|
|
Just givenAdminScrt ->
|
|
|
|
withNoExpTime $ userInfoWhenAdminSecret adminScrt givenAdminScrt
|
|
|
|
Nothing ->
|
|
|
|
withNoExpTime $ userInfoWhenNoAdminSecret unAuthRole
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
AMAdminSecretAndHook accKey hook ->
|
2019-05-14 09:24:46 +03:00
|
|
|
whenAdminSecretAbsent accKey $
|
2020-04-03 03:00:13 +03:00
|
|
|
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
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
AMAdminSecretAndJWT accKey jwtSecret unAuthRole ->
|
2020-04-03 03:00:13 +03:00
|
|
|
whenAdminSecretAbsent accKey $ processJwt jwtSecret rawHeaders unAuthRole
|
2018-07-20 10:22:46 +03:00
|
|
|
|
|
|
|
where
|
2019-02-14 12:37:47 +03:00
|
|
|
-- when admin secret is absent, run the action to retrieve UserInfo, otherwise
|
|
|
|
-- adminsecret override
|
|
|
|
whenAdminSecretAbsent ak action =
|
2019-05-14 09:24:46 +03:00
|
|
|
maybe action (withNoExpTime . userInfoWhenAdminSecret ak) adminSecretM
|
2019-02-14 12:37:47 +03:00
|
|
|
|
2019-05-14 09:24:46 +03:00
|
|
|
adminSecretM= foldl1 (<|>) $
|
2020-04-24 12:10:53 +03:00
|
|
|
map (`getSessionVariableValue` sessionVariables) [adminSecretHeader, deprecatedAccessKeyHeader]
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2020-04-24 12:10:53 +03:00
|
|
|
sessionVariables = mkSessionVariables rawHeaders
|
2018-07-20 10:22:46 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
userInfoWhenAdminSecret key reqKey = do
|
2019-05-14 09:24:46 +03:00
|
|
|
when (reqKey /= getAdminSecret key) $ throw401 $
|
|
|
|
"invalid " <> adminSecretHeader <> "/" <> deprecatedAccessKeyHeader
|
2020-04-24 12:10:53 +03:00
|
|
|
userInfoFromHeaders UAdminSecretSent
|
2018-10-25 21:16:25 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
userInfoWhenNoAdminSecret = \case
|
2019-05-14 09:24:46 +03:00
|
|
|
Nothing -> throw401 $ adminSecretHeader <> "/"
|
2020-03-20 09:46:45 +03:00
|
|
|
<> deprecatedAccessKeyHeader <> " required, but not found"
|
2020-04-24 12:10:53 +03:00
|
|
|
Just roleName -> mkUserInfo UAdminSecretNotSent sessionVariables $ Just roleName
|
2019-05-14 09:24:46 +03:00
|
|
|
|
|
|
|
withNoExpTime a = (, Nothing) <$> a
|
2019-11-26 15:14:21 +03:00
|
|
|
|
2020-04-24 12:10:53 +03:00
|
|
|
userInfoFromHeaders uas =
|
|
|
|
mkUserInfo uas sessionVariables $ Just adminRoleName
|