1. Go to this page and download the library: Download baconfy/secure-tokens library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
return [
// Token prefixes (appear in the generated keys)
'prefix' => [
'secret' => env('API_KEYS_SECRET_PREFIX', 'sk'),
'public' => env('API_KEYS_PUBLIC_PREFIX', 'pk'),
],
// Valid environment identifiers
'environments' => ['live', 'test'],
// Default expiration in minutes (null = never expires)
'expiration' => null,
// Require Ed25519 signature on all requests (global toggle)
'
use Baconfy\ApiKeys\Contracts\HasApiKeys as HasApiKeysContract;
use Baconfy\ApiKeys\HasApiKeys;
class User extends Authenticatable implements HasApiKeysContract
{
use HasApiKeys;
}
// Create with default abilities (wildcard) and no expiration
$apiKey = $user->createApiKey('Production', 'live');
// Access the keys (only available at creation time)
$apiKey->secretKey; // "sk_live_xxxxx" — give this to the client
$apiKey->publicKey; // "pk_live_xxxxx"
$apiKey->apiKey; // The persisted ApiKey Eloquent model
// Create with specific abilities
$apiKey = $user->createApiKey('Read Only', 'live', ['invoices:read', 'customers:read']);
// Create with expiration
$apiKey = $user->createApiKey('Temp Key', 'test', ['*'], now()->addDays(30));
Route::middleware('auth:api-key')->get('/invoices', function (Request $request) {
$apiKey = $request->apiKey();
if ($apiKey->can('invoices:read')) {
// The key has the "invoices:read" ability
}
if ($apiKey->cant('invoices:delete')) {
abort(403, 'Insufficient permissions.');
}
// Keys with ["*"] abilities pass all checks
});
// Revoke a specific key by ID
$user->apiKeys()->where('id', $keyId)->delete();
// Revoke all keys for a user
$user->apiKeys()->delete();
$apiKey = $user->apiKeys()->first();
$apiKey->isExpired(); // false
$apiKey->can('invoices:read'); // true
$apiKey->cant('invoices:delete'); // true
$apiKey->tokenable; // User model instance
// All API keys for the user
$user->apiKeys()->get();
$user->apiKeys()->count();
$user->apiKeys()->where('name', 'Production')->first();
// Create new key
$new = $user->createApiKey('My Key', 'live', ['read', 'write'], now()->addDays(90));