Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

(no description)

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

Defines 1 class

JWT:: (14 methods):
  decode()
  encode()
  sign()
  verify()
  jsonDecode()
  jsonEncode()
  urlsafeB64Decode()
  urlsafeB64Encode()
  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, $key, array $allowed_algs = array()   X-Ref
Decodes a JWT string into a PHP object.

param: string                    $jwt            The JWT
param: string|array|resource     $key            The key, or map of keys.
param: array                     $allowed_algs   List of supported verification algorithms
return: object The JWT's payload as a PHP object

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

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

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

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

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.

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
return: bool

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

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

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

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

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

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

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

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

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

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

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

param: string $str
return: int

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

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

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

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

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

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

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

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