From 204d5223b703ec93091f12cef718ae670bff0e4c Mon Sep 17 00:00:00 2001 From: Dudarev Ilia Date: Wed, 21 Jun 2017 13:11:47 +0400 Subject: [PATCH 1/2] Add support state get param in auth. --- src/getjump/Vk/Auth.php | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/getjump/Vk/Auth.php b/src/getjump/Vk/Auth.php index abfcee8..9bb5c77 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 * @@ -162,13 +174,20 @@ 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); + + $uri = self::URL_ACCESS_TOKEN . http_build_query($params); $data = $this->guzzle->get($uri)->getBody(); $data = json_decode($data); From 2bb505367cef7bc49dbddeb17a200db821a9f0f2 Mon Sep 17 00:00:00 2001 From: Dudarev Ilia Date: Wed, 21 Jun 2017 13:57:05 +0400 Subject: [PATCH 2/2] Add simple validation for required GET param in auth. --- src/getjump/Vk/Auth.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/getjump/Vk/Auth.php b/src/getjump/Vk/Auth.php index 9bb5c77..70ef984 100644 --- a/src/getjump/Vk/Auth.php +++ b/src/getjump/Vk/Auth.php @@ -136,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; } /** @@ -176,17 +178,21 @@ public function getToken($code) $params = [ - 'client_id' => $this->g('client_id'), + 'client_id' => $this->g('client_id'), 'client_secret' => $this->g('client_secret'), - 'code' => $code, - 'redirect_uri' => $this->g('redirect_uri'), - 'state' => $this->g('state'), + '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();