Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

(no description)

File Size: 600 lines (22 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

JWT:: (16 methods):
  decode()
  encode()
  sign()
  verify()
  jsonDecode()
  jsonEncode()
  urlsafeB64Decode()
  urlsafeB64Encode()
  getKey()
  constantTimeEquals()
  handleJsonError()
  safeStrlen()
  signatureToDER()
  encodeDER()
  signatureFromDER()
  readDER()


Class: JWT  - X-Ref

JSON Web Token implementation, based on this spec:
https://tools.ietf.org/html/rfc7519

PHP version 5

decode($jwt, $keyOrKeyArray)   X-Ref
Decodes a JWT string into a PHP object.

return: object The JWT's payload as a PHP object
param: string                    $jwt            The JWT
param: Key|array<string, Key>    $keyOrKeyArray  The Key or associative array of key IDs (kid) to Key objects.

encode($payload, $key, $alg, $keyId = null, $head = null)   X-Ref
Converts and signs a PHP object or array into a JWT string.

return: string A signed JWT
param: object|array      $payload    PHP object or array
param: string|resource   $key        The secret key.
param: string            $alg        The signing algorithm.
param: mixed             $keyId
param: array             $head       An array with header elements to attach

sign($msg, $key, $alg)   X-Ref
Sign a string with a given key and algorithm.

return: string An encrypted message
param: string            $msg    The message to sign
param: string|resource   $key    The secret key
param: string            $alg    The signing algorithm.

verify($msg, $signature, $key, $alg)   X-Ref
Verify a signature with the message, key and method. Not all methods
are symmetric, so we must have a separate verify and sign method.

return: bool
param: string            $msg        The original message (header and body)
param: string            $signature  The original signature
param: string|resource   $key        For HS*, a string key works. for RS*, must be a resource of an openssl public key
param: string            $alg        The algorithm

jsonDecode($input)   X-Ref
Decode a JSON string into a PHP object.

return: object Object representation of JSON string
param: string $input JSON string

jsonEncode($input)   X-Ref
Encode a PHP object into a JSON string.

return: string JSON representation of the PHP object or array
param: object|array $input A PHP object or array

urlsafeB64Decode($input)   X-Ref
Decode a string with URL-safe Base64.

return: string A decoded string
param: string $input A Base64 encoded string

urlsafeB64Encode($input)   X-Ref
Encode a string with URL-safe Base64.

return: string The base64 encode of what you passed in
param: string $input The string you want encoded

getKey($keyOrKeyArray, $kid = null)   X-Ref
Determine if an algorithm has been provided for each Key

return: array containing the keyMaterial and algorithm
param: Key|array<string, Key> $keyOrKeyArray
param: string|null            $kid

constantTimeEquals($left, $right)   X-Ref

return: bool
param: string $left
param: string $right

handleJsonError($errno)   X-Ref
Helper method to create a JSON error.

return: void
param: int $errno An error number from json_last_error()

safeStrlen($str)   X-Ref
Get the number of bytes in cryptographic strings.

return: int
param: string $str

signatureToDER($sig)   X-Ref
Convert an ECDSA signature to an ASN.1 DER sequence

return: string The encoded DER object
param: string $sig The ECDSA signature to convert

encodeDER($type, $value)   X-Ref
Encodes a value into a DER object.

return: string  the encoded object
param: int     $type DER tag
param: string  $value the value to encode

signatureFromDER($der, $keySize)   X-Ref
Encodes signature from a DER object.

return: string  the signature
param: string  $der binary signature in DER format
param: int     $keySize the number of bits in the key

readDER($der, $offset = 0)   X-Ref
Reads binary DER-encoded data and decodes into a single object

return: array [$offset, $data] the new offset and the decoded object
param: string $der the binary data in DER format
param: int $offset the offset of the data stream containing the object