oauth2.php 2.65 KB
<?

/*

    OAuth2 auth client lib for NRNU MEPhI



    Copyright (C) 2014, Dmitry Y Okunev <dyokunev@ut.mephi.ru>



    This program is free software; you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation; either version 2 of the License, or

    (at your option) any later version.



    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.



    You should have received a copy of the GNU General Public License along

    with this program; if not, write to the Free Software Foundation, Inc.,

    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

/* 

 * D Y Okunev <xai@mephi.ru>: Sorry for this code. As always, no time to do something good :(

 *

 */ 



define("OAUTH2_LIB_DIR", __DIR__ . "/3rdparty/oauth_2.0_client_php");

require_once OAUTH2_LIB_DIR . "/OAuth2/Exception.php";



define('OAUTH2_URL',	'https://oauth2.mephi.ru/');



class OAuth2_MEPhI {

	private $client_id	= NULL;

	private $client 	= NULL;

	private $configuration 	= NULL;

	private $storage	= NULL;

	private $service	= NULL;

	private $scope		= NULL;



	function __construct($client_id, $client_secret, $backurl) {

		require_once OAUTH2_LIB_DIR . "/OAuth2/Client.php";

		$this->client_id = $client_id;



		$this->client = new OAuth2\Client(

			$client_id,

			$client_secret,

			$backurl

		);



		require_once OAUTH2_LIB_DIR . "/OAuth2/Service/Configuration.php";

		$this->configuration = new OAuth2\Service\Configuration(

			OAUTH2_URL.'authorize.php',

			OAUTH2_URL.'token.php'

		);

		$this->configuration->setAuthorizationMethod(OAuth2\Service\Configuration::AUTHORIZATION_METHOD_ALTERNATIVE);



		require_once OAUTH2_LIB_DIR . "/OAuth2/DataStore.php";

		require_once OAUTH2_LIB_DIR . "/OAuth2/DataStore/Session.php";

		$this->storage = new OAuth2\DataStore\Session();



		require_once OAUTH2_LIB_DIR . "/OAuth2/Service.php";

		$this->service = new OAuth2\Service($this->client, $this->configuration, $this->storage, $this->scope);

	}



	function get_token() {

		require_once OAUTH2_LIB_DIR . "/OAuth2/Token.php";

		require_once OAUTH2_LIB_DIR . "/OAuth2/HttpClient.php";

		$this->service->getAccessToken();



		return json_decode($this->service->callApiEndpoint(OAUTH2_URL.'token_data.php'));

	}



	function check_if_authed() {

		header('Location: '.OAUTH2_URL.'authorize.php?cmd=check&client_id='.$this->client_id.'&response_type=code&state=authentication');

		return NULL;

	}



}



?>