Skip to content

Commit

Permalink
Reduce restrictions for container
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed May 21, 2018
1 parent fdb2ba1 commit 744af8f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -19,7 +19,6 @@
"chadicus/psr-middleware": "^1.0",
"chadicus/slim-oauth2-http": "^3.1",
"container-interop/container-interop": "^1.1",
"psr/container": "^1.0",
"psr/http-message": "^1.0"
},
"require-dev": {
Expand Down
36 changes: 11 additions & 25 deletions src/Authorization.php
Expand Up @@ -5,10 +5,8 @@
use Chadicus\Slim\OAuth2\Http\RequestBridge;
use Chadicus\Slim\OAuth2\Http\ResponseBridge;
use Chadicus\Psr\Middleware\MiddlewareInterface;
use Interop\Container\ContainerInterface as InteropContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Container\ContainerInterface;
use OAuth2;

/**
Expand All @@ -33,22 +31,21 @@ class Authorization implements MiddlewareInterface
/**
* Container for token.
*
* @var ArrayAccess|ContainerInterface
* @var mixed
*/
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.
* @param OAuth2\Server $server The configured OAuth2 server.
* @param mixed $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.
*/
Expand Down Expand Up @@ -149,21 +146,10 @@ private function validateContainer($container)
return $container;
}

if (is_a($container, ContainerInterface::class)) {
if (method_exists($container, 'set')) {
return $container;
}

if (is_a($container, InteropContainerInterface::class)) {
return $container;
}

throw new \InvalidArgumentException(
sprintf(
'$container does not implement %s, %s, or %s',
ArrayAccess::class,
ContainerInterface::class,
InteropContainerInterface::class
)
);
throw new \InvalidArgumentException("\$container does not implement 'ArrayAccess' or contain a 'set' method");
}
}
19 changes: 18 additions & 1 deletion tests/AuthorizationTest.php
Expand Up @@ -466,7 +466,7 @@ public function invokeRetainsContentType()
* @test
* @covers ::__construct
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $container does not implement ArrayAccess, Psr\Container\ContainerInterface
* @expectedExceptionMessage $container does not implement 'ArrayAccess' or contain a 'set' method
*
* @return void
*/
Expand All @@ -476,6 +476,23 @@ public function constructWithInvalidContainer()
new Authorization($oauth2ServerMock, new \StdClass());
}

/**
* Verify middleware cannot be constructed with a pure PSR-11 container.
*
* @test
* @covers ::__construct
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $container does not implement 'ArrayAccess' or contain a 'set' method
*
* @return void
*/
public function constructWithPSR11Container()
{
$container = $this->getMockBuilder('\\Psr\\Container\\ContainerInterface')->getMock();
$oauth2ServerMock = $this->getMockBuilder('\\OAuth2\\Server')->disableOriginalConstructor()->getMock();
new Authorization($oauth2ServerMock, $container);
}

/**
* Verify middleware can use interop container.
*
Expand Down

0 comments on commit 744af8f

Please sign in to comment.