NAME

Twilio::Jwt::JWT - Encode, decode and verify JSON Web Tokens

SYNOPSIS

use Twilio::Jwt::JWT;

my $token   = Twilio::Jwt::JWT->encode({ sub => 'me' }, $secret);
my $payload = Twilio::Jwt::JWT->decode($token, $secret);

DESCRIPTION

The signing underneath Twilio::Jwt::AccessToken. Most code does not need it directly, except to read a token in a test.

Tokens are signed with HS256, HS384 or HS512 and a shared secret, or with RS256 and an RSA key in PEM form: the private key to sign, and the public one to verify. Everything else, including none, is refused, and so is a key in PEM form given as the secret of an HS method. These methods throw Twilio::Exceptions::TwilioException on any problem.

RS256 needs Crypt::OpenSSL::RSA, which is optional and loaded only then: version 0.38 or later, or one before 0.35, since 0.35 to 0.37 refuse the padding that RS256 uses. Twilio uses RS256 for client validation; see Twilio::Http::ValidationClient.

METHODS

encode

my $token = Twilio::Jwt::JWT->encode($payload, $key, $algorithm, \%headers);

Returns a signed token. $algorithm defaults to HS256. typ and alg are always set here, whatever \%headers says.

decode

my $payload = Twilio::Jwt::JWT->decode($token, $key);

Returns the payload after checking the signature: in constant time for the HS methods, and with the public key for RS256. Passing a false third argument skips that check, so only do it for a token you do not rely on.

get_header

my $header = Twilio::Jwt::JWT->get_header($token);

Returns the header. It does not check the signature.

sign

my $signature = Twilio::Jwt::JWT->sign($message, $key, $algorithm);

Returns the raw signature of a message.

urlsafe_b64_encode

urlsafe_b64_decode

Convert to and from the URL-safe Base64 that tokens use, with no padding.

LICENSE

Copyright (c) 2026 Francisco Zarabozo. Released under the MIT License.