1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2014-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Protocols\OAuth;
24:
25: use SimpleID\Protocols\HTTPResponse;
26: use SimpleID\Crypt\Random;
27:
28: /**
29: * A class representing an OAuth dynamic client.
30: */
31: class OAuthDynamicClient extends OAuthClient {
32:
33: protected $dynamic = true;
34:
35: public function __construct($data = []) {
36: parent::__construct($data);
37:
38: $rand = new Random();
39: $this->cid = '_' . $rand->id() . '.oauth';
40: }
41:
42: /**
43: * Fetches the JSON web key set from the `jwks_uri` parameter.
44: *
45: * @return void
46: */
47: public function fetchJWKs() {
48: if (isset($this->container['oauth']['jwks_uri'])) {
49: $web = \Web::instance();
50:
51: $response = new HTTPResponse($web->request($this->container['oauth']['jwks_uri'], [ 'headers' => [ 'Accept' => 'application/jwk-set+json,application/json,text/plain,application/octet-stream' ] ]));
52: if ($response->isHttpError()) return;
53:
54: $jwks = json_decode($response->getBody(), true);
55: if ($jwks == NULL) return;
56:
57: $this->container['oauth']['jwks'] = $jwks;
58: }
59: }
60:
61: /**
62: * Returns the dynamic client's metadata.
63: *
64: * @return array<string, mixed> the dynamic client's metadata
65: */
66: public function getDynamicClientInfo() {
67: $results = array_merge($this->container['oauth'], [
68: 'client_id' => $this->getStoreID()
69: ]);
70:
71: // if jwk_uri exists, we delete jwks as we retreived this ourselves
72: if (isset($results['jwk_uri'])) unset($results['jwks']);
73:
74: return $results;
75: }
76: }
77:
78: ?>