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.
π§ header π§
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
$peekcallback 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 π
Acme::JWTJSON::WebTokenhttp://jwt.io/
π¦ 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 AND LICENSE π
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
- opensuse-leap 16.0: perl-Mojo-JWT 1.20.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms