PluginProbe
JSON API Auth / 0.1
JSON API Auth v0.1
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 0.1, at controllers/Auth.php

102 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Controller Name: Auth
4 Controller Description: Authentication add-on controller for the Wordpress JSON API plugin
5 Controller Author: Matt Berg
6 Controller Author Twitter: @mattberg
7 */
8
9 class JSON_API_Auth_Controller {
10
11 public function validate_auth_cookie() {
12 global $json_api;
13
14 if (!$json_api->query->cookie) {
15 $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method.");
16 }
17
18 $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
19
20 return array(
21 "valid" => $valid
22 );
23 }
24
25 public function generate_auth_cookie() {
26 global $json_api;
27
28 $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie');
29 if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
30 $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
31 }
32
33 if (!$json_api->query->username) {
34 $json_api->error("You must include a 'username' var in your request.");
35 }
36
37 if (!$json_api->query->password) {
38 $json_api->error("You must include a 'password' var in your request.");
39 }
40
41 $user = wp_authenticate($json_api->query->username, $json_api->query->password);
42 if (is_wp_error($user)) {
43 $json_api->error("Invalid username and/or password.", 'error', '401');
44 remove_action('wp_login_failed', $json_api->query->username);
45 }
46
47 $expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user->ID, true);
48
49 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
50
51 return array(
52 "cookie" => $cookie,
53 "user" => array(
54 "id" => $user->ID,
55 "username" => $user->user_login,
56 "nicename" => $user->user_nicename,
57 "email" => $user->user_email,
58 "url" => $user->user_url,
59 "registered" => $user->user_registered,
60 "displayname" => $user->display_name,
61 "firstname" => $user->user_firstname,
62 "lastname" => $user->last_name,
63 "nickname" => $user->nickname,
64 "description" => $user->user_description,
65 "capabilities" => $user->wp_capabilities,
66 ),
67 );
68 }
69
70 public function get_currentuserinfo() {
71 global $json_api;
72
73 if (!$json_api->query->cookie) {
74 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
75 }
76
77 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
78 if (!$user_id) {
79 $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method.");
80 }
81
82 $user = get_userdata($user_id);
83
84 return array(
85 "user" => array(
86 "id" => $user->ID,
87 "username" => $user->user_login,
88 "nicename" => $user->user_nicename,
89 "email" => $user->user_email,
90 "url" => $user->user_url,
91 "registered" => $user->user_registered,
92 "displayname" => $user->display_name,
93 "firstname" => $user->user_firstname,
94 "lastname" => $user->last_name,
95 "nickname" => $user->nickname,
96 "description" => $user->user_description,
97 "capabilities" => $user->wp_capabilities,
98 )
99 );
100 }
101
102 }