Skip to content

Commit

Permalink
Collect all ips on member tokens generation
Browse files Browse the repository at this point in the history
  • Loading branch information
PGBI committed Jul 18, 2016
1 parent 2e1d54c commit bf862e9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 21 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ 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',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'username' => $username,
'password' => $password,
'ip' => $this->getClientIp(),
'ip' => empty($ips) ? null : implode(', ', $ips),
]
]);
} catch (APIResponseException $e) {
Expand All @@ -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;
}

/**
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit bf862e9

Please sign in to comment.