HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulejose-0.13GHC2021

Crypto.JOSE.JWS

JSON Web Signature (JWS) represents content secured with digital signatures or Message Authentication Codes (MACs) using JavaScript Object Notation (JSON) based data structures. It is defined in RFC 7515.

import Crypto.JOSE

doJwsSign :: JWK -> L.ByteString -> IO (Either Error (GeneralJWS JWSHeader))
doJwsSign jwk payload = runJOSE $ do
  alg <- bestJWSAlg jwk
  signJWS payload [(newJWSHeader (Protected, alg), jwk)]

doJwsVerify :: JWK -> GeneralJWS JWSHeader -> IO (Either Error ())
doJwsVerify jwk jws = runJOSE $
  verifyJWS' jwk jws
  • 9 types
  • 4 classes
  • 13 values
  • Packagejose-0.13
  • Exports26
  • LanguageGHC2021
  • LicenceApache-2.0
  • SourceJWS.hs

Overview

4 declarations
datadata JWS (t :: Type -> Type) (p :: k) (a :: k -> Type)
#

JSON Web Signature data type.

Parameterised by the signature container type, the header ProtectionSupport type, and the header record type.

Use encode and decode to convert a JWS to or from JSON. When encoding a JWS [] with exactly one signature, the flattened JWS JSON serialisation syntax is used, otherwise the general JWS JSON serialisation is used. When decoding a JWS [] either serialisation is accepted.

JWS Identity uses the flattened JSON serialisation or the JWS compact serialisation (see decodeCompact and encodeCompact).

Use signJWS to create a signed/MACed JWS.

Instances8Eq, Show, FromJSON, ToJSON, FromCompact, ToCompact, …

Defining additional header parameters

Several specifications extend JWS with additional header parameters. The JWS type is parameterised over the header type; this library provides the JWSHeader type which encompasses all the JWS header parameters defined in RFC 7515. To define an extended header type declare the data type, and instances for HasJWSHeader and HasParams. For example:

data ACMEHeader p = ACMEHeader
  { _acmeJwsHeader :: JWSHeader p
  , _acmeNonce :: Base64Octets
  }

acmeJwsHeader :: Lens' (ACMEHeader p) (JWSHeader p)
acmeJwsHeader f s@(ACMEHeader { _acmeJwsHeader = a}) =
  fmap (\a' -> s { _acmeJwsHeader = a'}) (f a)

acmeNonce :: Lens' (ACMEHeader p) Types.Base64Octets
acmeNonce f s@(ACMEHeader { _acmeNonce = a}) =
  fmap (\a' -> s { _acmeNonce = a'}) (f a)

instance HasJWSHeader ACMEHeader where
  jwsHeader = acmeJwsHeader

instance HasParams ACMEHeader where
  parseParamsFor proxy hp hu = ACMEHeader
    <$> parseParamsFor proxy hp hu
    <*> headerRequiredProtected "nonce" hp hu
  params h =
    (True, "nonce" .= view acmeNonce h)
    : params (view acmeJwsHeader h)
  extensions = const ["nonce"]

See also:

JWS creation

4 declarations
valuenewJWSHeader :: (p, Alg) -> JWSHeader p
#

Construct a minimal header with the given algorithm and protection value for the "alg" header.

JWS verification

3 declarations
valueverifyJWS
  1. :: (HasAlgorithms a, HasValidationPolicy a, AsError e, MonadError e m, HasJWSHeader h, HasParams h, VerificationKeyStore m (h p) s k, Cons s s Word8 Word8, AsEmpty s, Foldable t, ProtectionSupport p)
  2. => a

    validation settings

  3. -> k

    key or key store

  4. -> JWS t p h

    JWS

  5. -> m s
#

Verify a JWS.

Signatures made with an unsupported algorithms are ignored. If the validation policy is AnyValidated, a single successfully validated signature is sufficient. If the validation policy is AllValidated then all remaining signatures (there must be at least one) must be valid.

Returns the payload if successfully verified.

JWS validation settings

datadata ValidationPolicy
#

Validation policy.

Constructors

  • AnyValidated

    One successfully validated signature is sufficient

  • AllValidated

    All signatures in all configured algorithms must be validated. No signatures in configured algorithms is also an error.

Instances1Eq

Access payload without verification

1 declaration

Signature data

5 declarations
datadata Signature (p :: k) (a :: k -> Type)
#

Signature object containing header, and signature bytes.

If it was decoded from a serialised JWS, it "remembers" how the protected header was encoded; the remembered value is used when computing the signing input and when serialising the object.

The remembered value is not used in equality checks, i.e. two decoded signatures with differently serialised by otherwise equal protected headers, and equal signature bytes, are equal.

Instances4Eq, Show, FromJSON, ToJSON

Return the raw base64url-encoded protected header value. If the Signature was decoded from JSON, this returns the original string value as-is.

Application code should never need to use this. It is exposed for testing purposes.

JWS headers

3 declarations

Orphan instances

11 instances