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

Comments

frymorgan created an issue. See original summary.

frymorgan’s picture

StatusFileSize
new633 bytes

Patch to the above effect.

imclean’s picture

+++ 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()

imclean’s picture

Version: 1.0.0-alpha3 » 1.0.x-dev
Related issues: +#3316295: No reply address is registered for the application
frymorgan’s picture

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

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

Url::toString(TRUE) returns a GeneratedUrl object, 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.

imclean’s picture

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.

nathan tsai’s picture

Priority: Normal » Major
Status: Active » Needs review

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?

imclean’s picture

Status: Needs review » Fixed

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.

Status: Fixed » Closed (fixed)

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

frymorgan’s picture

StatusFileSize
new1.21 KB

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.