Ldap.php 6.39 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
<?php
namespace UnicaenAuth\Authentication\Adapter;

Bertrand Gauthier's avatar
Bertrand Gauthier committed
4
use UnicaenAuth\Options\ModuleOptions;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
5
6
use Zend\Authentication\Exception\UnexpectedValueException;
use Zend\Authentication\Result as AuthenticationResult;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
7
8
9
10
use Zend\Authentication\Adapter\Ldap as LdapAuthAdapter;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
11
12
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Authentication\Adapter\ChainableAdapter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
16
17
18
19
20
21
22
23

/**
 * LDAP authentication adpater
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
 */
class Ldap extends AbstractAdapter implements ServiceManagerAwareInterface, EventManagerAwareInterface
{
24
25
    const USURPATION_USERNAMES_SEP = '=';
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
26
27
28
29
30
31
    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
32
     * @var EventManager
Bertrand Gauthier's avatar
Bertrand Gauthier committed
33
34
     */
    protected $eventManager;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
35
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
36
    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
37
     * @var LdapAuthAdapter
Bertrand Gauthier's avatar
Bertrand Gauthier committed
38
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
39
    protected $ldapAuthAdapter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
40
41

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
42
     * @var ModuleOptions
Bertrand Gauthier's avatar
Bertrand Gauthier committed
43
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
44
45
    protected $options;
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    /**
     * 
     * @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;
        }

Bertrand Gauthier's avatar
Bertrand Gauthier committed
63
        $username   = $e->getRequest()->getPost()->get('identity');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
64
65
        $credential = $e->getRequest()->getPost()->get('credential');

66
67
68
69
70
71
72
73
74
75
76
        // si 2 logins sont fournis, cela active l'usurpation d'identité (à n'utiliser que pour les tests) :
        // - le format attendu est "loginUsurpateur=loginUsurpé"
        // - le mot de passe attendu est celui du compte usurpateur (loginUsurpateur)
        $usernameUsurpe = null;
        if (strpos($username, self::USURPATION_USERNAMES_SEP) > 0) {
            list($username, $usernameUsurpe) = explode(self::USURPATION_USERNAMES_SEP, $username, 2);
            if (!in_array($username, $this->getOptions()->getUsurpationAllowedUsernames())) {
                $usernameUsurpe = null;
            }
        }
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
77
78
79
80
81
//        // 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.");
//        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
82
83

        // LDAP auth
Bertrand Gauthier's avatar
Bertrand Gauthier committed
84
        $result = $this->getLdapAuthAdapter()->setUsername($username)->setPassword($credential)->authenticate();
85
        $failure = !$result->isValid();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
86
        
87
88
89
90
91
92
93
        // verif existence du login usurpé
        if ($usernameUsurpe) {
            if (!$this->getLdapAuthAdapter()->getLdap()->searchEntries("(supannAliasLogin=$usernameUsurpe)")) {
                $usernameUsurpe = null;
            }
        }
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
94
95
96
97
98
99
100
101
        // Failure!
        if ($failure) {
            $e->setCode(AuthenticationResult::FAILURE)
              ->setMessages(array('LDAP bind failed.'));
            $this->setSatisfied(false);
            return false;
        }
        
102
        $e->setIdentity($usernameUsurpe ?: $username);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
103
104
105
106
107
108
109
110
111
112
113
        $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);
    }

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
114
     * @param ModuleOptions $options
Bertrand Gauthier's avatar
Bertrand Gauthier committed
115
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
116
    public function setOptions(ModuleOptions $options)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
117
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
118
        $this->options = $options;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
119
120
121
    }

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
122
     * @return ModuleOptions
Bertrand Gauthier's avatar
Bertrand Gauthier committed
123
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
124
    public function getOptions()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
125
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
126
127
128
129
130
131
132
        if (!$this->options instanceof ModuleOptions) {
            $options = array_merge(
                    $this->getServiceManager()->get('zfcuser_module_options')->toArray(),
                    $this->getServiceManager()->get('unicaen-auth_module_options')->toArray());
            $this->setOptions(new ModuleOptions($options));
        }
        return $this->options;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
133
    }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
134

Bertrand Gauthier's avatar
Bertrand Gauthier committed
135
    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
136
     * @return \UnicaenApp\Options\ModuleOptions
Bertrand Gauthier's avatar
Bertrand Gauthier committed
137
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
138
    public function getAppModuleOptions()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
139
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
140
        return $this->getServiceManager()->get('unicaen-app_module_options');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
141
142
143
144
145
    }

    /**
     * get ldap connection adapter
     * 
Bertrand Gauthier's avatar
Bertrand Gauthier committed
146
     * @return LdapAuthAdapter
Bertrand Gauthier's avatar
Bertrand Gauthier committed
147
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
148
    public function getLdapAuthAdapter()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
149
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
150
151
152
153
154
155
156
157
        if (null === $this->ldapAuthAdapter) {
            $options = array();
            if (($config = $this->getAppModuleOptions()->getLdap())) {
                foreach ($config['connection'] as $name => $connection) {
                    $options[$name] = $connection['params'];
                }
            }
            $this->ldapAuthAdapter = new LdapAuthAdapter($options); // NB: array(array)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
158
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
159
        return $this->ldapAuthAdapter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
160
161
162
163
164
    }

    /**
     * set ldap connection adapter
     *  
Bertrand Gauthier's avatar
Bertrand Gauthier committed
165
     * @param LdapAuthAdapter $authAdapter
Bertrand Gauthier's avatar
Bertrand Gauthier committed
166
167
     * @return Ldap
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
168
    public function setLdapAuthAdapter(LdapAuthAdapter $authAdapter)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
169
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
170
        $this->ldapAuthAdapter = $authAdapter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
171
172
173
174
        return $this;
    }

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
175
176
177
     * Get service manager
     *
     * @return ServiceManager
Bertrand Gauthier's avatar
Bertrand Gauthier committed
178
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
179
    public function getServiceManager()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
180
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
181
        return $this->serviceManager;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
182
183
184
    }

    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
185
186
187
188
     * Set service manager
     *
     * @param ServiceManager $serviceManager
     * @return Ldap
Bertrand Gauthier's avatar
Bertrand Gauthier committed
189
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
190
    public function setServiceManager(ServiceManager $serviceManager)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
191
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
192
193
        $this->serviceManager = $serviceManager;
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
194
    }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
195
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
196
    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
197
198
199
     * Retrieve EventManager instance
     *
     * @return EventManagerInterface
Bertrand Gauthier's avatar
Bertrand Gauthier committed
200
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
201
    public function getEventManager()
Bertrand Gauthier's avatar
Bertrand Gauthier committed
202
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
203
        return $this->eventManager;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
204
    }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
205
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
206
    /**
Bertrand Gauthier's avatar
Bertrand Gauthier committed
207
208
209
210
     * Inject an EventManager instance
     *
     * @param  EventManagerInterface $eventManager
     * @return Ldap
Bertrand Gauthier's avatar
Bertrand Gauthier committed
211
     */
Bertrand Gauthier's avatar
Bertrand Gauthier committed
212
    public function setEventManager(EventManagerInterface $eventManager)
Bertrand Gauthier's avatar
Bertrand Gauthier committed
213
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
214
215
        $this->eventManager = $eventManager;
        return $this;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
216
217
    }
}