# json-api-auth/1.0/controllers/Auth.php

JSON API Auth, version 1.0. 110 lines.

- Page: https://pluginprobe.com/plugins/json-api-auth/1.0/code/controllers/Auth.php
- Raw: https://pluginprobe.com/plugins/json-api-auth/1.0/raw/controllers/Auth.php
- Modified: 2014-01-27T08:07:00+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/json-api-auth/1.0/code/controllers/Auth.php#L10-L20`.

```php
<?php
/*
Controller Name: Auth
Controller Description: Authentication add-on controller for the Wordpress JSON API plugin
Controller Author: Matt Berg
Controller Author Twitter: @mattberg
*/

class JSON_API_Auth_Controller {

	public function validate_auth_cookie() {
		global $json_api;

		if (!$json_api->query->cookie) {
			$json_api->error("You must include a 'cookie' authentication cookie. Use the `create_auth_cookie` Auth API method.");
		}		

    	$valid = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in') ? true : false;

		return array(
			"valid" => $valid
		);
	}

	public function generate_auth_cookie() {
		global $json_api;

		$nonce_id = $json_api->get_nonce_id('auth', 'generate_auth_cookie');
		if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
			$json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
		}

		if (!$json_api->query->username) {
			$json_api->error("You must include a 'username' var in your request.");
		}
		
		if (!$json_api->query->password) {
			$json_api->error("You must include a 'password' var in your request.");
		}		

    	$user = wp_authenticate($json_api->query->username, $json_api->query->password);
    	if (is_wp_error($user)) {
    		$json_api->error("Invalid username and/or password.", 'error', '401');
    		remove_action('wp_login_failed', $json_api->query->username);
    	}

    	$expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user->ID, true);

    	$cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in');


		preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);		
   
		
		
		return array(
			"cookie" => $cookie,
			"user" => array(
				"id" => $user->ID,
				"username" => $user->user_login,
				"nicename" => $user->user_nicename,
				"email" => $user->user_email,
				"url" => $user->user_url,
				"registered" => $user->user_registered,
				"displayname" => $user->display_name,
				"firstname" => $user->user_firstname,
				"lastname" => $user->last_name,
				"nickname" => $user->nickname,
				"description" => $user->user_description,
				"capabilities" => $user->wp_capabilities,
				"avatar" => $avatar[1]
			),
		);
	}
	
	public function get_currentuserinfo() {
		global $json_api;

		if (!$json_api->query->cookie) {
			$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method.");
		}

		$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in');
		if (!$user_id) {
			$json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` Auth API method.");
		}

		$user = get_userdata($user_id);
        preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar);
		
		return array(
			"user" => array(
				"id" => $user->ID,
				"username" => $user->user_login,
				"nicename" => $user->user_nicename,
				"email" => $user->user_email,
				"url" => $user->user_url,
				"registered" => $user->user_registered,
				"displayname" => $user->display_name,
				"firstname" => $user->user_firstname,
				"lastname" => $user->last_name,
				"nickname" => $user->nickname,
				"description" => $user->user_description,
				"capabilities" => $user->wp_capabilities,
				"avatar" => $avatar[1]
			)
		);
	}	

}
```
