Skip to content

Commit

Permalink
Client credentials provider. Resource owner not implemented (#1)
Browse files Browse the repository at this point in the history
Client credentials provider. Resource owner not implemented
  • Loading branch information
rccrdpccl authored and joskfg committed Aug 16, 2017
1 parent e9f8012 commit 2f868af
Show file tree
Hide file tree
Showing 13 changed files with 551 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
composer.lock
.php_cs.cache
37 changes: 37 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
;
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'new_with_braces' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_phpdoc' => true,
'no_empty_comment' => true,
'no_leading_import_slash' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_unused_imports' => true,
'ordered_imports' => ['importsOrder' => null, 'sortAlgorithm' => 'alpha'],
'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
'phpdoc_align' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_to_comment' => true,
'psr0' => false,
'psr4' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_blank_line_before_namespace' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'whitespace_after_comma_in_array' => true,
])
->setFinder($finder)
;
17 changes: 17 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
filter:
paths: [src/*]
excluded_paths: [tests/*]
checks:
php:
code_rating: true
tools:
external_code_coverage:
timeout: 600
runs: 2
php_code_coverage: false
php_loc:
enabled: true
excluded_dirs: [tests, vendor]
php_cpd:
enabled: true
excluded_dirs: [tests, vendor]
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
language: php

sudo: false

matrix:
include:
- php: 7.0
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=false
- php: 7.1
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=true
- php: master
env: COLLECT_COVERAGE=true VALIDATE_CODING_STYLE=false
allow_failures:
- php: master
fast_finish: true

cache:
directories:
- $HOME/.composer/cache

before_install:
- travis_retry composer self-update

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

script:
- composer phpunit

after_script:
- if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi
- if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Softonic OAuth2 Provider
=====

[![Latest Version](https://img.shields.io/github/release/softonic/oauth2-provider.svg?style=flat-square)](https://github.com/softonic/oauth2-provider/releases)
[![Software License](https://img.shields.io/badge/license-Apache%202.0-blue.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/softonic/oauth2-provider/master.svg?style=flat-square)](https://travis-ci.org/softonic/oauth2-provider)
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/softonic/oauth2-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/softonic/oauth2-provider/code-structure)
[![Quality Score](https://img.shields.io/scrutinizer/g/softonic/oauth2-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/softonic/oauth2-provider)
[![Total Downloads](https://img.shields.io/packagist/dt/softonic/oauth2-provider.svg?style=flat-square)](https://packagist.org/packages/softonic/oauth2-provider)

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

Installation
-------

To install, use composer:

```
composer require softonic/oauth2-provider
```

Usage
-------

``` php
<?php

$options = [
'clientId' => 'myClient',
'clientSecret' => 'mySecret'
];

$client = new Softonic\OAuth2\Client\Provider\Softonic($options);

$token = $client->getAccessToken('client_credentials', ['scope' => 'myscope']);
```


Testing
-------

`softonic/oauth2-provider` has a [PHPUnit](https://phpunit.de) test suite and a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/).

To run the tests, run the following command from the project folder.

``` bash
$ docker-compose run test
```

To run interactively using [PsySH](http://psysh.org/):
``` bash
$ docker-compose run interactive
```

License
-------

The Apache 2.0 license. Please see [LICENSE](LICENSE) for more information.

[PSR-2]: http://www.php-fig.org/psr/psr-2/
[PSR-4]: http://www.php-fig.org/psr/psr-4/
2 changes: 2 additions & 0 deletions build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "softonic/oauth2-provider",
"type": "library",
"description" : "Softonic OAuth2 provider",
"keywords": ["softonic", "oauth2", "provider"],
"license": "Apache-2.0",
"homepage": "https://github.com/softonic/oauth2-provider",
"support": {
"issues": "https://github.com/softonic/oauth2-provider/issues"
},
"require": {
"php": ">=7.0",
"league/oauth2-client": "^2.2",
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"friendsofphp/php-cs-fixer": "^2.4"
},
"autoload": {
"psr-4": {
"Softonic\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Softonic\\OAuth2\\Client\\Test\\": "tests/"
}
},
"scripts": {
"test": "phpunit --coverage-text; php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
"phpunit": "phpunit --coverage-text",
"phpcs": "php-cs-fixer fix -v --diff --dry-run --allow-risky=yes;",
"fix-cs": "php-cs-fixer fix -v --diff --allow-risky=yes;"
}
}
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.2'

services:
test:
volumes:
- ./:/srv
image: ypereirareis/prestissimo:latest
command: composer run test

interactive:
working_dir: /srv
volumes:
- ./:/srv
entrypoint: /psysh/psysh
image: habitissimo/psysh:latest
33 changes: 33 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuites>
<testsuite name="Softonic OAuth2 Provider">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<logging>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/clover.xml"/>
</logging>

</phpunit>
130 changes: 130 additions & 0 deletions src/Provider/Softonic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Softonic\OAuth2\Client\Provider;

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

class Softonic extends AbstractProvider
{
const OAUTH_HOSTNAME = 'oauth-v2.softonic.com';

/**
* Returns the base URL for authorizing a client.
*
* Eg. https://oauth.service.com/authorize
*
* @return string
*/
public function getBaseAuthorizationUrl()
{
return 'https://' . static::OAUTH_HOSTNAME . '/authorize';
}

/**
* Returns the base URL for requesting an access token.
*
* Eg. https://oauth.service.com/token
*
* @param array $params
*
* @return string
*/
public function getBaseAccessTokenUrl(array $params)
{
return 'https://' . static::OAUTH_HOSTNAME . '/token';
}

/**
* Returns the URL for requesting the resource owner's details.
*
* @param AccessToken $token
*
* @return string
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
throw new \BadMethodCallException('Method not implemented.');
}

/**
* Returns the default scopes used by this provider.
*
* This should only be the scopes that are required to request the details
* of the resource owner, rather than all the available scopes.
*
* @return array
*/
protected function getDefaultScopes()
{
return [];
}

/**
* Checks a provider response for errors.
*
* @param ResponseInterface $response
* @param array|string $data Parsed response data
*
* @throws IdentityProviderException If parsed response data contains an error.
*/
protected function checkResponse(ResponseInterface $response, $data)
{
$message = $this->getErrorMessage($data);

if (!empty($message)) {
throw new IdentityProviderException(
$message,
$response->getStatusCode(),
$response
);
}
}

/**
* Returns error message if any, otherwise null.
*
* @param array $parsedResponse
*
* @return string|null
*/
private function getErrorMessage(array $parsedResponse)
{
if ($this->responseHasError($parsedResponse)) {
return !empty($parsedResponse['exception']) ?
$parsedResponse['exception']
: $parsedResponse['error_description'];
}

return null;
}

/**
* Returns true if response has any error.
*
* @param array $parsedResponse
*
* @return bool
*/
private function responseHasError(array $parsedResponse)
{
return !empty($parsedResponse['exception']) ||
!empty($parsedResponse['error_description']);
}

/**
* Generates a resource owner object from a successful resource owner
* details request.
*
* @param array $response
* @param AccessToken $token
*
* @return ResourceOwnerInterface
*/
protected function createResourceOwner(array $response, AccessToken $token)
{
return new SoftonicResourceOwner($response);
}
}
Loading

0 comments on commit 2f868af

Please sign in to comment.