1<?php
2/**
3 * Copyright 1999-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you did
6 * not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @author   Chuck Hagenbuch <chuck@horde.org>
9 * @category Horde
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL-2.1
11 * @package  Auth
12 */
13
14/**
15 * The Horde_Auth_Auto class transparently logs users in to Horde using ONE
16 * username, either defined in the config or defaulting to 'horde_user'.
17 *
18 * This is only for use in testing or behind a firewall; it should NOT be used
19 * on a public, production machine.
20 *
21 * @author    Chuck Hagenbuch <chuck@horde.org>
22 * @category  Horde
23 * @copyright 1999-2017 Horde LLC
24 * @license   http://www.horde.org/licenses/lgpl21 LGPL-2.1
25 * @package   Auth
26 */
27class Horde_Auth_Auto extends Horde_Auth_Base
28{
29    /**
30     * An array of capabilities, so that the driver can report which
31     * operations it supports and which it doesn't.
32     *
33     * @var array
34     */
35    protected $_capabilities = array(
36        'transparent' => true
37    );
38
39    /**
40     * Constructor.
41     *
42     * @param array $params  Optional parameters:
43     * <pre>
44     * 'password' - (string) The password to record in the user's credentials.
45     *              DEFAULT: none
46     * 'requestuser' - (boolean) If true, allow username to be passed by GET,
47     *                 POST or cookie.
48     *                 DEFAULT: No
49     * 'username' - (string) The username to authenticate everyone as.
50     *              DEFAULT: 'horde_user'
51     * </pre>
52     */
53    public function __construct(array $params = array())
54    {
55        $params = array_merge(array(
56            'password' => '',
57            'requestuser' => false,
58            'username' => 'horde_user'
59        ), $params);
60
61        parent::__construct($params);
62    }
63
64    /**
65     * Not implemented.
66     *
67     * @param string $userId      The userID to check.
68     * @param array $credentials  An array of login credentials.
69     *
70     * @throws Horde_Auth_Exception
71     */
72    protected function _authenticate($userId, $credentials)
73    {
74        throw new Horde_Auth_Exception('Unsupported.');
75    }
76
77    /**
78     * Automatic authentication.
79     *
80     * @return boolean  Whether or not the client is allowed.
81     */
82    public function transparent()
83    {
84        $this->_credentials['userId'] = (!empty($this->_params['requestuser']) && isset($_REQUEST['username']))
85            ? $_REQUEST['username']
86            : $this->_params['username'];
87        $this->_credentials['credentials'] = array(
88            'password' => isset($this->_params['password']) ? $this->_params['password'] : null
89        );
90
91        return true;
92    }
93
94}
95