Jwt
extends AbstractAuth
in package
uses
AdapterUserTrait
Jwt auth class
Tags
Table of Contents
Constants
- ALGORITHMS = ['HS256', 'RS256', 'ES256']
- Supported algorithms
- ES256_SIGNATURE_LENGTH = 64
- Expected byte length of a raw ES256 (P-256) JOSE signature: 32-byte R + 32-byte S (RFC 7518 §3.4)
- NOT_VALID = 0
- Constant for auth result
- VALID = 1
Properties
- $algorithm : string
- Algorithm
- $audience : string|null
- Audience to validate the 'aud' claim against
- $issuer : string|null
- Issuer to validate the 'iss' claim against
- $key : string
- Key (shared secret for HS256, PEM public key for RS256/ES256)
- $leeway : int
- Leeway (in seconds) allowed for exp/nbf claim comparisons
- $needsRehash : bool
- Whether the last verified hash should be rehashed
- $password : string|null
- Authentication password
- $passwordField : string
- Password field
- $result : int
- Authentication result
- $user : mixed
- User data
- $username : string|null
- Authentication username
- $usernameField : string
- Username field
Methods
- __construct() : mixed
- Constructor
- authenticate() : int
- Method to authenticate
- getPassword() : string|null
- Get the password
- getPasswordField() : string
- Get the password field
- getResult() : int
- Get the authentication result
- getUser() : mixed
- Get the user record
- getUsername() : string|null
- Get the username
- getUsernameField() : string
- Get the username field
- isAuthenticated() : bool
- Determine if the authentication attempt was successful
- needsRehash() : bool
- Determine if the last verified hash should be rehashed
- setAudience() : Jwt
- Set the audience to validate the 'aud' claim against
- setIssuer() : Jwt
- Set the issuer to validate the 'iss' claim against
- setLeeway() : Jwt
- Set the leeway (in seconds) allowed for exp/nbf claim comparisons
- setPassword() : AbstractAuth
- Set the password
- setPasswordField() : static
- Set the password field
- setUsername() : AbstractAuth
- Set the username
- setUsernameField() : static
- Set the username field
- verify() : bool
- Method to verify a password against a hash
- base64UrlDecode() : string
- Base64url-decode a JWT segment
- claimsAreValid() : bool
- Determine if the token's claims (exp/nbf/aud/iss) are valid
- derInteger() : string
- DER-encode a single ASN.1 INTEGER
- derLength() : string
- DER-encode a length value
- esSignatureToDer() : string
- Convert a JOSE ES256 raw R||S signature into the DER-encoded ASN.1 sequence openssl_verify() expects
Constants
ALGORITHMS
Supported algorithms
public
array<string|int, mixed>
ALGORITHMS
= ['HS256', 'RS256', 'ES256']
ES256_SIGNATURE_LENGTH
Expected byte length of a raw ES256 (P-256) JOSE signature: 32-byte R + 32-byte S (RFC 7518 §3.4)
public
int
ES256_SIGNATURE_LENGTH
= 64
NOT_VALID
Constant for auth result
public
int
NOT_VALID
= 0
VALID
public
mixed
VALID
= 1
Properties
$algorithm
Algorithm
protected
string
$algorithm
$audience
Audience to validate the 'aud' claim against
protected
string|null
$audience
= null
$issuer
Issuer to validate the 'iss' claim against
protected
string|null
$issuer
= null
$key
Key (shared secret for HS256, PEM public key for RS256/ES256)
protected
string
$key
$leeway
Leeway (in seconds) allowed for exp/nbf claim comparisons
protected
int
$leeway
= 0
$needsRehash
Whether the last verified hash should be rehashed
protected
bool
$needsRehash
= false
$password
Authentication password
protected
string|null
$password
= null
$passwordField
Password field
protected
string
$passwordField
= 'password'
$result
Authentication result
protected
int
$result
= 0
$user
User data
protected
mixed
$user
= null
$username
Authentication username
protected
string|null
$username
= null
$usernameField
Username field
protected
string
$usernameField
= 'username'
Methods
__construct()
Constructor
public
__construct(string $algorithm, string $key) : mixed
Instantiate the Jwt auth adapter object
Parameters
- $algorithm : string
- $key : string
Tags
authenticate()
Method to authenticate
public
authenticate(string $token[, string|null $secondary = null ]) : int
Parameters
- $token : string
- $secondary : string|null = null
Tags
Return values
intgetPassword()
Get the password
public
getPassword() : string|null
Return values
string|nullgetPasswordField()
Get the password field
public
getPasswordField() : string
Return values
stringgetResult()
Get the authentication result
public
getResult() : int
Return values
intgetUser()
Get the user record
public
getUser() : mixed
getUsername()
Get the username
public
getUsername() : string|null
Return values
string|nullgetUsernameField()
Get the username field
public
getUsernameField() : string
Return values
stringisAuthenticated()
Determine if the authentication attempt was successful
public
isAuthenticated() : bool
Return values
boolneedsRehash()
Determine if the last verified hash should be rehashed
public
needsRehash() : bool
Return values
boolsetAudience()
Set the audience to validate the 'aud' claim against
public
setAudience(string $audience) : Jwt
Parameters
- $audience : string
Return values
JwtsetIssuer()
Set the issuer to validate the 'iss' claim against
public
setIssuer(string $issuer) : Jwt
Parameters
- $issuer : string
Return values
JwtsetLeeway()
Set the leeway (in seconds) allowed for exp/nbf claim comparisons
public
setLeeway(int $seconds) : Jwt
Parameters
- $seconds : int
Return values
JwtsetPassword()
Set the password
public
setPassword(string $password) : AbstractAuth
Parameters
- $password : string
Return values
AbstractAuthsetPasswordField()
Set the password field
public
setPasswordField(string $passwordField) : static
Parameters
- $passwordField : string
Return values
staticsetUsername()
Set the username
public
setUsername(string $username) : AbstractAuth
Parameters
- $username : string
Return values
AbstractAuthsetUsernameField()
Set the username field
public
setUsernameField(string $usernameField) : static
Parameters
- $usernameField : string
Return values
staticverify()
Method to verify a password against a hash
public
verify(string $password, string $hash) : bool
Parameters
- $password : string
- $hash : string
Return values
boolbase64UrlDecode()
Base64url-decode a JWT segment
protected
static base64UrlDecode(string $data) : string
Parameters
- $data : string
Return values
stringclaimsAreValid()
Determine if the token's claims (exp/nbf/aud/iss) are valid
protected
claimsAreValid(array<string|int, mixed> $payload) : bool
Parameters
- $payload : array<string|int, mixed>
Return values
boolderInteger()
DER-encode a single ASN.1 INTEGER
protected
static derInteger(string $bytes) : string
Parameters
- $bytes : string
Return values
stringderLength()
DER-encode a length value
protected
static derLength(int $length) : string
Parameters
- $length : int
Return values
stringesSignatureToDer()
Convert a JOSE ES256 raw R||S signature into the DER-encoded ASN.1 sequence openssl_verify() expects
protected
static esSignatureToDer(string $signature) : string
Parameters
- $signature : string