Skip to content

Commit c61ae96

Browse files
committed
Merge branch 'update_thoth_auth-3_5_0-i1171' into 'main'
Update Thoth auth to use personal access token See merge request softwares-pkp/plugins_ojs/thoth-omp-plugin!98
2 parents 67794ef + 442952b commit c61ae96

File tree

13 files changed

+95
-91
lines changed

13 files changed

+95
-91
lines changed

README.md

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

3131
After enabling the plugin, go to the plugin settings and fill in:
3232

33-
- **Email** and **Password**: Credentials for a Thoth account to connect with the API.
33+
- **Personal access token**: A valid Thoth personal access token used to authenticate API requests.
3434
- **Custom Thoth API**: Check this option to use a custom Thoth API instead of the official one.
3535
- **Thoth API URL**: The URL of the custom Thoth API (only required when the custom API option is enabled).
3636

37-
<img src="/docs/images/plugin_settings.png" alt="Plugin settings form with email, password, custom API and URL fields" width="700">
37+
<img src="/docs/images/plugin_settings.png" alt="Plugin settings form with personal access token, custom API and URL fields" width="700">
3838

3939
### Registering Monographs
4040

ThothSettingsForm.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
class ThothSettingsForm extends Form
3535
{
3636
private const SETTINGS = [
37-
'email',
38-
'password',
37+
'token',
3938
'customThothApi',
4039
'customThothApiUrl',
4140
];
@@ -84,10 +83,10 @@ function ($customThothApiUrl) {
8483

8584
$this->addCheck(new FormValidatorCustom(
8685
$this,
87-
'password',
86+
'token',
8887
'required',
8988
'plugins.generic.thoth.settings.invalidCredentials',
90-
fn ($password) => $this->validateCredentials(trim($this->getData('email')), $password)
89+
fn ($token) => $this->validateCredentials(trim($token))
9190
));
9291
}
9392

@@ -97,7 +96,7 @@ public function initData(): void
9796
$value = $this->plugin->getSetting($this->contextId, $setting);
9897
$this->setData(
9998
$setting,
100-
$setting === 'password' && $value ? Crypt::decrypt($value) : $value
99+
$setting === 'token' && $value ? Crypt::decrypt($value) : $value
101100
);
102101
}
103102
}
@@ -116,7 +115,7 @@ public function fetch($request, $template = null, $display = false): string
116115

117116
public function execute(...$functionArgs): void
118117
{
119-
$this->setData('password', Crypt::encrypt($this->getData('password')));
118+
$this->setData('token', Crypt::encrypt(trim($this->getData('token'))));
120119

121120
foreach (self::SETTINGS as $setting) {
122121
$this->plugin->updateSetting($this->contextId, $setting, trim($this->getData($setting)), 'string');
@@ -125,15 +124,15 @@ public function execute(...$functionArgs): void
125124
parent::execute(...$functionArgs);
126125
}
127126

128-
private function validateCredentials(string $email, string $password): bool
127+
private function validateCredentials(string $token): bool
129128
{
130129
$httpConfig = [];
131130
if ($this->getData('customThothApi') && $this->getData('customThothApiUrl')) {
132131
$httpConfig['base_uri'] = trim($this->getData('customThothApiUrl'));
133132
}
134133

135134
try {
136-
(new Client($httpConfig))->login($email, $password);
135+
(new Client($httpConfig))->setToken($token)->me();
137136
return true;
138137
} catch (QueryException) {
139138
return false;

classes/container/providers/ThothRepositoryProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ public function register($container)
4848

4949
$customThothApi = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'customThothApi');
5050
$customThothApiUrl = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'customThothApiUrl');
51-
$email = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'email');
52-
$password = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'password') ?? '';
51+
$token = $pluginSettingsDao->getSetting($contextId, 'ThothPlugin', 'token') ?? '';
5352

5453
return [
5554
'customThothApi' => $customThothApi,
5655
'customThothApiUrl' => $customThothApiUrl,
57-
'email' => $email,
58-
'password' => Crypt::decrypt($password)
56+
'token' => $token ? Crypt::decrypt($token) : ''
5957
];
6058
});
6159

@@ -68,7 +66,7 @@ public function register($container)
6866
}
6967

7068
$client = new Client($httpConfig);
71-
return $client->login($config['email'], $config['password']);
69+
return $client->setToken($config['token']);
7270
});
7371

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

classes/repositories/ThothAccountRepository.php

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

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

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.

docs/README-es.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Este plugin es compatible con las siguientes aplicaciones PKP:
3030

3131
Después de habilitar el plugin, vaya a la configuración del plugin y complete:
3232

33-
- **Correo electrónico** y **Contraseña**: Credenciales de una cuenta de Thoth para conectar con la API.
33+
- **Token de acceso personal**: Un token de acceso personal válido de Thoth para autenticar las solicitudes a la API.
3434
- **API Thoth personalizada**: Marque esta opción para usar una API Thoth personalizada en lugar de la oficial.
3535
- **URL de la API Thoth**: La URL de la API Thoth personalizada (solo requerida cuando la opción de API personalizada está habilitada).
3636

37-
<img src="/docs/images/plugin_settings.png" alt="Formulario de configuración del plugin con campos de correo, contraseña, API personalizada y URL" width="700">
37+
<img src="/docs/images/plugin_settings.png" alt="Formulario de configuración del plugin con token de acceso personal, API personalizada y URL" width="700">
3838

3939
### Registro de Monografías
4040

docs/README-pt_BR.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Este plugin é compatível com as seguintes aplicações PKP:
3030

3131
Após habilitar o plugin, vá nas configurações do plugin e preencha:
3232

33-
- **E-mail** e **Senha**: Credenciais de uma conta do Thoth para conectar com a API.
33+
- **Token de acesso pessoal**: Um token de acesso pessoal válido do Thoth para autenticar as requisições da API.
3434
- **API Thoth personalizada**: Marque esta opção para usar uma API Thoth personalizada em vez da oficial.
3535
- **URL da API Thoth**: A URL da API Thoth personalizada (necessária apenas quando a opção de API personalizada está habilitada).
3636

37-
<img src="/docs/images/plugin_settings.png" alt="Formulário de configuração do plugin com campos de e-mail, senha, API personalizada e URL" width="700">
37+
<img src="/docs/images/plugin_settings.png" alt="Formulário de configuração do plugin com token de acesso pessoal, API personalizada e URL" width="700">
3838

3939
### Registro de Monografias
4040

0 commit comments

Comments
 (0)