Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
tgallice committed Mar 9, 2016
0 parents commit 8fb30ea
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source

script:
- ./vendor/bin/phpunit --coverage-text
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Thomas Gallice <tm.gallice@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Buffer Provider for OAuth 2.0 Client

This package provides Buffer OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).

[![Build Status](https://travis-ci.org/tgallice/oauth2-buffer.png?branch=master)](https://travis-ci.org/tgallice/oauth2-buffer)

## Installation

To install, use composer:

```
composer require tgallice/oauth2-buffer
```

## Usage

Usage is the same as The League's OAuth client, using `\Tgallice\OAuth2\Client\Provider\Buffer` as the provider.

### Authorization Code Flow

```php
$provider = new Tgallice\OAuth2\Client\Provider\Buffer([
'clientId' => '{buffer-client-id}',
'clientSecret' => '{buffer-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);

if (!isset($_GET['code'])) {

// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

unset($_SESSION['oauth2state']);
exit('Invalid state');

} else {

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);

// Optional: Now you have a token you can look up a users profile data
try {

// We got an access token, let's now get the user's details
$user = $provider->getResourceOwner($token);

// Use these details to create a new profile
printf('Hello %s!', $user->getName());

} catch (Exception $e) {

// Failed to get user details
exit('Oh dear...');
}

// Use this to interact with an API on the users behalf
echo $token->getToken();
}
```

## Testing

``` bash
$ ./vendor/bin/phpunit
```

## License

The MIT License (MIT). Please see [License File](https://github.com/tgallice/oauth2-buffer/blob/master/LICENSE) for more information.
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "tgallice/oauth2-buffer",
"description": "Buffer OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"authors": [
{
"name": "Thomas Gallice",
"email": "tm.gallice@gmail.com",
"homepage": "https://github.com/tgallice"
}
],
"keywords": [
"oauth",
"oauth2",
"client",
"authorization",
"authorisation",
"buffer"
],
"require": {
"php": ">=5.5.0",
"league/oauth2-client": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.5"
},
"autoload": {
"psr-4": {
"Tgallice\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tgallice\\OAuth2\\Client\\Test\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
106 changes: 106 additions & 0 deletions src/Provider/Buffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Tgallice\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use Psr\Http\Message\ResponseInterface;
use Tgallice\OAuth2\Client\Provider\Exception\BufferProviderException;

class Buffer extends AbstractProvider
{
/**
* Buffer app base url
*
* @const string
*/
const BASE_BUFFER_URL = 'https://bufferapp.com';

/**
* Buffer API base url
*
* @const string
*/
const BASE_BUFFER_API_URL = 'https://api.bufferapp.com';

/**
* Buffer API version
*
* @const string
*/
const BUFFER_API_VERSION = 1;

/**
* @inheritdoc
*/
public function getBaseAuthorizationUrl()
{
return static::BASE_BUFFER_URL . '/oauth2/authorize';
}

/**
* @inheritdoc
*/
public function getBaseAccessTokenUrl(array $params)
{
return $this->getApiUrl() . '/oauth2/token.json';
}

/**
* @inheritdoc
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
return $this->getApiUrl() . '/user.json?access_token=' . $token;
}

/**
* Get the Buffer API URL
*
* @return string
*/
public function getApiUrl()
{
return static::BASE_BUFFER_API_URL . '/' . static::BUFFER_API_VERSION;
}

/**
* @inheritdoc
*/
protected function getDefaultScopes()
{
return [];
}

/**
* @inheritdoc
*/
protected function getAuthorizationHeaders($token = null)
{
return [
'Authorization' => 'Bearer '. (string) $token,
];
}

/**
* @inheritdoc
*/
protected function checkResponse(ResponseInterface $response, $data)
{
if ($response->getStatusCode() >= 400) {
throw new BufferProviderException(
$data['error'],
isset($data['code']) ? (int) $data['code'] : $response->getStatusCode(),
$response
);
}
}

/**
* @inheritdoc
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new BufferUser($response);
}
}
59 changes: 59 additions & 0 deletions src/Provider/BufferUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tgallice\OAuth2\Client\Provider;

use League\OAuth2\Client\Provider\ResourceOwnerInterface;

class BufferUser implements ResourceOwnerInterface
{
/**
* @var array
*/
private $response;

/**
* @var array
*/
private $defaultFields = array(
'id' => null,
'name' => null,
);

/**
* @param array $response
*/
public function __construct(array $response = array())
{
$this->response = array_merge($this->defaultFields, $response);
}

/**
* Get the ID for the user
*
* @return string|null
*/
public function getId()
{
return $this->response['id'];
}

/**
* Get the name for the user
*
* @return string|null
*/
public function getName()
{
return $this->response['name'];
}

/**
* Return all the data for the user
*
* @return array
*/
public function toArray()
{
return $this->response;
}
}
9 changes: 9 additions & 0 deletions src/Provider/Exception/BufferProviderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tgallice\OAuth2\Client\Provider\Exception;

use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

class BufferProviderException extends IdentityProviderException
{
}
Loading

0 comments on commit 8fb30ea

Please sign in to comment.