HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulejose-0.11Haskell2010

Crypto.JWT

JSON Web Token implementation (RFC 7519). A JWT is a JWS with a payload of claims to be transferred between two parties.

JWTs use the JWS compact serialisation. See Crypto.JOSE.Compact for details.

  • 8 types
  • 7 classes
  • 14 values
  • Packagejose-0.11
  • Exports29
  • LanguageHaskell2010
  • LicenceApache-2.0
  • SourceJWT.hs

Overview / HOWTO

0 declarations

Basic usage

import Crypto.JWT

mkClaims :: IO ClaimsSet
mkClaims = do
  t <- currentTime
  pure $ emptyClaimsSet
    & claimIss ?~ "alice"
    & claimAud ?~ Audience ["bob"]
    & claimIat ?~ NumericDate t

doJwtSign :: JWK -> ClaimsSet -> IO (Either JWTError SignedJWT)
doJwtSign jwk claims = runJOSE $ do
  alg <- bestJWSAlg jwk
  signClaims jwk (newJWSHeader ((), alg)) claims

doJwtVerify :: JWK -> SignedJWT -> IO (Either JWTError ClaimsSet)
doJwtVerify jwk jwt = runJOSE $ do
  let config = defaultJWTValidationSettings (== "bob")
  verifyClaims config jwk jwt

Some JWT libraries have a function that takes two strings: the "secret" (a symmetric key) and the raw JWT. The following function achieves the same:

verify :: L.ByteString -> L.ByteString -> IO (Either JWTError ClaimsSet)
verify k s = runJOSE $ do
  let
    k' = fromOctets k      -- turn raw secret into symmetric JWK
    audCheck = const True  -- should be a proper audience check
  jwt <- decodeCompact s    -- decode JWT
  verifyClaims (defaultJWTValidationSettings audCheck) k' jwt

Supporting additional claims via subtypes

For applications that use additional claims, define a data type that wraps ClaimsSet and includes fields for the additional claims. You will also need to define FromJSON if verifying JWTs, and ToJSON if producing JWTs. The following example is taken from RFC 7519 §3.1.

import qualified Data.Aeson.KeyMap as M

data Super = Super { jwtClaims :: ClaimsSet, isRoot :: Bool }

instance HasClaimsSet Super where
  claimsSet f s = fmap (\a' -> s { jwtClaims = a' }) (f (jwtClaims s))

instance FromJSON Super where
  parseJSON = withObject "Super" $ \o -> Super
    <$> parseJSON (Object o)
    <*> o .: "http://example.com/is_root"

instance ToJSON Super where
  toJSON s =
    ins "http://example.com/is_root" (isRoot s) (toJSON (jwtClaims s))
    where
      ins k v (Object o) = Object $ M.insert k (toJSON v) o
      ins _ _ a = a

Use signJWT and verifyJWT when using custom payload types (instead of signClaims and verifyClaims which are specialised to ClaimsSet).

API

0 declarations

Creating a JWT

Validating a JWT and extracting claims

Cryptographically verify a JWS JWT, then validate the Claims Set, returning it if valid. The claims are validated at the current system time.

This is the only way to get at the claims of a JWS JWT, enforcing that the claims are cryptographically and semantically valid before the application can use them.

This function is abstracted over any payload type with HasClaimsSet and FromJSON instances. The verifyClaims variant uses ClaimsSet as the payload type.

See also verifyClaimsAt which allows you to explicitly specify the time of validation (against which time-related claims will be validated).

classclass HasJWTValidationSettings c where
#
Instances1HasJWTValidationSettings

Specifying the verification time

Claims Set

datadata ClaimsSet
#

The JWT Claims Set represents a JSON object whose members are the registered claims defined by RFC 7519. To construct a ClaimsSet use emptyClaimsSet then use the lenses defined in HasClaimsSet to set relevant claims.

For applications that use additional claims beyond those defined by RFC 7519, define a subtype and instance HasClaimsSet.

Instances5Eq, Show, FromJSON, ToJSON, HasClaimsSet
classclass HasClaimsSet a where
#

Methods

  • claimsSet :: Lens' a ClaimsSet
  • claimIss :: Lens' a (Maybe StringOrURI)

    The issuer claim identifies the principal that issued the JWT. The processing of this claim is generally application specific.

  • claimSub :: Lens' a (Maybe StringOrURI)

    The subject claim identifies the principal that is the subject of the JWT. The Claims in a JWT are normally statements about the subject. The subject value MAY be scoped to be locally unique in the context of the issuer or MAY be globally unique. The processing of this claim is generally application specific.

  • claimAud :: Lens' a (Maybe Audience)

    The audience claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the aud claim when this claim is present, then the JWT MUST be rejected.

  • claimExp :: Lens' a (Maybe NumericDate)

    The expiration time claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of exp claim requires that the current date/time MUST be before expiration date/time listed in the exp claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew.

  • claimNbf :: Lens' a (Maybe NumericDate)

    The not before claim identifies the time before which the JWT MUST NOT be accepted for processing. The processing of the nbf claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the nbf claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew.

  • claimIat :: Lens' a (Maybe NumericDate)

    The issued at claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT.

  • claimJti :: Lens' a (Maybe Text)

    The JWT ID claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object. The jti claim can be used to prevent the JWT from being replayed. The jti value is a case-sensitive string.

Instances1HasClaimsSet

Unregistered claims (deprecated)

JWT errors

Miscellaneous types

newtypenewtype Audience
#

Audience data. In the general case, the aud value is an array of case-sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the aud value MAY be a single case-sensitive string containing a StringOrURI value.

The ToJSON instance formats an Audience with one value as a string (some non-compliant implementations require this.)

Constructors

Instances4Eq, Show, FromJSON, ToJSON
datadata StringOrURI
#

A JSON string value, with the additional requirement that while arbitrary string values MAY be used, any value containing a : character MUST be a URI.

Note: the IsString instance will fail if the string contains a : but does not parse as a URI. Use stringOrUri directly in this situation.

Instances5Eq, Show, IsString, FromJSON, ToJSON

Re-exports

Orphan instances

1 instance