How best to authenticate a user and use the user's session cookie in guzzle requests

Created on 28 October 2024, 4 months ago

Problem/Motivation

I'm utilizing OpenAPI PSR-7 Message (HTTP Request/Response) Validator to validate a \Psr\Http\Message\ResponseInterface response against an openapi specification. To get a \Psr\Http\Message\ResponseInterface response object, I'm using a guzzle client to make the request. However, I need to create and authenticate a Drupal user to get the session cookie and add it to the guzzle client request.

When extending ExistingSiteBase, it does not appear that same functionality provided by BrowserTestBase (specifically BrowserTestBase::getSessionCookies()) is available to.

πŸ’¬ Support request
Status

Active

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States jasonawant New Orleans, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jasonawant
  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    In a client project I have a trait like this:

    <?php
    
    declare(strict_types=1);
    
    namespace Drupal\Tests\my_module;
    
    use GuzzleHttp\Cookie\CookieJar;
    use Symfony\Component\BrowserKit\CookieJar as BrowserKitCookieJar;
    
    trait SessionCookiesTestTrait {
    
      public function getSessionCookies(): CookieJar {
        /** @var \Behat\Mink\Driver\BrowserKitDriver $driver */
        $driver = $this->getSession()->getDriver();
        $cookieJar = $driver->getClient()->getCookieJar();
        $this->assertInstanceOf(BrowserKitCookieJar::class, $cookieJar);
        $domain = \parse_url($this->baseUrl, PHP_URL_HOST);
        \assert(\is_string($domain));
        return CookieJar::fromArray(
          $cookieJar->allRawValues($this->baseUrl),
          $domain,
        );
      }
    
    }
    

    Usage example:

    \Drupal::service('http_client')->request('POST', $this->buildUrl($url), [
      'cookies' => $this->getSessionCookies(),
      'form_params' => ['ids' => $ids, 'tokens' => $tokens],
      'http_errors' => FALSE,
    ]);
    

    Would something like that work for you? Would it make sense to include this in DTT?

  • πŸ‡ΊπŸ‡ΈUnited States jasonawant New Orleans, USA

    Thanks mstrelan!

    I think I'm missing something. How does this get the user session cookie?

    Prior to the example usage, would you use UserCreationTrait::setUpCurrentUser() to create use and set as current user?

  • πŸ‡¦πŸ‡ΊAustralia mstrelan

    Sorry I omitted that. Essentially use \weitzman\DrupalTestTraits\Entity\UserCreationTrait::createUser and \Drupal\Tests\UiHelperTrait::drupalLogin.

    Example:

    $user = $this->createUser();
    $user->addRole('MY_ROLE_ID');
    $user->save();
    $this->drupalLogin($user);
    
  • πŸ‡ΊπŸ‡ΈUnited States jasonawant New Orleans, USA

    Thank you @mstrelan! This worked for my use case.

    I don't think its quite necessary to consider adding this to DTT, but perhaps if the request comes up again, maybe.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024