class JWT

This class provides a set of functions related to Json Web Token

A JSON Web Token (JWT) includes three sections:

  • 0: Header
  • 1: Payload
  • 2: Signature

They are encoded as Base64url strings, and are separated by dot "." characters.

Methods

static object|string
decode(string $jwt, int $section = 0, bool $object = true)

Decodes a section of the JSON Web Token and returns it.

static string
safeBase64Decode(string $part)

Decodes a string encoded in url-safe base64 and returns it.

static string
encodeASNLength(int $length)

Encodes a length according to ASN.1 encoding rules

static object
extractRSAKey(object $keys, object $header)

Extracts the JSON Web Key (JWK) for RSA signature from a JSON Web Key Set (JWKS) and the header of a JSON Web Token

static string
createPemPublicKey(object $key)

Creates a public key in PEM format from the given key which is extracted from a JSON Web Key Set (JWKS)

static bool
verifyRSASignature(object $header, object $keys, string $jwt)

Verifies the signature of a JSON Web Token using an RSA algorithm.

static bool
verifyHMACsignature(object $header, string $jwt, string $key)

Verifies the signature of a JSON Web Token using an HMAC algorithm.

Details

at line 31
static object|string decode(string $jwt, int $section = 0, bool $object = true)

Decodes a section of the JSON Web Token and returns it.

Parameters

string $jwt The string value of the JSON Web Token
int $section the section number to decode (0 to 2)
bool $object true if the decoded section must be returned as an object, false otherwise

Return Value

object|string

at line 45
static private string safeBase64Decode(string $part)

Decodes a string encoded in url-safe base64 and returns it.

Parameters

string $part The encoded string

Return Value

string the decoded string

at line 61
static private string encodeASNLength(int $length)

Encodes a length according to ASN.1 encoding rules

Parameters

int $length The length to encode

Return Value

string the encoded length

at line 79
static private object extractRSAKey(object $keys, object $header)

Extracts the JSON Web Key (JWK) for RSA signature from a JSON Web Key Set (JWKS) and the header of a JSON Web Token

Parameters

object $keys The JSON Web Key Set (JWKS)
object $header The header of a JSON Web Token

Return Value

object The JSON Web Key (JWK) for the RSA algorithm

at line 103
static private string createPemPublicKey(object $key)

Creates a public key in PEM format from the given key which is extracted from a JSON Web Key Set (JWKS)

Parameters

object $key JSON Web Key (JWK)

Return Value

string The public key

at line 141
static bool verifyRSASignature(object $header, object $keys, string $jwt)

Verifies the signature of a JSON Web Token using an RSA algorithm.

Parameters

object $header The header of the JSON Web Token
object $keys The JSON Web Key Set (JWKS)
string $jwt The JSON Web Token

Return Value

bool true if the signature is valid, false otherwise

Exceptions

OAuthClientSignatureException if an error occurs

at line 176
static bool verifyHMACsignature(object $header, string $jwt, string $key)

Verifies the signature of a JSON Web Token using an HMAC algorithm.

Parameters

object $header The header of the JSON Web Token
string $jwt The JSON Web Token
string $key The shared secret key used for generating the HMAC variant

Return Value

bool true if the signature is valid, false otherwise

Exceptions

OAuthClientSignatureException if an error occurs