|
static array static | decode (string $jwt, $keyOrKeyArray, stdClass &$headers=null) |
| Decodes a JWT string into a PHP object. More...
|
|
static | encode (array $payload, $key, string $alg, string $keyId=null, array $head=null) |
| Converts and signs a PHP array into a JWT string. More...
|
|
static | sign (string $msg, $key, string $alg) |
| Sign a string with a given key and algorithm. More...
|
|
static | jsonDecode (string $input) |
| Decode a JSON string into a PHP object. More...
|
|
static | jsonEncode (array $input) |
| Encode a PHP array into a JSON string. More...
|
|
static | urlsafeB64Decode (string $input) |
| Decode a string with URL-safe Base64. More...
|
|
static | convertBase64UrlToBase64 (string $input) |
| Convert a string in the base64url (URL-safe Base64) encoding to standard base64. More...
|
|
static | urlsafeB64Encode (string $input) |
| Encode a string with URL-safe Base64. More...
|
|
static | constantTimeEquals (string $left, string $right) |
|
|
static | verify (string $msg, string $signature, $keyMaterial, string $alg) |
| Verify a signature with the message, key and method. More...
|
|
static | getKey ( $keyOrKeyArray, ?string $kid) |
| Determine if an algorithm has been provided for each Key. More...
|
|
static | handleJsonError (int $errno) |
| Helper method to create a JSON error. More...
|
|
static | safeStrlen (string $str) |
| Get the number of bytes in cryptographic strings. More...
|
|
static | signatureToDER (string $sig) |
| Convert an ECDSA signature to an ASN.1 DER sequence. More...
|
|
static | encodeDER (int $type, string $value) |
| Encodes a value into a DER object. More...
|
|
static | signatureFromDER (string $der, int $keySize) |
| Encodes signature from a DER object. More...
|
|
static | readDER (string $der, int $offset=0) |
| Reads binary DER-encoded data and decodes into a single object. More...
|
|
Definition at line 28 of file JWT.php.
◆ constantTimeEquals()
static Firebase\JWT\JWT::constantTimeEquals |
( |
string |
$left, |
|
|
string |
$right |
|
) |
| |
|
static |
- Parameters
-
string | $left | The string of known length to compare against |
string | $right | The user-supplied string |
- Returns
- bool
Definition at line 489 of file JWT.php.
References $i.
491 if (\function_exists(
'hash_equals')) {
492 return \hash_equals($left, $right);
494 $len = \min(self::safeStrlen($left), self::safeStrlen($right));
497 for (
$i = 0;
$i < $len;
$i++) {
498 $status |= (\ord($left[
$i]) ^ \ord($right[
$i]));
500 $status |= (self::safeStrlen($left) ^ self::safeStrlen($right));
502 return ($status === 0);
◆ convertBase64UrlToBase64()
static Firebase\JWT\JWT::convertBase64UrlToBase64 |
( |
string |
$input | ) |
|
|
static |
Convert a string in the base64url (URL-safe Base64) encoding to standard base64.
- Parameters
-
string | $input | A Base64 encoded string with URL-safe characters (-_ and no padding) |
- Returns
- string A Base64 encoded string with standard characters (+/) and padding (=), when needed.
- See also
- https://www.rfc-editor.org/rfc/rfc4648
Definition at line 427 of file JWT.php.
429 $remainder = \strlen($input) % 4;
431 $padlen = 4 - $remainder;
432 $input .= \str_repeat(
'=', $padlen);
434 return \strtr($input,
'-_',
'+/');
◆ decode()
static array static Firebase\JWT\JWT::decode |
( |
string |
$jwt, |
|
|
|
$keyOrKeyArray, |
|
|
stdClass & |
$headers = null |
|
) |
| |
|
static |
Decodes a JWT string into a PHP object.
- Parameters
-
string | $jwt | The 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 | $headers | Optional. Populates stdClass with headers. |
- Returns
- stdClass The JWT's payload as a PHP object
- Exceptions
-
InvalidArgumentException | Provided key/key-array was empty or malformed |
DomainException | Provided JWT is malformed |
UnexpectedValueException | Provided JWT was invalid |
SignatureInvalidException | Provided JWT was invalid because the signature verification failed |
BeforeValidException | Provided JWT is trying to be used before it's eligible as defined by 'nbf' |
BeforeValidException | Provided JWT is trying to be used before it's been created as defined by 'iat' |
ExpiredException | Provided JWT has since expired, as defined by the 'exp' claim |
jsonDecode urlsafeB64Decode
Definition at line 96 of file JWT.php.
References ILIAS\LTI\ToolProvider\$key, $payload, $timestamp, ILIAS\LTI\ToolProvider\getKey(), and ILIAS\Repository\object().
Referenced by ilObjLTIConsumerGUI\saveContentSelection(), and ILIAS\LTI\ToolProvider\Jwt\FirebaseClient\verify().
104 if (empty($keyOrKeyArray)) {
107 $tks = \explode(
'.',
$jwt);
108 if (\count($tks) !== 3) {
111 list($headb64, $bodyb64, $cryptob64) = $tks;
112 $headerRaw = static::urlsafeB64Decode($headb64);
113 if (null === ($header = static::jsonDecode($headerRaw))) {
116 if ($headers !== null) {
119 $payloadRaw = static::urlsafeB64Decode($bodyb64);
120 if (null === (
$payload = static::jsonDecode($payloadRaw))) {
130 $sig = static::urlsafeB64Decode($cryptob64);
131 if (empty($header->alg)) {
134 if (empty(static::$supported_algs[$header->alg])) {
138 $key =
self::getKey($keyOrKeyArray, property_exists($header,
'kid') ? $header->kid : null);
141 if (!self::constantTimeEquals(
$key->getAlgorithm(), $header->alg)) {
145 if (\in_array($header->alg, [
'ES256',
'ES256K',
'ES384'],
true)) {
147 $sig = self::signatureToDER($sig);
149 if (!self::verify(
"{$headb64}.{$bodyb64}", $sig,
$key->getKeyMaterial(), $header->alg)) {
150 throw new SignatureInvalidException(
'Signature verification failed');
156 $ex =
new BeforeValidException(
157 'Cannot handle token with nbf prior to ' . \date(DateTime::ISO8601, (
int)
$payload->nbf)
167 $ex =
new BeforeValidException(
168 'Cannot handle token with iat prior to ' . \date(DateTime::ISO8601, (
int)
$payload->iat)
176 $ex =
new ExpiredException(
'Expired token');
if(count($parts) !=3) $payload
foreach($mandatory_scripts as $file) $timestamp
◆ encode()
static Firebase\JWT\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 | $key | The secret key. |
string | $alg | Supported 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 |
- Returns
- string A signed JWT
jsonEncode urlsafeB64Encode
Definition at line 199 of file JWT.php.
References ILIAS\LTI\ToolProvider\$key.
Referenced by ilObjLTIConsumer\LTISignJWT(), and ILIAS\LTI\ToolProvider\Jwt\FirebaseClient\sign().
206 $header = [
'typ' =>
'JWT',
'alg' => $alg];
207 if ($keyId !== null) {
208 $header[
'kid'] = $keyId;
210 if (isset($head) && \is_array($head)) {
211 $header = \array_merge($head, $header);
214 $segments[] = static::urlsafeB64Encode((
string) static::jsonEncode($header));
215 $segments[] = static::urlsafeB64Encode((
string) static::jsonEncode(
$payload));
216 $signing_input = \implode(
'.', $segments);
218 $signature = static::sign($signing_input,
$key, $alg);
219 $segments[] = static::urlsafeB64Encode($signature);
221 return \implode(
'.', $segments);
if(count($parts) !=3) $payload
◆ encodeDER()
static Firebase\JWT\JWT::encodeDER |
( |
int |
$type, |
|
|
string |
$value |
|
) |
| |
|
staticprivate |
Encodes a value into a DER object.
- Parameters
-
int | $type | DER tag |
string | $value | the value to encode |
- Returns
- string the encoded object
Definition at line 585 of file JWT.php.
588 if (
$type === self::ASN1_SEQUENCE) {
593 $der = \chr($tag_header |
$type);
596 $der .= \chr(\strlen($value));
598 return $der . $value;
◆ getKey()
static Firebase\JWT\JWT::getKey |
( |
|
$keyOrKeyArray, |
|
|
?string |
$kid |
|
) |
| |
|
staticprivate |
Determine if an algorithm has been provided for each Key.
- Parameters
-
| Key|ArrayAccess<string,Key>|array<string,Key> | $keyOrKeyArray |
string | null | $kid | |
- Exceptions
-
- Returns
- Key
Definition at line 460 of file JWT.php.
References ILIAS\LTI\ToolProvider\$kid.
464 if ($keyOrKeyArray instanceof Key) {
465 return $keyOrKeyArray;
472 if ($keyOrKeyArray instanceof CachedKeySet) {
474 return $keyOrKeyArray[
$kid];
477 if (!isset($keyOrKeyArray[
$kid])) {
481 return $keyOrKeyArray[
$kid];
◆ handleJsonError()
static Firebase\JWT\JWT::handleJsonError |
( |
int |
$errno | ) |
|
|
staticprivate |
Helper method to create a JSON error.
- Parameters
-
int | $errno | An error number from json_last_error() |
- Exceptions
-
- Returns
- void
Definition at line 514 of file JWT.php.
References $messages.
517 JSON_ERROR_DEPTH =>
'Maximum stack depth exceeded',
518 JSON_ERROR_STATE_MISMATCH =>
'Invalid or malformed JSON',
519 JSON_ERROR_CTRL_CHAR =>
'Unexpected control character found',
520 JSON_ERROR_SYNTAX =>
'Syntax error, malformed JSON',
521 JSON_ERROR_UTF8 =>
'Malformed UTF-8 characters' 526 :
'Unknown JSON error: ' . $errno
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
◆ jsonDecode()
static Firebase\JWT\JWT::jsonDecode |
( |
string |
$input | ) |
|
|
static |
Decode a JSON string into a PHP object.
- Parameters
-
- Returns
- mixed The decoded JSON string
- Exceptions
-
DomainException | Provided string was invalid JSON |
Definition at line 363 of file JWT.php.
365 $obj = \json_decode($input,
false, 512, JSON_BIGINT_AS_STRING);
367 if ($errno = \json_last_error()) {
368 self::handleJsonError($errno);
369 } elseif ($obj === null && $input !==
'null') {
◆ jsonEncode()
static Firebase\JWT\JWT::jsonEncode |
( |
array |
$input | ) |
|
|
static |
Encode a PHP array into a JSON string.
- Parameters
-
array<mixed> | $input A PHP array |
- Returns
- string JSON representation of the PHP array
- Exceptions
-
DomainException | Provided object could not be encoded to valid JSON |
Definition at line 384 of file JWT.php.
386 if (PHP_VERSION_ID >= 50400) {
387 $json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
390 $json = \json_encode($input);
392 if ($errno = \json_last_error()) {
393 self::handleJsonError($errno);
394 } elseif ($json ===
'null') {
397 if ($json ===
false) {
398 throw new DomainException(
'Provided object could not be encoded to valid JSON');
◆ readDER()
static Firebase\JWT\JWT::readDER |
( |
string |
$der, |
|
|
int |
$offset = 0 |
|
) |
| |
|
staticprivate |
Reads binary DER-encoded data and decodes into a single object.
- Parameters
-
string | $der | the binary data in DER format |
int | $offset | the offset of the data stream containing the object to decode |
- Returns
- array{int, string|null} the new offset and the decoded object
Definition at line 637 of file JWT.php.
References $data, and $type.
640 $size = \strlen($der);
641 $constructed = (\ord($der[$pos]) >> 5) & 0x01;
642 $type = \ord($der[$pos++]) & 0x1f;
645 $len = \ord($der[$pos++]);
649 while ($n-- && $pos < $size) {
650 $len = ($len << 8) | \ord($der[$pos++]);
655 if (
$type === self::ASN1_BIT_STRING) {
657 $data = \substr($der, $pos, $len - 1);
659 } elseif (!$constructed) {
660 $data = \substr($der, $pos, $len);
666 return [$pos,
$data];
◆ safeStrlen()
static Firebase\JWT\JWT::safeStrlen |
( |
string |
$str | ) |
|
|
staticprivate |
Get the number of bytes in cryptographic strings.
- Parameters
-
- Returns
- int
Definition at line 537 of file JWT.php.
539 if (\function_exists(
'mb_strlen')) {
540 return \mb_strlen($str,
'8bit');
542 return \strlen($str);
◆ sign()
static Firebase\JWT\JWT::sign |
( |
string |
$msg, |
|
|
|
$key, |
|
|
string |
$alg |
|
) |
| |
|
static |
Sign a string with a given key and algorithm.
- Parameters
-
string | $msg | The message to sign |
string | resource | OpenSSLAsymmetricKey | OpenSSLCertificate | $key | The secret key. |
string | $alg | Supported algorithms are 'EdDSA', 'ES384', 'ES256', 'ES256K', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512' |
- Returns
- string An encrypted message
- Exceptions
-
DomainException | Unsupported algorithm or bad key was specified |
Definition at line 236 of file JWT.php.
References Vendor\Package\$e, and ILIAS\LTI\ToolProvider\$key.
241 if (empty(static::$supported_algs[$alg])) {
244 list($function, $algorithm) = static::$supported_algs[$alg];
247 if (!\is_string(
$key)) {
250 return \hash_hmac($algorithm, $msg,
$key,
true);
253 $success = \openssl_sign($msg, $signature,
$key, $algorithm);
257 if ($alg ===
'ES256' || $alg ===
'ES256K') {
258 $signature = self::signatureFromDER($signature, 256);
259 } elseif ($alg ===
'ES384') {
260 $signature = self::signatureFromDER($signature, 384);
263 case 'sodium_crypto':
264 if (!\function_exists(
'sodium_crypto_sign_detached')) {
267 if (!\is_string(
$key)) {
272 $lines = array_filter(explode(
"\n",
$key));
273 $key = base64_decode((
string) end($lines));
274 if (\strlen(
$key) === 0) {
277 return sodium_crypto_sign_detached($msg,
$key);
◆ signatureFromDER()
static Firebase\JWT\JWT::signatureFromDER |
( |
string |
$der, |
|
|
int |
$keySize |
|
) |
| |
|
staticprivate |
Encodes signature from a DER object.
- Parameters
-
string | $der | binary signature in DER format |
int | $keySize | the number of bits in the key |
- Returns
- string the signature
Definition at line 609 of file JWT.php.
612 list($offset, $_) = self::readDER($der);
613 list($offset, $r) = self::readDER($der, $offset);
614 list($offset, $s) = self::readDER($der, $offset);
618 $r = \ltrim($r,
"\x00");
619 $s = \ltrim($s,
"\x00");
622 $r = \str_pad($r, $keySize / 8,
"\x00", STR_PAD_LEFT);
623 $s = \str_pad($s, $keySize / 8,
"\x00", STR_PAD_LEFT);
◆ signatureToDER()
static Firebase\JWT\JWT::signatureToDER |
( |
string |
$sig | ) |
|
|
staticprivate |
Convert an ECDSA signature to an ASN.1 DER sequence.
- Parameters
-
string | $sig | The ECDSA signature to convert |
- Returns
- string The encoded DER object
Definition at line 551 of file JWT.php.
554 $length = max(1, (
int) (\strlen($sig) / 2));
555 list($r, $s) = \str_split($sig, $length);
558 $r = \ltrim($r,
"\x00");
559 $s = \ltrim($s,
"\x00");
563 if (\ord($r[0]) > 0x7f) {
566 if (\ord($s[0]) > 0x7f) {
570 return self::encodeDER(
572 self::encodeDER(self::ASN1_INTEGER, $r) .
573 self::encodeDER(self::ASN1_INTEGER, $s)
◆ urlsafeB64Decode()
static Firebase\JWT\JWT::urlsafeB64Decode |
( |
string |
$input | ) |
|
|
static |
◆ urlsafeB64Encode()
static Firebase\JWT\JWT::urlsafeB64Encode |
( |
string |
$input | ) |
|
|
static |
Encode a string with URL-safe Base64.
- Parameters
-
string | $input | The string you want encoded |
- Returns
- string The base64 encode of what you passed in
Definition at line 444 of file JWT.php.
Referenced by ILIAS\LTI\ToolProvider\Jwt\FirebaseClient\getJWKS().
446 return \str_replace(
'=',
'', \strtr(\base64_encode($input),
'+/',
'-_'));
◆ verify()
static Firebase\JWT\JWT::verify |
( |
string |
$msg, |
|
|
string |
$signature, |
|
|
|
$keyMaterial, |
|
|
string |
$alg |
|
) |
| |
|
staticprivate |
Verify a signature with the message, key and method.
Not all methods are symmetric, so we must have a separate verify and sign method.
- Parameters
-
string | $msg | The original message (header and body) |
string | $signature | The original signature |
string | resource | OpenSSLAsymmetricKey | OpenSSLCertificate | $keyMaterial | For Ed*, ES*, HS*, a string key works. for RS*, must be an instance of OpenSSLAsymmetricKey |
string | $alg | The algorithm |
- Returns
- bool
- Exceptions
-
DomainException | Invalid Algorithm, bad key, or OpenSSL failure |
Definition at line 299 of file JWT.php.
References Vendor\Package\$e, and ILIAS\LTI\ToolProvider\$key.
305 if (empty(static::$supported_algs[$alg])) {
309 list($function, $algorithm) = static::$supported_algs[$alg];
312 $success = \openssl_verify($msg, $signature, $keyMaterial, $algorithm);
313 if ($success === 1) {
316 if ($success === 0) {
321 'OpenSSL error: ' . \openssl_error_string()
323 case 'sodium_crypto':
324 if (!\function_exists(
'sodium_crypto_sign_verify_detached')) {
327 if (!\is_string($keyMaterial)) {
332 $lines = array_filter(explode(
"\n", $keyMaterial));
333 $key = base64_decode((
string) end($lines));
334 if (\strlen(
$key) === 0) {
337 if (\strlen($signature) === 0) {
340 return sodium_crypto_sign_verify_detached($signature, $msg,
$key);
346 if (!\is_string($keyMaterial)) {
349 $hash = \hash_hmac($algorithm, $msg, $keyMaterial,
true);
350 return self::constantTimeEquals($hash, $signature);
◆ $leeway
int Firebase\JWT\JWT::$leeway = 0 |
|
static |
◆ $supported_algs
array Firebase\JWT\JWT::$supported_algs |
|
static |
Initial value:= [
'ES384' => ['openssl', 'SHA384']
Definition at line 55 of file JWT.php.
◆ $timestamp
int Firebase\JWT\JWT::$timestamp = null |
|
static |
◆ ASN1_BIT_STRING
const Firebase\JWT\JWT::ASN1_BIT_STRING = 0x03 |
|
private |
◆ ASN1_INTEGER
const Firebase\JWT\JWT::ASN1_INTEGER = 0x02 |
|
private |
◆ ASN1_SEQUENCE
const Firebase\JWT\JWT::ASN1_SEQUENCE = 0x10 |
|
private |
The documentation for this class was generated from the following file: