Skip to content

Commit 19883b2

Browse files
50 error messages for invalid input (#56)
* Show an error message for wrong credentials
1 parent a1fc726 commit 19883b2

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Vaas/ScanClient.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Gdatacyberdefenseag\GdataAntivirus\Vaas;
44

5+
use Exception;
56
use Gdatacyberdefenseag\GdataAntivirus\Infrastructure\FileSystem\IGdataAntivirusFileSystem;
67
use Gdatacyberdefenseag\GdataAntivirus\PluginPage\AdminNoticesInterface;
78
use Gdatacyberdefenseag\GdataAntivirus\PluginPage\OnDemandScan\OnDemandScanOptions;
@@ -58,6 +59,9 @@ public function __construct(
5859
if ($post_scan_enabled === true) {
5960
add_filter('wp_insert_post_data', array($this, 'scan_post'), 10, 1);
6061
}
62+
add_action( 'admin_notices', array($this, 'credentials_check_admin_notice'));
63+
64+
$this->connect();
6165
}
6266

6367
public function reconnect()
@@ -66,10 +70,20 @@ public function reconnect()
6670
$this->connect();
6771
}
6872

69-
public function connect()
73+
public function credentials_check_admin_notice() {
74+
if ($this->vaas_options->credentials_configured()) {
75+
if ($this->connect() === false) {
76+
echo '<div class="notice notice-error is-dismissible">
77+
<p>VaaS - Error: The credentials did not work. Please check the settings.</p>
78+
</div>';
79+
}
80+
}
81+
}
82+
83+
public function connect(): bool
7084
{
7185
if ($this->connected === true) {
72-
return;
86+
return true;
7387
}
7488

7589
$options = $this->vaas_options->get_options();
@@ -79,31 +93,37 @@ public function connect()
7993
$vaas_parameters->useHashLookup = true;
8094
$vaas_parameters->vaasUrl = $options['vaas_url'];
8195
if (! $this->vaas_options->credentials_configured()) {
82-
return;
96+
return false;
8397
}
8498
if ($options['authentication_method'] == 'ResourceOwnerPasswordGrant') {
85-
$resource_owner_password_grant_authenticator = new ResourceOwnerPasswordGrantAuthenticator(
99+
$authenticator = new ResourceOwnerPasswordGrantAuthenticator(
86100
'wordpress-customer',
87101
$options['username'],
88102
$options['password'],
89103
$options['token_endpoint']
90104
);
91105
$this->vaas = Vaas::builder()
92106
->withOptions($vaas_parameters)
93-
->withAuthenticator($resource_owner_password_grant_authenticator)
107+
->withAuthenticator($authenticator)
94108
->build();
95109
} else {
96-
$client_credentials_grant_authenticator = new ClientCredentialsGrantAuthenticator(
110+
$authenticator = new ClientCredentialsGrantAuthenticator(
97111
$options['client_id'],
98112
$options['client_secret'],
99113
$options['token_endpoint']
100114
);
101115
$this->vaas = Vaas::builder()
102116
->withOptions($vaas_parameters)
103-
->withAuthenticator($client_credentials_grant_authenticator)
117+
->withAuthenticator($authenticator)
104118
->build();
105119
}
120+
try {
121+
$authenticator->getTokenAsync()->await();
122+
} catch (Exception $e) {
123+
return false;
124+
}
106125
$this->connected = true;
126+
return true;
107127
}
108128

109129
public function scan_post($data)

info.php

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)