SimpleOauthAuthenticationProvider.php in Simple OAuth (OAuth2) & OpenID Connect 8.4
File
src/Authentication/Provider/SimpleOauthAuthenticationProvider.php
View source
<?php
namespace Drupal\simple_oauth\Authentication\Provider;
use Drupal\Core\Authentication\AuthenticationProviderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\simple_oauth\Authentication\TokenAuthUser;
use Drupal\simple_oauth\PageCache\SimpleOauthRequestPolicyInterface;
use Drupal\simple_oauth\Server\ResourceServerInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
class SimpleOauthAuthenticationProvider implements AuthenticationProviderInterface {
protected $resourceServer;
protected $entityTypeManager;
protected $oauthPageCacheRequestPolicy;
public function __construct(ResourceServerInterface $resource_server, EntityTypeManagerInterface $entity_type_manager, SimpleOauthRequestPolicyInterface $page_cache_request_policy) {
$this->resourceServer = $resource_server;
$this->entityTypeManager = $entity_type_manager;
$this->oauthPageCacheRequestPolicy = $page_cache_request_policy;
}
public function applies(Request $request) {
return $this->oauthPageCacheRequestPolicy
->isOauth2Request($request);
}
public function authenticate(Request $request) {
try {
$auth_request = $this->resourceServer
->validateAuthenticatedRequest($request);
} catch (OAuthServerException $exception) {
watchdog_exception('simple_oauth', $exception);
throw new HttpException($exception
->getHttpStatusCode(), $exception
->getHint(), $exception);
}
$tokens = $this->entityTypeManager
->getStorage('oauth2_token')
->loadByProperties([
'value' => $auth_request
->get('oauth_access_token_id'),
]);
$token = reset($tokens);
$account = new TokenAuthUser($token);
if ($account
->isBlocked() && $account
->isAuthenticated()) {
$token
->revoke();
$token
->save();
$exception = OAuthServerException::accessDenied(t('%name is blocked or has not been activated yet.', [
'%name' => $account
->getAccountName(),
]));
watchdog_exception('simple_oauth', $exception);
throw new HttpException($exception
->getHttpStatusCode(), $exception
->getHint(), $exception);
}
$request->files
->add($auth_request->files
->all());
$request->headers
->set('X-Consumer-ID', $account
->getConsumer()
->uuid());
return $account;
}
}