Skip to content

Commit

Permalink
Importing the work so far, raw, not tested very well
Browse files Browse the repository at this point in the history
  • Loading branch information
konfirm committed Mar 2, 2012
0 parents commit 2691284
Show file tree
Hide file tree
Showing 37 changed files with 4,058 additions and 0 deletions.
35 changes: 35 additions & 0 deletions authentication/oauth.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedAuthenticationOAuth
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: Authentication/OAuth
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* @name BreedAuthenticationOAuth
* @type class
* @date 2/27/11
* @author Rogier Spieker <rogier@konsolidate.net>
*/

class BreedAuthenticationOAuth extends Konsolidate
{
const OAUTH_VERSION="1.0";


public function __construct( $oParent )
{
parent::__construct( $oParent );

$this->version = self::OAUTH_VERSION;
}
}
40 changes: 40 additions & 0 deletions authentication/oauth/consumer.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedAuthenticationOAuthConsumer
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: /Authentication/OAuth/Consumer
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* @name BreedAuthenticationOAuthConsumer
* @type class
* @date 2/28/11
* @author Rogier Spieker <rogier@konsolidate.net>
*/

class BreedAuthenticationOAuthConsumer extends Konsolidate
{
public $key;
public $secret;

public function initialize( $sKey=null, $sSecret=null )
{
$this->key = $sKey;
$this->secret = $sSecret;
}

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

163 changes: 163 additions & 0 deletions authentication/oauth/request.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedAuthenticationOAuthRequest
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: /Authentication/OAuth/Request
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* @name BreedAuthenticationOAuthRequest
* @type class
* @date 2/27/11
* @author Rogier Spieker <rogier@konsolidate.net>
*/

class BreedAuthenticationOAuthRequest extends Konsolidate
{
const DEFAULT_HTTP_METHOD = "POST";
const DEFAULT_SIGN_METHOD = "HMAC-SHA1";

public $url;
public $method;


public function getResponseData( $sURL, $aParam, $oConsumer, $oToken=null, $sHTTPMethod=null, $sSignatureMethod="HMAC-SHA1" )
{
$oReturn = $this->getResponse( $sURL, $aParam, $oConsumer, $oToken, $sHTTPMethod, $sSignatureMethod );
if ( $oReturn && $oReturn->getStatus() == "200" )
return $oReturn->getData();
return false;
}

public function getResponseHeaders( $sURL, $aParam, $oConsumer, $oToken=null, $sHTTPMethod=null, $sSignatureMethod="HMAC-SHA1" )
{
$oReturn = $this->getResponse( $sURL, $aParam, $oConsumer, $oToken, $sHTTPMethod, $sSignatureMethod );
if ( $oReturn && $oReturn->getStatus() == "200" )
return $oReturn->getHeader();
return false;
}

public function createParameters( $sURL, $aParam, $oConsumer, $oToken=null, $sHTTPMethod=null, $sSignatureMethod="HMAC-SHA1" )
{
// Set default HTTP method in case none is provided
if ( empty( $sHTTPMethod ) )
$sHTTPMethod = self::DEFAULT_HTTP_METHOD;

// Set all 'variable' OAuth variables
$aOAuth = Array(
"oauth_signature_method"=>$this->call( "../Signature/identify", $sSignatureMethod ),
"oauth_consumer_key"=>$oConsumer->key
);
if ( is_object( $oToken ) )
$aOAuth[ "oauth_token" ] = $oToken->key;

// Merge all request variables
$aParam = array_merge(
is_array( $aParam ) ? $aParam : Array(),
$this->_getDefaultParams(),
$aOAuth
);

// Create the OAuth signature and add it to the request variables and return the full URL
$aParam[ "oauth_signature" ] = $this->call(
"../Signature/create",
$sSignatureMethod,
$sHTTPMethod,
$sURL,
$aParam,
$oConsumer->secret,
is_object( $oToken ) ? $oToken->secret : null
);

return $aParam;
}

public function getResponse( $sURL, $aParam, $oConsumer, $oToken=null, $sHTTPMethod=null, $sSignatureMethod="HMAC-SHA1" )
{
$aParam = $this->createParameters(
$sURL,
$aParam,
$oConsumer,
$oToken,
$sHTTPMethod,
$sSignatureMethod
);
$oReturn = $this->instance( "/Request/Response" );
if ( $oReturn->execute( $this->_sanitizeURL( $sURL ), $aParam, null, $sHTTPMethod ) )
return $oReturn;
return false;
}

protected function _getDefaultParams()
{
return Array(
"oauth_nonce"=>$this->_getNonce(),
"oauth_timestamp"=>time(),
"oauth_version"=>$this->get( "../version" )
);
}

protected function _getNonce()
{
return md5( mt_rand() . microtime() );
}

protected function _sanitizeMethod( $sMethod )
{
return strToUpper( $sMethod );
}

protected function _sanitizeURL( $sURL )
{
$aPart = parse_url( $sURL );
$sScheme = CoreTool::arrayVal( "scheme", $aPart, "http" );
$nPort = (int) CoreTool::arrayVal( "port", $aPart, $sScheme == "https" ? 443 : 80 );
$sHost = CoreTool::arrayVal( "host", $aPart ) . ( ( $sScheme == "https" && $nPort == 443 ) || ( $sScheme == "http" && $nPort == 80 ) ? "" : ":{$nPort}" );
$sPath = CoreTool::arrayVal( "path", $aPart );

return "{$sScheme}://{$sHost}{$sPath}";
}

protected function _createQueryString( $aParam, $sAppendTo=null )
{
if ( is_array( $aParam ) && (bool) count( $aParam ) )
{
$aParam = array_combine(
$this->urlencode( array_keys( $aParam ) ),
$this->urlencode( array_values( $aParam ) )
);

$sReturn = "";
foreach( $aParam as $sKey=>$sValue )
$sReturn .= "&{$sKey}={$sValue}";
return trim( !empty( $sAppendTo ) ? "{$sAppendTo}{$sReturn}" : $sReturn, "&" );
}

return "";
}

public function urlencode( $mValue )
{
if ( is_scalar( $mValue ) )
return str_replace( Array( "+", "%7E" ), Array( " ", "~" ), rawurlencode( $mValue ) );
else if ( is_array( $mValue ) )
return array_map( Array( $this, "urlencode" ), $mValue );
return "";
}

public function urldecode( $mValue )
{
if ( is_array( $mValue ) )
return array_map( Array( $this, "urldecode" ), $mValue );
return rawurldecode( $mValue );
}
}
85 changes: 85 additions & 0 deletions authentication/oauth/signature.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedAuthenticationOAuthSignature
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: /Authentication/OAuth/Signature
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* @name BreedAuthenticationOAuthSignature
* @type class
* @date 2/27/11
* @author Rogier Spieker <rogier@konsolidate.net>
*/

class BreedAuthenticationOAuthSignature extends Konsolidate
{
public function __construct( $oParent )
{
parent::__construct( $oParent );
}

public function create( $sSignMethod, $sHTTPMethod, $sURL, $aParam, $sConsumerSecret, $sTokenSecret=null )
{
list( $sSignMethod, $sHashMethod ) = explode( "-", $sSignMethod );
return $this->call(
"{$sSignMethod}/create",
$this->_signatureBase(
strToUpper( $sHTTPMethod ),
$sURL,
$aParam
),
$this->call( "../Request/urlencode", $sConsumerSecret ),
$this->call( "../Request/urlencode", !empty( $sTokenSecret ) ? $sTokenSecret : "" ),
$sHashMethod
);
}

public function identify( $sSignMethod=null )
{
list( $sSignMethod, $sHash ) = explode( "-", $sSignMethod );
return $this->call( "{$sSignMethod}/identify", $sHash );
}

protected function _signableParams( $aParam )
{
$aParam = array_combine(
$this->call( "../Request/urlencode", array_keys( $aParam ) ),
$this->call( "../Request/urlencode", array_values( $aParam ) )
);

uksort( $aParam, "strnatcmp" );

$aReturn = Array();
foreach( $aParam as $sKey=>$mValue )
if ( $sKey == "oauth_signature" )
continue;
else if ( is_scalar( $mValue ) )
array_push( $aReturn, "{$sKey}={$mValue}" );
else if ( is_array( $mValue ) && natsort( $mValue ) )
foreach( $mValue as $sValue )
array_push( $aReturn, "{$sKey}={$sValue}" );
return implode( "&", $aReturn );
}

protected function _signatureBase( $sHTTPMethod, $sURL, $aParam )
{
return implode(
"&",
Array(
$this->call( "../Request/urlencode", strToUpper( $sHTTPMethod ) ),
$this->call( "../Request/urlencode", $sURL ),
$this->call( "../Request/urlencode", $this->_signableParams( $aParam ) )
)
);
}
}
41 changes: 41 additions & 0 deletions authentication/oauth/signature/hmac.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* ________ ___
* / / /\ /\ Konsolidate
* ____/ /___/ \/ \
* / /\ / http://www.konsolidate.net
* /___ ___/ \ /
* \ / /\ \ / \ Class: BreedAuthenticationOAuthSignatureHMAC
* \/___/ \___\/ \ Tier: Breed
* \ \ /\ \ /\ / Module: /Authentication/OAuth/Signature/HMAC
* \___\/ \___\/ \/
* \ \ / $Rev$
* \___ ___\/ $Author$
* \ \ / $Date$
* \___\/
*
* @name BreedAuthenticationOAuthSignatureHMAC
* @type class
* @date 2/27/11
* @author Rogier Spieker <rogier@konsolidate.net>
*/

class BreedAuthenticationOAuthSignatureHMAC extends Konsolidate
{
public function identify( $sHashMethod="sha1" )
{
return "HMAC-" . strToUpper( $sHashMethod );
}

public function create( $sBase, $sSecret, $sToken=null, $sHashMethod="sha1" )
{
$sKey = $sSecret . "&" . ( empty( $sToken ) ? "" : $sToken );

return base64_encode(
function_exists( "hash_hmac" )
? hash_hmac( $sHashMethod, $sBase, $sKey, true )
: $this->call( "/Encryption/HMAC/hash", $sKey, $sRequest, $sHashMethod )
);
}
}
Loading

0 comments on commit 2691284

Please sign in to comment.