PluginProbe
JSON API Auth / 1.2
JSON API Auth v1.2
3.1.2 3.1.1 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.7.0 2.7.1 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 trunk 0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.5.1 All 33 releases
json-api-auth / controllers / Auth.php

Auth.php in JSON API Auth 1.2, at controllers/Auth.php

499 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4
5 /*
6
7
8
9 Controller Name: Auth
10
11
12
13 Controller Description: Authentication add-on controller for the Wordpress JSON API plugin
14
15
16
17 Controller Author: Matt Berg
18
19
20
21 Controller Author Twitter: @mattberg
22
23
24
25 */
26
27
28
29
30
31
32
33 class JSON_API_Auth_Controller {
34
35
36
37
38
39
40
41 public function validate_auth_cookie() {
42
43
44
45 global $json_api;
46
47
48
49
50
51
52
53 if (!$json_api->query->cookie) {
54
55
56
57 $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method.");
58
59
60
61 }
62
63
64
65
66
67
68
69 $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
70
71
72
73
74
75
76
77 return array(
78
79
80
81 "valid" => $valid
82
83
84
85 );
86
87
88
89 }
90
91
92
93
94
95
96
97 public function generate_auth_cookie() {
98
99
100
101 global $json_api;
102
103
104
105
106
107
108
109 $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie');
110
111
112
113 if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
114
115
116
117 $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
118
119
120
121 }
122
123
124
125
126
127
128
129 if (!$json_api->query->username) {
130
131
132
133 $json_api->error("You must include a 'username' var in your request.");
134
135
136
137 }
138
139
140
141
142
143
144
145 if (!$json_api->query->password) {
146
147
148
149 $json_api->error("You must include a 'password' var in your request.");
150
151
152
153 }
154
155
156
157
158
159
160
161 $user = wp_authenticate($json_api->query->username, $json_api->query->password);
162
163
164
165 if (is_wp_error($user)) {
166
167
168
169 $json_api->error("Invalid username and/or password.", 'error', '401');
170
171
172
173 remove_action('wp_login_failed', $json_api->query->username);
174
175
176
177 }
178
179
180
181
182
183
184
185 $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user->ID, true);
186
187
188
189
190
191
192
193 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
194
195
196
197
198
199
200
201
202
203
204
205 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 return array(
222
223
224
225 "cookie" => $cookie,
226
227
228
229 "user" => array(
230
231
232
233 "id" => $user->ID,
234
235
236
237 "username" => $user->user_login,
238
239
240
241 "nicename" => $user->user_nicename,
242
243
244
245 "email" => $user->user_email,
246
247
248
249 "url" => $user->user_url,
250
251
252
253 "registered" => $user->user_registered,
254
255
256
257 "displayname" => $user->display_name,
258
259
260
261 "firstname" => $user->user_firstname,
262
263
264
265 "lastname" => $user->last_name,
266
267
268
269 "nickname" => $user->nickname,
270
271
272
273 "description" => $user->user_description,
274
275
276
277 "capabilities" => $user->wp_capabilities,
278
279
280
281 "avatar" => $avatar[1]
282
283
284
285 ),
286
287
288
289 );
290
291
292
293 }
294
295
296
297
298
299 public function clear_auth_cookie() {
300
301
302
303 global $json_api;
304
305
306
307 if (!$json_api->query->cookie) {
308
309
310
311 $json_api->error("You must include a valid 'cookie' to clear it.");
312
313
314
315 }
316
317
318
319 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
320
321
322
323 if (!$user_id) {
324
325
326
327 $json_api->error("Invalid authentication cookie. Provide a valid cookie to clear.");
328
329
330
331 }
332
333
334
335 $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user_id, true);
336
337
338
339 $cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
340
341
342
343 $valid = wp_validate_auth_cookie($cookie, 'logged_in') ? true : false;
344
345
346
347 return array(
348
349
350
351 "valid" => $valid
352
353
354
355 );
356
357
358
359 }
360
361
362
363 public function get_currentuserinfo() {
364
365
366
367 global $json_api;
368
369
370
371
372
373
374
375 if (!$json_api->query->cookie) {
376
377
378
379 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
380
381
382
383 }
384
385
386
387
388
389
390
391 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
392
393
394
395 if (!$user_id) {
396
397
398
399 $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method.");
400
401
402
403 }
404
405
406
407
408
409
410
411 $user = get_userdata($user_id);
412
413
414
415 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
416
417
418
419
420
421
422
423 return array(
424
425
426
427 "user" => array(
428
429
430
431 "id" => $user->ID,
432
433
434
435 "username" => $user->user_login,
436
437
438
439 "nicename" => $user->user_nicename,
440
441
442
443 "email" => $user->user_email,
444
445
446
447 "url" => $user->user_url,
448
449
450
451 "registered" => $user->user_registered,
452
453
454
455 "displayname" => $user->display_name,
456
457
458
459 "firstname" => $user->user_firstname,
460
461
462
463 "lastname" => $user->last_name,
464
465
466
467 "nickname" => $user->nickname,
468
469
470
471 "description" => $user->user_description,
472
473
474
475 "capabilities" => $user->wp_capabilities,
476
477
478
479 "avatar" => $avatar[1]
480
481
482
483 )
484
485
486
487 );
488
489
490
491 }
492
493
494
495
496
497
498
499 }