-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-dynamicclient.php
More file actions
404 lines (326 loc) · 8.88 KB
/
class-dynamicclient.php
File metadata and controls
404 lines (326 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* Dynamic Client type.
*
* @pacakage WordPress
* @subpackage JSON API
*/
namespace WP\OAuth2;
use WP\JWT\JWT;
use WP_Error;
use WP_User;
/**
* A Stateless Dynamic Client based on a signed Software Statement.
*
* @link https://tools.ietf.org/html/rfc7591#section-2.3
* @link https://tools.ietf.org/html/rfc7591#appendix-A.5.2
*/
class DynamicClient implements ClientInterface {
const SOFTWARE_ID_KEY = '_oauth2_software_id_';
const SOFTWARE_STATEMENT_KEY = '_oauth2_software_statement';
const VERIFIED_KEY = '_oauth2_verified_statement';
const SCHEMA = array(
'type' => 'object',
'properties' => array(
'software_id' => array(
'type' => 'string',
'format' => 'uuid',
'required' => true,
),
'client_name' => array(
'type' => 'string',
'required' => true,
'minLength' => 1,
'maxLength' => 255,
),
'client_uri' => array(
'type' => 'string',
'format' => 'uri',
'required' => true,
),
'redirect_uris' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
'format' => 'uri',
),
'required' => true,
'maxItems' => 1,
'minItems' => 1,
),
)
);
/** @var \stdClass */
private $statement;
/** @var bool */
private $verified;
/** @var Client|WP_Error */
private $persisted = false;
/**
* DynamicClient constructor.
*
* @param \stdClass $statement Software Statement.
* @param bool $verified Whether the statement was verified.
*/
protected function __construct( $statement, $verified ) {
$this->statement = $statement;
$this->verified = $verified;
}
/**
* Create a Dynamic Client from a JWT.
*
* @param string $jwt
*
* @return DynamicClient|WP_Error
*/
public static function from_jwt( $jwt ) {
$iss = JWT::get_claim( $jwt, 'iss' );
if ( ! is_wp_error( $iss ) ) {
$key = self::get_signing_key( $iss );
if ( is_wp_error( $key ) ) {
return $key;
}
$statement = JWT::decode( $jwt, $key, array( 'RS256' ) );
$verified = true;
} else {
$statement = JWT::decode( $jwt, '', array( 'none' ), 'unsecure' );
$verified = false;
}
$valid = static::validate_statement( $statement );
if ( is_wp_error( $valid ) ) {
return $valid;
}
return new static( $statement, $verified );
}
/**
* Gets the signing key for a JWT based on its ISS.
*
* @param string $iss
*
* @return resource|WP_Error
*/
protected static function get_signing_key( $iss ) {
$host = parse_url( $iss, PHP_URL_HOST );
if ( ! $host ) {
return new WP_Error( 'invalid_host', __( 'Could not get a valid host.', 'oauth2' ) );
}
$body = self::fetch_signing_key( $host );
if ( ! $body ) {
return new WP_Error( 'empty_body', __( 'Empty body returned by at the well known URL.', 'oauth2' ) );
}
$key = openssl_pkey_get_public( $body );
if ( ! is_resource( $key ) ) {
return new WP_Error( 'invalid_key', sprintf( __( 'Invalid public key: %s.', 'oauth2' ), openssl_error_string() ?: 'unknown' ) );
}
return $key;
}
/**
* Fetch the signing key from the given hostname.
*
* @param string $host
*
* @return string|WP_Error
*/
protected static function fetch_signing_key( $host ) {
$transient = 'oauth2_key_' . $host;
if ( false === ( $body = get_site_transient( $transient ) ) || ! is_string( $body ) ) {
$url = 'https://' . $host . '/.well-known/wp-api/oauth2.pem';
$response = wp_safe_remote_get( $url );
if ( is_wp_error( $response ) ) {
$body = '';
} else {
$body = trim( wp_remote_retrieve_body( $response ) );
}
set_site_transient( $transient, $body, 5 * MINUTE_IN_SECONDS );
}
return $body;
}
/**
* Validates the software statement.
*
* @param \stdClass $statement
*
* @return WP_Error|true
*/
public static function validate_statement( $statement ) {
$valid = rest_validate_value_from_schema( $statement, self::SCHEMA, 'statement' );
if ( is_wp_error( $valid ) ) {
return $valid;
}
$client_host = parse_url( $statement->client_uri, PHP_URL_HOST );
foreach ( $statement->redirect_uris as $redirect_uri ) {
$redirect_host = parse_url( $redirect_uri, PHP_URL_HOST );
if ( ! $redirect_host || $redirect_host !== $client_host ) {
return new WP_Error( 'client_uri_mismatch', __( 'The redirect URI is not on the same domain as the client URI.', 'oauth2' ) );
}
}
if ( isset( $statement->iss ) ) {
$iss_host = parse_url( $statement->iss, PHP_URL_HOST );
if ( ! $iss_host || $iss_host !== $client_host ) {
return new WP_Error( 'client_uri_mismatch', __( 'The statement issuing URI is not on the same domain as the client URI.', 'oauth2' ) );
}
}
return true;
}
public function get_id() {
return $this->statement->software_id;
}
public function get_name() {
return $this->statement->client_name;
}
public function get_description( $raw = false ) {
return sprintf( __( 'Unregistered application %s', 'oauth2' ), $this->get_name() );
}
public function get_type() {
return 'public';
}
public function get_secret() {
return '';
}
public function get_redirect_uris() {
return $this->statement->redirect_uris;
}
public function check_redirect_uri( $uri ) {
return validate_redirect_uri( $this, $uri );
}
public function generate_authorization_code( WP_User $user ) {
$client = $this->persist_dynamic_client();
if ( is_wp_error( $client ) ) {
return $client;
}
return $client->generate_authorization_code( $user );
}
public function get_authorization_code( $code ) {
$client = $this->persist_dynamic_client();
if ( is_wp_error( $client ) ) {
return $client;
}
return $client->get_authorization_code( $code );
}
public function regenerate_secret() {
$client = $this->persist_dynamic_client();
if ( is_wp_error( $client ) ) {
return $client;
}
return $client->regenerate_secret();
}
public function issue_token( WP_User $user, $meta = [] ) {
$client = $this->persist_dynamic_client();
if ( is_wp_error( $client ) ) {
return $client;
}
return $client->issue_token( $user, $meta );
}
public function update( $data ) {
return new WP_Error(
'oauth2.dynamic_client.no_update',
__( 'Dynamic Clients cannot be updated.', 'oauth2' )
);
}
public function delete() {
return false;
}
/**
* Check if a client has been approved for use.
*
* @return bool
*/
public function is_approved() {
$persisted = $this->find_persisted_dynamic_client();
if ( $persisted instanceof ClientInterface ) {
return $persisted->is_approved();
}
return false;
}
/**
* Approve a client.
*
* @return bool|WP_Error True if client was updated, error otherwise.
*/
public function approve() {
$persisted = $this->persist_dynamic_client();
if ( is_wp_error( $persisted ) ) {
return $persisted;
}
return $persisted->approve();
}
/**
* Get's the software statement.
*
* @return \stdClass
*/
public function get_software_statement() {
return $this->statement;
}
/**
* Checks if the software statement was verified as being signed by the client_uri.
*
* @return bool
*/
public function is_verified() {
return $this->verified;
}
/**
* Persists a dynamic client to a real client.
*
* @return Client|WP_Error
*/
public function persist_dynamic_client() {
if ( ! $this->persisted && ! $this->find_persisted_dynamic_client() ) {
$this->persisted = $this->create_persisted_dynamic_client();
}
return $this->persisted;
}
/**
* Find a persisted dynamic client by the software ID.
*
* @return Client|null
*/
protected function find_persisted_dynamic_client() {
if ( false === $this->persisted ) {
$query = new \WP_Query(
array(
'post_type' => Client::POST_TYPE,
'meta_key' => static::SOFTWARE_ID_KEY . $this->get_id(),
'meta_compare_key' => 'EXISTS',
'post_status' => 'any',
)
);
if ( $query->posts ) {
$this->persisted = Client::get_by_post_id( $query->posts[0]->ID );
} else {
$this->persisted = null;
}
}
return $this->persisted;
}
/**
* Creates the persisted form of the dynamic client.
*
* @return Client|WP_Error
*/
protected function create_persisted_dynamic_client() {
$data = array(
'name' => $this->get_name(),
'description' => '',
'meta' => array(
'callback' => $this->get_redirect_uris()[0],
'type' => $this->get_type(),
),
);
$client = Client::create( $data );
if ( is_wp_error( $client ) ) {
return $client;
}
update_post_meta( $client->get_post_id(), static::SOFTWARE_ID_KEY . $this->get_id(), 1 );
update_post_meta( $client->get_post_id(), static::SOFTWARE_STATEMENT_KEY, $this->statement );
update_post_meta( $client->get_post_id(), static::VERIFIED_KEY, $this->is_verified() );
if ( current_user_can( 'publish_post', $client->get_post_id() ) ) {
$approved = $client->approve();
if ( is_wp_error( $approved ) ) {
return $approved;
}
}
return $client;
}
}