Commit 32ed5e04 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Merge branch 'usurpation'

parents 30760bf8 35f8127d
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
<?php

use UnicaenAuth\Authentication\Adapter\ShibSimulatorAdapter;
use UnicaenAuth\Authentication\Storage\ShibSimulatorStorage;
use UnicaenAuth\Controller\AuthControllerFactory;
use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\ShibServiceFactory;
@@ -374,6 +376,7 @@ return [
            'UnicaenAuth\Authentication\Storage\Db'   => 'UnicaenAuth\Authentication\Storage\Db',
            'UnicaenAuth\Authentication\Storage\Ldap' => 'UnicaenAuth\Authentication\Storage\Ldap',
            'UnicaenAuth\Authentication\Storage\Shib' => 'UnicaenAuth\Authentication\Storage\Shib',
            'UnicaenAuth\Authentication\Storage\ShibSimulatorStorage' => ShibSimulatorStorage::class,
            'UnicaenAuth\View\RedirectionStrategy'    => 'UnicaenAuth\View\RedirectionStrategy',
            'UnicaenAuth\Service\User'                => 'UnicaenAuth\Service\User',
            'UnicaenAuth\Service\CategoriePrivilege'  => 'UnicaenAuth\Service\CategoriePrivilegeService',
+0 −33
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace UnicaenAuth\Authentication\Storage;

use UnicaenAuth\Entity\Shibboleth\ShibUser;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\ShibService;
use Zend\Authentication\Storage\Session;
use Zend\Authentication\Storage\StorageInterface;
@@ -25,11 +24,6 @@ class Shib implements ChainableStorage, ServiceLocatorAwareInterface
     */
    protected $storage;

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

    /**
     * @var ShibUser
     */
@@ -63,10 +57,6 @@ class Shib implements ChainableStorage, ServiceLocatorAwareInterface
     */
    private function getAuthenticatedUser()
    {
        if (! $this->isShibbolethEnabled()) {
            return null;
        }

        if (null !== $this->resolvedIdentity) {
            return $this->resolvedIdentity;
        }
@@ -79,29 +69,6 @@ class Shib implements ChainableStorage, ServiceLocatorAwareInterface
        return $this->resolvedIdentity;
    }

    /**
     * @return bool
     */
    private function isShibbolethEnabled()
    {
        $options = $this->getModuleOptions();
        $shibboleth = $options->getShibboleth();

        return isset($shibboleth['enable']) && (bool) $shibboleth['enable'];
    }

    /**
     * @return ModuleOptions
     */
    private function getModuleOptions()
    {
        if (null === $this->options) {
            $this->options = $this->getServiceLocator()->get('unicaen-auth_module_options');
        }

        return $this->options;
    }

    /**
     * Writes $contents to storage
     *
+70 −0
Original line number Diff line number Diff line
<?php

namespace UnicaenAuth\Authentication\Storage;

use UnicaenAuth\Service\ShibService;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

/**
 * Storage permettant de simuler un utilisateur authentifié via Shibboleth.
 *
 * @author Unicaen
 */
class ShibSimulatorStorage implements ChainableStorage, ServiceLocatorAwareInterface
{
    use ServiceLocatorAwareTrait;

    /**
     * @var ShibService
     */
    protected $shibService;

    /**
     * @var array
     */
    protected $shibbolethOptions;

    /**
     * {@inheritdoc}
     */
    public function read(ChainEvent $e)
    {
        if (! $this->getShibService()->isShibbolethEnabled()) {
            return;
        }
        if (! $this->getShibService()->isSimulationActive()) {
            return;
        }

        $this->getShibService()->handleSimulation();
    }

    /**
     * {@inheritdoc}
     */
    public function write(ChainEvent $e)
    {
        // nop
    }

    /**
     * {@inheritdoc}
     */
    public function clear(ChainEvent $e)
    {
        // nop
    }

    /**
     * @return ShibService
     */
    private function getShibService()
    {
        if ($this->shibService === null) {
            $this->shibService = $this->serviceLocator->get(ShibService::class);
        }

        return $this->shibService;
    }
}
 No newline at end of file
+60 −34
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@
namespace UnicaenAuth\Controller;

use UnicaenApp\Exception\RuntimeException;
use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\Traits\ShibServiceAwareTrait;
use UnicaenAuth\Service\Traits\UserServiceAwareTrait;
use Zend\Authentication\AuthenticationService;
@@ -24,15 +23,17 @@ class AuthController extends AbstractActionController
    use UserServiceAwareTrait;

    /**
     * Cette action n'est exécutée qu'une fois l'authentification Shibboleth réalisée avec succès.
     * Cette action peut être appelée lorsque l'authentification Shibboleth est activée
     * (unicaen-auth.shibboleth.enable === true).
     *
     * Lorsque l'authentification Shibboleth est activée (unicaen-auth.shibboleth.enable === true),
     * et que la config Apache est correcte, une requête à l'adresse correspondant à cette action
     * (suite au clic sur le bouton "Authentification Shibboleth, typiquement)
     * est détournée pour réaliser l'authentification.
     * Ce n'est qu'une fois l'authentification réalisée avec succès que cette action entre en jeu.
     * > Si la config Apache de Shibboleth est correcte, une requête à l'adresse correspondant à cette action
     * (suite au clic sur le bouton "Authentification Shibboleth", typiquement)
     * est détournée par Apache pour réaliser l'authentification Shibboleth.
     * Ce n'est qu'une fois l'authentification réalisée avec succès que cette action est appelée.
     *
     * @see ShibService::apacheConfigSnippet()
     * > Si la config Apache de Shibboleth est incorrecte ou absente (localhost par exemple), et que la simulation
     * Shibboleth est activée dans la config (unicaen-auth.shibboleth.simulate), cette action est appelée et
     * la simulation est enclenchée.
     *
     * @return Response|array
     */
@@ -41,6 +42,38 @@ class AuthController extends AbstractActionController
        $operation = $this->params()->fromRoute('operation');

        if ($operation === 'deconnexion') {
            return $this->shibbolethLogout();
        }

        $redirectUrl = $this->params()->fromQuery('redirect', '/');

        // enclenchement de la simulation shibboleth éventuellement activée dans la config
        if ($simulate = $this->shibService->getShibbolethSimulate()) {
            $this->setStoredAuthenticatedUsername($simulate['eppn']); // tout simplement!

            return $this->redirect()->toUrl($redirectUrl);
        }

        $shibUser = $this->shibService->getAuthenticatedUser();
        if ($shibUser === null) {
            return []; // une page d'aide s'affichera si les données issues de Shibboleth attendues sont absentes
        }

        // arrivé ici, l'authentification shibboleth a été faite en bonne et due forme et a réussie.

        $this->setStoredAuthenticatedUsername($shibUser->getUsername());
        $this->userService->userAuthenticated($shibUser);

        return $this->redirect()->toUrl($redirectUrl);
    }

    /**
     * Déconnexion Shibboleth.
     *
     * @return array|Response
     */
    private function shibbolethLogout()
    {
        // déconnexion applicative quoiqu'il arrive
        $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
        $this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
@@ -61,24 +94,17 @@ class AuthController extends AbstractActionController
        }
    }

        $shibUser = $this->shibService->getAuthenticatedUser();

        if ($shibUser === null) {
            return []; // une page d'aide s'affichera
        }

    /**
     * @param string $username
     */
    private function setStoredAuthenticatedUsername($username)
    {
        /** @var AuthenticationService $authService */
        $authService = $this->getServiceLocator()->get('zfcuser_auth_service');
        try {
            $authService->getStorage()->write($shibUser->getUsername());
            $authService->getStorage()->write($username);
        } catch (ExceptionInterface $e) {
            throw new RuntimeException("Impossible d'écrire dans le storage");
        }

        $this->userService->userAuthenticated($shibUser);

        $redirectUrl = $this->params()->fromQuery('redirect', '/');

        return $this->redirect()->toUrl($redirectUrl);
    }
}
 No newline at end of file
+133 −2
Original line number Diff line number Diff line
@@ -2,14 +2,30 @@

namespace UnicaenAuth\Controller;

use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Mapper\Ldap\People as LdapPeopleMapper;
use UnicaenAuth\Entity\Db\AbstractUser;
use UnicaenAuth\Entity\Ldap\People;
use UnicaenAuth\Entity\Shibboleth\ShibUser;
use UnicaenAuth\Options\ModuleOptions;
use UnicaenAuth\Service\ShibService;
use UnicaenAuth\Service\UserContext;
use Zend\Authentication\AuthenticationService;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use ZfcUser\Mapper\User as UserMapper;

/**
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UtilisateurController extends AbstractActionController
{
    /**
     * @var LdapPeopleMapper
     */
    protected $ldapPeopleMapper;

    /**
     * Traite les requêtes AJAX POST de sélection d'un profil utilisateur.
     * La sélection est mémorisé en session par le service AuthUserContext.
@@ -45,10 +61,125 @@ class UtilisateurController extends AbstractActionController
    }

    /**
     * @return \UnicaenAuth\Service\UserContext
     * Usurpe l'identité d'un autre utilisateur.
     *
     * @return Response
     */
    public function usurperIdentiteAction()
    {
        $request = $this->getRequest();
        if (! $request instanceof Request) {
            exit(1);
        }

        $newIdentity = $request->getQuery('identity', $request->getPost('identity'));
        if (! $newIdentity) {
            return $this->redirect()->toRoute('home');
        }

        /** @var AuthenticationService $authenticationService */
        $authenticationService = $this->getServiceLocator()->get(AuthenticationService::class);

        $currentIdentity = $authenticationService->getIdentity();
        if (! $currentIdentity || ! is_array($currentIdentity)) {
            return $this->redirect()->toRoute('home');
        }

        if (isset($currentIdentity['ldap'])) {
            // si l'identifiant demandé contient un @, on estime qu'il s'agit d'un eppn shibboleth : on autorise pas le mélange des genres!
            // todo: faire mieux
            if (strpos($newIdentity, '@') !== false) {
                throw new RuntimeException("Usurpation Shibboleth interdite depuis une authentification LDAP");
            }
            /** @var People $currentIdentity */
            $currentIdentity = $currentIdentity['ldap'];

            // vérif existence de l'individu dans l'annuaire LDAP
            $ldapPeople = $this->getLdapPeopleMapper()->findOneByUsername($newIdentity);
            if (!$ldapPeople) {
                throw new RuntimeException("Identifiant LDAP inconnu");
            }
        } elseif (isset($currentIdentity['shib'])) {
            // si l'identifiant demandé ne contient pas @, on estime qu'il s'agit d'un identifiant LDAP : on autorise pas le mélange des genres!
            // todo: faire mieux
            if (strpos($newIdentity, '@') === false) {
                throw new RuntimeException("Usurpation LDAP interdite depuis une authentification Shibboleth");
            }
            /** @var ShibUser $currentIdentity */
            $currentIdentity = $currentIdentity['shib'];
        }
        else {
            return $this->redirect()->toRoute('home');
        }

        // seuls les logins spécifiés dans la config sont habilités à usurper des identités
        /** @var ModuleOptions $options */
        $options = $this->getServiceLocator()->get('unicaen-auth_module_options');
        if (! in_array($currentIdentity->getUsername(), $options->getUsurpationAllowedUsernames())) {
            throw new RuntimeException("Usurpation non explicitement autorisée");
        }

        // cuisine spéciale pour Shibboleth
        if ($currentIdentity instanceof ShibUser) {
            $fromShibUser = $currentIdentity;
            $toShibUser = $this->createShibUserFromUtilisateurUsername($newIdentity);
            /** @var ShibService $shibService */
            $shibService = $this->getServiceLocator()->get(ShibService::class);
            $shibService->activateUsurpation($fromShibUser, $toShibUser);
        }

        $authenticationService->getStorage()->write($newIdentity);

        return $this->redirect()->toRoute('home');
    }

    /**
     * Recherche l'utilisateur dont le login est spécifié puis instancie un ShibUser à partir
     * des attributs de cet utilisateur.
     *
     * @param string $username Ex tartempion@unicaen.fr
     * @return ShibUser
     */
    protected function createShibUserFromUtilisateurUsername($username)
    {
        /** @var AbstractUser $utilisateur */
        $utilisateur = $this->getUserMapper()->findByUsername($username);
        if ($utilisateur === null) {
            throw new RuntimeException("L'utilisateur '$username' n'existe pas dans la table des utilisateurs");
        }

        $shibUser = new ShibUser();
        $shibUser->setEppn($utilisateur->getUsername());
        $shibUser->setId(uniqid()); // peut pas mieux faire pour l'instant
        $shibUser->setDisplayName($utilisateur->getDisplayName());
        $shibUser->setEmail($utilisateur->getEmail());
        $shibUser->setNom('?');     // peut pas mieux faire pour l'instant
        $shibUser->setPrenom('?');  // peut pas mieux faire pour l'instant

        return $shibUser;
    }

    /**
     * @return UserMapper
     */
    public function getUserMapper()
    {
        return $this->getServiceLocator()->get('zfcuser_user_mapper');
    }

    /**
     * @return UserContext
     */
    protected function getAuthUserContextService()
    {
        return $this->getServiceLocator()->get('AuthUserContext');
    }

    /**
     * @return LdapPeopleMapper
     */
    public function getLdapPeopleMapper()
    {
        return $this->serviceLocator->get('ldap_people_mapper');
    }
}
 No newline at end of file
Loading