Lines 90.69% 39 / 43
Methods 78.57% 11 / 14
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 2 / 2 100.00% 1 / 1 1
 create 100.00% 6 / 6 100.00% 1 / 1 1
 update 100.00% 1 / 1 100.00% 1 / 1 1
 delete 100.00% 1 / 1 100.00% 1 / 1 1
 checkCredentials 100.00% 7 / 7 100.00% 1 / 1 4
 isValidLogin 100.00% 1 / 1 100.00% 1 / 1 2
 [phpMyFAQ\Auth] getEncryptionContainer 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Auth] getErrors 100.00% 2 / 2 100.00% 1 / 1 3
 [phpMyFAQ\Auth] addError 0.00% 0 / 1 0.00% 0 / 1 2
 [phpMyFAQ\Auth] selectAuth 80.00% 8 / 10 0.00% 0 / 1 4.13
 [phpMyFAQ\Auth] enableReadOnly 100.00% 3 / 3 100.00% 1 / 1 1
 [phpMyFAQ\Auth] disableReadOnly 100.00% 3 / 3 100.00% 1 / 1 1
 [phpMyFAQ\Auth] isReadOnly 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Auth] encrypt 66.66% 2 / 3 0.00% 0 / 1 2.15
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}

Inherited from phpMyFAQ\Auth

73    public function getEncryptionContainer(string $encType): Encryption
74    {
75        $this->encContainer = Encryption::getInstance($encType, $this->configuration);
76        return $this->encContainer;
77    }
82    public function getErrors(): string
83    {
84        $message = $this->errors !== [] ? implode(separator: PHP_EOL, array: $this->errors) . PHP_EOL : '';
85        return $message . ($this->encContainer instanceof \phpMyFAQ\Encryption ? $this->encContainer->error() : '');
86    }
91    public function addError(string $message): void
92    {
93        $this->errors[] = $message;
94    }
101    public function selectAuth(string $method): Auth&AuthDriverInterface
102    {
103        $method = ucfirst(strtolower($method));
104        $authClass = '\\phpMyFAQ\\Auth\\Auth' . $method;
105
106        if (!class_exists($authClass) || !is_subclass_of($authClass, self::class)) {
107            $this->errors[] = self::PMF_ERROR_USER_NO_AUTH_TYPE;
108            throw new Exception(message: self::PMF_ERROR_USER_NO_AUTH_TYPE);
109        }
110
111        $auth = new $authClass($this->configuration);
112        if (!$auth instanceof AuthDriverInterface) {
113            $this->errors[] = self::PMF_ERROR_USER_NO_AUTH_TYPE;
114            throw new Exception(message: self::PMF_ERROR_USER_NO_AUTH_TYPE);
115        }
116
117        return $auth;
118    }
123    public function enableReadOnly(): bool
124    {
125        $oldReadOnly = $this->readOnly;
126        $this->readOnly = true;
127
128        return $oldReadOnly;
129    }
134    public function disableReadOnly(): bool
135    {
136        $oldReadOnly = $this->readOnly;
137        $this->readOnly = false;
138
139        return $oldReadOnly;
140    }
145    public function isReadOnly(): bool
146    {
147        return $this->readOnly;
148    }
155    public function encrypt(#[SensitiveParameter] string $string): string
156    {
157        if (!$this->encContainer instanceof \phpMyFAQ\Encryption) {
158            throw new Exception(message: 'No encryption container configured. Call getEncryptionContainer() first.');
159        }
160
161        return $this->encContainer->encrypt($string);
162    }