Skip to content
Snippets Groups Projects
Commit 9d1850eb authored by Nick Daugherty's avatar Nick Daugherty
Browse files

Updated to use latest (2.3.2) version of Codebird

Now supports app only authentication via Bearer
parent f75444a8
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@ WordPress Codebird
* _Contributors: @automattic, @batmoo, @danielbachuber, @nickdaugherty_
* _Tested up to: 3.5.1_
* _Stable tag: 1.0.1_
* _Stable tag: 1.1.0_
* _License: GPLv2 or later_
* _License URI: http://www.gnu.org/licenses/gpl-2.0.html_
......@@ -23,4 +23,10 @@ Include both the Codebird library and `class-wp-codebird.php`, then get a new in
$wp_codebird = WP_Codebird::getInstance();
```
The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code.
\ No newline at end of file
The rest of the api is identical to Codebird - it is a drop in replacement that does not require any modification to existing code.
Changes
--------------------
**1.1.0 (04/09/2013)**
* Updated to support Codebird 2.3.2 and Bearer authentication
\ No newline at end of file
......@@ -4,9 +4,14 @@
* An extension of the Codebird class to use Wordpress' HTTP API instead of
* cURL.
*
* @version 1.0.1
* @version 1.1.0
*/
class WP_Codebird extends Codebird {
/**
* The current singleton instance
*/
private static $_instance = null;
/**
* Returns singleton class instance
* Always use this method unless you're working with multiple authenticated
......@@ -39,8 +44,10 @@ class WP_Codebird extends Codebird {
*
* @return mixed The API reply, encoded in the set return_format.
*/
protected function _callApi($httpmethod, $method, $method_template, $params = array(), $multipart = false) {
$url = $this->_getEndpoint( $method, $method_template );
protected function _callApi( $httpmethod, $method, $method_template, $params = array(), $multipart = false, $app_only_auth = false ) {
$url = $this->_getEndpoint( $method, $method_template );
$url_with_params = null;
$remote_params = array(
'method' => 'GET',
'timeout' => 5,
......@@ -53,15 +60,19 @@ class WP_Codebird extends Codebird {
'sslverify' => false
);
if ($httpmethod == 'GET') {
$signed = $this->_sign( $httpmethod, $url, $params );
$reply = wp_remote_get( $signed, $remote_params );
if ( 'GET' == $httpmethod ) {
$authorization = $this->_sign( $httpmethod, $url, $params );
if ( count( $params ) > 0 ) {
$url_with_params = $url .= '?' . http_build_query( $params );
}
} else {
if ( $multipart ) {
$authorization = $this->_sign( 'POST', $url, array(), true );
$post_fields = $params;
} else {
$post_fields = $this->_sign( 'POST', $url, $params );
$authorization = $this->_sign( 'POST', $url, $params );
$post_fields = $this->_sign( 'POST', $url, $params );
}
$headers = array();
......@@ -80,7 +91,29 @@ class WP_Codebird extends Codebird {
'cookies' => array(),
'sslverify' => false
);
}
if ( $app_only_auth ){
if ( null == self::$_oauth_consumer_key )
throw new Exception( 'To make an app-only auth API request, the consumer key must be set' );
// automatically fetch bearer token, if necessary
if ( null == self::$_oauth_bearer_token )
$this->oauth2_token();
$bearer = 'Bearer ' . self::$_oauth_bearer_token;
$remote_params['headers']['authorization'] = $bearer;
} else {
// If this is a standard OAuth GET request, add on the authorization header
// Must be added here because $app_only_auth affects what the header will be
if ( 'GET' == $httpmethod )
$remote_params['headers'][] = $authorization;
}
if ( 'GET' == $httpmethod ) {
$reply = wp_remote_get( $url, $remote_params );
} else {
$reply = wp_remote_post( $url, $remote_params );
}
......@@ -104,6 +137,63 @@ class WP_Codebird extends Codebird {
return $reply;
}
/**
* Gets the OAuth bearer token
*
* Overridden to use the WordPress HTTP API
*
* @return string The OAuth bearer token
*/
public function oauth2_token() {
if ( null == self::$_oauth_consumer_key ) {
throw new Exception('To obtain a bearer token, the consumer key must be set.');
}
$post_fields = array(
'grant_type' => 'client_credentials'
);
$url = self::$_endpoint_oauth . 'oauth2/token';
$headers = array(
'Authorization' => 'Basic ' . base64_encode( self::$_oauth_consumer_key . ':' . self::$_oauth_consumer_secret ),
'Expect:'
);
$remote_params = array(
'method' => 'POST',
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => $post_fields,
'cookies' => array(),
'sslverify' => false
);
$reply = wp_remote_post( $url, $remote_params );
$httpstatus = wp_remote_retrieve_response_code( $reply );
$reply = $this->_parseApiReply( 'oauth2/token', $reply );
if ( CODEBIRD_RETURNFORMAT_OBJECT == $this->_return_format ) {
$reply->httpstatus = $httpstatus;
if ( 200 == $httpstatus )
self::setBearerToken( $reply->access_token );
} else {
$reply['httpstatus'] = $httpstatus;
if ( 200 == $httpstatus )
self::setBearerToken( $reply['access_token'] );
}
return $reply;
}
/**
* Parses the API reply to encode it in the set return_format.
*
......@@ -153,4 +243,4 @@ class WP_Codebird extends Codebird {
}
return $parsed;
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment