Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
AuthHttp
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkCredentials
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 isValidLogin
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Manages user authentication with Apache's HTTP authentication.
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 * @author    Alberto Cabello <alberto@unex.es>
13 * @copyright 2009-2026 phpMyFAQ Team
14 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
15 * @link      https://www.phpmyfaq.de
16 * @since     2009-03-01
17 */
18
19declare(strict_types=1);
20
21namespace phpMyFAQ\Auth;
22
23use phpMyFAQ\Auth;
24use phpMyFAQ\Configuration;
25use phpMyFAQ\Core\Exception;
26use phpMyFAQ\Enums\AuthenticationSourceType;
27use phpMyFAQ\User;
28use SensitiveParameter;
29use Symfony\Component\HttpFoundation\Request;
30
31/**
32 * Class AuthHttp
33 *
34 * @package phpMyFAQ\Auth
35 */
36class AuthHttp extends Auth implements AuthDriverInterface
37{
38    private readonly Request $request;
39
40    /**
41     * @inheritDoc
42     */
43    public function __construct(Configuration $configuration)
44    {
45        parent::__construct($configuration);
46
47        $this->request = Request::createFromGlobals();
48    }
49
50    /**
51     * @inheritDoc
52     * @throws Exception
53     */
54    public function create(string $login, #[SensitiveParameter] string $password, string $domain = ''): bool
55    {
56        $user = new User($this->configuration);
57        $user->createUser($login);
58
59        $user->setStatus('active');
60        $user->setAuthSource(AuthenticationSourceType::AUTH_HTTP->value);
61        $user->setUserData(['display_name' => $login]);
62
63        return true;
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function update(string $login, #[SensitiveParameter] string $password): bool
70    {
71        return true;
72    }
73
74    /**
75     * @inheritDoc
76     */
77    public function delete($login): bool
78    {
79        return true;
80    }
81
82    /**
83     * @inheritDoc
84     */
85    public function checkCredentials(string $login, #[SensitiveParameter] $password, ?array $optionalData = null): bool
86    {
87        if (
88            $this->request->server->get('PHP_AUTH_USER') === null
89            && (string) $this->request->server->get('PHP_AUTH_PW') !== ''
90        ) {
91            return false;
92        }
93
94        return (
95            hash_equals($login, (string) $this->request->server->get('PHP_AUTH_USER'))
96            && hash_equals($password, (string) $this->request->server->get('PHP_AUTH_PW'))
97        );
98    }
99
100    /**
101     * @inheritDoc
102     */
103    public function isValidLogin($login, ?array $optionalData = null): int
104    {
105        return $this->request->server->get('PHP_AUTH_USER') !== null ? 1 : 0;
106    }
107}