Download the PHP package uqi/cognito-token without Composer
On this page you can find all versions of the php package uqi/cognito-token. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download uqi/cognito-token
More information about uqi/cognito-token
Files in uqi/cognito-token
Package cognito-token
Short Description Lightweight JWT token management for cognito.
License MIT
Informations about the package cognito-token
Cognito Token
A PHP library for verifying AWS Cognito JWT tokens (ID tokens and Access tokens).
Overview
The CognitoTokenVerifier class provides functionality to verify JSON Web Tokens (JWTs) issued by AWS Cognito. It handles:
- Signature verification using JSON Web Keys (JWK)
- Token expiration validation
- Issuer validation
- Audience validation for ID tokens
- Client ID validation for access tokens
Installation
Requirements
- PHP 7.4 or higher
web-token/jwt-signaturepackageweb-token/jwt-corepackage
Implementation Options
You have two options for implementing token verification in your application:
Option 1: Use this library directly
The simplest approach is to use this library as-is, which provides a complete solution for Cognito token verification.
Option 2: Create your own implementation
If you need custom functionality or want to integrate with specific systems, you can create your own implementation based on the principles in this library:
- Fetch the JWKS (JSON Web Key Set) from Cognito
- Parse and verify the JWT signature using the appropriate JWK
- Validate token claims (expiration, issuer, audience, etc.)
This approach gives you more control but requires deeper understanding of JWT verification.
Basic Usage
Caching
The library supports caching of JWKS (JSON Web Key Sets) to improve performance. By default, it uses a no-cache implementation, but you can provide your own cache implementation:
Implementing a Cache Driver
Create a class that implements the CacheInterface:
Laravel Integration
For Laravel users, a ready-to-use cache implementation is included with the library:
Usage with Laravel:
Error Handling
The library throws CognitoTokenException with specific error codes:
| Error Code | Description |
|---|---|
| JWKS_FETCH_FAILED | Failed to fetch JWKS from the remote URL |
| JWKS_INVALID_FORMAT | Invalid JWKS format - 'keys' not found |
| NO_KID_IN_TOKEN | No 'kid' found in JWT header |
| NO_JWK_FOR_KID | No matching JWK found for the specified kid |
| SIGNATURE_VERIFICATION_FAILED | JWT signature verification failed |
| TOKEN_PAYLOAD_DECODING_FAILED | Failed to decode JWT payload |
| INVALID_TOKEN | Invalid token |
| INVALID_ISSUER | Invalid issuer in token |
| TOKEN_EXPIRED | Token is expired |
| INVALID_AUDIENCE | Invalid audience in ID token |
| MISSING_SUBJECT | Missing subject (sub) claim in ID token |
| INVALID_CLIENT_ID_ACCESS | Invalid client_id in access token |
Example error handling:
Class Reference
CognitoTokenVerifier
Constructor
Methods
CacheInterface
License
MIT