oauth2-server/src/Grant/AbstractGrant.php

142 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/**
2014-01-08 16:15:29 +00:00
* OAuth 2.0 Abstract grant
*
2014-01-08 16:15:29 +00:00
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-09 19:34:23 +00:00
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2014-03-09 20:05:38 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\Grant;
use League\Event\Emitter;
use League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntity;
2014-04-25 11:24:48 +01:00
use League\OAuth2\Server\Exception;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
2014-01-08 16:15:29 +00:00
/**
* Abstract grant class
*/
abstract class AbstractGrant implements GrantTypeInterface
{
/**
* Grant identifier
2014-12-10 13:10:35 +00:00
*
2014-01-08 16:15:29 +00:00
* @var string
*/
protected $identifier = '';
/**
* Grant responds with
2014-12-10 13:10:35 +00:00
*
2014-01-08 16:15:29 +00:00
* @var string
*/
protected $respondsWith = 'token';
2014-01-08 16:15:29 +00:00
/**
* @var \Symfony\Component\HttpFoundation\Request
2014-01-08 16:15:29 +00:00
*/
protected $request;
2014-01-08 16:15:29 +00:00
/**
* @var ClientRepositoryInterface
2014-01-08 16:15:29 +00:00
*/
protected $clientRepository;
/**
* @var AccessTokenRepositoryInterface
*/
protected $accessTokenRepository;
/**
* @var \League\Event\Emitter
*/
protected $emitter;
/**
* @var ScopeRepositoryInterface
*/
protected $scopeRepository;
/**
* @param \League\Event\Emitter $emitter
* @param \League\OAuth2\Server\Repositories\ClientRepositoryInterface $clientRepository
* @param \League\OAuth2\Server\Repositories\ScopeRepositoryInterface $scopeRepository
* @param \League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface $accessTokenRepository
*/
public function __construct(
Emitter $emitter,
ClientRepositoryInterface $clientRepository,
ScopeRepositoryInterface $scopeRepository,
AccessTokenRepositoryInterface $accessTokenRepository
) {
$this->emitter = $emitter;
$this->clientRepository = $clientRepository;
$this->scopeRepository = $scopeRepository;
$this->accessTokenRepository = $accessTokenRepository;
}
/**
* {@inheritdoc}
*/
public function getIdentifier()
{
return $this->identifier;
}
/**
* {@inheritdoc}
*/
public function respondsWith()
{
return $this->respondsWith;
}
2014-01-08 16:15:29 +00:00
/**
* @param string $scopeParamValue A string containing a delimited set of scope identifiers
* @param string $scopeDelimiter The delimiter between the scopes in the value string
* @param ClientEntityInterface $client
* @param string $redirectUri
2014-12-10 13:10:35 +00:00
*
* @return \League\OAuth2\Server\Entities\ScopeEntity[]
* @throws \League\OAuth2\Server\Exception\InvalidScopeException
2014-01-08 16:15:29 +00:00
*/
public function validateScopes(
$scopeParamValue,
$scopeDelimiter,
ClientEntityInterface $client,
$redirectUri = null
) {
$scopesList = explode($scopeDelimiter, trim($scopeParamValue));
2013-12-24 17:01:56 +00:00
for ($i = 0; $i < count($scopesList); $i++) {
$scopesList[$i] = trim($scopesList[$i]);
2014-05-03 11:08:33 +01:00
if ($scopesList[$i] === '') {
unset($scopesList[$i]); // Remove any junk scopes
}
2013-12-24 17:01:56 +00:00
}
$scopes = [];
foreach ($scopesList as $scopeItem) {
$scope = $this->scopeRepository->get(
2013-12-24 17:01:56 +00:00
$scopeItem,
2014-09-30 23:55:21 +01:00
$this->getIdentifier(),
$client->getIdentifier()
2013-12-24 17:01:56 +00:00
);
2014-05-02 17:21:53 +01:00
if (($scope instanceof ScopeEntity) === false) {
throw new Exception\InvalidScopeException($scopeItem, $redirectUri);
2013-12-24 17:01:56 +00:00
}
$scopes[] = $scope;
2014-01-10 12:30:13 +00:00
}
2014-05-03 10:53:43 +01:00
2014-01-10 12:30:13 +00:00
return $scopes;
}
}