Skip to content
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
45 changes: 35 additions & 10 deletions src/getjump/Vk/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class Auth
{
const URL_ACCESS_TOKEN = 'https://oauth.vk.com/access_token?client_id=%s&client_secret=%s&code=%s&redirect_uri=%s';
const URL_ACCESS_TOKEN = 'https://oauth.vk.com/access_token?';

/**
* @var \GuzzleHttp\Client
Expand Down Expand Up @@ -99,6 +99,18 @@ public function setScope($scope)
return $this;
}

/**
* @param $state
*
* @return $this
*/
public function setState($state)
{
$this->options['state'] = $state;

return $this;
}

/**
* @param $v
*
Expand All @@ -124,11 +136,13 @@ public function getUrl()
*
* @param $d
*
* @param $default
*
* @return mixed
*/
public function g($d)
public function g($d, $default = null)
{
return $this->options[$d];
return isset($this->options[$d]) ? $this->options[$d] : $default;
}

/**
Expand Down Expand Up @@ -162,13 +176,24 @@ public function getToken($code)
$this->guzzle = new \GuzzleHttp\Client();
}

$uri = sprintf(
self::URL_ACCESS_TOKEN,
$this->g('client_id'),
$this->g('client_secret'),
$code,
urlencode($this->g('redirect_uri'))
);

$params = [
'client_id' => $this->g('client_id'),
'client_secret' => $this->g('client_secret'),
'code' => $code,
'redirect_uri' => $this->g('redirect_uri'),
'state' => $this->g('state'),
];

$params = array_filter($params, function ($value) {
return strlen($value) > 0;
}, ARRAY_FILTER_USE_BOTH);

if (!isset($params['client_id'], $params['client_secret'], $params['code'], $params['redirect_uri'])) {
throw new \InvalidArgumentException('Params client_id, client_secret, code and redirect_uri is required.');
}

$uri = self::URL_ACCESS_TOKEN . http_build_query($params);

$data = $this->guzzle->get($uri)->getBody();
$data = json_decode($data);
Expand Down