You are here

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

Same name and namespace in other branches
  1. 8.4 src/Authentication/Provider/SimpleOauthAuthenticationProvider.php \Drupal\simple_oauth\Authentication\Provider\SimpleOauthAuthenticationProvider
  2. 8.2 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

Class SimpleOauthAuthenticationProvider.

@package Drupal\simple_oauth\Authentication\Provider

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 15

Namespace

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

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a HTTP basic authentication provider object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager) {
    $this->configFactory = $config_factory;
    $this->entityManager = $entity_manager;
  }

  /**
   * Checks whether suitable authentication credentials are on the request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return bool
   *   TRUE if authentication credentials suitable for this provider are on the
   *   request, FALSE otherwise.
   */
  public function applies(Request $request) {

    // Check for the presence of the token.
    return (bool) $this::getTokenValue($request);
  }

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

    // Check the header. See: http://tools.ietf.org/html/rfc6750#section-2.1
    $auth_header = $request->headers
      ->get('Authorization', '', TRUE);
    $prefix = 'Bearer ';
    if (strpos($auth_header, $prefix) === 0) {
      return substr($auth_header, strlen($prefix));
    }

    // Form encoded parameter. See:
    // http://tools.ietf.org/html/rfc6750#section-2.2
    $ct_header = $request->headers
      ->get('Content-Type', '', TRUE);
    $is_get = $request
      ->getMethod() == Request::METHOD_GET;
    $token = $request->request
      ->get('access_token');
    if (!$is_get && $ct_header == 'application/x-www-form-urlencoded' && $token) {
      return $token;
    }

    // This module purposely refuses to implement
    // http://tools.ietf.org/html/rfc6750#section-2.3 for security resons.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate(Request $request) {
    $token_storage = $this->entityManager
      ->getStorage('access_token');
    $ids = $token_storage
      ->getQuery()
      ->condition('value', $this::getTokenValue($request))
      ->condition('expire', REQUEST_TIME, '>')
      ->range(0, 1)
      ->execute();
    if (!empty($ids)) {

      /* @var \Drupal\simple_oauth\AccessTokenInterface $token */
      $token = $token_storage
        ->load(reset($ids));
      try {
        return new TokenAuthUser($token);
      } catch (\Exception $e) {
      }
    }
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimpleOauthAuthenticationProvider::$configFactory protected property The config factory.
SimpleOauthAuthenticationProvider::$entityManager protected property The entity manager.
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::getTokenValue public static function Gets the access token from the request. Overrides SimpleOauthAuthenticationProviderInterface::getTokenValue
SimpleOauthAuthenticationProvider::__construct public function Constructs a HTTP basic authentication provider object.