Created on 23 February 2023, over 1 year ago
Updated 16 October 2023, about 1 year ago

Problem/Motivation

Retrieving token raises

LogicException: The controller result claims to be providing relevant cache metadata, but leaked metadata was detected. Please ensure you are not rendering content too early. Returned object class: Drupal\Core\Routing\TrustedRedirectResponse. in Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (line 154 of core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php).

Steps to reproduce

'Get auth token' button at /admin/config/system/phpmailer-oauth2

Proposed resolution

Patch MSLoginController (47): 'redirect_uri' => Url...->tostring(TRUE) so it contains a GeneratedUrl object.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States frymorgan

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

Comments & Activities

  • Issue created by @frymorgan
  • πŸ‡ΊπŸ‡ΈUnited States frymorgan

    Patch to the above effect.

  • πŸ‡¦πŸ‡ΊAustralia imclean Tasmania
    +++ b/src/Controller/MsLoginController.php
    @@ -44,7 +44,7 @@ class MsLoginController extends ControllerBase {
       public function login() {
         $authorizationUrl = $this->azureProvider->getAuthorizationUrl([
    -      'redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(),
    +      'redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE),
           'scope' => $this->azureProvider->scope,
         ]); 
    

    Is the return type the same? Or should it be:

    'redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl()

  • πŸ‡¦πŸ‡ΊAustralia imclean Tasmania
  • πŸ‡ΊπŸ‡ΈUnited States frymorgan

    Is the return type the same? Or should it be:

    'redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl()

    toString(TRUE) returns a genreatedURL type, appending getGeneratedUrl() will return a string.

    NB, the previous version (before the change in #3316295), after this patch also works. (Drupal 9.5, PHP 8.1)

    With redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl(), Azure returns an error because /admin/config/system/phpmailer-oauth2 is not the callback url defined in the Azure app.

  • πŸ‡¦πŸ‡ΊAustralia imclean Tasmania

    Url::toString(TRUE) returns a GeneratedUrl object, appending getGeneratedUrl() will return a string.

    Which is what is returned without this patch.

    With redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl(), Azure returns an error because /admin/config/system/phpmailer-oauth2 is not the callback url defined in the Azure app.

    How does adding "TRUE" fix the error? It's the same (incorrect?) route being returned, just as a GeneratedUrl object instead of a string.

  • Status changed to Needs review over 1 year ago
  • πŸ‡¨πŸ‡¦Canada Nathan Tsai

    Increasing the priority of this issue to "Major" because the module is currently unusable without a workaround.

    I got the same error as above, but applying the patch allowed me to successfully generate the tokens:

    Auth token retrieved.
    Access token retrieved.
    Refresh token retrieved.

    I have no clue why returning a GeneratedUrl fixes the bug, though, hence the "Needs Review" status.

    @frymorgan, perhaps you can speak more about this issue?

  • Status changed to Fixed over 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia imclean Tasmania

    the module is currently unusable without a workaround.

    I don't fully understand why as we've used this module in the past without it. That said, it works for at least 2 people so I've committed it.

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

  • Status changed to Fixed about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States frymorgan

    Is the return type the same? Or should it be:

    'redirect_uri' => Url::fromRoute('phpmailer_oauth2.settings')->setAbsolute()->toString(TRUE)->getGeneratedUrl()

    Using getGeneratedUrl() is correct after all. Problem was the route should be phpmailer_oauth2.aad_callback, not phpmailer_oauth2.settings.

    As it stands, it does not insert a redirect_url into the response at all, which works if the Azure app has only one Redirect URI.

Production build 0.71.5 2024