Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PasswordAccess
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 getConfig
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 authenticate
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Password Access authentication class
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2019.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Authentication
25 * @author   Chris Hallberg <challber@villanova.edu>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org Main Page
29 */
30
31namespace VuFind\Auth;
32
33use VuFind\Db\Entity\UserEntityInterface;
34use VuFind\Exception\Auth as AuthException;
35
36use function in_array;
37
38/**
39 * Password Access authentication class
40 *
41 * @category VuFind
42 * @package  Authentication
43 * @author   Chris Hallberg <challber@villanova.edu>
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org Main Page
47 */
48class PasswordAccess extends AbstractBase
49{
50    /**
51     * Get configuration (load automatically if not previously set). Throw an
52     * exception if the configuration is invalid.
53     *
54     * @throws AuthException
55     * @return \Laminas\Config\Config
56     */
57    public function getConfig()
58    {
59        // Validate configuration if not already validated:
60        if (!$this->configValidated) {
61            $this->validateConfig();
62            $this->configValidated = true;
63        }
64
65        return $this->config;
66    }
67
68    /**
69     * Attempt to authenticate the current user. Throws exception if login fails.
70     *
71     * @param \Laminas\Http\PhpEnvironment\Request $request Request object containing
72     * account credentials.
73     *
74     * @throws AuthException
75     * @return UserEntityInterface Object representing logged-in user.
76     */
77    public function authenticate($request)
78    {
79        $config = $this->getConfig()->toArray();
80        $req_password = trim($request->getPost()->get('password', ''));
81        $accessConfig = $config['PasswordAccess']['access_user'] ?? [];
82        if (!in_array($req_password, $accessConfig)) {
83            throw new AuthException('authentication_error_invalid');
84        }
85
86        $userMap = array_flip($accessConfig);
87        return $this->getOrCreateUserByUsername($userMap[$req_password]);
88    }
89}