vonage/jwt
PHP 8.1+ library to generate JWTs for authenticating with Vonage APIs. Install via Composer, create a TokenGenerator with your Vonage Application ID and private key, then call generate() to get a bearer token for requests.
Installation
composer require vonage/jwt
No additional configuration is required—this is a standalone library.
First Use Case: Generating a JWT for Vonage APIs
use Vonage\JWT\JWT;
$jwt = new JWT();
$token = $jwt->generate([
'iss' => 'your-app-id', // Vonage API issuer (your app ID)
'sub' => 'user-123', // Subject (e.g., user ID or endpoint)
'iat' => time(), // Issued at (timestamp)
'exp' => time() + 3600,// Expiration (1 hour later)
'aud' => 'vonage', // Audience (Vonage APIs)
'jti' => bin2hex(random_bytes(16)) // Unique token ID
], 'your-secret-key'); // Your Vonage API secret key
Where to Look First
JWT.php (minimal class with no external dependencies).tests/ for edge cases (e.g., invalid keys, expired tokens).Payload Structure Vonage APIs enforce specific claims. Use this template:
$payload = [
'iss' => env('VONAGE_APP_ID'), // Required by Vonage
'sub' => $user->id, // Customizable (e.g., user ID or endpoint)
'iat' => time(),
'exp' => time() + 300, // Short-lived tokens (5 mins) for security
'aud' => 'vonage',
'jti' => Str::uuid()->toString(),
'scope' => ['sms:send', 'verify:start'], // Permissions (API-specific)
];
VONAGE_APP_ID and VONAGE_API_SECRET in .env.Token Rotation Generate new tokens for each request to Vonage APIs (avoid long-lived tokens):
$token = $jwt->generate($payload, env('VONAGE_API_SECRET'));
$client->setAuth($token); // Integrate with Vonage SDK (e.g., `vonage/client`)
Validation (Optional) Verify tokens before use (e.g., in middleware):
use Vonage\JWT\JWT;
$jwt = new JWT();
$isValid = $jwt->verify($token, env('VONAGE_API_SECRET'));
Service Provider Bind the JWT class for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(JWT::class, fn() => new JWT());
}
API Requests Use the token in Vonage SDK calls:
$client = new \Vonage\Client\Credentials\Basic(
env('VONAGE_API_KEY'),
env('VONAGE_API_SECRET')
);
$client->setAuth($jwt->generate($payload, env('VONAGE_API_SECRET')));
Caching Tokens (Advanced) Cache tokens for 1 minute to reduce generation overhead:
$token = Cache::remember('vonage_jwt_token', 60, function () {
return $jwt->generate($payload, env('VONAGE_API_SECRET'));
});
Secret Key Management
.env.env() or config() helper:
$secret = config('services.vonage.secret');
Token Expiration
exp > 24 hours.exp to time() + 3600 (1 hour) or shorter.Payload Validation
iss, aud, sub) will fail silently in some Vonage APIs.$required = ['iss', 'sub', 'iat', 'exp', 'aud', 'jti'];
foreach ($required as $claim) {
if (!array_key_exists($claim, $payload)) {
throw new \InvalidArgumentException("Missing claim: {$claim}");
}
}
Clock Skew
exp validation failures.exp.Decode Tokens Locally Use jwt.io to debug payloads/headers. For PHP:
$decoded = (array) JWT::decode($token, env('VONAGE_API_SECRET'), ['HS256']);
Vonage API Errors
scope claims.Custom Claims
Add API-specific claims (e.g., application_id for Vonage Verify):
$payload['application_id'] = env('VONAGE_APPLICATION_ID');
Algorithm Support
The package defaults to HS256. For RS256 (public/private keys):
$jwt->generate($payload, ['private_key' => file_get_contents('path/to/key.pem'), 'algorithm' => 'RS256']);
Testing Mock the JWT class in tests:
$mockJWT = Mockery::mock(JWT::class);
$mockJWT->shouldReceive('generate')->andReturn('mock-token');
$this->app->instance(JWT::class, $mockJWT);
HS256/HS512 (avoid weak keys).How can I help you explore Laravel packages today?