|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use OCP\AppFramework\Controller; |
|
13
|
|
|
use OCP\AppFramework\Http\Attribute\ApiRoute; |
|
14
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
|
15
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
16
|
|
|
use OCP\IAppConfig; |
|
17
|
|
|
use OCP\IRequest; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
|
|
20
|
|
|
class WhatsAppWebhookController extends Controller { |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
IRequest $request, |
|
23
|
|
|
private IAppConfig $appConfig, |
|
24
|
|
|
private LoggerInterface $logger, |
|
25
|
|
|
) { |
|
26
|
|
|
parent::__construct('twofactor_gateway', $request); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Verify webhook (Facebook sends GET request during setup) |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $hub_mode |
|
33
|
|
|
* @param string $hub_challenge |
|
34
|
|
|
* @param string $hub_verify_token |
|
35
|
|
|
* @return DataResponse |
|
36
|
|
|
*/ |
|
37
|
|
|
#[ApiRoute(verb: 'GET', url: '/api/v1/webhooks/whatsapp')] |
|
38
|
|
|
#[NoAdminRequired] |
|
39
|
|
|
public function verify( |
|
40
|
|
|
string $hub_mode = '', |
|
41
|
|
|
string $hub_challenge = '', |
|
42
|
|
|
string $hub_verify_token = '', |
|
43
|
|
|
): DataResponse { |
|
44
|
|
|
try { |
|
45
|
|
|
// Get stored verification token |
|
46
|
|
|
$storedToken = $this->appConfig->getValueString('twofactor_gateway', 'whatsapp_cloud_verify_token', ''); |
|
47
|
|
|
|
|
48
|
|
|
// Verify the mode and token |
|
49
|
|
|
if ($hub_mode === 'subscribe' && $hub_verify_token === $storedToken) { |
|
50
|
|
|
$this->logger->info('WhatsApp webhook verified successfully'); |
|
51
|
|
|
return new DataResponse($hub_challenge, 200, [ |
|
52
|
|
|
'Content-Type' => 'text/plain', |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->logger->warning('Invalid webhook verification token'); |
|
57
|
|
|
return new DataResponse(['error' => 'Invalid verification token'], 403); |
|
58
|
|
|
} catch (\Exception $e) { |
|
59
|
|
|
$this->logger->error('Error verifying webhook', ['exception' => $e]); |
|
60
|
|
|
return new DataResponse(['error' => 'Error verifying webhook'], 500); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Handle incoming webhook messages (Facebook sends POST requests) |
|
66
|
|
|
* |
|
67
|
|
|
* @return DataResponse |
|
68
|
|
|
*/ |
|
69
|
|
|
#[ApiRoute(verb: 'POST', url: '/api/v1/webhooks/whatsapp')] |
|
70
|
|
|
#[NoAdminRequired] |
|
71
|
|
|
public function webhook(): DataResponse { |
|
72
|
|
|
try { |
|
73
|
|
|
$body = $this->request->getParams(); |
|
74
|
|
|
|
|
75
|
|
|
// Log the webhook payload |
|
76
|
|
|
$this->logger->debug('WhatsApp webhook received', ['payload' => $body]); |
|
77
|
|
|
|
|
78
|
|
|
// TODO: Process incoming messages from WhatsApp |
|
79
|
|
|
// This is where you would handle message status updates, incoming messages, etc. |
|
80
|
|
|
|
|
81
|
|
|
// Facebook requires a 200 response quickly |
|
82
|
|
|
return new DataResponse(['success' => true], 200); |
|
83
|
|
|
} catch (\Exception $e) { |
|
84
|
|
|
$this->logger->error('Error processing webhook', ['exception' => $e]); |
|
85
|
|
|
return new DataResponse(['success' => false], 500); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|