Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gheydon/2160d6b8abaec3b93197677379e0bf65 to your computer and use it in GitHub Desktop.
Save gheydon/2160d6b8abaec3b93197677379e0bf65 to your computer and use it in GitHub Desktop.
From 9575a6c176acad4cda98faedd1a473ad5c4a1949 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <[email protected]>
Date: Thu, 27 Sep 2018 23:35:22 +1000
Subject: [PATCH] [CLI-415] Fix issue with Janrain not redirecting properly
from ajax
---
src/Controller/AuthenticationController.php | 20 +++++++++++++++++---
src/JanrainCaptureApi.php | 8 +++++---
src/JanrainCaptureApiInterface.php | 3 ++-
3 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/src/Controller/AuthenticationController.php b/src/Controller/AuthenticationController.php
index 25b2758..1680d35 100644
--- a/src/Controller/AuthenticationController.php
+++ b/src/Controller/AuthenticationController.php
@@ -2,6 +2,8 @@
namespace Drupal\janrain_capture\Controller;
+use Drupal\Core\Ajax\AjaxResponse;
+use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\janrain_capture\JanrainCaptureApi;
@@ -118,6 +120,8 @@ EOF;
// browser, this controller must show the real HTML page instead of
// just a URI.
$response_class = Response::class;
+ $redirect_class = RedirectResponse::class;
+
// Read (and remove) authorization code from the request.
$authorization_code = $this->getAuthorizationCode($request);
// Form destination URL here since the "$request" is modified above.
@@ -131,7 +135,7 @@ EOF;
try {
// The authentication can throw exceptions so their messages
// will be exposed on the frontend.
- $this->captureApi->authenticate($authorization_code, $request->getUri());
+ $redirect = $this->captureApi->authenticate($authorization_code, $request->getUri());
}
catch (\Throwable $e) {
if ($e->getMessage() !== '') {
@@ -143,7 +147,7 @@ EOF;
if ($request->get('url_type') === 'forgot') {
// Now we're going to redirect a user to the previous location (front
// page if missing).
- $response_class = RedirectResponse::class;
+ $response_class = $redirect_class;
// The authentication request ended with an error.
if (isset($e)) {
@@ -166,9 +170,19 @@ EOF;
$destination_url->setRouteParameter('changePassword', 'yes');
}
}
+ else if (isset($redirect)) {
+ $response_class = $redirect_class;
+ $destination_url = $redirect;
+ }
}
- return new $response_class($destination_url->setAbsolute()->toString());
+ if ($request->headers->has('x-requested-with') && 'XMLHttpRequest' == $request->headers->get('x-requested-with')) {
+ $response_class = AjaxResponse::class;
+ return new $response_class(new RedirectCommand($destination_url->setAbsolute()->toString()));
+ }
+ else {
+ return new $response_class($destination_url->setAbsolute()->toString());
+ }
}
/**
diff --git a/src/JanrainCaptureApi.php b/src/JanrainCaptureApi.php
index 0c0da15..70c4223 100644
--- a/src/JanrainCaptureApi.php
+++ b/src/JanrainCaptureApi.php
@@ -8,6 +8,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\KeyValueStore\KeyValueDatabaseFactory;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface;
+use Drupal\Core\Url;
use Drupal\janrain_capture\Authentication\AccessToken;
use Drupal\janrain_capture\Authentication\RefreshToken;
use Drupal\janrain_capture\Exception\JanrainApiCallError;
@@ -109,6 +110,7 @@ class JanrainCaptureApi implements JanrainCaptureApiInterface {
$this->clientId = $config['client_id'] ?? '';
$this->clientSecret = $config['client_secret'] ?? '';
$this->captureAddress = $config['capture_server'] ?? '';
+ $this->mesageCountryRestricted = $config['validate']['mesage_country_restricted'] ?? '';
$this->logger = $logger_factory->get('janrain_capture');
$this->userData = $user_data;
@@ -122,7 +124,7 @@ class JanrainCaptureApi implements JanrainCaptureApiInterface {
/**
* {@inheritdoc}
*/
- public function authenticate(string $auth_code, string $redirect_uri): UserInterface {
+ public function authenticate(string $auth_code, string $redirect_uri): ?Url {
$token = $this->getToken(static::GRANT_TYPE_AUTHORIZATION_CODE, [
'code' => $auth_code,
'redirect_uri' => $redirect_uri,
@@ -161,7 +163,7 @@ class JanrainCaptureApi implements JanrainCaptureApiInterface {
$account = $this->userStorage->load(reset($accounts));
}
- user_login_finalize($account);
+ $redirect = user_login_finalize($account);
// Update the current user account in memory. This needed to provide
// a correct user account for calls to "getAccessToken()" method in
@@ -180,7 +182,7 @@ class JanrainCaptureApi implements JanrainCaptureApiInterface {
// Save the token to the database.
$this->cache($token);
- return $account;
+ return $redirect;
}
/**
diff --git a/src/JanrainCaptureApiInterface.php b/src/JanrainCaptureApiInterface.php
index 9544fdf..3d710a8 100644
--- a/src/JanrainCaptureApiInterface.php
+++ b/src/JanrainCaptureApiInterface.php
@@ -2,6 +2,7 @@
namespace Drupal\janrain_capture;
+use Drupal\Core\Url;
use Drupal\janrain_capture\User\JanrainUserProfile;
use Drupal\user\UserInterface;
use Drupal\janrain_capture\Authentication\AccessToken;
@@ -36,7 +37,7 @@ interface JanrainCaptureApiInterface {
* @throws \Drupal\janrain_capture\Exception\JanrainApiCallError
* @throws \Drupal\Core\Entity\EntityStorageException
*/
- public function authenticate(string $auth_code, string $redirect_uri): UserInterface;
+ public function authenticate(string $auth_code, string $redirect_uri): ?Url;
/**
* Returns an access token from the database and prolongs it automatically.
--
2.18.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment