-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from nitinjavakid/master
Changes to implement OAuth2 based authentication
- Loading branch information
Showing
6 changed files
with
230 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace DMS\Service\Meetup; | ||
|
||
use DMS\Service\Meetup\Plugin\OAuth2Plugin; | ||
|
||
/** | ||
* Meetup.com API Client based on OAuth 2.0. | ||
* | ||
* Docs: OAuth 2.0 requests -- for apps that carry out actions for many Meetup users. | ||
* | ||
* @link http://www.meetup.com/meetup_api/auth/ | ||
*/ | ||
class MeetupOAuth2Client extends AbstractMeetupClient | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return array | ||
*/ | ||
public static function getDefaultParameters() | ||
{ | ||
return array( | ||
'base_url' => '{scheme}://api.meetup.com/', | ||
'scheme' => 'https', | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @return array | ||
*/ | ||
public static function getRequiredParameters() | ||
{ | ||
return array('access_token'); | ||
} | ||
|
||
/** | ||
* Factory Method to build new Client. | ||
* | ||
* @param array $config | ||
* | ||
* @return MeetupOAuth2Client | ||
*/ | ||
public static function factory($config = array()) | ||
{ | ||
$configuration = static::buildConfig($config); | ||
|
||
$client = new self($configuration->get('base_url'), $configuration); | ||
|
||
$client->addSubscriber( | ||
new OAuth2Plugin($configuration->get('access_token') | ||
) | ||
); | ||
|
||
static::loadDefinitions($client); | ||
static::toggleRateLimitingPlugin($client, $config); | ||
|
||
return $client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace DMS\Service\Meetup\Plugin; | ||
|
||
use Guzzle\Common\Event; | ||
use Guzzle\Http\Message\Request; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Class OAuth2Plugin. | ||
* | ||
* This Guzzle plugin implements OAuth2 based authorization that is supported by the Meetup.com API | ||
*/ | ||
class OAuth2Plugin implements EventSubscriberInterface | ||
{ | ||
protected $access_token; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param $access_token | ||
*/ | ||
public function __construct($access_token) | ||
{ | ||
$this->access_token = $access_token; | ||
} | ||
|
||
/** | ||
* Returns an array of event names this subscriber wants to listen to. | ||
* | ||
* The array keys are event names and the value can be: | ||
* | ||
* * The method name to call (priority defaults to 0) | ||
* * An array composed of the method name to call and the priority | ||
* * An array of arrays composed of the method names to call and respective | ||
* priorities, or 0 if unset | ||
* | ||
* For instance: | ||
* | ||
* * array('eventName' => 'methodName') | ||
* * array('eventName' => array('methodName', $priority)) | ||
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')) | ||
* | ||
* @return array The event names to listen to | ||
* | ||
* @api | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
'request.before_send' => array('onRequestBeforeSend', -1000), | ||
); | ||
} | ||
|
||
/** | ||
* Request before-send event handler. | ||
* | ||
* @param Event $event Event received | ||
* | ||
* @return array | ||
*/ | ||
public function onRequestBeforeSend(Event $event) | ||
{ | ||
/** @var $request Request */ | ||
$request = $event['request']; | ||
|
||
$this->signRequest($request); | ||
} | ||
|
||
/** | ||
* Adds "access_token" parameters as a authentication header | ||
* | ||
* @param Request $request | ||
*/ | ||
protected function signRequest($request) | ||
{ | ||
$request->addHeader('Authorization', 'Bearer ' . $this->access_token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace DMS\Service\Meetup; | ||
|
||
use Guzzle\Tests\GuzzleTestCase; | ||
|
||
class MeetupOAuth2ClientTest extends GuzzleTestCase | ||
{ | ||
public function testFactory() | ||
{ | ||
$config = array( | ||
'access_token' => '**', | ||
); | ||
|
||
$client = MeetupOAuth2Client::factory($config); | ||
|
||
$this->assertInstanceOf('DMS\Service\Meetup\MeetupOAuth2Client', $client); | ||
} | ||
|
||
/** | ||
* @expectedException Guzzle\Common\Exception\InvalidArgumentException | ||
*/ | ||
public function testFactoryValidation() | ||
{ | ||
$config = array(); | ||
MeetupOAuth2Client::factory($config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace DMS\Service\Meetup\Plugin; | ||
|
||
use Guzzle\Common\Event; | ||
use Guzzle\Http\Message\Request; | ||
use PHPUnit_Framework_TestCase; | ||
|
||
class OAuth2PluginTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var OAuth2Plugin | ||
*/ | ||
protected $plugin; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $access_token = 'my_access_token'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->plugin = new OAuth2Plugin($this->access_token); | ||
} | ||
|
||
public function testGetSubscribedEvents() | ||
{ | ||
$events = OAuth2Plugin::getSubscribedEvents(); | ||
|
||
$this->assertArrayHasKey('request.before_send', $events); | ||
} | ||
|
||
public function testOnRequestBeforeSendGET() | ||
{ | ||
$request = new Request('GET', 'www.url.com'); | ||
|
||
$event = new Event(array('request' => $request)); | ||
|
||
$this->plugin->onRequestBeforeSend($event); | ||
|
||
$this->assertEquals('Bearer ' . $this->access_token, $request->getHeader('Authorization')); | ||
} | ||
|
||
public function testOnRequestBeforeSendPOST() | ||
{ | ||
$request = new Request('POST', 'www.url.com'); | ||
|
||
$event = new Event(array('request' => $request)); | ||
|
||
$this->plugin->onRequestBeforeSend($event); | ||
|
||
$this->assertEquals('Bearer ' . $this->access_token, $request->getHeader('Authorization')); | ||
} | ||
} |