Cas.php 5.48 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
11
12
13
14
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use ZfcUser\Authentication\Adapter\AbstractAdapter;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;
use ZfcUser\Authentication\Adapter\ChainableAdapter;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
15
16
17
18
19
20

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

    /**
     * @var EventManager
     */
    protected $eventManager;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
32

Bertrand Gauthier's avatar
Bertrand Gauthier committed
33
34
35
36
37
    /**
     * @var ModuleOptions
     */
    protected $options;
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
38
39
40
41
42
43
44
45
46
    /**
     * 
     * @param AuthEvent $e
     * @return boolean
     * @throws UnexpectedValueException
     * @see ChainableAdapter
     */
    public function authenticate(AuthEvent $e)
    {
Bertrand Gauthier's avatar
Bertrand Gauthier committed
47
48
49
50
51
        $config = $this->getOptions()->getCas();
        if (!$config) {
            return; // NB: l'authentification CAS est désactivée ssi le tableau des options est vide
        }
        
Bertrand Gauthier's avatar
Bertrand Gauthier committed
52
53
        error_reporting($oldErrorReporting = error_reporting() & ~E_NOTICE);

54
        $this->initCasClient();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
55
56
57
58
59
60
61
62
63
        \phpCAS::forceAuthentication();

        // at this step, the user has been authenticated by the CAS server
        // and the user's login name can be read with phpCAS::getUser().

        $identity = \phpCAS::getUser();

        error_reporting($oldErrorReporting);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
64
65
66
67
//        // nécessaire pour que le "base DN" de l'objet \Zend\Ldap\Ldap soit bien initialisé
//        $this->getLdapAdapter()->setUsername($identity)
//                ->setPassword('xxx') // peu importe mais pas null
//                ->authenticate();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        
        $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;
    }
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
    
    /**
     * 
     * @param AuthEvent $e
     * @see ChainableAdapter
     */
    public function logout(AuthEvent $e)
    {
        if (!$this->getOptions()->getCas()) {
            return; // NB: l'authentification CAS est désactivée ssi le tableau des options est vide
        }
        
        $this->initCasClient();
        if (\phpCAS::isAuthenticated()) {
            $urlHelper = $this->getServiceManager()->get('Router'); /* @var $urlHelper \Zend\Mvc\Router\Http\TreeRouteStack */
            $returnUrl = $urlHelper->getRequestUri()->setPath($urlHelper->getBaseUrl())->toString();
            \phpCAS::logoutWithRedirectService($returnUrl);
        }
    }
    
    /**
     * 
     * @throws \UnicaenApp\Exception
     */
    protected function initCasClient()
    {
        require_once __VENDOR_DIR__ . '/gorg/phpcas/CAS.php';
        
        $config = $this->getOptions()->getCas();
        if (!isset($config['connection']['default']['params'])) {
            throw new \UnicaenApp\Exception("Les paramètres de connexion au serveur CAS sont invalides.");
        }
        $options = $config['connection']['default']['params'];

        if (array_key_exists('debug', $options) && (bool) $options['debug']) {
            \phpCAS::setDebug();
        }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
120

121
122
123
124
125
126
        // initialize phpCAS
        \phpCAS::client($options['version'], $options['hostname'], $options['port'], $options['uri'], true);
        // no SSL validation for the CAS server
        \phpCAS::setNoCasServerValidation();
    }
    
Bertrand Gauthier's avatar
Bertrand Gauthier committed
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
    /**
     * @param ModuleOptions $options
     */
    public function setOptions(ModuleOptions $options)
    {
        $this->options = $options;
    }

    /**
     * @return ModuleOptions
     */
    public function getOptions()
    {
        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;
    }

    /**
     * Get service manager
     *
     * @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;
    }
Bertrand Gauthier's avatar
Bertrand Gauthier committed
192
}