Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
DecryptEmailTokenEndpoint
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
5.01
0.00% covered (danger)
0.00%
0 / 1
 handle
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
5.01
1<?php
2
3namespace Olz\Captcha\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use PhpTypeScriptApi\HttpError;
7
8/**
9 * @phpstan-type OlzEmailInfoData  array{
10 *   email: array<non-empty-string>,
11 *   text: non-empty-string,
12 *   subject?: ?non-empty-string,
13 * }
14 *
15 * @extends OlzTypedEndpoint<
16 *   array{
17 *     emailToken: non-empty-string,
18 *     captchaToken?: ?non-empty-string,
19 *   },
20 *   OlzEmailInfoData,
21 * >
22 */
23class DecryptEmailTokenEndpoint extends OlzTypedEndpoint {
24    protected function handle(mixed $input): mixed {
25        $has_access = $this->authUtils()->hasPermission('any');
26        $token = $input['captchaToken'] ?? null;
27        $is_valid_token = $token ? $this->captchaUtils()->validateToken($token) : false;
28        if (!$has_access && !$is_valid_token) {
29            throw new HttpError(403, 'Bot-Prüfung nicht bestanden!');
30        }
31
32        $key = $this->envUtils()->getEncryptionKey('email-captcha');
33        $decrypted = $this->generalUtils()->decrypt($key, $input['emailToken']);
34        $email = $decrypted['email'] ?? null;
35        if (!$email) {
36            throw new HttpError(400, 'Email token invalid');
37        }
38        return [
39            'email' => $this->emailUtils()->obfuscateEmail($email) ?? [],
40            'text' => $decrypted['text'] ?? 'E-Mail',
41            'subject' => $decrypted['subject'] ?? null,
42        ];
43    }
44}