| @@ -2,10 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | /* |
| 4 | 4 | Controller Name: Auth |
| 5 | 5 | Controller Description: Authentication add-on controller for the Wordpress JSON API plugin |
| 6 | -Controller Author: Matt Berg | |
| 7 | -Controller Author Twitter: @mattberg | |
| 6 | +Controller Author: Matt Berg, Ali Qureshi | |
| 7 | +Controller Author Twitter: @parorrey | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | |
| 11 | 11 | |
| @@ -15,168 +15,81 @@ | ||
| 15 | 15 | |
| 16 | 16 | class JSON_API_Auth_Controller { |
| 17 | 17 | |
| 18 | 18 | |
| 19 | - | |
| 20 | - | |
| 21 | - | |
| 22 | - | |
| 23 | - | |
| 24 | 19 | public function validate_auth_cookie() { |
| 25 | 20 | |
| 26 | - | |
| 27 | - | |
| 28 | 21 | global $json_api; |
| 29 | 22 | |
| 30 | - | |
| 31 | - | |
| 32 | - | |
| 33 | - | |
| 34 | - | |
| 35 | - | |
| 36 | 23 | if (!$json_api->query->cookie) { |
| 37 | 24 | |
| 38 | - | |
| 39 | - | |
| 40 | 25 | $json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method."); |
| 41 | 26 | |
| 42 | - | |
| 43 | - | |
| 44 | 27 | } |
| 45 | 28 | |
| 46 | - | |
| 47 | - | |
| 48 | - | |
| 49 | - | |
| 50 | - | |
| 51 | - | |
| 52 | 29 | $valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false; |
| 53 | 30 | |
| 54 | - | |
| 55 | - | |
| 56 | - | |
| 57 | - | |
| 58 | - | |
| 59 | - | |
| 60 | 31 | return array( |
| 61 | 32 | |
| 62 | - | |
| 63 | - | |
| 64 | 33 | "valid" => $valid |
| 65 | 34 | |
| 66 | - | |
| 67 | - | |
| 68 | 35 | ); |
| 69 | 36 | |
| 70 | 37 | |
| 71 | - | |
| 72 | 38 | } |
| 73 | 39 | |
| 74 | - | |
| 75 | - | |
| 76 | - | |
| 77 | - | |
| 78 | - | |
| 79 | - | |
| 80 | 40 | public function generate_auth_cookie() { |
| 81 | - | |
| 82 | - | |
| 83 | - | |
| 41 | + | |
| 84 | 42 | global $json_api; |
| 85 | 43 | |
| 86 | - | |
| 87 | - | |
| 88 | - | |
| 89 | - | |
| 90 | - | |
| 91 | - | |
| 44 | + /* | |
| 92 | 45 | $nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie'); |
| 93 | 46 | |
| 94 | - | |
| 95 | - | |
| 96 | 47 | if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) { |
| 97 | 48 | |
| 98 | - | |
| 99 | - | |
| 100 | 49 | $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method."); |
| 50 | + }*/ | |
| 101 | 51 | |
| 102 | 52 | |
| 103 | - | |
| 104 | - } | |
| 105 | - | |
| 106 | - | |
| 107 | - | |
| 108 | - | |
| 109 | - | |
| 110 | - | |
| 111 | - | |
| 112 | 53 | if (!$json_api->query->username) { |
| 113 | 54 | |
| 114 | - | |
| 115 | - | |
| 116 | 55 | $json_api->error("You must include a 'username' var in your request."); |
| 117 | 56 | |
| 118 | - | |
| 119 | - | |
| 120 | 57 | } |
| 121 | 58 | |
| 122 | 59 | |
| 123 | - | |
| 124 | - | |
| 125 | - | |
| 126 | - | |
| 127 | - | |
| 128 | 60 | if (!$json_api->query->password) { |
| 129 | 61 | |
| 130 | - | |
| 131 | - | |
| 132 | 62 | $json_api->error("You must include a 'password' var in your request."); |
| 133 | 63 | |
| 64 | + } | |
| 65 | + | |
| 66 | + if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds; | |
| 134 | 67 | |
| 68 | + else $seconds = 1209600;//14 days | |
| 135 | 69 | |
| 136 | - } | |
| 137 | 70 | |
| 138 | 71 | |
| 139 | - | |
| 140 | - | |
| 141 | - | |
| 142 | - | |
| 143 | - | |
| 144 | 72 | $user = wp_authenticate($json_api->query->username, $json_api->query->password); |
| 145 | 73 | |
| 146 | - | |
| 147 | - | |
| 148 | 74 | if (is_wp_error($user)) { |
| 149 | 75 | |
| 150 | - | |
| 151 | - | |
| 152 | 76 | $json_api->error("Invalid username and/or password.", 'error', '401'); |
| 153 | 77 | |
| 154 | - | |
| 155 | - | |
| 156 | 78 | remove_action('wp_login_failed', $json_api->query->username); |
| 157 | 79 | |
| 158 | - | |
| 159 | - | |
| 160 | 80 | } |
| 161 | 81 | |
| 162 | 82 | |
| 83 | + $expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true); | |
| 163 | 84 | |
| 164 | - | |
| 165 | - | |
| 166 | - | |
| 167 | - | |
| 168 | - $expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user->ID, true); | |
| 169 | - | |
| 170 | - | |
| 171 | 85 | $cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in'); |
| 172 | 86 | |
| 173 | - | |
| 174 | 87 | preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); |
| 175 | 88 | |
| 176 | - | |
| 177 | 89 | return array( |
| 178 | 90 | "cookie" => $cookie, |
| 91 | + "cookie_name" => LOGGED_IN_COOKIE, | |
| 179 | 92 | "user" => array( |
| 180 | 93 | "id" => $user->ID, |
| 181 | 94 | "username" => $user->user_login, |
| 182 | 95 | "nicename" => $user->user_nicename, |
| @@ -193,41 +106,8 @@ | ||
| 193 | 106 | |
| 194 | 107 | ), |
| 195 | 108 | ); |
| 196 | 109 | } |
| 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 | 110 | |
| 231 | 111 | |
| 232 | 112 | public function get_currentuserinfo() { |
| 233 | 113 | global $json_api; |