namshi/jose
PHP library implementing JSON Object Signing and Encryption (JOSE): JWT, JWS and JWE. Create, sign, verify, encrypt and decrypt tokens using common algorithms and key formats. Useful for authentication, API security and secure data exchange.
Install via Composer:
composer require namshi/jose
Start with JWS for token signing — the most common use case (e.g., API authentication). For a minimal working example:
use Namshi\JOSE\JWS;
$jws = new JWS('HS256');
$token = $jws->encode(['sub' => 'user-123'], env('JWT_SECRET'));
$payload = $jws->decodeAndVerify($token, env('JWT_SECRET'));
First step: review tests/ in the repo — they provide clean, minimal examples for signing, verifying, and encrypting tokens with common algorithms.
JwtService class (e.g., app/Services/JwtService.php) with methods createToken(), validateToken(), revokeToken() (for refresh tokens). Avoid ad-hoc usage in controllers.ServiceProvider and inject via constructor. Use config('app.key') only for fallback; prefer config('auth.jwt.secret') for explicit control.JWE with RSA-OAEP + AES-GCM. Store private keys securely via Laravel’s Crypt facade or external KMS.exp, iat, jti) manually in payload — namshi/jose won’t auto-add them. Use Carbon::now()->addMinutes(15)->timestamp for exp.JWS/JWE in feature tests; use hardcoded secret in phpunit.xml for deterministic token generation.JWS::setAllowedAlgorithms(['RS256', 'HS256']).decodeAndVerify() does not validate time-based claims — add manual checks:
if ($payload->exp && $payload->exp < time()) { /* reject */ }
if ($payload->nbf && $payload->nbf > time()) { /* reject */ }
openssl_pkey_get_private() to validate keys before encoding/decoding.decode() bypasses signature checks — never use in production. Always pair with decodeAndVerify() or validate signature manually.composer.json:
"replace": {
"namshi/jose": "self.version"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/your-fork/namshi-jose"
}
]
Then apply patches for PHP 8+ type deprecations. Prefer modern alternatives like web-token/jwt-library for new apps.How can I help you explore Laravel packages today?