Oauth2Token.php in Simple OAuth (OAuth2) & OpenID Connect 5.x
File
src/Controller/Oauth2Token.php
View source
<?php
namespace Drupal\simple_oauth\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\simple_oauth\Plugin\Oauth2GrantManagerInterface;
use GuzzleHttp\Psr7\Response;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Oauth2Token extends ControllerBase {
protected $grantManager;
public function __construct(Oauth2GrantManagerInterface $grant_manager) {
$this->grantManager = $grant_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.oauth2_grant.processor'));
}
public function token(ServerRequestInterface $request) {
$body = $request
->getParsedBody();
$grant_type_id = !empty($body['grant_type']) ? $body['grant_type'] : 'implicit';
$client_drupal_entity = NULL;
if (!empty($body['client_id'])) {
$consumer_storage = $this
->entityTypeManager()
->getStorage('consumer');
$client_drupal_entities = $consumer_storage
->loadByProperties([
'uuid' => $body['client_id'],
]);
if (empty($client_drupal_entities)) {
return OAuthServerException::invalidClient($request)
->generateHttpResponse(new Response());
}
$client_drupal_entity = reset($client_drupal_entities);
}
try {
$auth_server = $this->grantManager
->getAuthorizationServer($grant_type_id, $client_drupal_entity);
$response = $this
->handleToken($request, $auth_server);
} catch (OAuthServerException $exception) {
watchdog_exception('simple_oauth', $exception);
$response = $exception
->generateHttpResponse(new Response());
}
return $response;
}
protected function handleToken(ServerRequestInterface $psr7_request, AuthorizationServer $auth_server) {
return $auth_server
->respondToAccessTokenRequest($psr7_request, new Response());
}
}