• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

avoutic / web-framework / 45418632783

24 Nov 2026 02:47PM UTC coverage: 69.305% (-4.3%) from 73.637%
45418632783

push

github

avoutic
Mark the config_values table as deprecated

2872 of 4144 relevant lines covered (69.31%)

2.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.23
/src/Security/UserVerificationService.php
1
<?php
2

3
/*
4
 * This file is part of WebFramework.
5
 *
6
 * (c) Avoutic <avoutic@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WebFramework\Security;
13

14
use Carbon\Carbon;
15
use Psr\Log\LoggerInterface;
16
use Slim\Http\ServerRequest as Request;
17
use WebFramework\Entity\User;
18
use WebFramework\Event\EventService;
19
use WebFramework\Event\UserVerified;
20
use WebFramework\Exception\CodeVerificationException;
21
use WebFramework\Mail\UserMailer;
22
use WebFramework\Repository\UserRepository;
23
use WebFramework\Repository\VerificationCodeRepository;
24
use WebFramework\Support\StoredUserValuesService;
25

26
/**
27
 * Handles user verification processes.
28
 */
29
class UserVerificationService
30
{
31
    /**
32
     * Stored user value that holds the registration after verify data until first verification.
33
     */
34
    public const AFTER_VERIFY_DATA_KEY = 'registration.after_verify_data';
35

36
    /**
37
     * UserVerificationService constructor.
38
     *
39
     * @param EventService               $eventService               The event service
40
     * @param LoggerInterface            $logger                     The logger service
41
     * @param StoredUserValuesService    $storedUserValuesService    The stored user values service
42
     * @param UserCodeService            $userCodeService            The user code service
43
     * @param UserMailer                 $userMailer                 The user mailer service
44
     * @param UserRepository             $userRepository             The user repository
45
     * @param VerificationCodeRepository $verificationCodeRepository The verification code repository
46
     * @param int                        $codeExpiryMinutes          The number of minutes until verification codes expire
47
     */
48
    public function __construct(
×
49
        private EventService $eventService,
50
        private LoggerInterface $logger,
51
        private StoredUserValuesService $storedUserValuesService,
52
        private UserCodeService $userCodeService,
53
        private UserMailer $userMailer,
54
        private UserRepository $userRepository,
55
        private VerificationCodeRepository $verificationCodeRepository,
56
        private int $codeExpiryMinutes,
57
    ) {}
×
58

59
    /**
60
     * Send a verification email to a user and return the GUID for passing to the verify action.
61
     *
62
     * @param User         $user            The user to send the verification email to
63
     * @param string       $action          The action to send the verification email for
64
     * @param array<mixed> $afterVerifyData Additional data to include after verification
65
     *
66
     * @return string The GUID for passing to the verify action
67
     */
68
    public function sendVerifyMail(User $user, string $action, array $afterVerifyData = []): string
1 ✔
69
    {
70
        $this->logger->debug('Sending verification mail', ['user_id' => $user->getId()]);
1 ✔
71

72
        ['guid' => $guid, 'code' => $code] = $this->userCodeService->generateVerificationCodeEntry(
1 ✔
73
            $user,
1 ✔
74
            $action,
1 ✔
75
            [
1 ✔
76
                'after_verify_data' => $afterVerifyData,
1 ✔
77
            ]
1 ✔
78
        );
1 ✔
79

80
        $this->userMailer->emailVerificationCode(
1 ✔
81
            $user->getEmail(),
1 ✔
82
            [
1 ✔
83
                'user' => $user->toArray(),
1 ✔
84
                'code' => $code,
1 ✔
85
                'validity' => $this->codeExpiryMinutes,
1 ✔
86
            ]
1 ✔
87
        );
1 ✔
88

89
        return $guid;
1 ✔
90
    }
91

92
    /**
93
     * Handle the data for a verification request.
94
     *
95
     * @param Request $request The current request
96
     * @param string  $guid    The GUID of the verification code
97
     * @param string  $action  The action to handle the verification data for
98
     *
99
     * @return array{user: User, after_verify_data: array<mixed>, first_verification: bool, post_verify_data: array<mixed>} The user,
100
     *                                                                                                                      the after verify data of this code, whether this call verified the user for the first
101
     *                                                                                                                      time, and the registration after verify data (only filled on first verification)
102
     *
103
     * @throws CodeVerificationException If the code is invalid
104
     */
105
    public function handleData(Request $request, string $guid, string $action): array
14 ✔
106
    {
107
        $this->logger->debug('Handling verification data', ['guid' => $guid]);
14 ✔
108

109
        $verificationCode = $this->userCodeService->getRedeemableCode($guid, $action);
14 ✔
110

111
        $flowData = $verificationCode->getFlowData();
8 ✔
112
        $user = $this->userRepository->find($verificationCode->getUserId());
8 ✔
113

114
        if ($user === null)
8 ✔
115
        {
116
            throw new CodeVerificationException();
1 ✔
117
        }
118

119
        $firstVerification = !$user->isVerified();
7 ✔
120
        $postVerifyData = [];
7 ✔
121

122
        if ($firstVerification)
7 ✔
123
        {
124
            $postVerifyData = $this->takeRegistrationData($user, $action, $flowData['after_verify_data'] ?? []);
5 ✔
125

126
            $this->logger->info('Setting user to verified', ['user_id' => $user->getId()]);
5 ✔
127

128
            $user->setVerified(Carbon::now()->getTimestamp());
5 ✔
129
            $this->userRepository->save($user);
5 ✔
130

131
            $this->eventService->dispatch(new UserVerified($request, $user));
5 ✔
132
        }
133
        else
134
        {
135
            $this->logger->info('Updating user verified at', ['user_id' => $user->getId()]);
2 ✔
136

137
            $user->setVerifiedAt(Carbon::now()->getTimestamp());
2 ✔
138
            $this->userRepository->save($user);
2 ✔
139
        }
140

141
        // Mark as processed to prevent replay attacks
142
        $verificationCode->markAsProcessed();
7 ✔
143
        $this->verificationCodeRepository->save($verificationCode);
7 ✔
144

145
        return [
7 ✔
146
            'user' => $user,
7 ✔
147
            'after_verify_data' => $flowData['after_verify_data'] ?? [],
7 ✔
148
            'first_verification' => $firstVerification,
7 ✔
149
            'post_verify_data' => $postVerifyData,
7 ✔
150
        ];
7 ✔
151
    }
152

153
    /**
154
     * Get the registration after verify data for a user and remove it from storage.
155
     *
156
     * The data is stored with the user at registration. Users registered before that was
157
     * the case only have it in their register code, so that is the fallback.
158
     *
159
     * @param User         $user                The user being verified for the first time
160
     * @param string       $action              The action of the verification code
161
     * @param array<mixed> $codeAfterVerifyData The after verify data of the verification code
162
     *
163
     * @return array<mixed> The registration after verify data
164
     */
165
    private function takeRegistrationData(User $user, string $action, array $codeAfterVerifyData): array
5 ✔
166
    {
167
        $fallback = ($action === 'register') ? $codeAfterVerifyData : [];
5 ✔
168

169
        if (!$this->storedUserValuesService->valueExists($user->getId(), self::AFTER_VERIFY_DATA_KEY))
5 ✔
170
        {
171
            return $fallback;
2 ✔
172
        }
173

174
        $data = json_decode($this->storedUserValuesService->getValue($user->getId(), self::AFTER_VERIFY_DATA_KEY), true);
3 ✔
175

176
        $this->storedUserValuesService->deleteValue($user->getId(), self::AFTER_VERIFY_DATA_KEY);
3 ✔
177

178
        return is_array($data) ? $data : $fallback;
3 ✔
179
    }
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc