PluginProbe
JSON API Auth / 1.3
JSON API Auth v1.3
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.3, at controllers/Auth.php

271 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 Controller Name: Auth
5 Controller Description: Authentication add-on controller for the Wordpress JSON API plugin
6 Controller Author: Matt Berg
7 Controller Author Twitter: @mattberg
8 */
9
10
11
12
13
14
15
16 class JSON_API_Auth_Controller {
17
18
19
20
21
22
23
24 public function validate_auth_cookie() {
25
26
27
28 global $json_api;
29
30
31
32
33
34
35
36 if (!$json_api->query->cookie) {
37
38
39
40 $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method.");
41
42
43
44 }
45
46
47
48
49
50
51
52 $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;
53
54
55
56
57
58
59
60 return array(
61
62
63
64 "valid" => $valid
65
66
67
68 );
69
70
71
72 }
73
74
75
76
77
78
79
80 public function generate_auth_cookie() {
81
82
83
84 global $json_api;
85
86
87
88
89
90
91
92 $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie');
93
94
95
96 if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
97
98
99
100 $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
101
102
103
104 }
105
106
107
108
109
110
111
112 if (!$json_api->query->username) {
113
114
115
116 $json_api->error("You must include a 'username' var in your request.");
117
118
119
120 }
121
122
123
124
125
126
127
128 if (!$json_api->query->password) {
129
130
131
132 $json_api->error("You must include a 'password' var in your request.");
133
134
135
136 }
137
138
139
140
141
142
143
144 $user = wp_authenticate($json_api->query->username, $json_api->query->password);
145
146
147
148 if (is_wp_error($user)) {
149
150
151
152 $json_api->error("Invalid username and/or password.", 'error', '401');
153
154
155
156 remove_action('wp_login_failed', $json_api->query->username);
157
158
159
160 }
161
162
163
164
165
166
167
168 $expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user->ID, true);
169
170
171 $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');
172
173
174 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
175
176
177 return array(
178 "cookie" => $cookie,
179 "user" => array(
180 "id" => $user->ID,
181 "username" => $user->user_login,
182 "nicename" => $user->user_nicename,
183 "email" => $user->user_email,
184 "url" => $user->user_url,
185 "registered" => $user->user_registered,
186 "displayname" => $user->display_name,
187 "firstname" => $user->user_firstname,
188 "lastname" => $user->last_name,
189 "nickname" => $user->nickname,
190 "description" => $user->user_description,
191 "capabilities" => $user->wp_capabilities,
192 "avatar" => $avatar[1]
193
194 ),
195 );
196 }
197
198
199
200 public function clear_auth_cookie() {
201 global $json_api;
202 if (!$json_api->query->cookie) {
203
204 $json_api->error("You must include a valid 'cookie' to clear it.");
205 }
206
207
208
209 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
210
211
212 if (!$user_id) {
213 $json_api->error("Invalid authentication cookie. Provide a valid cookie to clear.");
214 }
215
216
217 $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user_id, true);
218
219 $cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
220
221 $valid = wp_validate_auth_cookie($cookie, 'logged_in') ? true : false;
222
223
224 return array(
225 "valid" => $valid
226 );
227
228 }
229
230
231
232 public function get_currentuserinfo() {
233 global $json_api;
234
235 if (!$json_api->query->cookie) {
236 $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
237
238 }
239
240 $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
241
242 if (!$user_id) {
243 $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method.");
244 }
245
246 $user = get_userdata($user_id);
247 preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
248
249 return array(
250 "user" => array(
251 "id" => $user->ID,
252 "username" => $user->user_login,
253 "nicename" => $user->user_nicename,
254 "email" => $user->user_email,
255 "url" => $user->user_url,
256 "registered" => $user->user_registered,
257 "displayname" => $user->display_name,
258 "firstname" => $user->user_firstname,
259 "lastname" => $user->last_name,
260 "nickname" => $user->nickname,
261 "description" => $user->user_description,
262 "capabilities" => $user->wp_capabilities,
263
264 "avatar" => $avatar[1]
265
266 )
267
268 );
269
270 }
271 }