Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\Crypto;
4
5use Illuminate\Contracts\Support\Arrayable;
6use Illuminate\Contracts\Support\Jsonable;
7use Stringable;
8
9class JsonWebKeySet implements Arrayable, Jsonable, Stringable
10{
11 /**
12 * @var array<JsonWebKey>
13 */
14 protected array $keys = [];
15
16 /**
17 * Load default JWKS from OAuthKey
18 */
19 public static function load(): static
20 {
21 $key = \SocialDept\AtpClient\Auth\OAuthKey::load();
22 $kid = config('client.oauth.kid', 'key-1');
23
24 return (new static)->addKey($key->toJWK()->withKid($kid)->asPublic());
25 }
26
27 /**
28 * Add a key to the set
29 */
30 public function addKey(JsonWebKey $key): static
31 {
32 $this->keys[] = $key;
33
34 return $this;
35 }
36
37 /**
38 * Convert to array (JWKS format)
39 */
40 public function toArray(): array
41 {
42 return [
43 'keys' => collect($this->keys)->map(fn (JsonWebKey $key) => $key->toArray())->toArray(),
44 ];
45 }
46
47 /**
48 * Convert to JSON
49 */
50 public function toJson($options = 0): string
51 {
52 return json_encode($this->toArray(), JSON_THROW_ON_ERROR | $options);
53 }
54
55 /**
56 * Convert to string
57 */
58 public function __toString(): string
59 {
60 return $this->toJson();
61 }
62}