Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5 bad assumptions #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getConfigTreeBuilder()
->defaultValue(self::APPLICATION_TYPE_PRIVATE)
->end()
->scalarNode('base_url')
->defaultValue('https://api.xero.com/api.xro/2.0')
->defaultValue('https://api.xero.com/api.xro/2.0/')
->end()
->scalarNode('consumer_key')
->isRequired()
Expand Down
27 changes: 26 additions & 1 deletion Tests/XeroClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace BlackOptic\Bundle\XeroBundle\Tests;

use BlackOptic\Bundle\XeroBundle\XeroClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;

class XeroClientTest extends TestBase
{
Expand All @@ -13,7 +17,7 @@ protected function setUp()
parent::setUp();

$this->options = array(
'base_url' => 'https://api.xero.com/api.xro/2.0',
'base_url' => 'https://api.xero.com/api.xro/2.0/',
'consumer_key' => '',
'consumer_secret' => '',
'private_key' => '',
Expand All @@ -37,4 +41,25 @@ public function testInstantiationWithKey()
// Test set token methods.
$client->setToken('a', 'b');
}

public function testGetRequest() {
$mock = new MockHandler(array(
new Response(200, array('Content-Length' => 0))
));
$this->options['handler'] = HandlerStack::create($mock);
$this->options['private_key'] = $this->pemFile;

$client = new XeroClient($this->options);

try
{
$client->get('Accounts');
}
catch (RequestException $e)
{
$this->assertNotEquals('401', $e->getCode());
}

$this->assertEquals('/api.xro/2.0/Accounts', $mock->getLastRequest()->getUri()->getPath());
}
}
22 changes: 8 additions & 14 deletions XeroClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,22 @@ public function __construct($config = array())
throw new FileNotFoundException('Unable able to find file: ' . $config['private_key']);
}

$privateKey = file_get_contents($config['private_key']);

$config['signature_method'] = 'RSA-SHA1';
$config['signature_callback'] = function ($baseString) use ($privateKey) {
$signature = '';
$privateKeyId = openssl_pkey_get_private($privateKey);
openssl_sign($baseString, $signature, $privateKeyId);
openssl_free_key($privateKeyId);
return $signature;
};

if ($config['signature_callback'] === '') {
throw new \Exception('Could not create signature from key');
// Do not obliterate a stack that may be passed into the client.
if (isset($config['handler']) && is_a($config['handler'], '\GuzzleHttp\HandlerStack')) {
$stack = $config['handler'];
} else {
$stack = HandlerStack::create();
}

$stack = HandlerStack::create();
// Create an oauth middleware and push it onto the handler stack.
$middleware = new Oauth1([
'consumer_key' => $config['consumer_key'],
'consumer_secret' => $config['consumer_secret'],
'token' => $config['token'],
'token_secret' => $config['token_secret'],
'private_key_file' => $config['private_key'],
'private_key_passphrase' => NULL,
'signature_method' => Oauth1::SIGNATURE_METHOD_RSA,
]);
$stack->push($middleware);

Expand Down