Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
18.18% covered (danger)
18.18%
4 / 22
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserRepository
18.18% covered (danger)
18.18%
4 / 22
0.00% covered (danger)
0.00%
0 / 1
43.05
0.00% covered (danger)
0.00%
0 / 1
 getUserEntityByUserCredentials
18.18% covered (danger)
18.18%
4 / 22
0.00% covered (danger)
0.00%
0 / 1
43.05
1<?php
2
3/**
4 * OAuth2 user repository.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-02-09
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Auth\OAuth2\Repository;
21
22use League\OAuth2\Server\Entities\ClientEntityInterface;
23use League\OAuth2\Server\Entities\UserEntityInterface;
24use League\OAuth2\Server\Repositories\UserRepositoryInterface;
25use phpMyFAQ\Auth\AuthDatabase;
26use phpMyFAQ\Auth\OAuth2\Entity\UserEntity;
27
28final class UserRepository extends AbstractRepository implements UserRepositoryInterface
29{
30    public function getUserEntityByUserCredentials(
31        string $username,
32        #[\SensitiveParameter]
33        string $password,
34        string $grantType,
35        ClientEntityInterface $clientEntity,
36    ): ?UserEntityInterface {
37        try {
38            $authDatabase = new AuthDatabase($this->configuration);
39            if (!$authDatabase->checkCredentials($username, $password)) {
40                return null;
41            }
42        } catch (\Throwable) {
43            return null;
44        }
45
46        $query = sprintf(
47            "SELECT user_id FROM %s WHERE login = '%s'",
48            $this->table('faquser'),
49            $this->db()->escape($username),
50        );
51
52        $result = $this->db()->query($query);
53        if ($result === false) {
54            return null;
55        }
56
57        $row = $this->db()->fetchObject($result);
58        if (!is_object($row) || !property_exists($row, 'user_id') || $row->user_id === null) {
59            return null;
60        }
61
62        $userIdentifier = (string) $row->user_id;
63        if ($userIdentifier === '') {
64            return null;
65        }
66
67        $user = new UserEntity();
68        $user->setIdentifier($userIdentifier);
69
70        return $user;
71    }
72}