Skip to content

Commit

Permalink
Follow Redirects on TOTP page in the manager
Browse files Browse the repository at this point in the history
  • Loading branch information
matdave committed Jun 22, 2023
1 parent 3858b67 commit 974e986
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
2 changes: 1 addition & 1 deletion _build/gpm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"lowCaseName": "twilio",
"description": "Twilio for MODX Revolution 3.x",
"author": "John Peca",
"version": "2.0.2",
"version": "2.0.3",
"menus": [
{
"text": "twilio.users",
Expand Down
23 changes: 20 additions & 3 deletions assets/components/twilio/js/mgr/widgets/totp.panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ twilio.panel.Totp = function (config) {
localStorage.setItem('twilio_device_id', twilioDeviceId);
} else {
if (MODx.request.device_id == null) {
MODx.loadPage('totp', 'namespace=twilio&device_id=' + twilioDeviceId);
var url = 'namespace=twilio&device_id=' + twilioDeviceId;
if (MODx.request.return) {
url += '&return=' + MODx.request.return;
}
MODx.loadPage('totp', url);
}
}
config = config || {};
Expand All @@ -39,8 +43,21 @@ twilio.panel.Totp = function (config) {
url: MODx.config.connector_url,
waitMsg: _('twilio.verifying'),
success: function (form, action) {
console.log(action.result);
window.location = MODx.config.manager_url;
var url = MODx.config.manager_url;
if (MODx.request.return) {
var return_url = JSON.parse(decodeURIComponent(MODx.request.return));
console.log(return_url);
if (return_url.a) {
url += '?a=' + return_url.a;
}
if (return_url.namespace) {
url += '&namespace=' + return_url.namespace;
}
if (return_url.id) {
url += '&id=' + return_url.id;
}
}
window.location = url;
},
failure: function (form, action) {
Ext.Msg.alert(_('error'), action.result.message);
Expand Down
22 changes: 20 additions & 2 deletions core/components/twilio/controllers/totp.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function process(array $scriptProperties = array())
{
$deviceID = $_REQUEST['device_id'] ?? null;
if (isset($_SESSION['twilio_totp_verified']) && $_SESSION['twilio_totp_verified']) {
$this->modx->sendRedirect(MODX_MANAGER_URL);
$this->modx->sendRedirect($this->getManagerUrl());
}
$user = $this->modx->user;
$this->checkDevice($deviceID, $user);
Expand Down Expand Up @@ -65,7 +65,7 @@ public function checkDevice($device, $user)
in_array($device, $userTwilio['remembered'], true)
) {
$_SESSION['twilio_totp_verified'] = true;
$this->modx->sendRedirect(MODX_MANAGER_URL);
$this->modx->sendRedirect($this->getManagerUrl());
} elseif (!empty($device)) {
$failed = $profile->get('failedlogincount') ?? 0;
++$failed;
Expand All @@ -82,4 +82,22 @@ public function checkDevice($device, $user)
}
}
}
private function getManagerUrl()
{
$url = MODX_MANAGER_URL;
if ($_REQUEST['return']) {
$url .= '?';
$return = json_decode($_REQUEST['return'], true);
if (isset($return['a'])) {
$url .= '&a='.$return['a'];
}
if (isset($return['namespace'])) {
$url .= '&namespace='.$return['namespace'];
}
if (isset($return['id'])) {
$url .= '&id='.$return['id'];
}
}
return $url;
}
}
4 changes: 4 additions & 0 deletions core/components/twilio/docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Twilio 2.0.3
==============
- Follow Redirects on TOTP page in the manager

Twilio 2.0.2
==============
- Fixing permissions issue
Expand Down
2 changes: 1 addition & 1 deletion core/components/twilio/index.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

abstract class TwilioBaseManagerController extends modManagerController
{
public string $version = '1.0.0';
public string $version = '2.0.3';

public function checkPermissions()
{
Expand Down
13 changes: 12 additions & 1 deletion core/components/twilio/src/Event/OnBeforeManagerPageInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ public function run()
// System Wide
$enforceTotp = $this->getOption('twilio.totp_enforce', false);
$action = $this->getOption('action');
$namespace = $this->getOption('namespace');
$user = $this->modx->user;
if (!$user || $user->id === 0) {
return false;
}
// User Specific
$userTotp = $user->getOption('twilio.totp', $user->getSettings(), false);
if ($enforceTotp && $userTotp && !$_SESSION['twilio_totp_verified'] && $action !== 'totp') {
$this->modx->sendRedirect(MODX_MANAGER_URL . 'index.php?a=totp&namespace=twilio');
$return = [];
if (isset($action)) {
$return['a'] = $action;
}
if (isset($namespace)) {
$return['namespace'] = $namespace;
}
if (isset($_REQUEST['id'])) {
$return['id'] = $_REQUEST['id'];
}
$this->modx->sendRedirect(MODX_MANAGER_URL . 'index.php?a=totp&namespace=twilio&return='.json_encode($return));
}
}
}

0 comments on commit 974e986

Please sign in to comment.