|
7 | 7 | namespace OCA\GDataVaas\Controller; |
8 | 8 |
|
9 | 9 | use OCA\GDataVaas\Service\TagService; |
| 10 | +use OCA\GDataVaas\Service\VerdictService; |
10 | 11 | use OCP\AppFramework\Controller; |
11 | 12 | use OCP\AppFramework\Http\JSONResponse; |
12 | 13 | use OCP\DB\Exception; |
13 | 14 | use OCP\IAppConfig; |
14 | 15 | use OCP\IRequest; |
15 | 16 | use OCP\Mail\IMailer; |
| 17 | +use VaasSdk\Exceptions\VaasAuthenticationException; |
| 18 | +use VaasSdk\Options\VaasOptions; |
| 19 | +use VaasSdk\Vaas; |
| 20 | +use VaasSdk\Verdict; |
16 | 21 |
|
17 | 22 | class SettingsController extends Controller { |
18 | 23 | private IAppConfig $config; |
19 | 24 | private TagService $tagService; |
20 | 25 | private IMailer $mailer; |
| 26 | + private VerdictService $verdictService; |
21 | 27 |
|
22 | 28 | public function __construct( |
23 | 29 | $appName, |
24 | 30 | IRequest $request, |
25 | 31 | IAppConfig $config, |
26 | 32 | TagService $tagService, |
27 | 33 | IMailer $mailer, |
| 34 | + VerdictService $verdictService, |
28 | 35 | ) { |
29 | 36 | parent::__construct($appName, $request); |
30 | 37 | $this->config = $config; |
31 | 38 | $this->tagService = $tagService; |
32 | 39 | $this->mailer = $mailer; |
| 40 | + $this->verdictService = $verdictService; |
33 | 41 | } |
34 | 42 |
|
35 | 43 | public function setconfig( |
@@ -154,4 +162,28 @@ public function setSendMailSummaryOfMaliciousFiles(bool $sendMailSummaryOfMalici |
154 | 162 | $this->config->setValueBool($this->appName, 'notifyAdminEnabled', $sendMailSummaryOfMaliciousFiles); |
155 | 163 | return new JSONResponse(['status' => 'success']); |
156 | 164 | } |
| 165 | + |
| 166 | + public function testSettings(string $tokenEndpoint, string $vaasUrl): JSONResponse { |
| 167 | + try { |
| 168 | + $authenticator = $this->verdictService->getAuthenticator($this->verdictService->authMethod, $tokenEndpoint); |
| 169 | + $options = new VaasOptions(true, true, $vaasUrl); |
| 170 | + $vaas = Vaas::builder() |
| 171 | + ->withAuthenticator($authenticator) |
| 172 | + ->withOptions($options) |
| 173 | + ->build(); |
| 174 | + $verdict = $vaas->forUrlAsync('https://www.gdata.de')->await(); |
| 175 | + if ($verdict->verdict === Verdict::CLEAN) { |
| 176 | + return new JSONResponse(['status' => 'success']); |
| 177 | + } |
| 178 | + return new JSONResponse(['status' => 'error', 'message' => 'Test URL verdict: ' . $verdict->verdict->value]); |
| 179 | + } catch (VaasAuthenticationException $e) { |
| 180 | + return new JSONResponse([ |
| 181 | + 'status' => 'error', |
| 182 | + 'message' => 'Authentication failed. Please also check your login details above and save them before |
| 183 | + taking the test. ' . $e->getMessage() |
| 184 | + ]); |
| 185 | + } catch (\Exception $e) { |
| 186 | + return new JSONResponse(['status' => 'error', 'message' => $e->getMessage()]); |
| 187 | + } |
| 188 | + } |
157 | 189 | } |
0 commit comments