*/ class Cas extends AbstractAdapter implements ServiceManagerAwareInterface, EventManagerAwareInterface { /** * @var ServiceManager */ protected $serviceManager; /** * @var EventManager */ protected $eventManager; /** * @var ModuleOptions */ protected $options; /** * * @param AuthEvent $e * @return boolean * @throws UnexpectedValueException * @see ChainableAdapter */ public function authenticate(AuthEvent $e) { $config = $this->getOptions()->getCas(); if (!$config) { return; // NB: l'authentification CAS est désactivée ssi le tableau des options est vide } error_reporting($oldErrorReporting = error_reporting() & ~E_NOTICE); require_once __VENDOR_DIR__ . '/gorg/phpcas/CAS.php'; 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(); } // initialize phpCAS \phpCAS::client($options['version'], $options['hostname'], $options['port'], $options['uri'], true); // 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); // // 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(); $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; } /** * @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; } }