You are here

class SimpleOauthAuthenticationProvider in Simple OAuth (OAuth2) & OpenID Connect 8.2

Same name and namespace in other branches
  1. 8.4 src/Authentication/Provider/SimpleOauthAuthenticationProvider.php \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider
  2. 8 src/Authentication/Provider/SimpleOauthAuthenticationProvider.php \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider
  3. 8.3 src/Authentication/Provider/SimpleOauthAuthenticationProvider.php \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider
  4. 5.x src/Authentication/Provider/SimpleOauthAuthenticationProvider.php \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider

@internal

Hierarchy

Expanded class hierarchy of SimpleOauthAuthenticationProvider

2 files declare their use of SimpleOauthAuthenticationProvider
DisallowSimpleOauthRequests.php in src/PageCache/DisallowSimpleOauthRequests.php
SimpleOauthAuthenticationTest.php in tests/src/Unit/Authentication/Provider/SimpleOauthAuthenticationTest.php
1 string reference to 'SimpleOauthAuthenticationProvider'
simple_oauth.services.yml in ./simple_oauth.services.yml
simple_oauth.services.yml
1 service uses SimpleOauthAuthenticationProvider
simple_oauth.authentication.simple_oauth in ./simple_oauth.services.yml
Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider

File

src/Authentication/Provider/SimpleOauthAuthenticationProvider.php, line 14

Namespace

Drupal\simple_oauth\Authentication\Provider
View source
class SimpleOauthAuthenticationProvider implements SimpleOauthAuthenticationProviderInterface {

  /**
   * @var \Drupal\simple_oauth\Server\ResourceServerInterface
   */
  protected $resourceServer;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a HTTP basic authentication provider object.
   *
   * @param \Drupal\simple_oauth\Server\ResourceServerInterface $resource_server
   *   The resource server object.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(ResourceServerInterface $resource_server, EntityTypeManagerInterface $entity_type_manager) {
    $this->resourceServer = $resource_server;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(Request $request) {

    // Check for the presence of the token.
    return $this
      ->hasTokenValue($request);
  }

  /**
   * {@inheritdoc}
   */
  public static function hasTokenValue(Request $request) {

    // Check the header. See: http://tools.ietf.org/html/rfc6750#section-2.1
    $auth_header = trim($request->headers
      ->get('Authorization', '', TRUE));
    return strpos($auth_header, 'Bearer ') !== FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate(Request $request) {

    // Update the request with the OAuth information.
    try {
      $request = $this->resourceServer
        ->validateAuthenticatedRequest($request);
    } catch (OAuthServerException $exception) {

      // Procedural code here is hard to avoid.
      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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleOauthAuthenticationProvider::$entityTypeManager protected property
SimpleOauthAuthenticationProvider::$resourceServer protected property
SimpleOauthAuthenticationProvider::applies public function Checks whether suitable authentication credentials are on the request. Overrides AuthenticationProviderInterface::applies
SimpleOauthAuthenticationProvider::authenticate public function Authenticates the user. Overrides AuthenticationProviderInterface::authenticate
SimpleOauthAuthenticationProvider::hasTokenValue public static function Gets the access token from the request. Overrides SimpleOauthAuthenticationProviderInterface::hasTokenValue
SimpleOauthAuthenticationProvider::__construct public function Constructs a HTTP basic authentication provider object.