Something went wrong. Try again.
Laravel AT Protocol Client (alpha & unstable)
Something went wrong. Try again.
2.0 kB · 103 lines
PHP
at dev
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104<?php
namespace SocialDept\AtpClient\Crypto;
use Illuminate\Contracts\Support\Arrayable;use Illuminate\Contracts\Support\Jsonable;use phpseclib3\Crypt\EC\PrivateKey;use phpseclib3\Crypt\EC\PublicKey;use Stringable;
class JsonWebKey implements Arrayable, Jsonable, Stringable{ protected PrivateKey|PublicKey $key;
protected string $kid = 'key-1';
public function __construct(PrivateKey|PublicKey $key) { $this->key = $key; }
/** * Get the underlying key */ public function key(): PrivateKey|PublicKey { return $this->key; }
/** * Get public key version */ public function asPublic(): static { if ($this->key instanceof PrivateKey) { $clone = clone $this; $clone->key = $this->key->getPublicKey();
return $clone; }
return $this; }
/** * Get key ID */ public function kid(): string { return $this->kid; }
/** * Set key ID */ public function withKid(string $kid): static { $this->kid = $kid;
return $this; }
/** * Export as PEM */ public function toPEM(): string { return $this->key->toString('PKCS8'); }
/** * Convert to array (JWK format) */ public function toArray(): array { $jwk = $this->key->getPublicKey()->toString('JWK');
return array_merge( json_decode($jwk, true), [ 'alg' => P256::ALG, 'use' => 'sig', 'kid' => $this->kid(), ] ); }
/** * Convert to JSON */ public function toJson($options = 0): string { return json_encode($this->toArray(), JSON_THROW_ON_ERROR | $options); }
/** * Convert to string */ public function __toString(): string { return $this->toJson(); }}