1: <?php
2: /**
3: * The Horde_Auth_Peclsasl:: class provides a SASL-based implementation of the
4: * Horde authentication system.
5: *
6: * SASL is the Simple Authentication and Security Layer (as defined by RFC
7: * 2222). It provides a system for adding plugable authenticating support to
8: * connection-based protocols.
9: *
10: * This driver relies on the PECL sasl package:
11: * http://pecl.php.net/package/sasl
12: *
13: * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
14: *
15: * See the enclosed file COPYING for license information (LGPL). If you did
16: * not receive this file, http://www.horde.org/licenses/lgpl21
17: *
18: * @author Jon Parise <jon@horde.org>
19: * @category Horde
20: * @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
21: * @package Auth
22: */
23: class Horde_Auth_Peclsasl extends Horde_Auth_Base
24: {
25: /**
26: * Constructor.
27: *
28: * @param array $params Optional parameters:
29: * <pre>
30: * 'app' - (string) The name of the authenticating application.
31: * DEFAULT: horde
32: * 'service' - (string) The name of the SASL service to use when
33: * authenticating.
34: * DEFAULT: php
35: * </pre>
36: *
37: * @throws Horde_Auth_Exception
38: */
39: public function __construct(array $params = array())
40: {
41: if (!Horde_Util::extensionExists('sasl')) {
42: throw new Horde_Auth_Exception('Horde_Auth_Peclsasl:: requires the sasl PECL extension to be loaded.');
43: }
44:
45: $params = array_merge(array(
46: 'app' => 'horde',
47: 'service' => 'php'
48: ), $params);
49:
50: parent::__construct($params);
51:
52: sasl_server_init($this->_params['app']);
53: }
54:
55: /**
56: * Find out if a set of login credentials are valid.
57: *
58: * @param string $userId The userId to check.
59: * @param array $credentials An array of login credentials.
60: *
61: * @throws Horde_Auth_Exception
62: */
63: protected function _authenticate($userId, $credentials)
64: {
65: if (empty($credentials['password'])) {
66: throw new Horde_Auth_Exception('', Horde_Auth::REASON_BADLOGIN);
67: }
68:
69: $conn = sasl_server_new($this->_params['service']);
70: if (!is_resource($conn)) {
71: throw new Horde_Auth_Exception('Failed to create new SASL connection.');
72: }
73:
74: if (!sasl_checkpass($conn, $userId, $credentials['password'])) {
75: throw new Horde_Auth_Exception(sasl_errdetail($conn));
76: }
77: }
78:
79: }
80: