codercat/jwk-to-pem
Convert RSA JSON Web Keys (JWK) to PEM public keys in PHP. Simple API via JWKConverter->toPEM() for turning JWK arrays into PEM strings, useful for verifying JWT signatures. Note: currently supports RSA keys only.
openssl).tymon/jwt-auth, spatie/laravel-ignition, or custom auth services) often requires PEM keys for cryptographic operations. This package bridges the gap between modern JWK-based APIs (e.g., Auth0, Okta) and PHP’s traditional PEM dependency.JWKConverter) with no framework assumptions, making it easy to integrate into existing Laravel services or commands.phpseclib (v3+), which is stable but adds ~1MB to your vendor directory. No direct Laravel dependencies, so no risk of version conflicts with the framework.openssl or a polyfill) for broader use cases.| Risk Area | Assessment |
|---|---|
| Functional Gaps | RSA-only limitation may force workarounds (e.g., manual PEM generation via openssl or a multi-package solution). |
| Compatibility | Tested on PHP 7.1+, but Laravel 10+ (PHP 8.1+) may expose edge cases (e.g., type hints, strict mode). |
| Performance | Minimal overhead for conversion; bottleneck would be in key size (e.g., 4096-bit RSA). |
| Security | MIT license is permissive; no known vulnerabilities in phpseclib or the package itself. However, validate JWK input to prevent malformed key injection (e.g., check kty, n, e fields). |
| Maintenance | Last release in 2021; no active maintenance. Risk of drift with PHP 8.x+ or phpseclib updates. |
openssl for ECC) be acceptable?kty, invalid base64 n/e)?webtoken/jwt-framework’s JWK validation)?openssl or reject request)?kty)?lucasluis/php-jwk, firebase/php-jwt)?config/cache need to support PEM keys dynamically?tymon/jwt-auth or custom JWT validation where JWKs are fetched from a remote provider (e.g., Auth0) and need PEM conversion.config or cache to store PEM keys for reuse (e.g., avoid repeated conversions).openssl: Native PHP support for PEM generation, but requires manual JWK parsing (higher error risk).webtoken/jwt-framework: Includes JWK-to-PEM utilities but is heavier for this specific use case.lucasluis/php-jwk: Supports more key types but adds more dependencies.JWKConverter and add validation/logging:
namespace App\Services;
use CoderCat\JWKToPEM\JWKConverter;
use InvalidArgumentException;
class JwkToPemService {
public function __construct(private JWKConverter $converter) {}
public function convert(array $jwk): string {
if ($jwk['kty'] !== 'RSA') {
throw new InvalidArgumentException('Only RSA keys supported');
}
return $this->converter->toPEM($jwk);
}
}
config/auth.php or app/Providers/AuthServiceProvider with dynamically converted keys.Cache::remember) to avoid repeated conversions.openssl:
if ($jwk['kty'] === 'EC') {
return $this->generatePemFromEcJwk($jwk); // Custom method using openssl
}
phpseclib’s dependencies (e.g., sodium) are compatible with your server.use: "sig"). Private key conversion would require additional logic.composer require codercat/jwk-to-pem phpseclib/phpseclib
JWKConverter and your wrapper service in AppServiceProvider:
$this->app->bind(JWKConverter::class, function ($app) {
return new JWKConverter();
});
use App\Services\JwkToPemService;
class ConvertJwkCommand extends Command {
protected $signature = 'keys:convert-jwk';
public function handle(JwkToPemService $service) {
$jwk = $this->getJwkInput();
$pem = $service->convert($jwk);
file_put_contents('key.pem', $pem);
}
}
Route::post('/validate-jwt', function (Request $request, JwkToPemService $service) {
$jwk = $request->json()->all();
$pem = $service->convert($jwk);
// Use $pem with a JWT library (e.g., firebase/php-jwt)
});
codercat/jwk-to-pem or phpseclib updates.phpseclib to a specific version (e.g., `How can I help you explore Laravel packages today?