Skip to content

Commit

Permalink
Merge pull request #58 from nitinjavakid/master
Browse files Browse the repository at this point in the history
Changes to implement OAuth2 based authentication
  • Loading branch information
rdohms authored Sep 1, 2019
2 parents 6abb76a + 20ebb68 commit e6344cf
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 12 deletions.
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions src/DMS/Service/Meetup/MeetupOAuth2Client.php
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;
}
}
79 changes: 79 additions & 0 deletions src/DMS/Service/Meetup/Plugin/OAuth2Plugin.php
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);
}
}
28 changes: 28 additions & 0 deletions tests/DMS/Service/Meetup/MeetupOAuth2ClientTest.php
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);
}
}
54 changes: 54 additions & 0 deletions tests/DMS/Service/Meetup/Plugin/OAuth2PluginTest.php
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'));
}
}

0 comments on commit e6344cf

Please sign in to comment.