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

doing some basic clean up for psr2 compliance #16

Open
wants to merge 1 commit 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
9 changes: 6 additions & 3 deletions src/MaxCDN/OAuth/OAuthConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
*/
namespace MaxCDN\OAuth;

class OAuthConsumer {
class OAuthConsumer
{
public $key;
public $secret;

function __construct($key, $secret, $callback_url=NULL) {
function __construct($key, $secret, $callback_url=NULL)
{
$this->key = $key;
$this->secret = $secret;
$this->callback_url = $callback_url;
}

function __toString() {
function __toString()
{
return "\\MaxCDN\\OAuth\\OAuthConsumer[key=$this->key,secret=$this->secret]";
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/MaxCDN/OAuth/OAuthDataStore.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
<?php
namespace MaxCDN\OAuth;

class OAuthDataStore {
function lookup_consumer($consumer_key) {
class OAuthDataStore
{
function lookup_consumer($consumer_key)
{
// implement me
}

function lookup_token($consumer, $token_type, $token) {
function lookup_token($consumer, $token_type, $token)
{
// implement me
}

function lookup_nonce($consumer, $token, $nonce, $timestamp) {
function lookup_nonce($consumer, $token, $nonce, $timestamp)
{
// implement me
}

function new_request_token($consumer, $callback = null) {
function new_request_token($consumer, $callback = null)
{
// return a new token attached to this consumer
}

function new_access_token($token, $consumer, $verifier = null) {
function new_access_token($token, $consumer, $verifier = null)
{
// return a new access token attached to this consumer
// for the user associated with this token if the request token
// is authorized
Expand Down
4 changes: 2 additions & 2 deletions src/MaxCDN/OAuth/OAuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
namespace MaxCDN\OAuth;

class OAuthException extends Exception {
class OAuthException extends Exception
{
// pass
}

7 changes: 4 additions & 3 deletions src/MaxCDN/OAuth/OAuthSignatureMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/
namespace MaxCDN\OAuth;

abstract class OAuthSignatureMethod {
abstract class OAuthSignatureMethod
{
/**
* Needs to return the name of the Signature Method (ie HMAC-SHA1)
* @return string
Expand All @@ -32,7 +33,8 @@ abstract public function build_signature($request, $consumer, $token);
* @param string $signature
* @return bool
*/
public function check_signature($request, $consumer, $token, $signature) {
public function check_signature($request, $consumer, $token, $signature)
{
$built = $this->build_signature($request, $consumer, $token);

// Check for zero length, although unlikely here
Expand All @@ -53,4 +55,3 @@ public function check_signature($request, $consumer, $token, $signature) {
return $result == 0;
}
}

34 changes: 18 additions & 16 deletions src/MaxCDN/OAuth/OAuthSignatureMethod_HMAC_SHA1.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
*/
namespace MaxCDN\OAuth;

class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod {
function get_name() {
return "HMAC-SHA1";
}
class OAuthSignatureMethod_HMAC_SHA1 extends OAuthSignatureMethod
{
function get_name()
{
return "HMAC-SHA1";
}

public function build_signature($request, $consumer, $token) {
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;
public function build_signature($request, $consumer, $token)
{
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;

$key_parts = array(
$consumer->secret,
($token) ? $token->secret : ""
);
$key_parts = array(
$consumer->secret,
($token) ? $token->secret : ""
);

$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
$key = implode('&', $key_parts);
$key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
$key = implode('&', $key_parts);

return base64_encode(hash_hmac('sha1', $base_string, $key, true));
}
return base64_encode(hash_hmac('sha1', $base_string, $key, true));
}
}

14 changes: 8 additions & 6 deletions src/MaxCDN/OAuth/OAuthSignatureMethod_PLAINTEXT.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
namespace MaxCDN\OAuth;


class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod {
public function get_name() {
class OAuthSignatureMethod_PLAINTEXT extends OAuthSignatureMethod
{
public function get_name()
{
return "PLAINTEXT";
}

/**
* oauth_signature is set to the concatenated encoded values of the Consumer Secret and
* Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
* oauth_signature is set to the concatenated encoded values of the Consumer Secret and
* Token Secret, separated by a '&' character (ASCII code 38), even if either secret is
* empty. The result MUST be encoded again.
* - Chapter 9.4.1 ("Generating Signatures")
*
* Please note that the second encoding MUST NOT happen in the SignatureMethod, as
* OAuthRequest handles this!
*/
public function build_signature($request, $consumer, $token) {
public function build_signature($request, $consumer, $token)
{
$key_parts = array(
$consumer->secret,
($token) ? $token->secret : ""
Expand All @@ -34,4 +37,3 @@ public function build_signature($request, $consumer, $token) {
return $key;
}
}

17 changes: 10 additions & 7 deletions src/MaxCDN/OAuth/OAuthSignatureMethod_RSA_SHA1.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
*/
namespace MaxCDN\OAuth;

abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
public function get_name() {
abstract class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod
{
public function get_name()
{
return "RSA-SHA1";
}

Expand All @@ -20,15 +22,16 @@ public function get_name() {
// (3) some sort of specific discovery code based on request
//
// Either way should return a string representation of the certificate
protected abstract function fetch_public_cert(&$request);
abstract protected function fetch_public_cert(&$request);

// Up to the SP to implement this lookup of keys. Possible ideas are:
// (1) do a lookup in a table of trusted certs keyed off of consumer
//
// Either way should return a string representation of the certificate
protected abstract function fetch_private_cert(&$request);
abstract protected function fetch_private_cert(&$request);

public function build_signature($request, $consumer, $token) {
public function build_signature($request, $consumer, $token)
{
$base_string = $request->get_signature_base_string();
$request->base_string = $base_string;

Expand All @@ -47,7 +50,8 @@ public function build_signature($request, $consumer, $token) {
return base64_encode($signature);
}

public function check_signature($request, $consumer, $token, $signature) {
public function check_signature($request, $consumer, $token, $signature)
{
$decoded_sig = base64_decode($signature);

$base_string = $request->get_signature_base_string();
Expand All @@ -67,4 +71,3 @@ public function check_signature($request, $consumer, $token, $signature) {
return $ok == 1;
}
}

13 changes: 8 additions & 5 deletions src/MaxCDN/OAuth/OAuthToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/
namespace MaxCDN\OAuth;

class OAuthToken {
class OAuthToken
{
// access tokens and request tokens
public $key;
public $secret;
Expand All @@ -13,7 +14,8 @@ class OAuthToken {
* key = the token
* secret = the token secret
*/
function __construct($key, $secret) {
function __construct($key, $secret)
{
$this->key = $key;
$this->secret = $secret;
}
Expand All @@ -22,15 +24,16 @@ function __construct($key, $secret) {
* generates the basic string serialization of a token that a server
* would respond to request_token and access_token calls with
*/
function to_string() {
function to_string()
{
return "oauth_token=" .
OAuthUtil::urlencode_rfc3986($this->key) .
"&oauth_token_secret=" .
OAuthUtil::urlencode_rfc3986($this->secret);
}

function __toString() {
function __toString()
{
return $this->to_string();
}
}

54 changes: 30 additions & 24 deletions src/MaxCDN/OAuth/OAuthUtil.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
namespace MaxCDN\OAuth;

class OAuthUtil {
public static function urlencode_rfc3986($input) {
class OAuthUtil
{
public static function urlencode_rfc3986($input)
{
if (is_array($input)) {
return array_map(array('\MaxCDN\OAuth\OAuthUtil', 'urlencode_rfc3986'), $input);
} else if (is_scalar($input)) {
return str_replace(
'+',
' ',
str_replace('%7E', '~', rawurlencode($input))
);
'+',
' ',
str_replace('%7E', '~', rawurlencode($input))
);
} else {
return '';
}
Expand All @@ -20,7 +22,8 @@ public static function urlencode_rfc3986($input) {
// This decode function isn't taking into consideration the above
// modifications to the encoding process. However, this method doesn't
// seem to be used anywhere so leaving it as is.
public static function urldecode_rfc3986($string) {
public static function urldecode_rfc3986($string)
{
return urldecode($string);
}

Expand All @@ -29,21 +32,23 @@ public static function urldecode_rfc3986($string) {
// Can filter out any non-oauth parameters if needed (default behaviour)
// May 28th, 2010 - method updated to tjerk.meesters for a speed improvement.
// see http://code.google.com/p/oauth/issues/detail?id=163
public static function split_header($header, $only_allow_oauth_parameters = true) {
public static function split_header($header, $only_allow_oauth_parameters = true)
{
$params = array();
if (preg_match_all('/('.($only_allow_oauth_parameters ? 'oauth_' : '').'[a-z_-]*)=(:?"([^"]*)"|([^,]*))/', $header, $matches)) {
foreach ($matches[1] as $i => $h) {
$params[$h] = OAuthUtil::urldecode_rfc3986(empty($matches[3][$i]) ? $matches[4][$i] : $matches[3][$i]);
}
if (isset($params['realm'])) {
unset($params['realm']);
if (isset($params['realm'])) {
unset($params['realm']);
}
}
}
return $params;
return $params;
}

// helper to try to sort out headers for people who aren't running apache
public static function get_headers() {
public static function get_headers()
{
if (function_exists('apache_request_headers')) {
// we need this to get the actual Authorization: header
// because apache tends to tell us it doesn't exist
Expand All @@ -56,10 +61,10 @@ public static function get_headers() {
$out = array();
foreach ($headers AS $key => $value) {
$key = str_replace(
" ",
"-",
ucwords(strtolower(str_replace("-", " ", $key)))
);
" ",
"-",
ucwords(strtolower(str_replace("-", " ", $key)))
);
$out[$key] = $value;
}
} else {
Expand All @@ -77,10 +82,10 @@ public static function get_headers() {
// letter of every word that is not an initial HTTP and strip HTTP
// code from przemek
$key = str_replace(
" ",
"-",
ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
);
" ",
"-",
ucwords(strtolower(str_replace("_", " ", substr($key, 5))))
);
$out[$key] = $value;
}
}
Expand All @@ -91,7 +96,8 @@ public static function get_headers() {
// This function takes a input like a=b&a=c&d=e and returns the parsed
// parameters like this
// array('a' => array('b','c'), 'd' => 'e')
public static function parse_parameters( $input ) {
public static function parse_parameters( $input )
{
if (!isset($input) || !$input) return array();

$pairs = explode('&', $input);
Expand Down Expand Up @@ -120,7 +126,8 @@ public static function parse_parameters( $input ) {
return $parsed_parameters;
}

public static function build_http_query($params) {
public static function build_http_query($params)
{
if (!$params) return '';

// Urlencode both keys and values
Expand Down Expand Up @@ -151,4 +158,3 @@ public static function build_http_query($params) {
return implode('&', $pairs);
}
}