Moodle PHP Documentation 4.3
Moodle 4.3.5 (Build: 20240610) (7dcfaa79f78)
JWT Class Reference

Static Public Member Functions

static constantTimeEquals (string $left, string $right)
 
static convertBase64UrlToBase64 (string $input)
 Convert a string in the base64url (URL-safe Base64) encoding to standard base64.
 
static static decode (string $jwt, $keyOrKeyArray, stdClass &$headers=null)
 Decodes a JWT string into a PHP object.
 
static encode (array $payload, $key, string $alg, string $keyId=null, array $head=null)
 Converts and signs a PHP array into a JWT string.
 
static jsonDecode (string $input)
 Decode a JSON string into a PHP object.
 
static jsonEncode (array $input)
 Encode a PHP array into a JSON string.
 
static sign (string $msg, $key, string $alg)
 Sign a string with a given key and algorithm.
 
static urlsafeB64Decode (string $input)
 Decode a string with URL-safe Base64.
 
static urlsafeB64Encode (string $input)
 Encode a string with URL-safe Base64.
 

Static Public Attributes

static int $leeway = 0
 When checking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
 
static int $timestamp = null
 Allow the current timestamp to be specified.
 
static array< string, $supported_algs=['ES384'=> ['openssl', 'SHA384']
 string[]>
 

Detailed Description

Member Function Documentation

◆ constantTimeEquals()

static JWT::constantTimeEquals ( string $left,
string $right )
static
Parameters
string$leftThe string of known length to compare against
string$rightThe user-supplied string
Return values
bool

◆ convertBase64UrlToBase64()

static JWT::convertBase64UrlToBase64 ( string $input)
static

Convert a string in the base64url (URL-safe Base64) encoding to standard base64.

Parameters
string$inputA Base64 encoded string with URL-safe characters (-_ and no padding)
Return values
stringA Base64 encoded string with standard characters (+/) and padding (=), when needed.
See also
https://www.rfc-editor.org/rfc/rfc4648

◆ decode()

static static JWT::decode ( string $jwt,
$keyOrKeyArray,
stdClass & $headers = null )
static

Decodes a JWT string into a PHP object.

Parameters
string$jwtThe JWT
Key|ArrayAccess<string,Key>|array<string,Key>$keyOrKeyArray The Key or associative array of key IDs (kid) to Key objects. If the algorithm used is asymmetric, this is the public key. Each Key object contains an algorithm and matching key. Supported algorithms are 'ES384','ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384' and 'RS512'.
stdClass$headersOptional. Populates stdClass with headers.
Return values
stdClassThe JWT's payload as a PHP object
Exceptions
InvalidArgumentExceptionProvided key/key-array was empty or malformed
DomainExceptionProvided JWT is malformed
UnexpectedValueExceptionProvided JWT was invalid
SignatureInvalidExceptionProvided JWT was invalid because the signature verification failed
BeforeValidExceptionProvided JWT is trying to be used before it's eligible as defined by 'nbf'
BeforeValidExceptionProvided JWT is trying to be used before it's been created as defined by 'iat'
ExpiredExceptionProvided JWT has since expired, as defined by the 'exp' claim

@uses jsonDecode @uses urlsafeB64Decode

◆ encode()

static JWT::encode ( array $payload,
$key,
string $alg,
string $keyId = null,
array $head = null )
static

Converts and signs a PHP array into a JWT string.

Parameters
array<mixed>$payload PHP array
string | resource | OpenSSLAsymmetricKey | OpenSSLCertificate$keyThe secret key.
string$algSupported algorithms are 'ES384','ES256', 'ES256K', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
string$keyId
array<string,string>$head An array with header elements to attach
Return values
stringA signed JWT

@uses jsonEncode @uses urlsafeB64Encode

◆ jsonDecode()

static JWT::jsonDecode ( string $input)
static

Decode a JSON string into a PHP object.

Parameters
string$inputJSON string
Return values
mixedThe decoded JSON string
Exceptions
DomainExceptionProvided string was invalid JSON

◆ jsonEncode()

static JWT::jsonEncode ( array $input)
static

Encode a PHP array into a JSON string.

Parameters
array<mixed>$input A PHP array
Return values
stringJSON representation of the PHP array
Exceptions
DomainExceptionProvided object could not be encoded to valid JSON

◆ sign()

static JWT::sign ( string $msg,
$key,
string $alg )
static

Sign a string with a given key and algorithm.

Parameters
string$msgThe message to sign
string | resource | OpenSSLAsymmetricKey | OpenSSLCertificate$keyThe secret key.
string$algSupported algorithms are 'EdDSA', 'ES384', 'ES256', 'ES256K', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
Return values
stringAn encrypted message
Exceptions
DomainExceptionUnsupported algorithm or bad key was specified

◆ urlsafeB64Decode()

static JWT::urlsafeB64Decode ( string $input)
static

Decode a string with URL-safe Base64.

Parameters
string$inputA Base64 encoded string
Return values
stringA decoded string
Exceptions
InvalidArgumentExceptioninvalid base64 characters

◆ urlsafeB64Encode()

static JWT::urlsafeB64Encode ( string $input)
static

Encode a string with URL-safe Base64.

Parameters
string$inputThe string you want encoded
Return values
stringThe base64 encode of what you passed in

Member Data Documentation

◆ $timestamp

int JWT::$timestamp = null
static

Allow the current timestamp to be specified.

Useful for fixing a value within unit testing. Will default to PHP time() value if null.


The documentation for this class was generated from the following file: