graphql-engine-1.0.0: GraphQL API over Postgres
CopyrightHasura
Safe HaskellNone
LanguageHaskell2010

Hasura.Server.Auth.JWT

Description

This module implements the bulk of Hasura's JWT capabilities and interactions. Its main point of non-testing invocation is Auth.

It exports both processJwt and processJwt_ with processJwt_ being the majority of the implementation with the JWT Token processing function passed in as an argument in order to enable mocking in test-code.

In processJwt_, prior to validation of the token, first the token locations and issuers are reconciled. Locations are either specified as auth or cookie (with cookie name) or assumed to be auth. Issuers can be omitted or specified, where an omitted configured issuer can match any issuer specified by a request.

If none match, then this is considered an no-auth request, if one matches, then normal token auth is performed, and if multiple match, then this is considered an ambiguity error.

Synopsis

Documentation

newtype RawJWT Source #

Constructors

RawJWT ByteString 

data JWTClaimsFormat Source #

Instances

Instances details
Eq JWTClaimsFormat Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show JWTClaimsFormat Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

FromJSON JWTClaimsFormat Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

parseJSON :: Value -> Parser JWTClaimsFormat

parseJSONList :: Value -> Parser [JWTClaimsFormat]

ToJSON JWTClaimsFormat Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSON :: JWTClaimsFormat -> Value

toEncoding :: JWTClaimsFormat -> Encoding

toJSONList :: [JWTClaimsFormat] -> Value

toEncodingList :: [JWTClaimsFormat] -> Encoding

data JWTHeader Source #

Constructors

JHAuthorization 
JHCookie Text 

Instances

Instances details
Eq JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Generic JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Associated Types

type Rep JWTHeader :: Type -> Type #

Hashable JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

FromJSON JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

parseJSON :: Value -> Parser JWTHeader

parseJSONList :: Value -> Parser [JWTHeader]

ToJSON JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSON :: JWTHeader -> Value

toEncoding :: JWTHeader -> Encoding

toJSONList :: [JWTHeader] -> Value

toEncodingList :: [JWTHeader] -> Encoding

type Rep JWTHeader Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

type Rep JWTHeader = D1 ('MetaData "JWTHeader" "Hasura.Server.Auth.JWT" "graphql-engine-1.0.0-inplace" 'False) (C1 ('MetaCons "JHAuthorization" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "JHCookie" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)))

data JWTCustomClaimsMapValueG v Source #

JWTCustomClaimsMapValueG is used to represent a single value of the JWTCustomClaimsMap. A JWTCustomClaimsMapValueG can either be an JSON object or the literal value of the claim. If the value is an JSON object, then it should contain a key path, which is the JSON path to the claim value in the JWT token. There's also an option to specify a default value in the map via the 'default' key, which will be used when a peek at the JWT token using the JSON path fails (key does not exist).

Constructors

JWTCustomClaimsMapJSONPath !JSONPath !(Maybe v)

JSONPath to the key in the claims map, in case the key doesn't exist in the claims map then the default value will be used (if provided)

JWTCustomClaimsMapStatic !v 

Instances

Instances details
Functor JWTCustomClaimsMapValueG Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Foldable JWTCustomClaimsMapValueG Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

fold :: Monoid m => JWTCustomClaimsMapValueG m -> m #

foldMap :: Monoid m => (a -> m) -> JWTCustomClaimsMapValueG a -> m #

foldMap' :: Monoid m => (a -> m) -> JWTCustomClaimsMapValueG a -> m #

foldr :: (a -> b -> b) -> b -> JWTCustomClaimsMapValueG a -> b #

foldr' :: (a -> b -> b) -> b -> JWTCustomClaimsMapValueG a -> b #

foldl :: (b -> a -> b) -> b -> JWTCustomClaimsMapValueG a -> b #

foldl' :: (b -> a -> b) -> b -> JWTCustomClaimsMapValueG a -> b #

foldr1 :: (a -> a -> a) -> JWTCustomClaimsMapValueG a -> a #

foldl1 :: (a -> a -> a) -> JWTCustomClaimsMapValueG a -> a #

toList :: JWTCustomClaimsMapValueG a -> [a] #

null :: JWTCustomClaimsMapValueG a -> Bool #

length :: JWTCustomClaimsMapValueG a -> Int #

elem :: Eq a => a -> JWTCustomClaimsMapValueG a -> Bool #

maximum :: Ord a => JWTCustomClaimsMapValueG a -> a #

minimum :: Ord a => JWTCustomClaimsMapValueG a -> a #

sum :: Num a => JWTCustomClaimsMapValueG a -> a #

product :: Num a => JWTCustomClaimsMapValueG a -> a #

Traversable JWTCustomClaimsMapValueG Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Eq v => Eq (JWTCustomClaimsMapValueG v) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show v => Show (JWTCustomClaimsMapValueG v) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

FromJSON v => FromJSON (JWTCustomClaimsMapValueG v) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

parseJSON :: Value -> Parser (JWTCustomClaimsMapValueG v)

parseJSONList :: Value -> Parser [JWTCustomClaimsMapValueG v]

ToJSON v => ToJSON (JWTCustomClaimsMapValueG v) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

data JWTCustomClaimsMap Source #

JWTClaimsMap is an option to provide a custom JWT claims map. The JWTClaimsMap should be specified in the HASURA_GRAPHQL_JWT_SECRET in the claims_map. The JWTClaimsMap, if specified, requires two mandatory fields, namely, `x-hasura-allowed-roles` and the `x-hasura-default-role`, other claims may also be provided in the claims map.

data JWTNamespace Source #

JWTNamespace is used to locate the claims map within the JWT token. The location can be either provided via a JSON path or the name of the key in the JWT token.

Constructors

ClaimNsPath JSONPath 
ClaimNs Text 

Instances

Instances details
Eq JWTNamespace Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show JWTNamespace Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

ToJSON JWTNamespace Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSON :: JWTNamespace -> Value

toEncoding :: JWTNamespace -> Encoding

toJSONList :: [JWTNamespace] -> Value

toEncodingList :: [JWTNamespace] -> Encoding

data JWTClaims Source #

Instances

Instances details
Eq JWTClaims Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show JWTClaims Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

newtype StringOrURI Source #

Hashable Wrapper for constructing a HashMap of JWTConfigs

Constructors

StringOrURI 

Fields

Instances

Instances details
Eq StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Hashable StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

FromJSONKey StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

fromJSONKey :: FromJSONKeyFunction StringOrURI

fromJSONKeyList :: FromJSONKeyFunction [StringOrURI]

FromJSON StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

parseJSON :: Value -> Parser StringOrURI

parseJSONList :: Value -> Parser [StringOrURI]

ToJSONKey StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSONKey :: ToJSONKeyFunction StringOrURI

toJSONKeyList :: ToJSONKeyFunction [StringOrURI]

ToJSON StringOrURI Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSON :: StringOrURI -> Value

toEncoding :: StringOrURI -> Encoding

toJSONList :: [StringOrURI] -> Value

toEncodingList :: [StringOrURI] -> Encoding

FromJSONKey (Maybe StringOrURI) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

fromJSONKey :: FromJSONKeyFunction (Maybe StringOrURI)

fromJSONKeyList :: FromJSONKeyFunction [Maybe StringOrURI]

ToJSONKey (Maybe StringOrURI) Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSONKey :: ToJSONKeyFunction (Maybe StringOrURI)

toJSONKeyList :: ToJSONKeyFunction [Maybe StringOrURI]

data JWTConfig Source #

The JWT configuration we got from the user.

Constructors

JWTConfig 

Fields

Instances

Instances details
Eq JWTConfig Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Show JWTConfig Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

FromJSON JWTConfig Source #

Parse from a json string like: | `{"type": RS256, "key": "PEM-encoded-public-key-or-X509-cert"}` | to JWTConfig

Instance details

Defined in Hasura.Server.Auth.JWT

Methods

parseJSON :: Value -> Parser JWTConfig

parseJSONList :: Value -> Parser [JWTConfig]

ToJSON JWTConfig Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

toJSON :: JWTConfig -> Value

toEncoding :: JWTConfig -> Encoding

toJSONList :: [JWTConfig] -> Value

toEncodingList :: [JWTConfig] -> Encoding

FromEnv JWTConfig Source # 
Instance details

Defined in Hasura.Server.Init.Env

FromEnv [JWTConfig] Source # 
Instance details

Defined in Hasura.Server.Init.Env

data JWTCtx Source #

The validated runtime JWT configuration returned by mkJwtCtx in setupAuthMode.

This is also evidence that the jwkRefreshCtrl thread is running, if an expiration schedule could be determined.

Constructors

JWTCtx 

Fields

Instances

Instances details
Eq JWTCtx Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

Methods

(==) :: JWTCtx -> JWTCtx -> Bool #

(/=) :: JWTCtx -> JWTCtx -> Bool #

Show JWTCtx Source # 
Instance details

Defined in Hasura.Server.Auth.JWT

jwkRefreshCtrl :: (MonadIO m, MonadBaseControl IO m, HasReporter m) => Logger Hasura -> Manager -> URI -> IORef JWKSet -> DiffTime -> m void Source #

An action that refreshes the JWK at intervals in an infinite loop.

updateJwkRef :: (MonadIO m, MonadBaseControl IO m, MonadError JwkFetchError m, MonadTrace m) => Logger Hasura -> Manager -> URI -> IORef JWKSet -> m (Maybe NominalDiffTime) Source #

Given a JWK url, fetch JWK from it and update the IORef

determineJwkExpiryLifetime :: forall m. (MonadIO m, MonadError JwkFetchError m) => m UTCTime -> Logger Hasura -> ResponseHeaders -> m (Maybe NominalDiffTime) Source #

First check for Cache-Control header, if not found, look for Expires header

type ClaimsMap = HashMap SessionVariable Value Source #

decodeClaimsSet :: RawJWT -> Maybe ClaimsSet Source #

Decode a Jose ClaimsSet without verifying the signature

tokenIssuer :: RawJWT -> Maybe StringOrURI Source #

Extract the issuer from a bearer tokena _without_ verifying it.

processJwt :: (MonadIO m, MonadError QErr m) => [JWTCtx] -> RequestHeaders -> Maybe RoleName -> m (UserInfo, Maybe UTCTime, [Header]) Source #

Process the request headers to verify the JWT and extract UserInfo from it From the JWT config, we check which header to expect, it can be the Authorization or Cookie header

Iff no Authorization/Cookie header was passed, we will fall back to the unauthenticated user role [1], if one was configured at server start.

When no 'x-hasura-user-role' is specified in the request, the mandatory 'x-hasura-default-role' [2] from the JWT claims will be used.

processJwt_ Source #

Arguments

:: MonadError QErr m 
=> (JWTCtx -> ByteString -> m (ClaimsMap, Maybe UTCTime))

mock processAuthZOrCookieHeader

-> (RawJWT -> Maybe StringOrURI) 
-> (JWTCtx -> JWTHeader) 
-> [JWTCtx] 
-> RequestHeaders 
-> Maybe RoleName 
-> m (UserInfo, Maybe UTCTime, [Header]) 

processHeaderSimple :: (MonadIO m, MonadError QErr m) => JWTCtx -> ByteString -> m (ClaimsMap, Maybe UTCTime) Source #

Processes a token payload (excluding the `Bearer ` prefix in the context of a JWTCtx)

parseClaimsMap Source #

Arguments

:: MonadError QErr m 
=> ClaimsSet

Unregistered JWT claims

-> JWTClaims

Claims config

-> m ClaimsMap

Hasura claims and other claims

parse the claims map from the JWT token or custom claims from the JWT config

verifyJwt :: (MonadError JWTError m, MonadIO m) => JWTCtx -> RawJWT -> m ClaimsSet Source #

Verify the JWT against given JWK

parseJwtClaim :: (FromJSON a, MonadError QErr m) => Value -> Text -> m a Source #