\Firebase\JWTJWT

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

PHP version 5

Summary

Methods
Properties
Constants
decode()
encode()
sign()
jsonDecode()
jsonEncode()
urlsafeB64Decode()
urlsafeB64Encode()
$leeway
$timestamp
$supported_algs
No constants found
No protected methods found
No protected properties found
N/A
verify()
handleJsonError()
safeStrlen()
No private properties found
N/A

Properties

$leeway

$leeway : 

When checking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.

Type

$timestamp

$timestamp : 

Allow the current timestamp to be specified.

Useful for fixing a value within unit testing.

Will default to PHP time() value if null.

Type

$supported_algs

$supported_algs : 

Type

Methods

decode()

decode(string  $jwt, string|array  $key, array  $allowed_algs = array()) : object

Decodes a JWT string into a PHP object.

Parameters

string $jwt

The JWT

string|array $key

The key, or map of keys. If the algorithm used is asymmetric, this is the public key

array $allowed_algs

List of supported verification algorithms Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'

Throws

\UnexpectedValueException

Provided JWT was invalid

\Firebase\JWT\SignatureInvalidException

Provided JWT was invalid because the signature verification failed

\Firebase\JWT\BeforeValidException

Provided JWT is trying to be used before it's eligible as defined by 'nbf'

\Firebase\JWT\BeforeValidException

Provided JWT is trying to be used before it's been created as defined by 'iat'

\Firebase\JWT\ExpiredException

Provided JWT has since expired, as defined by the 'exp' claim

Returns

object —

The JWT's payload as a PHP object

encode()

encode(object|array  $payload, string  $key, string  $alg = 'HS256', mixed  $keyId = null, array  $head = null) : string

Converts and signs a PHP object or array into a JWT string.

Parameters

object|array $payload

PHP object or array

string $key

The secret key. If the algorithm used is asymmetric, this is the private key

string $alg

The signing algorithm. Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'

mixed $keyId
array $head

An array with header elements to attach

Returns

string —

A signed JWT

sign()

sign(string  $msg, string|resource  $key, string  $alg = 'HS256') : string

Sign a string with a given key and algorithm.

Parameters

string $msg

The message to sign

string|resource $key

The secret key

string $alg

The signing algorithm. Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'

Throws

\DomainException

Unsupported algorithm was specified

Returns

string —

An encrypted message

jsonDecode()

jsonDecode(string  $input) : object

Decode a JSON string into a PHP object.

Parameters

string $input

JSON string

Throws

\DomainException

Provided string was invalid JSON

Returns

object —

Object representation of JSON string

jsonEncode()

jsonEncode(object|array  $input) : string

Encode a PHP object into a JSON string.

Parameters

object|array $input

A PHP object or array

Throws

\DomainException

Provided object could not be encoded to valid JSON

Returns

string —

JSON representation of the PHP object or array

urlsafeB64Decode()

urlsafeB64Decode(string  $input) : string

Decode a string with URL-safe Base64.

Parameters

string $input

A Base64 encoded string

Returns

string —

A decoded string

urlsafeB64Encode()

urlsafeB64Encode(string  $input) : string

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

verify()

verify(string  $msg, string  $signature, string|resource  $key, string  $alg) : boolean

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 $key

For HS, a string key works. for RS, must be a resource of an openssl public key

string $alg

The algorithm

Throws

\DomainException

Invalid Algorithm or OpenSSL failure

Returns

boolean

handleJsonError()

handleJsonError(integer  $errno) : void

Helper method to create a JSON error.

Parameters

integer $errno

An error number from json_last_error()

safeStrlen()

safeStrlen(  $str) : integer

Get the number of bytes in cryptographic strings.

Parameters

$str

Returns

integer