diff --git a/src/getjump/Vk/Auth.php b/src/getjump/Vk/Auth.php index abfcee8..70ef984 100644 --- a/src/getjump/Vk/Auth.php +++ b/src/getjump/Vk/Auth.php @@ -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 @@ -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 * @@ -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; } /** @@ -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);