2016-04-17 17:03:29 +05:30
|
|
|
<?php
|
2016-04-17 17:36:05 +05:30
|
|
|
/**
|
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
|
|
* @license http://mit-license.org/
|
|
|
|
*
|
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
|
|
|
*/
|
2016-07-09 04:30:44 +05:30
|
|
|
|
2016-04-17 17:03:29 +05:30
|
|
|
namespace League\OAuth2\Server;
|
|
|
|
|
|
|
|
use League\OAuth2\Server\AuthorizationValidators\AuthorizationValidatorInterface;
|
|
|
|
use League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator;
|
2016-07-09 04:30:44 +05:30
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
2016-04-17 17:03:29 +05:30
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
|
|
class ResourceServer
|
|
|
|
{
|
|
|
|
/**
|
2016-07-09 04:30:44 +05:30
|
|
|
* @var AccessTokenRepositoryInterface
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
private $accessTokenRepository;
|
2016-07-09 04:30:44 +05:30
|
|
|
|
2016-04-17 17:03:29 +05:30
|
|
|
/**
|
2016-07-09 04:30:44 +05:30
|
|
|
* @var CryptKey
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
private $publicKey;
|
2016-07-09 04:30:44 +05:30
|
|
|
|
2016-04-17 17:03:29 +05:30
|
|
|
/**
|
2016-07-09 04:30:44 +05:30
|
|
|
* @var null|AuthorizationValidatorInterface
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
private $authorizationValidator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New server instance.
|
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @param AccessTokenRepositoryInterface $accessTokenRepository
|
|
|
|
* @param CryptKey|string $publicKey
|
|
|
|
* @param null|AuthorizationValidatorInterface $authorizationValidator
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AccessTokenRepositoryInterface $accessTokenRepository,
|
|
|
|
$publicKey,
|
|
|
|
AuthorizationValidatorInterface $authorizationValidator = null
|
|
|
|
) {
|
|
|
|
$this->accessTokenRepository = $accessTokenRepository;
|
|
|
|
|
2016-07-09 15:39:21 +05:30
|
|
|
if ($publicKey instanceof CryptKey === false) {
|
2016-04-17 17:03:29 +05:30
|
|
|
$publicKey = new CryptKey($publicKey);
|
|
|
|
}
|
|
|
|
$this->publicKey = $publicKey;
|
|
|
|
|
|
|
|
$this->authorizationValidator = $authorizationValidator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-09 04:30:44 +05:30
|
|
|
* @return AuthorizationValidatorInterface
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
protected function getAuthorizationValidator()
|
|
|
|
{
|
2016-07-09 15:39:21 +05:30
|
|
|
if ($this->authorizationValidator instanceof AuthorizationValidatorInterface === false) {
|
2016-04-17 17:03:29 +05:30
|
|
|
$this->authorizationValidator = new BearerTokenValidator($this->accessTokenRepository);
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:21:47 +05:30
|
|
|
if ($this->authorizationValidator instanceof BearerTokenValidator === true) {
|
|
|
|
$this->authorizationValidator->setPublicKey($this->publicKey);
|
|
|
|
}
|
2016-04-17 17:03:29 +05:30
|
|
|
|
|
|
|
return $this->authorizationValidator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the access token validity.
|
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @param ServerRequestInterface $request
|
2016-04-17 17:03:29 +05:30
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @throws OAuthServerException
|
2016-04-17 17:03:29 +05:30
|
|
|
*
|
2016-07-09 04:30:44 +05:30
|
|
|
* @return ServerRequestInterface
|
2016-04-17 17:03:29 +05:30
|
|
|
*/
|
|
|
|
public function validateAuthenticatedRequest(ServerRequestInterface $request)
|
|
|
|
{
|
|
|
|
return $this->getAuthorizationValidator()->validateAuthorization($request);
|
|
|
|
}
|
|
|
|
}
|