From bf862e99da6782983f254b6d8c90277354b95535 Mon Sep 17 00:00:00 2001 From: PGBI Date: Mon, 18 Jul 2016 10:54:12 +0200 Subject: [PATCH] Collect all ips on member tokens generation --- composer.json | 3 +-- src/Client.php | 36 +++++++++++++++++++++--------------- tests/ClientTest.php | 3 ++- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index b15016a..3a13453 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,7 @@ "type": "library", "description": "Php HTTP client library for Classy API", "require": { - "guzzlehttp/guzzle": "^6.1", - "symfony/http-foundation": "^3.0" + "guzzlehttp/guzzle": "^6.1" }, "require-dev": { "phpunit/phpunit": "~4.0", diff --git a/src/Client.php b/src/Client.php index 1af5a0e..fdb331a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -114,6 +114,7 @@ public function newMemberSessionFromCode($code) public function newMemberSessionFromCredentials($username, $password) { try { + $ips = $this->getClientIps(); $response = $this->request('POST', '/oauth2/auth', null, [ 'form_params' => [ 'grant_type' => 'password', @@ -121,7 +122,7 @@ public function newMemberSessionFromCredentials($username, $password) 'client_secret' => $this->client_secret, 'username' => $username, 'password' => $password, - 'ip' => $this->getClientIp(), + 'ip' => empty($ips) ? null : implode(', ', $ips), ] ]); } catch (APIResponseException $e) { @@ -140,16 +141,9 @@ public function newMemberSessionFromCredentials($username, $password) */ public function newMemberSessionFromRefreshToken($refresh_token) { - $response = $this->request('POST', '/oauth2/auth', null, [ - 'form_params' => [ - 'grant_type' => 'refresh_token', - 'client_id' => $this->client_id, - 'client_secret' => $this->client_secret, - 'refresh_token' => $refresh_token, - 'ip' => $this->getClientIp(), - ] - ]); - return new Session($response); + $session = new Session(['refresh_token' => $refresh_token]); + $this->refresh($session); + return $session; } /** @@ -158,12 +152,14 @@ public function newMemberSessionFromRefreshToken($refresh_token) public function refresh(Session $session) { if (!is_null($session->getRefreshToken())) { + $ips = $this->getClientIps(); $response = $this->request('POST', '/oauth2/auth', null, [ 'form_params' => [ 'grant_type' => 'refresh_token', 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, - 'refresh_token' => $session->getRefreshToken() + 'refresh_token' => $session->getRefreshToken(), + 'ip' => empty($ips) ? null : implode(', ', $ips), ] ]); } else { @@ -266,9 +262,19 @@ private function applyVersion($version, $endpoint) return "/$version/$endpoint"; } - private function getClientIp() + + /** + * @return array + */ + protected function getClientIps() { - $httpRequest = Request::createFromGlobals(); - return $httpRequest->getClientIp(); + $ips = []; + if (!empty($_SERVER['REMOTE_ADDR'])) { + array_push($ips, $_SERVER['REMOTE_ADDR']); + } + if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + array_push($ips, $_SERVER['HTTP_X_FORWARDED_FOR']); + } + return $ips; } } diff --git a/tests/ClientTest.php b/tests/ClientTest.php index f55a350..cb54178 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -193,7 +193,8 @@ public function testRefreshMemberToken() 'grant_type' => 'refresh_token', 'client_id' => '123', 'client_secret' => '456', - 'refresh_token' => '55555' + 'refresh_token' => '55555', + 'ip' => null, ]; })) ->andReturn(new Response(200, [], json_encode([