Something went wrong. Try again.
Laravel AT Protocol Client (alpha & unstable)
Something went wrong. Try again.
2.0 kB · 78 lines
PHP
at dev
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879<?php
namespace SocialDept\AtpClient\Data;
use phpseclib3\Crypt\Common\PrivateKey;use phpseclib3\Crypt\Common\PublicKey;use phpseclib3\Crypt\PublicKeyLoader;
class DPoPKey{ protected string $privateKeyPem;
protected string $publicKeyPem;
public function __construct( PrivateKey|string $privateKey, PublicKey|string $publicKey, public readonly string $keyId, ) { // Store as PEM strings for serialization $this->privateKeyPem = $privateKey instanceof PrivateKey ? $privateKey->toString('PKCS8') : $privateKey;
$this->publicKeyPem = $publicKey instanceof PublicKey ? $publicKey->toString('PKCS8') : $publicKey; }
public function getPrivateKey(): PrivateKey { return PublicKeyLoader::load($this->privateKeyPem); }
public function getPublicKey(): PublicKey { return PublicKeyLoader::load($this->publicKeyPem); }
public function getPublicJwk(): array { $jwks = json_decode($this->getPublicKey()->toString('JWK'), true);
// phpseclib returns JWKS format {"keys":[...]}, extract the first key $jwk = $jwks['keys'][0] ?? $jwks;
return array_merge( $jwk, [ 'alg' => 'ES256', 'use' => 'sig', 'kid' => $this->keyId, ] ); }
public function getPrivateJwk(): array { $jwks = json_decode($this->getPrivateKey()->toString('JWK'), true);
// phpseclib returns JWKS format {"keys":[...]}, extract the first key $jwk = $jwks['keys'][0] ?? $jwks;
return array_merge( $jwk, [ 'alg' => 'ES256', 'use' => 'sig', 'kid' => $this->keyId, ] ); }
public function toPEM(): string { return $this->privateKeyPem; }}