PluginProbe
JSON API Auth / 1.9.4
JSON API Auth v1.9.4
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
← All changes | controllers/Auth.php +37 -181 1.11.9.4 View file →
@@ -1,250 +1,106 @@
1 1 <?php
2 -
3 2 /*
4 -
5 3 Controller Name: Auth
6 -
7 4 Controller Description: Authentication add-on controller for the Wordpress JSON API plugin
8 -
9 -Controller Author: Matt Berg
10 -
11 -Controller Author Twitter: @mattberg
12 -
5 +Controller Author: Matt Berg, Ali Qureshi
6 +Controller Author Twitter: @parorrey
13 7 */
14 8
15 9
16 -
17 10 class JSON_API_Auth_Controller {
18 -
19 -
20 -
11 + public function __construct() {
12 + global $json_api;
13 + // allow only connection over https. because, well, you care about your passwords and sniffing.
14 + // turn this sanity-check off if you feel safe inside your localhost or intranet.
15 + // send an extra POST parameter: insecure=cool
16 + if (empty($_SERVER['HTTPS']) ||
17 + (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'off')) {
18 + if (empty($_REQUEST['insecure']) || $_REQUEST['insecure'] != 'cool') {
19 + $json_api->error("I'm sorry Dave. I'm afraid I can't do that. (use _https_ please)");
20 + }
21 + }
22 + $allowed_from_post = array('cookie', 'username', 'password', 'seconds', 'nonce');
23 + foreach($allowed_from_post as $param) {
24 + if (isset($_POST[$param])) {
25 + $json_api->query->$param = $_POST[$param];
26 + }
27 + }
28 + }
21 29 public function validate_auth_cookie() {
22 -
23 30 global $json_api;
24 -
25 -
26 -
27 31 if (!$json_api->query->cookie) {
28 -
29 32 $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method.");
30 -
31 - }
32 -
33 -
34 -
35 - $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
36 -
37 -
38 -
33 + }
34 + $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
39 35 return array(
40 -
41 36 "valid" => $valid
42 -
43 - );
44 -
37 + );
45 38 }
46 -
47 -
48 -
49 39 public function generate_auth_cookie() {
50 -
51 40 global $json_api;
52 -
53 -
54 -
55 - $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie');
56 -
57 - if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
58 -
59 - $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
60 -
61 - }
62 -
63 -
64 -
65 41 if (!$json_api->query->username) {
66 -
67 42 $json_api->error("You must include a 'username' var in your request.");
68 -
69 43 }
70 -
71 -
72 -
73 44 if (!$json_api->query->password) {
74 -
75 45 $json_api->error("You must include a 'password' var in your request.");
76 -
77 - }
78 -
79 -
80 -
81 - $user = wp_authenticate($json_api->query->username, $json_api->query->password);
82 -
83 - if (is_wp_error($user)) {
84 -
85 - $json_api->error("Invalid username and/or password.", 'error', '401');
86 -
87 - remove_action('wp_login_failed', $json_api->query->username);
88 -
89 - }
90 -
91 -
92 -
93 - $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user->ID, true);
94 -
95 -
96 -
97 - $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
98 -
99 -
100 -
101 -
102 -
103 - preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
104 -
105 -
106 -
107 -
108 -
109 -
110 -
46 + }
47 + if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds;
48 + else $seconds = 1209600;//14 days
49 + $user = wp_authenticate($json_api->query->username, $json_api->query->password);
50 + if (is_wp_error($user)) {
51 + $json_api->error("Invalid username and/or password.", 'error', '401');
52 + remove_action('wp_login_failed', $json_api->query->username);
53 + }
54 + $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true);
55 + $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
56 + preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
111 57 return array(
112 -
113 58 "cookie" => $cookie,
114 -
59 + "cookie_name" => LOGGED_IN_COOKIE,
115 60 "user" => array(
116 -
117 61 "id" => $user->ID,
118 -
119 62 "username" => $user->user_login,
120 -
121 63 "nicename" => $user->user_nicename,
122 -
123 64 "email" => $user->user_email,
124 -
125 65 "url" => $user->user_url,
126 -
127 66 "registered" => $user->user_registered,
128 -
129 67 "displayname" => $user->display_name,
130 -
131 68 "firstname" => $user->user_firstname,
132 -
133 69 "lastname" => $user->last_name,
134 -
135 70 "nickname" => $user->nickname,
136 -
137 71 "description" => $user->user_description,
138 -
139 72 "capabilities" => $user->wp_capabilities,
140 -
141 73 "avatar" => $avatar[1]
142 -
143 74 ),
144 -
145 75 );
146 -
147 76 }
148 -
149 -
150 -public function clear_auth_cookie() {
151 -
152 - global $json_api;
153 -
154 - if (!$json_api->query->cookie) {
155 -
156 - $json_api->error("You must include a valid 'cookie' to clear it.");
157 -
158 - }
159 -
160 - $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
161 -
162 - if (!$user_id) {
163 -
164 - $json_api->error("Invalid authentication cookie. Provide a valid cookie to clear.");
165 -
166 - }
167 -
168 - $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user_id, true);
169 -
170 - $cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
171 -
172 - $valid = wp_validate_auth_cookie($cookie, 'logged_in') ? true : false;
173 -
174 - return array(
175 -
176 - "valid" => $valid
177 -
178 - );
179 -
180 - }
181 -
182 77 public function get_currentuserinfo() {
183 -
184 78 global $json_api;
185 -
186 -
187 -
188 79 if (!$json_api->query->cookie) {
189 -
190 80 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
191 -
192 81 }
193 -
194 -
195 -
196 82 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
197 -
198 83 if (!$user_id) {
199 -
200 84 $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method.");
201 -
202 85 }
203 -
204 -
205 -
206 86 $user = get_userdata($user_id);
207 -
208 - preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
209 -
210 -
211 -
87 + preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
212 88 return array(
213 -
214 89 "user" => array(
215 -
216 90 "id" => $user->ID,
217 -
218 91 "username" => $user->user_login,
219 -
220 92 "nicename" => $user->user_nicename,
221 -
222 93 "email" => $user->user_email,
223 -
224 94 "url" => $user->user_url,
225 -
226 95 "registered" => $user->user_registered,
227 -
228 96 "displayname" => $user->display_name,
229 -
230 97 "firstname" => $user->user_firstname,
231 -
232 98 "lastname" => $user->last_name,
233 -
234 99 "nickname" => $user->nickname,
235 -
236 100 "description" => $user->user_description,
237 -
238 101 "capabilities" => $user->wp_capabilities,
239 -
240 102 "avatar" => $avatar[1]
241 -
242 103 )
243 -
244 104 );
245 -
246 - }
247 -
248 -
249 -
105 + }
250 106 }