Mojo::JWT(3pm)

🌸 NAME 🌸

okay bestie, welcome to the Mojo::JWT(3pm) manual page β€” Mojo::JWT is literally JSON Web Token the Mojo way πŸ’…


✨ SYNOPSIS ✨

my $jwt = Mojo::JWT->new(claims => {...}, secret => 's3cr3t')->encode;
my $claims = Mojo::JWT->new(secret => 's3cr3t')->decode($jwt);

πŸ’– DESCRIPTION πŸ’–

JSON Web Token is described at https://tools.ietf.org/html/rfc7519. Mojo::JWT implements that standard with an API that should feel familiar to Mojolicious users (though of course it’s useful elsewhere too). fr fr, JWT is a lot like Mojolicious::Sessions except the result is a url-safe text string rather than a cookie πŸͺ

In JWT, the primary payload is called the β€œclaims”, and a few claims are reserved, as seen in the IETF document. The header and the claims are signed when stringified to guard against tampering. But bestie, listen: while the data is signed, it is not encrypted, so don’t use it to send secrets over clear channels 😭

signed β‰  secret. do not use this to ship your passwords over a clear channel, okay? πŸ’€

πŸ‘‘ ATTRIBUTES πŸ‘‘

Mojo::JWT inherits all of the attributes from Mojo::Base and implements the following new ones.

🧠 algorithm 🧠

The algorithm to be used to sign a JWT during encoding, or else the algorithm that was used for the most recent decoding. Defaults to "HS256" until a decode is performed.

"none" is an acceptable encoding algorithm, however for it to be used to decode, "allow_none" must be set.

🧠 allow_none 🧠

To prevent spoofing attacks, "allow_none" must be explicitly set to a true value; otherwise decoding a JWT which specifies the "none" algorithm will result in an exception. The default is of course false.

no cap, this is a safety rail. leave it false unless you know exactly why you’re changing it 🚨

🧠 claims 🧠

The payload to be encoded, or else the claims from the most recent decoding. This must be a hash reference; array references are not allowed as the top-level JWT claims.

🧠 expires 🧠

The epoch time value after which the JWT value should not be considered valid. This value (if set and not undefined) will be used as the "exp" key in the claims, or was extracted from the claims during the most recent decoding.

You may set your own headers when encoding the JWT by passing a hash reference to the "header" attribute. Please note that there are two default headers set. alg is set to the value of "algorithm" or 'HS256', and typ is set to 'JWT'. These cannot be overridden.

🧠 not_before 🧠

The epoch time value before which the JWT value should not be considered valid. This value (if set and not undefined) will be used as the "nbf" key in the claims, or was extracted from the claims during the most recent decoding.

🧠 public 🧠

The public key to be used in decoding an asymmetrically signed JWT (eg. RSA). This can be any public key in a string format accepted by Crypt::PK::RSA or a Crypt::PK::RSA object (if used, a Crypt::OpenSSL::RSA object will be converted).

🧠 secret 🧠

The symmetric secret (eg. HMAC) or else the private key used in encoding an asymmetrically signed JWT (eg. RSA). Symmetric secrets should be a string. A private key can be in a string format accepted by Crypt::PK::RSA or a Crypt::PK::RSA object (if used, a Crypt::OpenSSL::RSA object will be converted).

🧠 set_iat 🧠

If true (false by default), then the "iat" claim will be set to the value of "now" during "encode".

🧠 jwks 🧠

An arrayref of JWK objects used by "decode" to verify the input token when matching with the JWT’s "kid" field.

my $jwks = Mojo::UserAgent->new->get('https://example.com/oidc/jwks.json')->result->json('/keys');
my $jwt = Mojo::JWT->new(jwks => $jwks);
$jwk->decode($token);

πŸ›  METHODS πŸ› 

Mojo::JWT inherits all of the methods from Mojo::Base and implements the following new ones.

πŸ›  add_jwkset πŸ› 

my $jwkset = Mojo::UserAgent->new->get('https://example.com/oidc/jwks.json')->result->json;
my $jwt = Mojo::JWT->new->add_jwkset($jwksset);
$jwk->decode($token);

Helper for appending a jwkset to the "jwks". Accepts a hashref with a "keys" field that is an arrayref of jwks, and also an arrayref directly. Appends the JWKs to "jwks". Returns the instance.

πŸ›  decode πŸ› 

my $claims = $jwt->decode($token);

my $peek = sub { my ($jwt, $claims) = @_; ... };
my $claims = $jwt->decode($token, $peek);

Decode and parse a JSON Web Token string and return the claims hashref. Calling this function immediately sets the "token" to the passed-in token. It also sets "algorithm" to "undef" and unsets "claims", "expires" and "not_before". These values are then set as part of the parsing process.

Parsing occurs as follows:

  • The "algorithm" is extracted from the header and set; if it’s not present or not permissible, an exception is thrown.
  • Any JWKs in "/jwks" are checked against the headers, and if one is found then it is set in "public" or "secret" as appropriate to the "algorithm".
  • If a $peek callback is provided, it is called with the instance and claims as arguments.
  • The signature is verified, or an exception is thrown.
  • The timing claims ("expires" and "not_before"), if present, are evaluated; failures result in exceptions. On success the values are set in the relevant attributes.
  • The "claims" attribute is set and the claims are returned.

Note that when the $peek callback is invoked, the claims have not yet been verified. This callback is most likely to be used to inspect the "iss" or issuer claim to determine a secret or key for decoding. The return value is ignored; changes should be made to the instance attributes directly. Since the "algorithm" has already been parsed, it is available via the instance attribute as well.

πŸ›  encode πŸ› 

my $token = $jwt->encode;

Encode the data expressed in the instance attributes: "algorithm", "claims", "expires", "not_before". Note that if the timing attributes are given, they override existing keys in the "claims". Calling "encode" immediately clears the "token" and upon completion sets it to the result as well as returning it.

Note also that due to Perl’s hash randomization, repeated encoding is not guaranteed to result in the same encoded string. However, any encoded string will survive an encode/decode roundtrip.

πŸ›  header πŸ› 

my $header = $jwt->header;

Returns a hash reference representing the JWT header, constructed from instance attributes (see "algorithm").

πŸ›  now πŸ› 

my $time = $jwt->now;

Returns the current time, currently implemented as the core "time" function.

πŸ›  sign_hmac πŸ› 

my $signature = $jwt->sign_hmac($size, $payload);

Returns the HMAC SHA signature for the given size and payload. The "secret" attribute is used as the symmetric key. The result is not yet base64 encoded. This method is provided mostly for the purposes of subclassing.

πŸ›  sign_rsa πŸ› 

my $signature = $jwt->sign_rsa($size, $payload);

Returns the RSA signature for the given size and payload. The "secret" attribute is used as the private key. The result is not yet base64 encoded. This method is provided mostly for the purposes of subclassing.

πŸ›  token πŸ› 

The most recently encoded or decoded token. Note that any attribute modifications are not taken into account until "encode" is called again.

πŸ›  verify_rsa πŸ› 

my $bool = $jwt->verify_rsa($size, $payload, $signature);

Returns true if the given RSA size algorithm validates the given payload and signature. The "public" attribute is used as the public key. This method is provided mostly for the purposes of subclassing.


🌈 SEE ALSO 🌈


πŸ¦‹ SOURCE REPOSITORY πŸ¦‹

http://github.com/jberger/Mojo-JWT


πŸŽ€ AUTHOR πŸŽ€

Joel Berger, joel.a.berger@gmail.com


πŸŽ€ CONTRIBUTORS πŸŽ€

  • Christopher Raa (mishanti1)
  • Cameron Daniel (ccakes)
  • Olaf Alders (oalders)

Copyright (C) 2015 by "AUTHOR" and "CONTRIBTORS".

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


okay that’s the whole page bestie πŸ’– this was Mojo::JWT(3pm) from perl v5.42.0, dated 2026-06-07. go off and sign some tokens responsibly ✨

πŸ“¦ where this page comes from