-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-dynamicclient.php
309 lines (252 loc) · 6.52 KB
/
class-dynamicclient.php
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
<?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 SCHEMA = array(
'type' => 'object',
'properties' => array(
'software_id' => array(
'type' => 'string',
'format' => 'uuid', // Todo support in rest_validate
'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 Client|WP_Error */
private $persisted = false;
/**
* DynamicClient constructor.
*
* @param \stdClass $statement Software Statement.
*/
protected function __construct( $statement ) {
$this->statement = $statement;
}
/**
* Create a Dynamic Client from a JWT.
*
* @param string $jwt
*
* @return DynamicClient|WP_Error
*/
public static function from_jwt( $jwt ) {
$statement = JWT::decode( $jwt, '', array( 'none' ), 'unsecure' );
$valid = static::validate_statement( $statement );
if ( is_wp_error( $valid ) ) {
return $valid;
}
return new static( $statement );
}
/**
* 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' ) );
}
}
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;
}
/**
* 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 );
if ( current_user_can( 'publish_post', $client->get_post_id() ) ) {
$approved = $client->approve();
if ( is_wp_error( $approved ) ) {
return $approved;
}
}
return $client;
}
}