Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/GrantType/AbstractGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace AuthBucket\OAuth2\GrantType;

use AuthBucket\OAuth2\Exception\InvalidGrantException;
use AuthBucket\OAuth2\Exception\InvalidRequestException;
use AuthBucket\OAuth2\Exception\InvalidScopeException;
use AuthBucket\OAuth2\Exception\ServerErrorException;
use AuthBucket\OAuth2\Model\AuthorizeInterface;
use AuthBucket\OAuth2\Model\ModelManagerFactoryInterface;
use AuthBucket\OAuth2\Symfony\Component\Security\Core\Authentication\Token\ClientCredentialsToken;
use AuthBucket\OAuth2\TokenType\TokenTypeHandlerFactoryInterface;
Expand All @@ -30,6 +32,8 @@
*/
abstract class AbstractGrantTypeHandler implements GrantTypeHandlerInterface
{
const GRANT_TYPE = null;

protected $tokenStorage;
protected $encoderFactory;
protected $validator;
Expand Down Expand Up @@ -122,19 +126,27 @@ protected function checkScope(

// Compare if given scope within all authorized scopes.
$scopeAuthorized = [];
$grantTypeAuthorized = [];
$authorizeManager = $this->modelManagerFactory->getModelManager('authorize');
/** @var AuthorizeInterface $result */
$result = $authorizeManager->readModelOneBy([
'clientId' => $clientId,
'username' => $username,
]);
if ($result !== null) {
$scopeAuthorized = $result->getScope();
$grantTypeAuthorized = $result->getGrantType();
}
if (array_intersect($scope, $scopeAuthorized) !== $scope) {
throw new InvalidScopeException([
'error_description' => 'The requested scope exceeds the scope granted by the resource owner.',
]);
}
if (!in_array(static::GRANT_TYPE, $grantTypeAuthorized)) {
throw new InvalidGrantException([
'error_description' => 'The requested grant is invalid.',
]);
}

return $scope;
}
Expand Down
2 changes: 2 additions & 0 deletions src/GrantType/AuthorizationCodeGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class AuthorizationCodeGrantTypeHandler extends AbstractGrantTypeHandler
{
const GRANT_TYPE = 'authorization_code';

public function handle(Request $request)
{
// Fetch client_id from authenticated token.
Expand Down
2 changes: 2 additions & 0 deletions src/GrantType/ClientCredentialsGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class ClientCredentialsGrantTypeHandler extends AbstractGrantTypeHandler
{
const GRANT_TYPE = 'client_credentials';

public function handle(Request $request)
{
// Fetch client_id from authenticated token.
Expand Down
2 changes: 2 additions & 0 deletions src/GrantType/PasswordGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
class PasswordGrantTypeHandler extends AbstractGrantTypeHandler
{
const GRANT_TYPE = 'password';

public function handle(Request $request)
{
// Fetch client_id from authenticated token.
Expand Down
2 changes: 2 additions & 0 deletions src/GrantType/RefreshTokenGrantTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
class RefreshTokenGrantTypeHandler extends AbstractGrantTypeHandler
{
const GRANT_TYPE = 'refresh_token';

public function handle(Request $request)
{
// Fetch client_id from authenticated token.
Expand Down
7 changes: 7 additions & 0 deletions src/Model/AuthorizeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,11 @@ public function setScope($scope);
* @return array
*/
public function getScope();

/**
* Get Grant types that this client is allowed to use.
*
* @return array
*/
public function getGrantType();
}
11 changes: 11 additions & 0 deletions src/ResponseType/AbstractResponseTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace AuthBucket\OAuth2\ResponseType;

use AuthBucket\OAuth2\Exception\InvalidGrantException;
use AuthBucket\OAuth2\Exception\InvalidRequestException;
use AuthBucket\OAuth2\Exception\InvalidScopeException;
use AuthBucket\OAuth2\Exception\ServerErrorException;
use AuthBucket\OAuth2\Exception\UnauthorizedClientException;
use AuthBucket\OAuth2\Model\AuthorizeInterface;
use AuthBucket\OAuth2\Model\ModelManagerFactoryInterface;
use AuthBucket\OAuth2\TokenType\TokenTypeHandlerFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -30,6 +32,7 @@
*/
abstract class AbstractResponseTypeHandler implements ResponseTypeHandlerInterface
{
const GRANT_TYPE = null;
protected $tokenStorage;
protected $validator;
protected $modelManagerFactory;
Expand Down Expand Up @@ -230,6 +233,7 @@ protected function checkScope(
// Compare if given scope within all authorized scopes.
$scopeAuthorized = [];
$authorizeManager = $this->modelManagerFactory->getModelManager('authorize');
/** @var AuthorizeInterface $result */
$result = $authorizeManager->readModelOneBy([
'clientId' => $clientId,
'username' => $username,
Expand All @@ -245,6 +249,13 @@ protected function checkScope(
]);
}

if (!in_array(static::GRANT_TYPE, $result->getGrantType())) {
throw new InvalidGrantException([
'state' => $state,
'error_description' => 'The requested grant is invalid.',
]);
}

return $scope;
}
}
3 changes: 3 additions & 0 deletions src/ResponseType/CodeResponseTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace AuthBucket\OAuth2\ResponseType;

use AuthBucket\OAuth2\GrantType\AuthorizationCodeGrantTypeHandler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -21,6 +22,8 @@
*/
class CodeResponseTypeHandler extends AbstractResponseTypeHandler
{
const GRANT_TYPE = AuthorizationCodeGrantTypeHandler::GRANT_TYPE;

public function handle(Request $request)
{
// Fetch username from authenticated token.
Expand Down
2 changes: 2 additions & 0 deletions src/ResponseType/TokenResponseTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
class TokenResponseTypeHandler extends AbstractResponseTypeHandler
{
const GRANT_TYPE = 'implicit';

public function handle(Request $request)
{
// Fetch username from authenticated token.
Expand Down
16 changes: 16 additions & 0 deletions tests/TestBundle/DataFixtures/ORM/AuthorizeFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function load(ObjectManager $manager)
->setUsername('demousername1')
->setScope([
'demoscope1',
])
->setGrantType([
'authorization_code',
'password',
'implicit',
]);
$manager->persist($model);

Expand All @@ -86,6 +91,9 @@ public function load(ObjectManager $manager)
->setScope([
'demoscope1',
'demoscope2',
])
->setGrantType([
'authorization_code',
]);
$manager->persist($model);

Expand All @@ -96,6 +104,11 @@ public function load(ObjectManager $manager)
'demoscope1',
'demoscope2',
'demoscope3',
])
->setGrantType([
'authorization_code',
'password',
'implicit',
]);
$manager->persist($model);

Expand All @@ -106,6 +119,9 @@ public function load(ObjectManager $manager)
'demoscope1',
'demoscope2',
'demoscope3',
])
->setGrantType([
'client_credentials',
]);
$manager->persist($model);

Expand Down
29 changes: 29 additions & 0 deletions tests/TestBundle/Entity/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Authorize implements AuthorizeInterface
*/
protected $scope;

/**
* @var array
*
* @ORM\Column(name="grant_type", type="array")
*/
protected $grantType;

/**
* Get id.
*
Expand Down Expand Up @@ -133,4 +140,26 @@ public function getScope()
{
return $this->scope;
}

/**
* Set grant type.
*
* @param array $grantType
*
* @return Authorize
*/
public function setGrantType($grantType)
{
$this->grantType = $grantType;
}

/**
* Get grant type.
*
* @return array
*/
public function getGrantType()
{
return $this->grantType;
}
}