diff --git a/.travis.yml b/.travis.yml index 9228786..1570118 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,7 @@ language: php php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 - 7.0 -matrix: - include: - - php: 5.3 - dist: precise - exclude: - - php: 5.3 - dist: trusty - before_script: - composer selfupdate - composer install --prefer-source --dev diff --git a/README.md b/README.md index 4844995..c373014 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ The library is available through Composer, so its easy to get it. Just Run this: * Legacy v1, except methods tagged as deprecated * Key authentication * OAuth 1.0 Authentication +* OAuth 2.0 Authentication * POST, GET and DELETE methods ## Usage @@ -37,6 +38,12 @@ $config = array( 'token_secret' => '*****', ); $client = MeetupOAuthClient::factory($config); + +// OAuth2 Authentication +$config = array( + 'access_token' => 'access_token', +); +$client = MeetupOAuth2Client::factory($config); ``` Invoke Commands using our `__call` method (auto-complete phpDocs are included) diff --git a/src/DMS/Service/Meetup/MeetupOAuth2Client.php b/src/DMS/Service/Meetup/MeetupOAuth2Client.php new file mode 100644 index 0000000..c5c5a90 --- /dev/null +++ b/src/DMS/Service/Meetup/MeetupOAuth2Client.php @@ -0,0 +1,62 @@ + '{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; + } +} diff --git a/src/DMS/Service/Meetup/Plugin/OAuth2Plugin.php b/src/DMS/Service/Meetup/Plugin/OAuth2Plugin.php new file mode 100644 index 0000000..ed28bd9 --- /dev/null +++ b/src/DMS/Service/Meetup/Plugin/OAuth2Plugin.php @@ -0,0 +1,79 @@ +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); + } +} diff --git a/tests/DMS/Service/Meetup/MeetupOAuth2ClientTest.php b/tests/DMS/Service/Meetup/MeetupOAuth2ClientTest.php new file mode 100644 index 0000000..b681a01 --- /dev/null +++ b/tests/DMS/Service/Meetup/MeetupOAuth2ClientTest.php @@ -0,0 +1,28 @@ + '**', + ); + + $client = MeetupOAuth2Client::factory($config); + + $this->assertInstanceOf('DMS\Service\Meetup\MeetupOAuth2Client', $client); + } + + /** + * @expectedException Guzzle\Common\Exception\InvalidArgumentException + */ + public function testFactoryValidation() + { + $config = array(); + MeetupOAuth2Client::factory($config); + } +} diff --git a/tests/DMS/Service/Meetup/Plugin/OAuth2PluginTest.php b/tests/DMS/Service/Meetup/Plugin/OAuth2PluginTest.php new file mode 100644 index 0000000..df2c97d --- /dev/null +++ b/tests/DMS/Service/Meetup/Plugin/OAuth2PluginTest.php @@ -0,0 +1,54 @@ +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')); + } +}