| @@ -1,17 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /* |
| 4 | - | |
| 5 | 4 | Controller Name: Auth |
| 5 | +Controller Description: Authentication add-on controller for the Wordpress JSON API plugin | |
| 6 | +Controller Author: Matt Berg, Ali Qureshi | |
| 7 | +Controller Author Twitter: @parorrey | |
| 8 | +*/ | |
| 6 | 9 | |
| 7 | -Controller Description: Authentication add-on controller for the Wordpress JSON API plugin | |
| 8 | 10 | |
| 9 | -Controller Author: Matt Berg | |
| 10 | 11 | |
| 11 | -Controller Author Twitter: @mattberg | |
| 12 | 12 | |
| 13 | -*/ | |
| 14 | 13 | |
| 15 | 14 | |
| 16 | 15 | |
| 17 | 16 | class JSON_API_Auth_Controller { |
| @@ -16,15 +15,12 @@ | ||
| 16 | 15 | |
| 17 | 16 | class JSON_API_Auth_Controller { |
| 18 | 17 | |
| 19 | 18 | |
| 20 | - | |
| 21 | 19 | public function validate_auth_cookie() { |
| 22 | 20 | |
| 23 | 21 | global $json_api; |
| 24 | 22 | |
| 25 | - | |
| 26 | - | |
| 27 | 23 | if (!$json_api->query->cookie) { |
| 28 | 24 | |
| 29 | 25 | $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method."); |
| 30 | 26 | |
| @@ -29,14 +25,10 @@ | ||
| 29 | 25 | $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method."); |
| 30 | 26 | |
| 31 | 27 | } |
| 32 | 28 | |
| 33 | - | |
| 34 | - | |
| 35 | 29 | $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false; |
| 36 | 30 | |
| 37 | - | |
| 38 | - | |
| 39 | 31 | return array( |
| 40 | 32 | |
| 41 | 33 | "valid" => $valid |
| 42 | 34 | |
| @@ -41,28 +33,24 @@ | ||
| 41 | 33 | "valid" => $valid |
| 42 | 34 | |
| 43 | 35 | ); |
| 44 | 36 | |
| 37 | + | |
| 45 | 38 | } |
| 46 | 39 | |
| 47 | - | |
| 48 | - | |
| 49 | 40 | public function generate_auth_cookie() { |
| 50 | - | |
| 41 | + | |
| 51 | 42 | global $json_api; |
| 52 | 43 | |
| 53 | - | |
| 54 | - | |
| 44 | + /* | |
| 55 | 45 | $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie'); |
| 56 | 46 | |
| 57 | 47 | if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) { |
| 58 | 48 | |
| 59 | 49 | $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method."); |
| 50 | + }*/ | |
| 60 | 51 | |
| 61 | - } | |
| 62 | 52 | |
| 63 | - | |
| 64 | - | |
| 65 | 53 | if (!$json_api->query->username) { |
| 66 | 54 | |
| 67 | 55 | $json_api->error("You must include a 'username' var in your request."); |
| 68 | 56 | |
| @@ -67,18 +55,21 @@ | ||
| 67 | 55 | $json_api->error("You must include a 'username' var in your request."); |
| 68 | 56 | |
| 69 | 57 | } |
| 70 | 58 | |
| 71 | - | |
| 72 | 59 | |
| 73 | 60 | if (!$json_api->query->password) { |
| 74 | 61 | |
| 75 | 62 | $json_api->error("You must include a 'password' var in your request."); |
| 76 | 63 | |
| 77 | - } | |
| 64 | + } | |
| 65 | + | |
| 66 | + if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds; | |
| 78 | 67 | |
| 68 | + else $seconds = 1209600;//14 days | |
| 79 | 69 | |
| 80 | 70 | |
| 71 | + | |
| 81 | 72 | $user = wp_authenticate($json_api->query->username, $json_api->query->password); |
| 82 | 73 | |
| 83 | 74 | if (is_wp_error($user)) { |
| 84 | 75 | |
| @@ -88,154 +79,67 @@ | ||
| 88 | 79 | |
| 89 | 80 | } |
| 90 | 81 | |
| 91 | 82 | |
| 83 | + $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true); | |
| 92 | 84 | |
| 93 | - $expiration = time() + apply_filters('auth_cookie_expiration', -1209600, $user->ID, true); | |
| 94 | - | |
| 95 | - | |
| 96 | - | |
| 97 | 85 | $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in'); |
| 98 | 86 | |
| 87 | + preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); | |
| 99 | 88 | |
| 100 | - | |
| 101 | - | |
| 102 | - | |
| 103 | - preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); | |
| 104 | - | |
| 105 | - | |
| 106 | - | |
| 107 | - | |
| 108 | - | |
| 109 | - | |
| 110 | - | |
| 111 | 89 | return array( |
| 112 | - | |
| 113 | 90 | "cookie" => $cookie, |
| 114 | - | |
| 91 | + "cookie_name" => LOGGED_IN_COOKIE, | |
| 115 | 92 | "user" => array( |
| 116 | - | |
| 117 | 93 | "id" => $user->ID, |
| 118 | - | |
| 119 | 94 | "username" => $user->user_login, |
| 120 | - | |
| 121 | 95 | "nicename" => $user->user_nicename, |
| 122 | - | |
| 123 | 96 | "email" => $user->user_email, |
| 124 | - | |
| 125 | 97 | "url" => $user->user_url, |
| 126 | - | |
| 127 | 98 | "registered" => $user->user_registered, |
| 128 | - | |
| 129 | 99 | "displayname" => $user->display_name, |
| 130 | - | |
| 131 | 100 | "firstname" => $user->user_firstname, |
| 132 | - | |
| 133 | 101 | "lastname" => $user->last_name, |
| 134 | - | |
| 135 | 102 | "nickname" => $user->nickname, |
| 136 | - | |
| 137 | 103 | "description" => $user->user_description, |
| 138 | - | |
| 139 | 104 | "capabilities" => $user->wp_capabilities, |
| 140 | - | |
| 141 | 105 | "avatar" => $avatar[1] |
| 142 | 106 | |
| 143 | 107 | ), |
| 144 | - | |
| 145 | 108 | ); |
| 146 | - | |
| 147 | 109 | } |
| 148 | 110 | |
| 149 | - | |
| 150 | -public function clear_auth_cookie() { | |
| 151 | 111 | |
| 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 | 112 | public function get_currentuserinfo() { |
| 183 | - | |
| 184 | 113 | global $json_api; |
| 185 | 114 | |
| 186 | - | |
| 187 | - | |
| 188 | 115 | if (!$json_api->query->cookie) { |
| 189 | - | |
| 190 | 116 | $json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method."); |
| 191 | 117 | |
| 192 | - } | |
| 118 | + } | |
| 193 | 119 | |
| 194 | - | |
| 195 | - | |
| 196 | 120 | $user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 197 | 121 | |
| 198 | 122 | if (!$user_id) { |
| 199 | - | |
| 200 | 123 | $json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method."); |
| 201 | - | |
| 202 | 124 | } |
| 203 | 125 | |
| 204 | - | |
| 205 | - | |
| 206 | 126 | $user = get_userdata($user_id); |
| 207 | - | |
| 208 | 127 | preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); |
| 209 | 128 | |
| 210 | - | |
| 211 | - | |
| 212 | 129 | return array( |
| 213 | - | |
| 214 | 130 | "user" => array( |
| 215 | - | |
| 216 | 131 | "id" => $user->ID, |
| 217 | - | |
| 218 | 132 | "username" => $user->user_login, |
| 219 | - | |
| 220 | 133 | "nicename" => $user->user_nicename, |
| 221 | - | |
| 222 | 134 | "email" => $user->user_email, |
| 223 | - | |
| 224 | 135 | "url" => $user->user_url, |
| 225 | - | |
| 226 | 136 | "registered" => $user->user_registered, |
| 227 | - | |
| 228 | 137 | "displayname" => $user->display_name, |
| 229 | - | |
| 230 | 138 | "firstname" => $user->user_firstname, |
| 231 | - | |
| 232 | 139 | "lastname" => $user->last_name, |
| 233 | - | |
| 234 | 140 | "nickname" => $user->nickname, |
| 235 | - | |
| 236 | 141 | "description" => $user->user_description, |
| 237 | - | |
| 238 | 142 | "capabilities" => $user->wp_capabilities, |
| 239 | 143 | |
| 240 | 144 | "avatar" => $avatar[1] |
| 241 | 145 | |
| @@ -243,8 +147,5 @@ | ||
| 243 | 147 | |
| 244 | 148 | ); |
| 245 | 149 | |
| 246 | 150 | } |
| 247 | - | |
| 248 | - | |
| 249 | - | |
| 250 | 151 | } |