Skip to content
This repository was archived by the owner on Jul 28, 2018. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 77 additions & 28 deletions library/SAuth/Adapter/Vkontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/**
* @see SAuth_Adapter_Abstract
*/
require_once 'SAuth/Adapter/Abstract.php';
require_once 'Abstract.php';

/** require_once 'SAuth/Adapter/Abstract.php'; **/

/**
* @see Zend_Auth_Adapter_Interface
Expand All @@ -13,17 +15,22 @@
/**
* Authentication with vkontakte
*
* http://vkontakte.ru/developers.php?o=-1&p=Open+API
* http://vkontakte.ru/developers.php?oid=-1&p=Авторизация_сайтов
*/
class SAuth_Adapter_Vkontakte extends SAuth_Adapter_Abstract implements Zend_Auth_Adapter_Interface {

/**
* @var array Configuration array
*/
protected $_config = array(
'consumerId' => '',
'consumerSecret' => '',
'callbackUrl' => '',
'consumerId' => '',
'consumerSecret' => '',
'callbackUrl' => '',
'userAuthorizationUrl' => 'http://api.vkontakte.ru/oauth/authorize',
'accessTokenUrl' => 'https://api.vkontakte.ru/oauth/access_token',
'requestDatarUrl' => 'https://graph.facebook.com/me',
'responseType' => 'code',
'scope' => array(),
);

/**
Expand All @@ -39,37 +46,79 @@ public function authenticate() {

$config = $this->getConfig();

$apiId = $config['consumerId'];
$apiSecret = $config['consumerSecret'];
$authorizationUrl = $config['userAuthorizationUrl'];
$accessTokenUrl = $config['accessTokenUrl'];
$clientId = $config['consumerId'];
$clientSecret = $config['consumerSecret'];
$redirectUrl = $config['callbackUrl'];
$responseType = $config['responseType'];

if (empty($apiId) || empty($apiSecret)) {
if (isset($_GET['code']) && !empty($_GET['code'])) {

$accessConfig = array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'code' => trim($_GET['code']),
);

require_once 'Zend/Auth/Adapter/Exception.php';
throw new Zend_Auth_Adapter_Exception('Vkontakte auth configuration not specifed');
}

$appCookie = isset($_COOKIE['vk_app_' . $apiId]) ? $this->parseResponseUrl($_COOKIE['vk_app_' . $apiId]) : null;
$vkUserCookie = isset($_COOKIE['vk_user_info_' . $apiId]) ? $this->parseResponseUrl($_COOKIE['vk_user_info_' . $apiId]) : null;

if (!empty($appCookie)) {
//create sign
$sign = 'expire=' . $appCookie['expire'] . 'mid=' . $appCookie['mid'] . 'secret=' . $appCookie['secret']
. 'sid=' . $appCookie['sid'];
$sign = md5($sign . $apiSecret);
$response = $this->httpRequest('GET', $accessTokenUrl, $accessConfig);

if ($appCookie['sig'] == $sign) {

//unset vk info cookie
setcookie('vk_user_info_' . $apiId, '', time() - 1000, '/');
if ($response->isError()) {
//vkontakte return 400 http code on error
switch ($response->getStatus()) {
case '400':
$parsedErrors = $this->parseResponseJson($response->getBody());
$error = $parsedErrors['error']['message'];
break;
default:
$error = 'Vkontakte Oauth service unavailable';
break;
}

return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, false, array($error));

$identity = $this->_prepareIdentity(array_merge($appCookie, $vkUserCookie));
} elseif ($response->isSuccessful()) {

$parsedResponse = $this->parseResponseJson($response->getBody());

/*
Дополнительные поля, список с обозначениями тут:
http://vkontakte.ru/developers.php?oid=-1&p=Описание_полей_параметра_fields
*/

$userConfig = array(
'uid' => $parsedResponse['user_id'],
'fields' => 'photo_rec,screen_name',
'access_token' => $parsedResponse['access_token'],
);

$userRequest = $this->httpRequest('GET', 'https://api.vkontakte.ru/method/getProfiles', $userConfig);
$userParameters = $this->parseResponseJson($userRequest->getBody());

$identity = $this->_prepareIdentity(array_merge($parsedResponse, $userParameters['response']['0']));

return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);

}
} elseif (!isset($_GET['error'])) {

$authorizationConfig = array(
'client_id' => $clientId,
'redirect_uri' => $redirectUrl.'/index/auth/by/vkontakte',
'scope' => 'audio',
'response_type' => 'code',
);

$url = 'http://api.vkontakte.ru/oauth/authorize?';
$url .= http_build_query($authorizationConfig, null, '&');
header('Location: ' . $url);
exit(1);

} else {

return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, false, array($_GET['error']));

}

$error = 'Vkontakte auth failed';
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, false, array($error));
}

}