SimpleOauthAuthenticationProvider.php in Simple OAuth (OAuth2) & OpenID Connect 8.2
File
src/Authentication/Provider/SimpleOauthAuthenticationProvider.php
View source
<?php
namespace Drupal\simple_oauth\Authentication\Provider;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\simple_oauth\Authentication\TokenAuthUser;
use Drupal\simple_oauth\Server\ResourceServerInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Component\HttpFoundation\Request;
class SimpleOauthAuthenticationProvider implements SimpleOauthAuthenticationProviderInterface {
protected $resourceServer;
protected $entityTypeManager;
public function __construct(ResourceServerInterface $resource_server, EntityTypeManagerInterface $entity_type_manager) {
$this->resourceServer = $resource_server;
$this->entityTypeManager = $entity_type_manager;
}
public function applies(Request $request) {
return $this
->hasTokenValue($request);
}
public static function hasTokenValue(Request $request) {
$auth_header = trim($request->headers
->get('Authorization', '', TRUE));
return strpos($auth_header, 'Bearer ') !== FALSE;
}
public function authenticate(Request $request) {
try {
$request = $this->resourceServer
->validateAuthenticatedRequest($request);
} catch (OAuthServerException $exception) {
watchdog_exception('simple_oauth', $exception);
return NULL;
}
$tokens = $this->entityTypeManager
->getStorage('oauth2_token')
->loadByProperties([
'value' => $request
->get('oauth_access_token_id'),
]);
$token = reset($tokens);
return new TokenAuthUser($token);
}
}