Authentication
The Cat Data API uses Auth0 for secure JWT (JSON Web Token) authentication. All API endpoints except the root health check require a valid JWT token in the request headers.Overview
Authentication is handled using the RS256 asymmetric algorithm. The API validates tokens by:- Extracting the JWT from the
Authorizationheader - Fetching Auth0’s public keys from the JWKS endpoint
- Verifying the token signature using the public key
- Validating the token’s audience and issuer claims
Authentication Flow
Required Headers
Every authenticated request must include theAuthorization header with a Bearer token:
Example Request
Getting an Auth0 Token
To authenticate with the Cat Data API, you need to obtain a JWT token from Auth0. Here’s the general process:Configure Auth0 API
In your Auth0 dashboard:
- Create an API (or use an existing one)
- Set the API identifier (this becomes your
AUTH0_AUDIENCE) - Choose RS256 as the signing algorithm
Create an Auth0 Application
Create an application that will request tokens:
- Go to Applications in Auth0 dashboard
- Create a new application (Machine to Machine, SPA, or Regular Web App)
- Authorize the application to access your API
JWT Validation Implementation
The API uses theexpress-jwt and jwks-rsa packages to validate tokens. Here’s the actual middleware code from the Cat Data API:
Key Configuration Parameters
| Parameter | Value | Description |
|---|---|---|
cache | true | Caches signing keys to reduce requests to Auth0 |
rateLimit | true | Prevents excessive requests to JWKS endpoint |
jwksRequestsPerMinute | 5 | Maximum JWKS requests per minute |
audience | From .env | Expected audience claim in the token |
issuer | From .env | Expected issuer claim (Auth0 domain) |
algorithms | ['RS256'] | Allowed signing algorithm |
The JWKS (JSON Web Key Set) endpoint is automatically constructed by appending
.well-known/jwks.json to your Auth0 domain.Environment Configuration
The authentication middleware requires these environment variables:Important: The
AUTH0_DOMAIN must include the trailing slash (/) since the code directly appends .well-known/jwks.json to it.Token Validation Process
When a request arrives, thecheckJwt middleware:
- Extracts the token from the
Authorization: Bearerheader - Decodes the token header to identify which key was used to sign it
- Fetches the signing key from Auth0’s JWKS endpoint (or uses cached key)
- Verifies the signature using the RS256 algorithm and public key
- Validates claims:
aud(audience) matchesAUTH0_AUDIENCEiss(issuer) matchesAUTH0_DOMAINexp(expiration) is not in the past
- Attaches decoded token to
req.authfor use in route handlers
Common Authentication Errors
401 Unauthorized - No Token Provided
Authorization header is missing from the request.
Solution: Include the header with format Authorization: Bearer YOUR_TOKEN
401 Unauthorized - Invalid Token
- Token is not properly formatted
- Token is corrupted or incomplete
401 Unauthorized - Expired Token
exp (expiration) claim is in the past.
Solution: Request a new token from Auth0
401 Unauthorized - Invalid Signature
- Token was signed with a different key
- Token was manually modified
- Wrong Auth0 domain configured
AUTH0_DOMAIN environment variable matches the token issuer
401 Unauthorized - Invalid Audience
aud claim doesn’t match the configured AUTH0_AUDIENCE.
Solution: Ensure you’re requesting tokens with the correct audience parameter
401 Unauthorized - Invalid Issuer
iss claim doesn’t match the configured AUTH0_DOMAIN.
Solution: Verify the AUTH0_DOMAIN in your .env file
Testing Authentication
You can test the authentication flow using these approaches:Using Auth0’s Test Tab
- Go to your API in the Auth0 dashboard
- Click the “Test” tab
- Copy the test token provided
- Use it in your API requests
Using Postman
Configure Token Request
- Token URL:
https://YOUR_DOMAIN.auth0.com/oauth/token - Client ID: Your Auth0 application client ID
- Client Secret: Your Auth0 application client secret
- Audience: Your API identifier
- Grant Type: Client Credentials
Security Best Practices
Use HTTPS
Always use HTTPS in production to prevent token interception
Short Token Lifetimes
Configure short expiration times (e.g., 24 hours or less)
Secure Storage
Store tokens securely (e.g., httpOnly cookies, secure storage)
Validate Scopes
Implement scope-based authorization for granular permissions
Protected Endpoints
All endpoints except/ require authentication. Here’s how the middleware is applied in the API:
checkJwt middleware is applied to each protected route, ensuring that only authenticated requests can access sensitive operations.