JsonWebToken
in package
JsonWebToken class.
Simple Json Web Token generator & verifier static utility class, currently supporting only HS256 signatures.
Table of Contents
- $algorithm : string
- JWT algorithm to generate signature.
- $type : string
- JWT header type.
- create() : string
- Generates a token from provided data and secret.
- get_parts() : object
- Returns the decoded/encoded header, payload and signature from a token string.
- validate() : bool
- Validates a provided token against the provided secret.
- from_base_64_url() : string
- Decodes a string encoded using url safe base64, supporting auto padding.
- generate_header() : string|bool
- Generates the json formatted header for our HS256 JWT token.
- generate_payload() : string|bool
- Generates the payload in json formatted string.
- generate_signature() : false|string
- Generates a sha256 signature for the provided string using the provided secret.
- to_base_64_url() : string
- Encodes a string to url safe base64.
Properties
$algorithm
JWT algorithm to generate signature.
private
static string
$algorithm
= 'HS256'
$type
JWT header type.
private
static string
$type
= 'JWT'
Methods
create()
Generates a token from provided data and secret.
public
static create(array<string|int, mixed> $payload, string $secret) : string
Parameters
- $payload : array<string|int, mixed>
-
Payload data.
- $secret : string
-
The secret used to generate the signature.
Return values
string —get_parts()
Returns the decoded/encoded header, payload and signature from a token string.
public
static get_parts(string $token) : object
Parameters
- $token : string
-
Full token string.
Return values
object —validate()
Validates a provided token against the provided secret.
public
static validate(string $token, string $secret) : bool
Checks for format, valid header for our class, expiration claim validity and signature. https://datatracker.ietf.org/doc/html/rfc7519#section-7.2
Parameters
- $token : string
-
Full token string.
- $secret : string
-
The secret used to generate the signature.
Return values
bool —from_base_64_url()
Decodes a string encoded using url safe base64, supporting auto padding.
private
static from_base_64_url(string $string) : string
Parameters
- $string : string
-
the string to be decoded.
Return values
string —generate_header()
Generates the json formatted header for our HS256 JWT token.
private
static generate_header() : string|bool
Return values
string|bool —generate_payload()
Generates the payload in json formatted string.
private
static generate_payload(array<string|int, mixed> $payload) : string|bool
Parameters
- $payload : array<string|int, mixed>
-
Payload data.
Return values
string|bool —generate_signature()
Generates a sha256 signature for the provided string using the provided secret.
private
static generate_signature(string $string, string $secret) : false|string
Parameters
- $string : string
-
Header + Payload token substring.
- $secret : string
-
The secret used to generate the signature.
Return values
false|string —to_base_64_url()
Encodes a string to url safe base64.
private
static to_base_64_url(string $string) : string
Parameters
- $string : string
-
The string to be encoded.