Kotchasan

Jwt
in package

JWT encoding, decoding, and verification class

Tags
see
https://www.kotchasan.com/

Table of Contents

$hashHmacAlgorithms  : array<string|int, mixed>
Algorithms supported by hash_hmac
$algorithm  : string
Algorithm used for encoding with hash_hmac
$expireTime  : int
JWT expiration time 3600 = 1 hour.
$secretKey  : string
Secret key for JWT encoding
create()  : static
Create a Jwt instance
decode()  : array<string|int, mixed>
Decodes a JWT and retrieves the payload.
encode()  : string
Encodes the payload into a JWT.
verify()  : array<string|int, mixed>
Verifies the integrity and validity of a JWT.
__construct()  : mixed
Class constructor
base64UrlDecode()  : string
Decodes data encoded with base64UrlEncode.
base64UrlEncode()  : string
Encodes data using Base64.
generateSignature()  : string
Generates a signature using the specified algorithm.

Properties

$hashHmacAlgorithms

Algorithms supported by hash_hmac

protected array<string|int, mixed> $hashHmacAlgorithms = ['HS256' => 'sha256', 'HS384' => 'sha384', 'HS512' => 'sha512']

$algorithm

Algorithm used for encoding with hash_hmac

private string $algorithm

$expireTime

JWT expiration time 3600 = 1 hour.

private int $expireTime

0 = no expiration time (default). If an expiration time is specified, it will be checked during verification. The expired time will be added to the payload automatically during encoding, and removed when decoding. It is not recommended to specify the expiration time in the payload that needs to be encoded separately.

$secretKey

Secret key for JWT encoding

private string $secretKey

Methods

create()

Create a Jwt instance

public static create([string $secretKey = 'my_secret_key' ], int $expireTime[, string $algo = 'HS256' ]) : static
Parameters
$secretKey : string = 'my_secret_key'

Secret key for JWT encoding

$expireTime : int

JWT expiration time 0 = no expiration time (default), > 0 specifies expiration time in seconds

$algo : string = 'HS256'

Algorithm used for encoding, supported by $hashHmacAlgorithms

Return values
static

decode()

Decodes a JWT and retrieves the payload.

public decode(string $jwt) : array<string|int, mixed>
Parameters
$jwt : string

The JWT to decode.

Tags
assert

('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiXHUwZTIwXHUwZTMyXHUwZTI5XHUwZTMyXHUwZTQ0XHUwZTE3XHUwZTIyIiwiaWQiOjEyMzQ1Njc4OTB9.fAdzmsl4AIGAyNGt7MfNum9DUIxn6DGMhdn1hw4PwwE') [==] array('name' => 'ภาษาไทย', 'id' => 1234567890)

throws
Exception

If the token format is invalid.

Return values
array<string|int, mixed>

The decoded payload data.

encode()

Encodes the payload into a JWT.

public encode(array<string|int, mixed> $payload) : string
Parameters
$payload : array<string|int, mixed>

The payload data to be encoded.

Tags
assert

(array('name' => 'ภาษาไทย', 'id' => 1234567890)) [==] 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiXHUwZTIwXHUwZTMyXHUwZTI5XHUwZTMyXHUwZTQ0XHUwZTE3XHUwZTIyIiwiaWQiOjEyMzQ1Njc4OTB9.fAdzmsl4AIGAyNGt7MfNum9DUIxn6DGMhdn1hw4PwwE'

Return values
string

The encoded JWT.

verify()

Verifies the integrity and validity of a JWT.

public verify(string $jwt) : array<string|int, mixed>
Parameters
$jwt : string

The JWT to verify.

Tags
assert

('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiXHUwZTIwXHUwZTMyXHUwZTI5XHUwZTMyXHUwZTQ0XHUwZTE3XHUwZTIyIiwiaWQiOjEyMzQ1Njc4OTB9.fAdzmsl4AIGAyNGt7MfNum9DUIxn6DGMhdn1hw4PwwE') [==] array('name' => 'ภาษาไทย', 'id' => 1234567890)

assert

('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiXHUwZTIwXHUwZTMyXHUwZTI5XHUwZTMyXHUwZTQ0XHAwZTE3XHUwZTIyIiwiaWQiOjEyMzQ1Njc4OTB9.fAdzmsl4AIGAyNGt7MfNum9DUIxn6DGMhdn1hw4PwwE') [throws] \Exception

throws
Exception

If the token format is invalid, the signature is invalid, or the token has expired.

Return values
array<string|int, mixed>

The decoded payload data.

__construct()

Class constructor

private __construct(string $secretKey, int $expireTime, int $algo) : mixed
Parameters
$secretKey : string

Secret key for JWT encoding

$expireTime : int

JWT expiration time 0 = no expiration time (default), > 0 specifies expiration time in seconds

$algo : int

Algorithm used for encoding, supported by $hashHmacAlgorithms

Return values
mixed

base64UrlDecode()

Decodes data encoded with base64UrlEncode.

private base64UrlDecode(string $data) : string
Parameters
$data : string
Return values
string

base64UrlEncode()

Encodes data using Base64.

private base64UrlEncode(string $data) : string
Parameters
$data : string
Return values
string

generateSignature()

Generates a signature using the specified algorithm.

private generateSignature(string $header, string $payload) : string
Parameters
$header : string

The JWT header.

$payload : string

The JWT payload.

Return values
string

The generated signature.

Search results