Skip to content

Commit d2f4461

Browse files
committed
Merge branch 'update_thoth_auth-3_4_0-i1171' into 'stable-3_4_0'
Update Thoth auth to use personal access token See merge request softwares-pkp/plugins_ojs/thoth-omp-plugin!100
2 parents 1e1339f + 6156695 commit d2f4461

File tree

14 files changed

+104
-100
lines changed

14 files changed

+104
-100
lines changed

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ variables:
33

44
include:
55
- project: 'documentacao-e-tarefas/modelosparaintegracaocontinua'
6-
ref: main
6+
ref: stable-3_4_0
77
file:
88
- 'templates/groups/pkp_plugin.yml'
99
- 'templates/groups/omp/unit_tests.yml'

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This plugin is compatible with the following PKP applications:
1919

2020
The OMP instance must have the `api_key_secret` configuration set up, you may contact your system administrator to do that (see [this post](https://forum.pkp.sfu.ca/t/how-to-generate-a-api-key-secret-code-in-ojs-3/72008)).
2121

22-
This is required to use the API credentials provided, that are stored encrypted in the OMP database.
22+
This is required to store the Thoth personal access token encrypted in the OMP database.
2323

2424
## Installation
2525

@@ -41,7 +41,7 @@ This is required to use the API credentials provided, that are stored encrypted
4141

4242
To configure the plugin:
4343

44-
- **E-mail** and **Password**: Enter the credentials for a Thoth account to connect with the API.
44+
- **Personal access token**: Enter a valid Thoth personal access token to authenticate API requests.
4545
- **Test Environment**: Check this option if you are using a local instance of the Thoth API for testing purposes.
4646

4747
![settings](/images/settings.png)

ThothSettingsForm.inc.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class ThothSettingsForm extends Form
3333
private $encryption;
3434

3535
private const SETTINGS = [
36-
'email',
37-
'password',
36+
'token',
3837
'testEnvironment',
3938
];
4039

@@ -50,11 +49,10 @@ public function __construct($plugin, $contextId)
5049
$form = $this;
5150
$this->addCheck(new FormValidatorCustom(
5251
$this,
53-
'password',
52+
'token',
5453
'required',
5554
'plugins.generic.thoth.settings.invalidCredentials',
56-
function ($password) use ($form) {
57-
$email = trim($this->getData('email'));
55+
function ($token) use ($form) {
5856
$testEnvironment = $this->getData('testEnvironment');
5957
$httpConfig = [];
6058
if ($testEnvironment) {
@@ -64,7 +62,7 @@ function ($password) use ($form) {
6462
$client = new Client($httpConfig);
6563

6664
try {
67-
$client->login($email, $password);
65+
$client->setToken(trim($token))->me();
6866
} catch (QueryException $e) {
6967
return false;
7068
}
@@ -79,10 +77,10 @@ function ($password) use ($form) {
7977
public function initData()
8078
{
8179
foreach (self::SETTINGS as $setting) {
82-
if ($setting == 'password') {
83-
$password = $this->plugin->getSetting($this->contextId, $setting);
84-
$this->_data[$setting] = ($this->encryption->secretConfigExists() && $password) ?
85-
$this->encryption->decryptString($password) :
80+
if ($setting == 'token') {
81+
$token = $this->plugin->getSetting($this->contextId, $setting);
82+
$this->_data[$setting] = ($this->encryption->secretConfigExists() && $token) ?
83+
$this->encryption->decryptString($token) :
8684
null;
8785
continue;
8886
}
@@ -104,20 +102,20 @@ public function fetch($request, $template = null, $display = false)
104102

105103
public function execute(...$functionArgs)
106104
{
107-
$this->encryptPassword();
105+
$this->encryptToken();
108106
foreach (self::SETTINGS as $setting) {
109107
$this->plugin->updateSetting($this->contextId, $setting, trim($this->getData($setting)), 'string');
110108
}
111109
parent::execute(...$functionArgs);
112110
}
113111

114-
private function encryptPassword()
112+
private function encryptToken()
115113
{
116-
$password = $this->getData('password');
114+
$token = trim($this->getData('token'));
117115

118-
if (!$this->encryption->textIsEncrypted($password)) {
119-
$encryptedPassword = $this->encryption->encryptString($password);
120-
$this->setData('password', $encryptedPassword);
116+
if (!$this->encryption->textIsEncrypted($token)) {
117+
$encryptedToken = $this->encryption->encryptString($token);
118+
$this->setData('token', $encryptedToken);
121119
}
122120
}
123121
}

classes/container/providers/ThothRepositoryProvider.inc.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ public function register($container)
5454
$contextId = Application::get()->getRequest()->getContext()->getId();
5555

5656
$testEnvironment = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'testEnvironment');
57-
$email = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'email');
58-
$password = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'password') ?? '';
57+
$token = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'token') ?? '';
5958

6059
return [
6160
'testEnvironment' => $testEnvironment,
62-
'email' => $email,
63-
'password' => $encryption->decryptString($password)
61+
'token' => $token ? $encryption->decryptString($token) : ''
6462
];
6563
});
6664

@@ -73,7 +71,7 @@ public function register($container)
7371
}
7472

7573
$client = new Client($httpConfig);
76-
return $client->login($config['email'], $config['password']);
74+
return $client->setToken($config['token']);
7775
});
7876

7977
$container->set('accountRepository', function ($container) {

classes/repositories/ThothAccountRepository.inc.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,23 @@ public function __construct($thothClient)
2525

2626
public function getLinkedPublishers()
2727
{
28-
$details = $this->thothClient->accountDetails();
29-
return $details['resourceAccess']['linkedPublishers'];
28+
$result = $this->thothClient->rawQuery(
29+
<<<'GQL'
30+
query {
31+
me {
32+
publisherContexts {
33+
publisher {
34+
publisherId
35+
}
36+
}
37+
}
38+
}
39+
GQL
40+
);
41+
42+
return array_map(
43+
fn ($publisherContext) => $publisherContext['publisher'],
44+
$result['me']['publisherContexts'] ?? []
45+
);
3046
}
3147
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
33
"biblys/isbn": "~3.0",
4-
"thoth-pub/thoth-client-php": "^1.3"
4+
"thoth-pub/thoth-client-php": "^1.4"
55
}
66
}

composer.lock

Lines changed: 39 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)