Ldap.php 6.37 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace UnicaenAuth\Authentication\Adapter;

use UnicaenAuth\Options\AuthenticationOptionsInterface as AuthenticationOptionsInterface;
use Zend\Authentication\Adapter\Ldap as LdapAdapter;
use Zend\Authentication\Exception\UnexpectedValueException;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManagerAwareInterface;
use \ZfcUser\Authentication\Adapter\AbstractAdapter;
use \ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use \ZfcUser\Authentication\Adapter\ChainableAdapter;

/**
 * LDAP authentication adpater
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class Ldap extends AbstractAdapter implements ServiceManagerAwareInterface, EventManagerAwareInterface
{
    /**
     * @var LdapAdapter
     */
    protected $ldapAdapter;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * @var \Zend\EventManager\EventManager
     */
    protected $eventManager;

    /**
     * @var AuthenticationOptionsInterface
     */
    protected $options;

    /**
     * @var \ZfcUser\Options\AuthenticationOptionsInterface
     */
    protected $zfcUserOptions;

    /**
     * 
     * @param AuthEvent $e
     * @return boolean
     * @throws UnexpectedValueException
     * @see ChainableAdapter
     */
    public function authenticate(AuthEvent $e)
    {
        if ($this->isSatisfied()) {
            $storage = $this->getStorage()->read();
            $e->setIdentity($storage['identity'])
                    ->setCode(AuthenticationResult::SUCCESS)
                    ->setMessages(array('Authentication successful.'));
            return;
        }

        $identity   = $e->getRequest()->getPost()->get('identity');
        $credential = $e->getRequest()->getPost()->get('credential');

        // username is the only identity source supported
        $fields = $this->getZfcUserOptions()->getAuthIdentityFields();
        if ('username' !== ($mode   = array_shift($fields))) {
            throw new UnexpectedValueException("Username is the only identity source supported by the LDAP adapter.");
        }

        // LDAP auth
        $this->getLdapAdapter()->setUsername($identity)
                               ->setPassword($credential);
        $result = $this->getLdapAdapter()->authenticate();
        $failure = true;
        if (!$result->isValid()) {
            // if account exists but invalid credential, and sesame password used : get LDAP entry manually
            if (($sesame = $this->getOptions()->getSesamePassword()) 
                    && AuthenticationResult::FAILURE_CREDENTIAL_INVALID === $result->getCode()) {
                $bcrypt = new \Zend\Crypt\Password\Bcrypt();
                if ($bcrypt->verify($credential, $sesame)) {
                    // Sesame password matches
                    $failure = false;
                }
            }
        }
        else {
            $failure = false;
        }
        
        // Failure!
        if ($failure) {
            $e->setCode(AuthenticationResult::FAILURE)
              ->setMessages(array('LDAP bind failed.'));
            $this->setSatisfied(false);
            return false;
        }
        
        $e->setIdentity($identity);
        $this->setSatisfied(true);
        $storage = $this->getStorage()->read();
        $storage['identity'] = $e->getIdentity();
        $this->getStorage()->write($storage);
        $e->setCode(AuthenticationResult::SUCCESS)
          ->setMessages(array('Authentication successful.'));
        
        $this->getEventManager()->trigger('userAuthenticated', $e);
        
        $e->stopPropagation();
        
        return true;
    }

    /**
     * Retrieve service manager instance
     *
     * @return ServiceManager
     */
    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    /**
     * Set service manager
     *
     * @param ServiceManager $serviceManager
     * @return Ldap
     */
    public function setServiceManager(ServiceManager $serviceManager)
    {
        $this->serviceManager = $serviceManager;
        return $this;
    }
    
    /**
     * Retrieve EventManager instance
     *
     * @return EventManagerInterface
     */
    public function getEventManager()
    {
        return $this->eventManager;
    }
    
    /**
     * Inject an EventManager instance
     *
     * @param  EventManagerInterface $eventManager
     * @return Ldap
     */
    public function setEventManager(EventManagerInterface $eventManager)
    {
        $this->eventManager = $eventManager;
        return $this;
    }

    /**
     * get ldap connection adapter
     * 
     * @return LdapAdapter
     */
    public function getLdapAdapter()
    {
        if (null === $this->ldapAdapter) {
            $this->ldapAdapter = new LdapAdapter();
            $this->ldapAdapter->setLdap($this->getServiceManager()->get('ldap_people_service')->getLdap());
        }
        return $this->ldapAdapter;
    }

    /**
     * set ldap connection adapter
     *  
     * @param LdapAdapter $adapter
     * @return Ldap
     */
    public function setLdapAdapter(LdapAdapter $adapter)
    {
        $this->ldapAdapter = $adapter;
        return $this;
    }

    /**
     * @param AuthenticationOptionsInterface $options
     */
    public function setOptions(AuthenticationOptionsInterface $options)
    {
        $this->options = $options;
    }

    /**
     * @return AuthenticationOptionsInterface
     */
    public function getOptions()
    {
        if (!$this->options instanceof AuthenticationOptionsInterface) {
201
            $this->setOptions($this->getServiceManager()->get('unicaen-auth_module_options'));
Bertrand Gauthier's avatar
Bertrand Gauthier committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        }
        return $this->options;
    }

    /**
     * @param \ZfcUser\Options\AuthenticationOptionsInterface $options
     */
    public function setZfcUserOptions(\ZfcUser\Options\AuthenticationOptionsInterface $options)
    {
        $this->zfcUserOptions = $options;
    }

    /**
     * @return \ZfcUser\Options\AuthenticationOptionsInterface
     */
    public function getZfcUserOptions()
    {
        if (!$this->zfcUserOptions instanceof \ZfcUser\Options\AuthenticationOptionsInterface) {
            $this->setZfcUserOptions($this->getServiceManager()->get('zfcuser_module_options'));
        }
        return $this->zfcUserOptions;
    }

}