JWT
JSON Web Token implementation, based on this spec: https://tools.ietf.org/html/rfc7519
PHP version 5
Tags
Table of Contents
Constants
- ASN1_BIT_STRING = 0x3
- ASN1_INTEGER = 0x2
- ASN1_SEQUENCE = 0x10
Properties
- $leeway : mixed
- When checking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
- $supported_algs : mixed
- $timestamp : mixed
- Allow the current timestamp to be specified.
Methods
- decode() : object
- Decodes a JWT string into a PHP object.
- encode() : string
- Converts and signs a PHP object or array into a JWT string.
- jsonDecode() : object
- Decode a JSON string into a PHP object.
- jsonEncode() : string
- Encode a PHP object into a JSON string.
- sign() : string
- Sign a string with a given key and algorithm.
- urlsafeB64Decode() : string
- Decode a string with URL-safe Base64.
- urlsafeB64Encode() : string
- Encode a string with URL-safe Base64.
Constants
ASN1_BIT_STRING
public
mixed
ASN1_BIT_STRING
= 0x3
ASN1_INTEGER
public
mixed
ASN1_INTEGER
= 0x2
ASN1_SEQUENCE
public
mixed
ASN1_SEQUENCE
= 0x10
Properties
$leeway
When checking nbf, iat or expiration times, we want to provide some extra leeway time to account for clock skew.
public
static mixed
$leeway
= 0
$supported_algs
public
static mixed
$supported_algs
= array('ES256' => array('openssl', 'SHA256'), 'HS256' => array('hash_hmac', 'SHA256'), 'HS384' => array('hash_hmac', 'SHA384'), 'HS512' => array('hash_hmac', 'SHA512'), 'RS256' => array('openssl', 'SHA256'), 'RS384' => array('openssl', 'SHA384'), 'RS512' => array('openssl', 'SHA512'))
$timestamp
Allow the current timestamp to be specified.
public
static mixed
$timestamp
= null
Useful for fixing a value within unit testing.
Will default to PHP time() value if null.
Methods
decode()
Decodes a JWT string into a PHP object.
public
static decode(string $jwt, string|array<string|int, mixed>|resource $key[, array<string|int, mixed> $allowed_algs = array() ]) : object
Parameters
- $jwt : string
-
The JWT
- $key : string|array<string|int, mixed>|resource
-
The key, or map of keys. If the algorithm used is asymmetric, this is the public key
- $allowed_algs : array<string|int, mixed> = array()
-
List of supported verification algorithms Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
Tags
Return values
object —The JWT's payload as a PHP object
encode()
Converts and signs a PHP object or array into a JWT string.
public
static encode(object|array<string|int, mixed> $payload, string $key[, string $alg = 'HS256' ][, mixed $keyId = null ][, array<string|int, mixed> $head = null ]) : string
Parameters
- $payload : object|array<string|int, mixed>
-
PHP object or array
- $key : string
-
The secret key. If the algorithm used is asymmetric, this is the private key
- $alg : string = 'HS256'
-
The signing algorithm. Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
- $keyId : mixed = null
- $head : array<string|int, mixed> = null
-
An array with header elements to attach
Tags
Return values
string —A signed JWT
jsonDecode()
Decode a JSON string into a PHP object.
public
static jsonDecode(string $input) : object
Parameters
- $input : string
-
JSON string
Tags
Return values
object —Object representation of JSON string
jsonEncode()
Encode a PHP object into a JSON string.
public
static jsonEncode(object|array<string|int, mixed> $input) : string
Parameters
- $input : object|array<string|int, mixed>
-
A PHP object or array
Tags
Return values
string —JSON representation of the PHP object or array
sign()
Sign a string with a given key and algorithm.
public
static sign(string $msg, string|resource $key[, string $alg = 'HS256' ]) : string
Parameters
- $msg : string
-
The message to sign
- $key : string|resource
-
The secret key
- $alg : string = 'HS256'
-
The signing algorithm. Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
Tags
Return values
string —An encrypted message
urlsafeB64Decode()
Decode a string with URL-safe Base64.
public
static urlsafeB64Decode(string $input) : string
Parameters
- $input : string
-
A Base64 encoded string
Return values
string —A decoded string
urlsafeB64Encode()
Encode a string with URL-safe Base64.
public
static urlsafeB64Encode(string $input) : string
Parameters
- $input : string
-
The string you want encoded
Return values
string —The base64 encode of what you passed in