Download the PHP package andrewdyer/jwt-auth without Composer
On this page you can find all versions of the php package andrewdyer/jwt-auth. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andrewdyer/jwt-auth
More information about andrewdyer/jwt-auth
Files in andrewdyer/jwt-auth
Package jwt-auth
Short Description A framework-agnostic library for issuing, parsing, and authenticating JWT tokens via pluggable providers
License MIT
Homepage https://github.com/andrewdyer/jwt-auth
Informations about the package jwt-auth

JWT Auth
A framework-agnostic PHP JWT authentication library for managing tokens, built around contracts for user resolution and claims generation.
Introduction
This library provides a clean, contract-driven approach to JSON Web Token authentication by coordinating token issuance and parsing while delegating token handling, user resolution, and claims generation to user-defined implementations. By relying on simple interfaces, it remains fully framework-agnostic and unopinionated, allowing integration with any authentication system or JWT library.
Prerequisites
Installation
Getting Started
1. Implement the JWT subject
Any class that represents an authenticated user or entity must implement JwtSubjectInterface. This provides the identifier that will be stored in the token's sub claim.
2. Implement the auth provider
A class implementing AuthProviderInterface must be provided to resolve users by credentials or by ID. JwtAuth calls these methods internally during attempt() and authenticate().
3. Implement the JWT provider
A class implementing JwtProviderInterface handles token encoding and decoding. This is the integration point for a preferred JWT library such as firebase/php-jwt or lcobucci/jwt.
4. Implement the claims factory
A class implementing ClaimsFactoryInterface builds the JWT claims for a given user. The iat, nbf, and exp fields accept plain Unix timestamps from time(), Carbon, or any other source.
When using Carbon, Carbon::now()->timestamp is a drop-in replacement for time().
Usage
Create a JwtAuth instance
The three dependencies are wired up to create a JwtAuth instance:
Attempt a login
Validates a username and password and returns a signed token. Throws InvalidCredentialsException if the credentials are invalid.
Authenticate a token
Decodes a token, verifies it, and returns the corresponding user. Throws InvalidTokenException if the token is invalid or the user cannot be found.
Parse a token
Decodes a token into a Claims object without looking up the user.
Claims
The Claims class is a read-only value object representing the payload of a JWT. It exposes the standard registered claims as typed public properties:
| Property | Type | Description |
|---|---|---|
iss |
string |
Issuer |
aud |
?string |
Audience |
iat |
int |
Issued-at timestamp |
nbf |
int |
Not-before timestamp |
exp |
int |
Expiry timestamp |
jti |
string |
Unique token identifier |
sub |
int\|string |
Subject identifier (user ID) |
custom |
array |
Any additional custom claims |
A Claims instance can be serialized back to an array using toArray(), which omits null values:
A Claims instance can also be constructed directly from an array. The claims iss, iat, nbf, exp, jti, and sub are all required; aud is optional and defaults to null if omitted. All values must match their expected types — throws InvalidTokenException if any required claim is missing or any claim has an invalid type:
Any keys not in the standard set are captured in the custom array.
Exceptions
| Exception | Thrown when |
|---|---|
InvalidCredentialsException |
attempt() is called and the credentials do not resolve to a valid user |
InvalidTokenException |
A token cannot be decoded, required claims are missing or have invalid types, or the subject cannot be resolved to a valid user |
Both extend RuntimeException.
License
Licensed under the MIT license and is free for private or commercial projects.