Skip to content

Commit

Permalink
Merge a6cd3d0 into 27a0feb
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed May 18, 2018
2 parents 27a0feb + a6cd3d0 commit b363b4a
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 147 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/vendor/
composer.lock
phpcs.xml
15 changes: 6 additions & 9 deletions .scrutinizer.yml
Expand Up @@ -4,15 +4,12 @@ filter:
- 'tests/*'
before_commands:
- 'composer install --prefer-source'
checks:
php:
custom_coding_standard:
git_repository: 'https://github.com/chadicus/coding-standard'
git_version: '971fac1625463a72df0034fbfdd263561f2ccb4f'
ruleset_path: 'Chadicus/ruleset.xml'
tools:
php_analyzer: true
php_mess_detector: true
php_code_sniffer:
config:
standard: PSR2
sensiolabs_security_checker: true
php_loc:
excluded_dirs:
Expand All @@ -21,7 +18,7 @@ tools:
php_pdepend: true
php_sim: true
build_failure_conditions:
- 'elements.rating(< B).new.exists'
- 'issues.label("coding-style").new.exists'
- 'issues.severity(>= MAJOR).new.exists'
- 'elements.rating(< B).new.exists'
- 'issues.label("coding-style").new.exists'
- 'issues.severity(>= MAJOR).new.exists'
- 'project.metric("scrutinizer.quality", < 6)'
5 changes: 1 addition & 4 deletions .travis.yml
@@ -1,10 +1,8 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm
- nightly
env:
- PREFER_LOWEST="--prefer-lowest --prefer-stable"
Expand All @@ -13,10 +11,9 @@ matrix:
fast_finish: true
allow_failures:
- php: 7.2
- php: hhvm
- php: nightly
before_script:
- composer update $PREFER_LOWEST
script:
- ./vendor/bin/phpunit --coverage-clover clover.xml
after_success: sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then ./vendor/bin/coveralls -v; fi'
after_success: ./vendor/bin/coveralls -v
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2017 Chad Gray
Copyright (c) 2015 Chad Gray

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 4 additions & 6 deletions composer.json
Expand Up @@ -14,18 +14,16 @@
"sort-packages": true
},
"require": {
"php": "^5.6 || ^7.0",
"php": "^7.0",
"bshaffer/oauth2-server-php": "^1.9",
"chadicus/slim-oauth2-http": "^3.1",
"chadicus/psr-middleware": "^1.0",
"psr/http-message": "^1.0",
"container-interop/container-interop": "^1.1"
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"chadicus/coding-standard": "^1.3",
"php-coveralls/php-coveralls": "^1.0",
"php-di/php-di": "^5.4",
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.2",
"zendframework/zend-diactoros": ">=1.3.2"
},
"autoload": {
Expand Down
File renamed without changes.
95 changes: 46 additions & 49 deletions src/Authorization.php
@@ -1,13 +1,12 @@
<?php
namespace Chadicus\Slim\OAuth2\Middleware;

use ArrayAccess;
use Chadicus\Slim\OAuth2\Http\RequestBridge;
use Chadicus\Slim\OAuth2\Http\ResponseBridge;
use Chadicus\Psr\Middleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Interop\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use OAuth2;

/**
Expand All @@ -29,43 +28,24 @@ class Authorization implements MiddlewareInterface
*/
private $scopes;

/**
* Container for token.
*
* @var ArrayAccess|ContainerInterface
*/
private $container;

/**
* Create a new instance of the Authroization middleware.
*
* @param OAuth2\Server $server The configured OAuth2 server.
* @param ArrayAccess|ContainerInterface $container A container object in which to store the token from the
* request.
* @param array $scopes Scopes required for authorization. $scopes can be given as an
* array of arrays. OR logic will use with each grouping.
* Example:
* Given ['superUser', ['basicUser', 'aPermission']], the request
* will be verified if the request token has 'superUser' scope
* OR 'basicUser' and 'aPermission' as its scope.
*
* @throws \InvalidArgumentException Thrown if $container is not an instance of ArrayAccess or ContainerInterface.
* @param OAuth2\Server $server The configured OAuth2 server.
* @param array $scopes Scopes required for authorization. $scopes can be given as an array of arrays.
* OR logic will use with each grouping.
* Example: Given ['superUser', ['basicUser', 'aPermission']], the request will be
* verified if the request token has 'superUser' scope OR 'basicUser' and
* 'aPermission' as its scope.
*/
public function __construct(OAuth2\Server $server, $container, array $scopes = [])
public function __construct(OAuth2\Server $server, array $scopes = [])
{
$this->server = $server;
if (!is_a($container, '\\ArrayAccess') && !is_a($container, '\\Interop\\Container\\ContainerInterface')) {
throw new \InvalidArgumentException(
'$container does not implement \\ArrayAccess or \\Interop\\Container\\ContainerInterface'
);
}

$this->container = $container;
$this->scopes = $this->formatScopes($scopes);
}

/**
* Execute this middleware.
* Execute this middleware as a function.
*
* @param ServerRequestInterface $request The PSR7 request.
* @param ResponseInterface $response The PSR7 response.
Expand All @@ -74,12 +54,46 @@ public function __construct(OAuth2\Server $server, $container, array $scopes = [
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$handler = new class implements RequestHandlerInterface
{
public $next;
public $response;

/**
* Handle the request and return a response.
*
* @param ServerRequestInterface $request The request to handle.
*
* @return ResponseInterface
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
return call_user_func_array($this->next, [$request, $this->response]);
}
};

$handler->next = $next;
$handler->response = $response;

return $this->process($request, $handler);
}

/**
* Execute this middleware.
*
* @param ServerRequestInterface $request The PSR-7 request.
* @param RequestHandlerInterface $handler The PSR-15 request handler.
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$oauth2Request = RequestBridge::toOAuth2($request);
foreach ($this->scopes as $scope) {
if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) {
$this->setToken($this->server->getResourceController()->getToken());
return $next($request, $response);
$token = $this->server->getResourceController()->getToken();
return $handler->handle($request->withAttribute('oauth2-token', $token));
}
}

Expand All @@ -92,23 +106,6 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $response->withHeader('Content-Type', 'application/json');
}

/**
* Helper method to set the token value in the container instance.
*
* @param array $token The token from the incoming request.
*
* @return void
*/
private function setToken(array $token)
{
if (is_a($this->container, '\\ArrayAccess')) {
$this->container['token'] = $token;
return;
}

$this->container->set('token', $token);
}

/**
* Returns a callable function to be used as a authorization middleware with a specified scope.
*
Expand Down

0 comments on commit b363b4a

Please sign in to comment.