-
Notifications
You must be signed in to change notification settings - Fork 7
/
SubscriptionSSOPlugin.php
169 lines (150 loc) · 5.34 KB
/
SubscriptionSSOPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @file SubscriptionSSOPlugin.inc.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2014-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file LICENSE.
*
* Plugin to defer subscription checks to an external system.
*/
namespace APP\plugins\generic\subscriptionSSO;
use PKP\linkAction\LinkAction;
use PKP\plugins\GenericPlugin;
use PKP\linkAction\request\AjaxModal;
use PKP\config\Config;
use PKP\plugins\Hook;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use APP\core\Application;
class SubscriptionSSOPlugin extends GenericPlugin {
/**
* @copydoc GenericPlugin::register
*/
function register($category, $path, $mainContextId = null) {
$success = parent::register($category, $path, $mainContextId);
if (!Config::getVar('general', 'installed') || defined('RUNNING_UPGRADE')) return true;
if ($success && $this->getEnabled()) {
$this->addLocaleData();
Hook::add('LoadHandler', [&$this, 'loadHandlerCallback']);
Hook::add('IssueAction::subscribedUser', [&$this, 'subscribedUserCallback']);
return true;
}
return $success;
}
/**
* Callback when a handler is loaded. Used to check for the presence
* of an incoming authentication, which needs to be verified.
* @param string $hookName Hook name
* @param array $args Hook arguments
* @return boolean Hook return status
*/
function loadHandlerCallback($hookName, $args) {
$request = Application::get()->getRequest();
$journal = $request->getJournal();
if (!$journal) return false;
$incomingParameterName = $this->getSetting($journal->getId(), 'incomingParameterName');
// Using $_GET rather than Request because this may be case
// sensitive (e.g. differentiating myid from myId)
if ($incomingParameterName != '' && isset($_GET[$incomingParameterName])) {
$incomingKey = $_GET[$incomingParameterName];
// This is an incoming authorization. Contact the remote service.
$verificationUrl = $this->getSetting($journal->getId(), 'verificationUrl');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $verificationUrl . urlencode($incomingKey));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
// Verify the result.
$resultRegexp = $this->getSetting($journal->getId(), 'resultRegexp');
if (preg_match($resultRegexp, $result)) {
// Successfully validated.
$_SESSION['subscriptionSSOTimestamp'] = time();
} else {
// Failed to validate.
unset($_SESSION['subscriptionSSOTimestamp']);
$request->redirectUrl($this->getSetting($journal->getId(), 'redirectUrl'));
}
}
return false;
}
/**
* Callback when a handler is loaded. Used to check for the presence
* of an incoming authentication, which needs to be verified.
* @param string $hookName Hook name
* @param array $args Hook arguments
* @return boolean Hook return status
*/
function subscribedUserCallback($hookName, $args) {
// Exclude the index and issue pages.
$request = Application::get()->getRequest();
if (in_array($request->getRequestedPage(), ['', 'index', 'search'])) return false;
// Capture issue galley requests, but not e.g. issue archive
if ($request->getRequestedPage() == 'issue' && count($request->getRequestedArgs()) != 2) return false;
// Permit an abstract view.
if ($request->getRequestedPage() == 'article' && $request->getRequestedOp() == 'view' && count($request->getRequestedArgs())==1) return false;
$journal = $args[1];
$result =& $args[4]; // Reference required
if ($result) return false; // If a subscription has already been established, respect that
$result = isset($_SESSION['subscriptionSSOTimestamp']) && $_SESSION['subscriptionSSOTimestamp'] + ($this->getSetting($journal->getId(), 'hoursValid') * 3600) + 1 >= time();
if (!$result) {
// If we're not subscribed, redirect.
$request->redirectUrl($this->getSetting($journal->getId(), 'redirectUrl') . '?redirectUrl=' . urlencode($request->getRequestUrl()));
}
}
/**
* @copydoc Plugin::getActions()
*/
function getActions($request, $actionArgs) {
$router = $request->getRouter();
return array_merge(
$this->getEnabled()?[
new LinkAction(
'settings',
new AjaxModal(
$router->url($request, null, null, 'manage', null, array_merge($actionArgs, array('verb' => 'settings'))),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
),
]:[],
parent::getActions($request, $actionArgs)
);
}
/**
* @copydoc PKPPlugin::manage()
*/
function manage($args, $request) {
$context = $request->getContext();
$templateMgr = TemplateManager::getManager($request);
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new SubscriptionSSOSettingsForm($this, $context->getId());
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage();
}
} else {
$form->initData();
}
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
/**
* @copydoc Plugin::getDisplayName
*/
function getDisplayName() {
return __('plugins.generic.subscriptionSSO.name');
}
/**
* @copydoc Plugin::getDescription
*/
function getDescription() {
return __('plugins.generic.subscriptionSSO.description');
}
}