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