Skip to content

Commit

Permalink
Subtree split / initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kamermans committed May 24, 2017
0 parents commit f4ee4a7
Show file tree
Hide file tree
Showing 57 changed files with 3,228 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
vendor/
*.swp
.php-cs.cache
guzzle_environments/*/vendor/
guzzle_environments/*/composer.lock
composer.lock
examples/
18 changes: 18 additions & 0 deletions .php_cs
@@ -0,0 +1,18 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude("vendor")
->exclude("guzzle_environments")
;

return PhpCsFixer\Config::create()
->setRules(array(
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
'blank_line_after_opening_tag' => true,
'blank_line_after_namespace' => true,
))
->setFinder($finder)
;
23 changes: 23 additions & 0 deletions README.md
@@ -0,0 +1,23 @@
=======================
Guzzle OAuth 2.0 Subscriber
=======================

This is an OAuth 2.0 client for Guzzle which aims to be 100% compatible with Guzzle 4, 5, 6 and all future versions within a single package.
Although I love Guzzle, its interfaces keep changing, causing massive breaking changes every 12 months or so, so I have created this package
to help reduce the dependency hell that most third-party Guzzle dependencies bring with them. I wrote the official Guzzle OAuth 2.0 plugin
which is still on the `oauth2` branch, [over at the official Guzzle repo](https://github.com/guzzle/oauth-subscriber/tree/oauth2), but I
see that they have dropped support for Guzzle < v6 on `master`, which prompted me to split this back off to a separate package.

Installing
==========

This project can be installed using Composer. Add the following to your
composer.json:

```javascript
{
"require": {
"kamermans/guzzle-oauth2-subscriber": "~1.0"
}
}
```
30 changes: 30 additions & 0 deletions composer.json
@@ -0,0 +1,30 @@
{
"name": "kamermans/guzzle-oauth-subscriber",
"description": "OAuth 2.0 client for Guzzle 4, 5 and 6+",
"keywords": ["oauth", "guzzle"],
"license": "MIT",
"authors": [
{
"name": "Steve Kamerman",
"email": "stevekamerman@gmail.com"
}
],
"require": {
"php": ">=5.4.0",
},
"autoload": {
"psr-4": {
"kamermans\\OAuth2\\": "src/OAuth2"
}
},
"autoload-dev": {
"psr-4": {
"kamermans\\OAuth2\\Tests\\": "tests/OAuth2"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
}
}
}
24 changes: 24 additions & 0 deletions guzzle_environments/4/composer.json
@@ -0,0 +1,24 @@
{
"name": "kamermans/unit-tests",
"authors": [
{
"name": "Steve Kamerman",
"email": "stevekamerman@gmail.com"
}
],
"require-dev": {
"guzzlehttp/guzzle": "~4",
"phpunit/phpunit": "~4.0",
"doctrine/cache": "~1.5"
},
"autoload": {
"psr-4": {
"kamermans\\OAuth2\\": "../../src"
}
},
"autoload-dev": {
"psr-4": {
"kamermans\\OAuth2\\Tests\\": "../../tests"
}
}
}
14 changes: 14 additions & 0 deletions guzzle_environments/4/phpunit.xml.dist
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="All Tests">
<directory>../../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix="Test.php">.</directory>
</whitelist>
</filter>
</phpunit>
24 changes: 24 additions & 0 deletions guzzle_environments/5/composer.json
@@ -0,0 +1,24 @@
{
"name": "kamermans/unit-tests",
"authors": [
{
"name": "Steve Kamerman",
"email": "stevekamerman@gmail.com"
}
],
"require-dev": {
"guzzlehttp/guzzle": "~5",
"phpunit/phpunit": "~4.0",
"doctrine/cache": "~1.5"
},
"autoload": {
"psr-4": {
"kamermans\\OAuth2\\": "../../src"
}
},
"autoload-dev": {
"psr-4": {
"kamermans\\OAuth2\\Tests\\": "../../tests"
}
}
}
14 changes: 14 additions & 0 deletions guzzle_environments/5/phpunit.xml.dist
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="All Tests">
<directory>../../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix="Test.php">.</directory>
</whitelist>
</filter>
</phpunit>
24 changes: 24 additions & 0 deletions guzzle_environments/6/composer.json
@@ -0,0 +1,24 @@
{
"name": "kamermans/unit-tests",
"authors": [
{
"name": "Steve Kamerman",
"email": "stevekamerman@gmail.com"
}
],
"require-dev": {
"guzzlehttp/guzzle": "~6",
"phpunit/phpunit": "~4.0",
"doctrine/cache": "~1.5"
},
"autoload": {
"psr-4": {
"kamermans\\OAuth2\\": "../../src"
}
},
"autoload-dev": {
"psr-4": {
"kamermans\\OAuth2\\Tests\\": "../../tests"
}
}
}
14 changes: 14 additions & 0 deletions guzzle_environments/6/phpunit.xml.dist
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="All Tests">
<directory>../../tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix="Test.php">.</directory>
</whitelist>
</filter>
</phpunit>
7 changes: 7 additions & 0 deletions src/Exception/AccessTokenRequestException.php
@@ -0,0 +1,7 @@
<?php

namespace kamermans\OAuth2\Exception;

class AccessTokenRequestException extends ReauthorizationRequestException
{
}
7 changes: 7 additions & 0 deletions src/Exception/OAuth2Exception.php
@@ -0,0 +1,7 @@
<?php

namespace kamermans\OAuth2\Exception;

class OAuth2Exception extends \RuntimeException
{
}
7 changes: 7 additions & 0 deletions src/Exception/ReauthorizationException.php
@@ -0,0 +1,7 @@
<?php

namespace kamermans\OAuth2\Exception;

class ReauthorizationException extends OAuth2Exception
{
}
23 changes: 23 additions & 0 deletions src/Exception/ReauthorizationRequestException.php
@@ -0,0 +1,23 @@
<?php

namespace kamermans\OAuth2\Exception;

use GuzzleHttp\Exception\TransferException;

class ReauthorizationRequestException extends ReauthorizationException
{
public function __construct($message, TransferException $guzzleException)
{
parent::__construct($message, 0, $guzzleException);
}

/**
* Get the Guzzle Exception that was thrown while trying to reauthorize.
*
* @return GuzzleHttp\Exception\TransferException
*/
public function getGuzzleException()
{
return $this->getPrevious();
}
}
93 changes: 93 additions & 0 deletions src/GrantType/AuthorizationCode.php
@@ -0,0 +1,93 @@
<?php

namespace kamermans\OAuth2\GrantType;

use GuzzleHttp\Post\PostBody;
use GuzzleHttp\ClientInterface;
use kamermans\OAuth2\Utils\Helper;
use kamermans\OAuth2\Utils\Collection;
use kamermans\OAuth2\Signer\ClientCredentials\SignerInterface;

/**
* Authorization code grant type.
*
* @link http://tools.ietf.org/html/rfc6749#section-4.1
*/
class AuthorizationCode implements GrantTypeInterface
{
/**
* The token endpoint client.
*
* @var ClientInterface
*/
private $client;

/**
* Configuration settings.
*
* @var Collection
*/
private $config;

public function __construct(ClientInterface $client, array $config)
{
$this->client = $client;
$this->config = Collection::fromConfig($config,
// Defaults
[
'client_secret' => '',
'scope' => '',
'redirect_uri' => '',
],
// Required
[
'client_id',
'code',
]
);
}

public function getRawData(SignerInterface $clientCredentialsSigner, $refreshToken = null)
{
if (Helper::guzzleIs('>=', 6)) {
$request = new \GuzzleHttp\Psr7\Request('POST', null, [
'body' => $this->getPostBody(),
]);
} else {
$request = $this->client->createRequest('POST', null);
$request->setBody($this->getPostBody());
}

$request = $clientCredentialsSigner->sign(
$request,
$this->config['client_id'],
$this->config['client_secret']
);

$response = $this->client->send($request);

return $response->json();
}

/**
* @return PostBody
*/
protected function getPostBody()
{
$postBody = new PostBody();
$postBody->replaceFields([
'grant_type' => 'authorization_code',
'code' => $this->config['code'],
]);

if ($this->config['scope']) {
$postBody->setField('scope', $this->config['scope']);
}

if ($this->config['redirect_uri']) {
$postBody->setField('redirect_uri', $this->config['redirect_uri']);
}

return $postBody;
}
}

0 comments on commit f4ee4a7

Please sign in to comment.