Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 94... Línea 94...
94
     * @uses urlsafeB64Decode
94
     * @uses urlsafeB64Decode
95
     */
95
     */
96
    public static function decode(
96
    public static function decode(
97
        string $jwt,
97
        string $jwt,
98
        $keyOrKeyArray,
98
        $keyOrKeyArray,
99
        stdClass &$headers = null
99
        ?stdClass &$headers = null
100
    ): stdClass {
100
    ): stdClass {
101
        // Validate JWT
101
        // Validate JWT
102
        $timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp;
102
        $timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp;
Línea 103... Línea 103...
103
 
103
 
Línea 198... Línea 198...
198
     */
198
     */
199
    public static function encode(
199
    public static function encode(
200
        array $payload,
200
        array $payload,
201
        $key,
201
        $key,
202
        string $alg,
202
        string $alg,
203
        string $keyId = null,
203
        ?string $keyId = null,
204
        array $head = null
204
        ?array $head = null
205
    ): string {
205
    ): string {
206
        $header = ['typ' => 'JWT'];
206
        $header = ['typ' => 'JWT'];
207
        if (isset($head) && \is_array($head)) {
207
        if (isset($head)) {
208
            $header = \array_merge($header, $head);
208
            $header = \array_merge($header, $head);
209
        }
209
        }
210
        $header['alg'] = $alg;
210
        $header['alg'] = $alg;
211
        if ($keyId !== null) {
211
        if ($keyId !== null) {
212
            $header['kid'] = $keyId;
212
            $header['kid'] = $keyId;
Línea 249... Línea 249...
249
                    throw new InvalidArgumentException('key must be a string when using hmac');
249
                    throw new InvalidArgumentException('key must be a string when using hmac');
250
                }
250
                }
251
                return \hash_hmac($algorithm, $msg, $key, true);
251
                return \hash_hmac($algorithm, $msg, $key, true);
252
            case 'openssl':
252
            case 'openssl':
253
                $signature = '';
253
                $signature = '';
-
 
254
                if (!\is_resource($key) && !openssl_pkey_get_private($key)) {
-
 
255
                    throw new DomainException('OpenSSL unable to validate key');
-
 
256
                }
254
                $success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line
257
                $success = \openssl_sign($msg, $signature, $key, $algorithm); // @phpstan-ignore-line
255
                if (!$success) {
258
                if (!$success) {
256
                    throw new DomainException('OpenSSL unable to sign data');
259
                    throw new DomainException('OpenSSL unable to sign data');
257
                }
260
                }
258
                if ($alg === 'ES256' || $alg === 'ES256K') {
261
                if ($alg === 'ES256' || $alg === 'ES256K') {
Línea 382... Línea 385...
382
     *
385
     *
383
     * @throws DomainException Provided object could not be encoded to valid JSON
386
     * @throws DomainException Provided object could not be encoded to valid JSON
384
     */
387
     */
385
    public static function jsonEncode(array $input): string
388
    public static function jsonEncode(array $input): string
386
    {
389
    {
387
        if (PHP_VERSION_ID >= 50400) {
-
 
388
            $json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
390
        $json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
389
        } else {
-
 
390
            // PHP 5.3 only
-
 
391
            $json = \json_encode($input);
-
 
392
        }
-
 
393
        if ($errno = \json_last_error()) {
391
        if ($errno = \json_last_error()) {
394
            self::handleJsonError($errno);
392
            self::handleJsonError($errno);
395
        } elseif ($json === 'null') {
393
        } elseif ($json === 'null') {
396
            throw new DomainException('Null result with non-null input');
394
            throw new DomainException('Null result with non-null input');
397
        }
395
        }