-
Notifications
You must be signed in to change notification settings - Fork 332
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
RSA-based and SHA-256-involving signature methods #194
base: master
Are you sure you want to change the base?
Changes from 1 commit
db68652
35631a1
3d8bae4
aad9ecf
edb2934
3c3c210
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ public function reconfigure($config=array()) { | |
'token' => '', | ||
'secret' => '', | ||
|
||
// RSA private key (for RSA-SHA1 and RSA-SHA256 methods) | ||
'private_key_pem' => '', | ||
|
||
// OAuth2 bearer token. This should already be URL encoded | ||
'bearer' => '', | ||
|
||
|
@@ -453,11 +456,48 @@ private function prepare_base_string() { | |
* @return void oauth_signature is added to the parameters in the class array variable '$this->request_settings' | ||
*/ | ||
private function prepare_oauth_signature() { | ||
$this->request_settings['oauth1_params']['oauth_signature'] = $this->safe_encode( | ||
base64_encode( | ||
hash_hmac( | ||
'sha1', $this->request_settings['basestring'], $this->request_settings['signing_key'], true | ||
))); | ||
switch ($this->config['oauth_signature_method']) { | ||
case 'HMAC-SHA1': | ||
$signature = $this->sign_with_hmac('sha1'); | ||
break; | ||
case 'HMAC-SHA256': | ||
$signature = $this->sign_with_hmac('sha256'); | ||
break; | ||
case 'RSA-SHA1': | ||
$signature = $this->sign_with_rsa('sha1'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know 'sha1' is supported but what about using OPENSSL_ALGO_SHA1 instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced 'sha1' with OPENSSL_ALGO_SHA1 |
||
break; | ||
case 'RSA-SHA256': | ||
$signature = $this->sign_with_rsa('sha256'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know 'sha256' is supported but what about using OPENSSL_ALGO_SHA256 instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced 'sha256' with OPENSSL_ALGO_SHA256 |
||
break; | ||
default: | ||
throw new Exception("Unsupported oauth_signature_method: '" . $this->config['oauth_signature_method'] . "'"); | ||
} | ||
$this->request_settings['oauth1_params']['oauth_signature'] = $this->safe_encode(base64_encode($signature)); | ||
} | ||
|
||
/** | ||
* Signs the OAuth 1 request with HMAC-based signature | ||
* | ||
* @param string $algorithm algorithm name (like sha1 or sha256) | ||
* @return binary signature | ||
*/ | ||
private function sign_with_hmac($algorithm) { | ||
return hash_hmac( | ||
$algorithm, $this->request_settings['basestring'], $this->request_settings['signing_key'], true | ||
); | ||
} | ||
|
||
/** | ||
* Signs the OAuth 1 request with RSA-based signature | ||
* | ||
* @param string $algorithm name of hash algorithm that will be | ||
* used to compute base string hash before encrypting it with RSA | ||
* (like sha1 or sha256) | ||
* @return binary signature | ||
*/ | ||
private function sign_with_rsa($algorithm) { | ||
openssl_sign($this->request_settings['basestring'], $signature, $this->config['private_key_pem'], $algorithm); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thinking this might need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a check for whether 'openssl_sign' function exists. If not, an exception is thrown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for contributing this, love it. just a couple of thoughts on improvements before a pull it in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bumped version and date |
||
return $signature; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
openssl_sign is a messy function as it overloads the types that can be used for the key and algorithm variables.
it's possible to send a resource instead of a string as the private_key_pem but the implementation you are using assumes it's string. wanna call that out in the comment - that if used this is expecting a string representation of a private key and not a file resource?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A clarifying comment added