ResourceServer.php in Simple OAuth (OAuth2) & OpenID Connect 5.x
File
src/Server/ResourceServer.php
View source
<?php
namespace Drupal\simple_oauth\Server;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\ResourceServer as LeageResourceServer;
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
class ResourceServer implements ResourceServerInterface {
protected $subject;
protected $messageFactory;
protected $foundationFactory;
protected $fileSystem;
public function __construct(AccessTokenRepositoryInterface $access_token_repository, ConfigFactoryInterface $config_factory, HttpMessageFactoryInterface $message_factory, HttpFoundationFactoryInterface $foundation_factory) {
try {
$public_key = $config_factory
->get('simple_oauth.settings')
->get('public_key');
$public_key_real = $this
->fileSystem()
->realpath($public_key);
if ($public_key && $public_key_real) {
$this->subject = new LeageResourceServer($access_token_repository, $public_key_real);
}
} catch (\LogicException $exception) {
trigger_error($exception, E_USER_WARNING);
}
$this->messageFactory = $message_factory;
$this->foundationFactory = $foundation_factory;
}
protected function fileSystem() : FileSystemInterface {
if (!isset($this->fileSystem)) {
$this->fileSystem = \Drupal::service('file_system');
}
return $this->fileSystem;
}
public function validateAuthenticatedRequest(Request $request) {
if (!$this->subject) {
throw new \LogicException('Unable to create resource server. Make sure public and private keys are correctly configured.');
}
$psr7_request = $this->messageFactory
->createRequest($request);
$output_psr7_request = $this->subject
->validateAuthenticatedRequest($psr7_request);
return $this->foundationFactory
->createRequest($output_psr7_request);
}
}