TokenKeep is a ultra-lightweight, self-hosted PHP & SQLite microservice designed to safely run the OAuth2 Authorization Code Flow once per external provider (e.g., Zoho, Pipedrive, Google) and act as an internal gateway to fetch valid, auto-refreshing access tokens via simple HTTP API.
sequenceDiagram
autonumber
actor Dev as Developer / App
participant TK as TokenKeep Service
participant SQLite as SQLite DB
participant OAuth as External Provider (API)
Note over Dev, OAuth: 1. Setup Phase (Once)
Dev->>TK: Register provider in DB (via CLI or SQL)
Dev->>TK: Direct browser to /auth.php?provider=zoho
TK-->>Dev: Redirect to Provider Auth Page
Dev->>OAuth: Authorize Application
OAuth-->>TK: Callback to /callback.php with ?code=XYZ
TK->>OAuth: Exchanges code for Tokens (POST)
OAuth-->>TK: Returns Access & Refresh tokens
TK->>SQLite: Saves tokens with expires_at timestamp
Note over Dev, OAuth: 2. Operation Phase (Token request)
Dev->>TK: GET /api/token.php?provider=zoho with Bearer Key
alt Token is valid (> 5 minutes left)
TK->>SQLite: Reads access_token
SQLite-->>TK: Token details
TK-->>Dev: Returns {"access_token": "..."}
else Token is expired (or close to it)
TK->>SQLite: Reads refresh_token
TK->>OAuth: Request new access_token using refresh_token
OAuth-->>TK: Returns new Access & Refresh tokens
TK->>SQLite: Updates tokens in Database
TK-->>Dev: Returns refreshed {"access_token": "..."}
end
- Zero Heavy Dependencies: Pure PHP 8.2+, no frameworks.
- SQLite Database: Maximum portability with a single
.sqlitefile. - Secure by Design: Core files, SQLite database, and configuration are placed outside the web-accessible
publicroot folder. - Lazy-Refresh: Checks token freshness and auto-refreshes on the fly before delivery.
- CLI Cron Utility: Optional cron job scripts to force-refresh tokens in the background.
- PHP 8.2 or higher
- PDO SQLite extension (
pdo_sqlite) - PHP cURL extension
- Clone or download the repository.
- Duplicate the default configuration:
cp config.example.php config.php
- Open
config.phpand set up yourapi_secret_tokenwith a strong random string. - Register your first OAuth Provider using the CLI helper:
php register_provider.php
- Point your web server's document root to
/public, or start the built-in PHP server:php -S localhost:8000 -t public/
You can launch TokenKeep in a single command using Docker:
cp config.example.php config.php
docker-compose up -d --buildThe application will be accessible at http://localhost:8000.
Direct your browser to:
GET http://localhost:8000/auth.php?provider=your_provider_aliasTokenKeep will automatically generate secure CSRF states and redirect your browser to the external provider's login screen.
Make an internal API request from your backend:
curl -X GET "http://localhost:8000/api/token.php?provider=your_provider_alias" \
-H "Authorization: Bearer YOUR_SUPER_SECRET_TOKEN_KEEP_KEY_HERE"Successful Response:
{
"access_token": "your_valid_oauth2_access_token_here"
}To ensure tokens never expire even without live traffic requests, register a system cron job to run the script every 15 minutes:
*/15 * * * * /usr/bin/php /path/to/tokenkeep/cron_refresh.php > /dev/null 2>&1This project is open-source and available under the MIT License.