Cas.php 4.62 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
54
        error_reporting($oldErrorReporting = error_reporting() & ~E_NOTICE);

        require_once __VENDOR_DIR__ . '/gorg/phpcas/CAS.php';
Bertrand Gauthier's avatar
Bertrand Gauthier committed
55
56
57
58
59
        
        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'];
Bertrand Gauthier's avatar
Bertrand Gauthier committed
60

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

        // initialize phpCAS
Bertrand Gauthier's avatar
Bertrand Gauthier committed
66
        \phpCAS::client($options['version'], $options['hostname'], $options['port'], $options['uri'], true);
Bertrand Gauthier's avatar
Bertrand Gauthier committed
67
68
69
70
71
72
73
74
75
76
77
        // no SSL validation for the CAS server
        \phpCAS::setNoCasServerValidation();
        \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
78
79
80
81
//        // 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        
        $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;
    }

Bertrand Gauthier's avatar
Bertrand Gauthier committed
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
    /**
     * @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
163
}