PHP Class Psecio\Jwt\Jwt

Show file Open project: psecio/jwt Class Usage Examples

Public Methods

Method Description
__call ( string $name, array $args ) : Jwt Magic method for setting claims by name Ex. "issuedAt()" calls Claims\IssuedAt
__construct ( Header $header, ClaimsCollection $collection = null ) Initialize the object and set the header and claims collection Empty claims collection is set if none is given
__get ( string $name ) : mixed See if a claim type matches the requested property
addClaim ( Claim $claim ) : Jwt Add a Claim to the current collection
base64Decode ( string $data ) : string Base64 decode (and url decode) the given data
base64Encode ( string $data ) : string Base64 encode data and prepare for the URL NOTE: The "=" is removed as it's just padding in base64 and not needed.
custom ( string | array $value, string $name = null ) : Jwt Allow for the insertion of multiple custom values at once
decode ( string $data, boolean $verify = true ) : stdClass Decode the data with the given key Optional "verify" parameter validates the signature as well (default is on)
decrypt ( string $data, string $algorithm, string $iv, $key ) : string Decrypt given data wtih given key (and algorithm/IV)
encode ( string $claims = null, $addIssued = false ) : string Encode the data, either given or from current object
encrypt ( string $algorithm, string $iv, $key ) : string Encrypt the data with the given key (and algorithm/IV)
getClaims ( ) : ClaimsCollection Get the current claims collection
getHeader ( ) : Header Get the currently assigned header instance
hash_equals ( $a, $b ) Polyfill PHP 5.6.0's hash_equals() feature
setClaims ( ClaimsCollection $collection ) : Jwt Set the claims collection
setHeader ( Header $header ) : Jwt Set the header instance
sign ( string $signWith, string $key ) : string Generate the signature with the given data, key and algorithm
verify ( string $key, stdClass $header, stdClass $claims, string $signature ) : boolean Verify the signature on the JWT message

Method Details

__call() public method

Magic method for setting claims by name Ex. "issuedAt()" calls Claims\IssuedAt
public __call ( string $name, array $args ) : Jwt
$name string Function name
$args array Arguments to pass
return Jwt instance

__construct() public method

Initialize the object and set the header and claims collection Empty claims collection is set if none is given
public __construct ( Header $header, ClaimsCollection $collection = null )
$header Header Header instance to set on JWT object
$collection ClaimsCollection Claims collection [optional]

__get() public method

See if a claim type matches the requested property
public __get ( string $name ) : mixed
$name string Property name
return mixed Either a null if not found or the matching data

addClaim() public method

Add a Claim to the current collection
public addClaim ( Claim $claim ) : Jwt
$claim Claim Claim instance to add
return Jwt Current Jwt instance

base64Decode() public method

Base64 decode (and url decode) the given data
public base64Decode ( string $data ) : string
$data string Data to decode
return string Decoded data

base64Encode() public method

Base64 encode data and prepare for the URL NOTE: The "=" is removed as it's just padding in base64 and not needed.
public base64Encode ( string $data ) : string
$data string Data string
return string Formatted data

custom() public method

Allow for the insertion of multiple custom values at once
public custom ( string | array $value, string $name = null ) : Jwt
$value string | array Either a string for a single claim or array for multiple
$name string Name of claim to use if string given for "name" [optional]
return Jwt instance

decode() public method

Decode the data with the given key Optional "verify" parameter validates the signature as well (default is on)
public decode ( string $data, boolean $verify = true ) : stdClass
$data string Data to decode (entire JWT data string)
$verify boolean Verify the signature on the data [optional]
return stdClass Decoded claims data

decrypt() public method

Decrypt given data wtih given key (and algorithm/IV)
public decrypt ( string $data, string $algorithm, string $iv, $key ) : string
$data string Data to decrypt
$algorithm string Algorithm to use for decrypting the data
$iv string
return string Decrypted data

encode() public method

Encode the data, either given or from current object
public encode ( string $claims = null, $addIssued = false ) : string
$claims string Claims string to encode [optional]
return string Encoded data, appended by periods

encrypt() public method

Encrypt the data with the given key (and algorithm/IV)
public encrypt ( string $algorithm, string $iv, $key ) : string
$algorithm string Algorithm to use for encryption
$iv string IV for encrypting data
return string Encrypted string

getClaims() public method

Get the current claims collection
public getClaims ( ) : ClaimsCollection
return ClaimsCollection instance

getHeader() public method

Get the currently assigned header instance
public getHeader ( ) : Header
return Header Header object instance

hash_equals() public method

Polyfill PHP 5.6.0's hash_equals() feature
public hash_equals ( $a, $b )

setClaims() public method

Set the claims collection
public setClaims ( ClaimsCollection $collection ) : Jwt
$collection ClaimsCollection Claims collection instance
return Jwt Current Jwt instance

setHeader() public method

Set the header instance
public setHeader ( Header $header ) : Jwt
$header Header [description]
return Jwt Current Jwt instance

sign() public method

Generate the signature with the given data, key and algorithm
public sign ( string $signWith, string $key ) : string
$signWith string Data to sign hash with
$key string Key for signing
return string Generated signature hash

verify() public method

Verify the signature on the JWT message
public verify ( string $key, stdClass $header, stdClass $claims, string $signature ) : boolean
$key string Key used for hashing
$header stdClass Header data (object)
$claims stdClass Set of claims
$signature string Signature string
return boolean Pass/fail of verification