Skip to content

Commit

Permalink
have normalizer gen signature
Browse files Browse the repository at this point in the history
* fixes #27
* Added simple unit test for PHP lib.
  • Loading branch information
jrconlin committed Mar 14, 2015
1 parent 90ec8b5 commit 7995215
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
17 changes: 12 additions & 5 deletions php/OAuthSimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ public function sign($args=array())
}
$this->setParameters($args['parameters']);
$normParams = $this->_normalizedParameters();
$this->_parameters['oauth_signature'] = $this->_generateSignature($normParams);

return Array (
'parameters' => $this->_parameters,
Expand Down Expand Up @@ -433,8 +432,13 @@ private function _normalizedParameters()
$normalized_keys = array();
$return_array = array();

foreach ( $this->_parameters as $paramName=>$paramValue) {
if (!preg_match('/\w+_secret/',$paramName) OR (strpos($paramValue, '@') !== 0 && !file_exists(substr($paramValue, 1))) )
foreach ( $this->_parameters as $paramName=>$paramValue) {
if (preg_match('/w+_secret/', $paramName) OR
$paramName == "oauth_signature") {
continue;

This comment has been minimized.

Copy link
@jrconlin

jrconlin Mar 14, 2015

Author Owner

continue is a bit nicer than complex logic.

}
// Read parameters from a file. Hope you're practicing safe PHP.
if (strpos($paramValue, '@') !== 0 && !file_exists(substr($paramValue, 1)))
{
if (is_array($paramValue))
{
Expand Down Expand Up @@ -468,8 +472,11 @@ private function _normalizedParameters()
array_push($return_array, $key .'='. $val);
}

}

}
$presig = join("&", $return_array);
$sig = $this->_generateSignature($presig);
$this->_parameters['oauth_signature']=$sig;
array_push($return_array, "oauth_signature=$sig");
return join("&", $return_array);
}

Expand Down
71 changes: 71 additions & 0 deletions php/testOAuthSimple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

// Craptastic UNIT test for PHP OAuthSimple

require 'OAuthSimple.php';

$path = 'http://example.com/test';
$static_nonce = 'abcd123';
$static_time = 1234567890;
$signatures = array('consumer_key' => 'test_key',
'shared_secret' => 'test_secret',
'oauth_token' => 'access_key',
'oauth_secret' => 'access_secret');
$parameters = array(
'fruit'=>'bananas are <Awe+some!>',
'number'=>42,
// defining these here overrides the auto-generator.
'oauth_nonce'=>$static_nonce,
'oauth_timestamp'=>$static_time);
$oauth = new OAuthSimple();
$results = $oauth->sign(array('path'=>$path,
'parameters'=>$parameters,
'signatures'=>$signatures));

// ====
$expected = array(
'fruit'=>'bananas are <Awe+some!>',
'number'=>42,
'oauth_nonce'=>$static_nonce,
'oauth_timestamp'=>$static_time,
'oauth_consumer_key'=>$signatures['consumer_key'],
'oauth_token'=>$signatures['oauth_token'],
'oauth_signature_method'=>'HMAC-SHA1',
'oauth_version'=>1.0,
'oauth_signature'=>'IkTXsl3d/FV7uOY0p9CFFCxpdyQ=');
if ($results['parameters'] != $expected) {
print_r($results['parameters']);
throw new OAuthSimpleException("Failure: incorrect parameters returned");
}


// ====
$expected="IkTXsl3d%2FFV7uOY0p9CFFCxpdyQ%3D";
if ($results['signature'] != $expected) {
print $results['signature']."\n$expected\n";
throw new OAuthSimpleException("Failure: incorrect signature returned");
}


// ====
$expected="http://example.com/test?fruit=bananas%20are%20%3CAwe%2Bsome%21%3E&number=42&oauth_consumer_key=test_key&oauth_nonce=abcd123&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1234567890&oauth_token=access_key&oauth_version=1.0&oauth_signature=IkTXsl3d/FV7uOY0p9CFFCxpdyQ=";
if ($results['signed_url'] != $expected){
print $results['signed_url']."\n$expected\n";
throw new OAuthSimpleException("Failure: Invalid signed URL returned");
}

// ====
$expected='OAuth oauth_nonce="abcd123", oauth_timestamp="1234567890", oauth_consumer_key="test_key", oauth_token="access_key", oauth_signature_method="HMAC-SHA1", oauth_version="1.0", oauth_signature="IkTXsl3d%2FFV7uOY0p9CFFCxpdyQ%3D"';
if ($results['header'] != $expected) {
print $results['header']."\n$expected\n";
throw new OAuthSimpleException("Failure: Invalid Header returned");
}

// ====
$expected='GET&http%3A%2F%2Fexample.com%2Ftest&fruit%3Dbananas%2520are%2520%253CAwe%252Bsome%2521%253E%26number%3D42%26oauth_consumer_key%3Dtest_key%26oauth_nonce%3Dabcd123%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1234567890%26oauth_token%3Daccess_key%26oauth_version%3D1.0';
if ($results['sbs'] != $expected) {
print $results['sbs']."\n$expected\n";
throw new OAuthSimpleException("Failure: Invalid Base String returned");
}

print("ok\n");

0 comments on commit 7995215

Please sign in to comment.