PHP Class JWT

PHP version 5
Author: Neuman Vong ([email protected])
Author: Anant Narayanan ([email protected])
Show file Open project: sjardim/Hugopit Class Usage Examples

Public Properties

Property Type Description
$methods

Public Methods

Method Description
decode ( string $jwt, string | Array | null $key = null, boolean $verify = true ) : object Decodes a JWT string into a PHP object.
encode ( object | array $payload, string $key, string $algo = 'HS256', $keyId = null ) : string Converts and signs a PHP object or array into a JWT string.
jsonDecode ( string $input ) : object Decode a JSON string into a PHP object.
jsonEncode ( object | array $input ) : string Encode a PHP object into a JSON string.
sign ( string $msg, string | resource $key, string $method = 'HS256' ) : string Sign a string with a given key and algorithm.
urlsafeB64Decode ( string $input ) : string Decode a string with URL-safe Base64.
urlsafeB64Encode ( string $input ) : string Encode a string with URL-safe Base64.
verify ( string $msg, string $signature, string | resource $key, string $method = 'HS256' ) : boolean Verify a signature with the mesage, key and method. Not all methods are symmetric, so we must have a separate verify and sign method.

Private Methods

Method Description
handleJsonError ( integer $errno ) : void Helper method to create a JSON error.

Method Details

decode() public static method

Decodes a JWT string into a PHP object.
public static decode ( string $jwt, string | Array | null $key = null, boolean $verify = true ) : object
$jwt string The JWT
$key string | Array | null The secret key, or map of keys
$verify boolean Don't skip verification process
return object The JWT's payload as a PHP object

encode() public static method

Converts and signs a PHP object or array into a JWT string.
public static encode ( object | array $payload, string $key, string $algo = 'HS256', $keyId = null ) : string
$payload object | array PHP object or array
$key string The secret key
$algo string The signing algorithm. Supported algorithms are 'HS256', 'HS384' and 'HS512'
return string A signed JWT

jsonDecode() public static method

Decode a JSON string into a PHP object.
public static jsonDecode ( string $input ) : object
$input string JSON string
return object Object representation of JSON string

jsonEncode() public static method

Encode a PHP object into a JSON string.
public static jsonEncode ( object | array $input ) : string
$input object | array A PHP object or array
return string JSON representation of the PHP object or array

sign() public static method

Sign a string with a given key and algorithm.
public static sign ( string $msg, string | resource $key, string $method = 'HS256' ) : string
$msg string The message to sign
$key string | resource The secret key
$method string The signing algorithm. Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
return string An encrypted message

urlsafeB64Decode() public static method

Decode a string with URL-safe Base64.
public static urlsafeB64Decode ( string $input ) : string
$input string A Base64 encoded string
return string A decoded string

urlsafeB64Encode() public static method

Encode a string with URL-safe Base64.
public static urlsafeB64Encode ( string $input ) : string
$input string The string you want encoded
return string The base64 encode of what you passed in

verify() public static method

Verify a signature with the mesage, key and method. Not all methods are symmetric, so we must have a separate verify and sign method.
public static verify ( string $msg, string $signature, string | resource $key, string $method = 'HS256' ) : boolean
$msg string the original message
$signature string
$key string | resource for HS*, a string key works. for RS*, must be a resource of an openssl public key
$method string
return boolean

Property Details

$methods public static property

public static $methods