How to build an OAuth authentication system using user credentials

See OAuth Example Demo in action

OAuth Demo Controller

				
<?php

namespace ProtOn\Demos\Controllers;

use ProtOn\Demos\OAuthPHPDemo;

class DemosController extends Controller {
	
	/**
	 * Represents a request to http://developers.prot-on.com/demos/oauth-cred-demo
	 */
	public function getOauthUserDemo() {
		return "Your app view";
	}
	
	/**
	 * Represents a request to http://developers.prot-on.com/demos/oauth-cred-demo
	 */
	public function postOauthUserDemo() {
		$client_id =     $_POST['client_id'];
		$client_secret = $_POST['client_secret'];
		$username =      $_POST['username'];
		$password =      $_POST['password'];
		
		$oauth = new OAuthPHP($client_id, $client_secret);
		$token = $oauth->getAccessToken($client_id, $client_secret, $username, $password);
		
		return self::getOauthUserDemoResult($token);
		
	}
	
	/**
	 * Represents a request made to http://developers.prot-on.com/demos/oauth-cred-result
	 */
	public function getOauthUserDemoResult($token) {
	
		return "Your result view";

	}
	
}
?>
				
			

OAuth Service

				
<?php

namespace ProtOn\Demos;

use ProtOn\Utils\OAuth2\Client;
use ProtOn\Utils\OAuth2\GrantType\IGrantType;
use ProtOn\Utils\OAuth2\GrantType\AuthorizationCode;
use ProtOn\Utils\OAuth2\GrantType\RefreshToken;

class OAuthPHP {

	/* Constants */
	
	const AUTHORIZATION_ENDPOINT	= '/external/oauth/authorize';
	const TOKEN_ENDPOINT		= '/external/oauth/token';
	const PROTON_URL		= 'https://proton.prot-on.com';
	const REDIRECT_URL		= 'http://developers.prot-on.com';
	
	/* Attributes */
	
	private $client;
	private $id;
	private $secret;
	
	/**
	 * Constructor with args
	 */
	public function __construct($id, $secret){
		$this->id = $id;
		$this->secret = $secret;
		$this->buildClient();
	}
	
	/**
	 * Builds a client for this instance
	 */
	public function buildClient(){
		$this->client = new Client($this->id, $this->secret, Client::AUTH_TYPE_AUTHORIZATION_BASIC);
	}
	
	/**
	 * Gets access token using user credentials
	 * @param    $code the authorization code
	 */
	public function getAccessToken($client_id, $client_secret, $username, $password){
		$params = array('client_id' => $client_id, 'client_secret'=>$client_secret, 'username'=>$username, 'password'=>$password, 'redirect_uri' => self::REDIRECT_URL);
		$response = $this->client->getAccessToken(self::PROTON_URL.self::TOKEN_ENDPOINT, Client::GRANT_TYPE_PASSWORD, $params);
		$token = self::parseOAuthTokenResponse($response); // Array. I.e.: $access_token = $token['access_token'];
		if(empty($token['error'])){
		$expired = self::checkExpiration($token);
			if($expired){
				$params['refresh_token'] = $token['refresh_token'];
				$response = $this->client->getAccessToken(self::PROTON_URL.self::TOKEN_ENDPOINT, Client::GRANT_TYPE_REFRESH_TOKEN, $params);
				$token = self::parseOAuthTokenResponse($response);
			}
		}
		
		return $token;
	}
	
	public function checkExpiration($token){
	
		$expired = false;
		
		$expiration = $token['expiration'];
		$currentDate = new \DateTime();//echo $expiration->format('Y-m-d H:i:s')."  --  ".$currentDate->format('Y-m-d H:i:s');
	
		if($expiration < $currentDate){
			$expired = true;
		}
		
		return $expired;
	}
	
	/**
	 * Parses the response received from the getAccessToken method
	 * @param    $response the response from the getAccessToken method 
	 */
	public static function parseOAuthTokenResponse($response) {
		$token = array();
		switch ($response['code']){
			case 200:
				$token = $response['result'];
				$date = new \DateTime("now");
				$token['expiration'] = $date->add(new \DateInterval('PT'.$token['expires_in'].'S'));
				break;
			default:
				$token = $response['result'];
				break;
		}
		return $token;
	}
}
?>
				
			

See OAuth Example Demo in action