22
33namespace Gdatacyberdefenseag \GdataAntivirus \Vaas ;
44
5+ use Exception ;
56use Gdatacyberdefenseag \GdataAntivirus \Infrastructure \FileSystem \IGdataAntivirusFileSystem ;
67use Gdatacyberdefenseag \GdataAntivirus \PluginPage \AdminNoticesInterface ;
78use 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 )
0 commit comments