You are here

class BasicAuthSwap in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8.2 src/HttpMiddleware/BasicAuthSwap.php \Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap
  2. 8.3 src/HttpMiddleware/BasicAuthSwap.php \Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap
  3. 5.x src/HttpMiddleware/BasicAuthSwap.php \Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap

Uses the basic auth information to provide the client credentials for OAuth2.

Hierarchy

  • class \Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of BasicAuthSwap

1 string reference to 'BasicAuthSwap'
simple_oauth.services.yml in ./simple_oauth.services.yml
simple_oauth.services.yml
1 service uses BasicAuthSwap
simple_oauth.http_middleware.basic_auth_swap in ./simple_oauth.services.yml
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap

File

src/HttpMiddleware/BasicAuthSwap.php, line 11

Namespace

Drupal\simple_oauth\HttpMiddleware
View source
class BasicAuthSwap implements HttpKernelInterface {

  /**
   * The wrapped HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * Constructs a BasicAuthSwap object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The decorated kernel.
   */
  public function __construct(HttpKernelInterface $http_kernel) {
    $this->httpKernel = $http_kernel;
  }

  /**
   * Handles a Request to convert it to a Response.
   *
   * If the request appears to be an OAuth2 token request with Basic Auth,
   * swap the Basic Auth credentials into the request body and then remove the
   * Basic Auth credentials from the request so that core authentication is
   * not performed later.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The input request.
   * @param int $type
   *   The type of the request. One of HttpKernelInterface::MASTER_REQUEST or
   *   HttpKernelInterface::SUB_REQUEST.
   * @param bool $catch
   *   Whether to catch exceptions or not.
   *
   * @throws \Exception
   *   When an Exception occurs during processing.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   A Response instance
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    if (strpos($request
      ->getPathInfo(), '/oauth/token') !== FALSE && $request->headers
      ->has('PHP_AUTH_USER') && $request->headers
      ->has('PHP_AUTH_PW')) {

      // Swap the Basic Auth credentials into the request data.
      $request->request
        ->set('client_id', $request->headers
        ->get('PHP_AUTH_USER'));
      $request->request
        ->set('client_secret', $request->headers
        ->get('PHP_AUTH_PW'));

      // Remove the Basic Auth credentials to prevent later authentication.
      $request->headers
        ->remove('PHP_AUTH_USER');
      $request->headers
        ->remove('PHP_AUTH_PW');
    }
    return $this->httpKernel
      ->handle($request, $type, $catch);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BasicAuthSwap::$httpKernel protected property The wrapped HTTP kernel.
BasicAuthSwap::handle public function Handles a Request to convert it to a Response.
BasicAuthSwap::__construct public function Constructs a BasicAuthSwap object.