NAME

Crypt::JWT - JSON Web Token (JWT, JWS, JWE) as defined by RFC7519, RFC7515, RFC7516

SYNOPSIS

# encoding
use Crypt::JWT qw(encode_jwt);
my $jws_token = encode_jwt(payload=>$data, alg=>'HS256', key=>'secret');
my $jwe_token = encode_jwt(payload=>$data, alg=>'PBES2-HS256+A128KW', enc=>'A128GCM', key=>'secret');

# decoding
use Crypt::JWT qw(decode_jwt);
my $data1 = decode_jwt(token=>$jws_token, key=>'secret');
my $data2 = decode_jwt(token=>$jwe_token, key=>'secret');

DESCRIPTION

Implements JSON Web Token (JWT) - https://tools.ietf.org/html/rfc7519. The implementation covers not only JSON Web Signature (JWS) - https://tools.ietf.org/html/rfc7515, but also JSON Web Encryption (JWE) - https://tools.ietf.org/html/rfc7516.

The module implements all algorithms defined in https://tools.ietf.org/html/rfc7518 - JSON Web Algorithms (JWA).

This module supports Compact JWS/JWE and Flattened JWS/JWE JSON serialization. General (multi-recipient) JSON serialization is not supported.

EXPORT

Nothing is exported by default.

You can export selected functions:

use Crypt::JWT qw(decode_jwt encode_jwt);

Or all of them at once:

use Crypt::JWT ':all';

FUNCTIONS

decode_jwt

my $data              = decode_jwt(%named_args);
my ($header, $data)   = decode_jwt(%named_args, decode_header=>1);

Returns the decoded payload (in scalar context) or the decoded header followed by the decoded payload (when decode_header => 1). Croaks on any verification, decryption, or claim-check failure.

Named arguments:

encode_jwt

my $token = encode_jwt(%named_args);

Returns the encoded JWT as a string - either compact serialization (the default; three or five .-separated segments) or flattened JSON serialization (when serialization => 'flattened'; a JSON object). Croaks on bad arguments or unsupported algorithm combinations.

Named arguments:

SECURITY CONSIDERATIONS

Configuration knobs

The library exposes four tunable package variables. Set them once at program startup (typically in a BEGIN block) before any encode_jwt/decode_jwt call.

Key-strength minimums

The library enforces the following minimums; tokens that try to sign or verify with weaker keys are rejected with a croak. Both knobs are package variables and can be tuned at startup if a deployer has a stricter or looser policy.

SEE ALSO

Crypt::Cipher::AES, Crypt::AuthEnc::GCM, Crypt::PK::RSA, Crypt::PK::ECC, Crypt::KeyDerivation, Crypt::KeyWrap

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

COPYRIGHT

Copyright (c) 2015-2026 DCIT, a.s. https://www.dcit.cz / Karel Miko