Skip to content

Commit

Permalink
Added parameter userIP when authenticating with user credentials to s…
Browse files Browse the repository at this point in the history
…end the ip of the user
  • Loading branch information
jdeveloper committed Oct 2, 2015
1 parent dfeb5d3 commit 1177012
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Client/HttpAdapter/GuzzleHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Guzzle\Http\Exception\ServerErrorResponseException;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Exception\CurlException;
use Symfony\Component\HttpFoundation\Request;

/**
* Class GuzzleHttpAdapter
* @package Ant\Chatea\SecureBundle\Client\HttpAdapter
Expand All @@ -38,6 +40,11 @@ class GuzzleHttpAdapter implements HttpAdapterInterface
*/
private $secret;

/**
* @var Request
*/
private $request;

/**
* @param string $base_url The Server end point
* @param string $clientId The public key of client
Expand Down Expand Up @@ -71,6 +78,11 @@ public function __construct($base_url, $clientId, $secret, ClientInterface $clie
$this->client->setDescription(ServiceDescription::factory(__DIR__.'./../../Resources/config/api-services.json'));
}

public function setRequest(Request $request = null)
{
$this->request = $request;
}


/**
* The public key of client
Expand Down Expand Up @@ -126,9 +138,13 @@ public function withUserCredentials($username, $password)
throw new InvalidArgumentException("password must be a non-empty string");
}

$command = $this->client->getCommand('withUserCredentials',
array('client_id'=>$this->getClientId(),'client_secret'=>$this->getSecret(),'username'=>$username,'password'=>$password)
);
$params = array('client_id'=>$this->getClientId(),'client_secret'=>$this->getSecret(),'username'=>$username,'password'=>$password);

if($this->request != null){
$params['userIP'] = $this->request->getClientIP();
}

$command = $this->client->getCommand('withUserCredentials', $params);

try{
return $command->execute();
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/api-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"type": "string",
"required": true,
"description": "the password"
},
"userIP":{
"location": "json",
"type": "string",
"required": false,
"description": "user ip"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<argument key="client_id">%chatea_secure.app_auth.client_id%</argument>
<argument key="secret">%chatea_secure.app_auth.secret%</argument>
<argument type="service" id="antwebs_chateasecure.guzzle_client" />
<call method="setRequest">
<argument type="service" id="request" on-invalid="null" strict="false" />
</call>
</service>

<service id="antwebs_chateasecure_user_provider" class="%antwebs_chateasecure_user_provider.class%" >
Expand Down
67 changes: 67 additions & 0 deletions Tests/Security/Client/HttpAdapter/GuzzleHttpAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* User: José Ramón Fernandez Leis
* Email: [email protected]
* Date: 2/10/15
* Time: 9:23
*/

namespace Ant\Bundle\ChateaSecureBundle\Tests\Security\Client\HttpAdapter;


use Ant\Bundle\ChateaSecureBundle\Client\HttpAdapter\GuzzleHttpAdapter;

class GuzzleHttpAdapterTest extends \PHPUnit_Framework_TestCase
{
private $client;
private $guzzleHttpAdapter;

protected function setUp()
{
parent::setUp();

$this->client = $this->getMock('Guzzle\Service\ClientInterface');
$this->guzzleHttpAdapter = new GuzzleHttpAdapter('http://api.local', 'an_id', 'a_secret', $this->client);
}

public function testWithUserCredentials()
{
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
->disableOriginalConstructor()
->getMock();

$request->expects($this->any())
->method('getClientIP')
->will($this->returnValue('8.8.8.8'));

$command = $this->getMock('Guzzle\Service\Command\CommandInterface');

$command->expects($this->once())
->method('execute');

$this->client->expects($this->once())
->method('getCommand')
->with('withUserCredentials', array('client_id' => 'an_id', 'username' => 'AUSER', 'password' => 'APASSWORD', 'userIP' => '8.8.8.8', 'client_secret' => 'a_secret'))
->will($this->returnValue($command));

$this->guzzleHttpAdapter->setRequest($request);
$this->guzzleHttpAdapter->withUserCredentials('AUSER', 'APASSWORD');
}



public function testWithUserCredentialsWithNoRequest()
{
$command = $this->getMock('Guzzle\Service\Command\CommandInterface');

$command->expects($this->once())
->method('execute');

$this->client->expects($this->once())
->method('getCommand')
->with('withUserCredentials', array('client_id' => 'an_id', 'username' => 'AUSER', 'password' => 'APASSWORD', 'client_secret' => 'a_secret'))
->will($this->returnValue($command));

$this->guzzleHttpAdapter->withUserCredentials('AUSER', 'APASSWORD');
}
}

0 comments on commit 1177012

Please sign in to comment.