Commit f908e668 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

[FIX] Usurpation d'un compte local (db) depuis une authentification shib

parent f3344ee8
Loading
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -46,11 +46,10 @@ class Shib extends AbstractStorage
        $sessionIdentity = $this->storage->read();
        $username = $sessionIdentity->getUsername();

//        // L'identité en session doit ressembler à un EPPN.
//        $looksLikeEppn = strpos($username, '@') !== false;
//        if (! $looksLikeEppn) {
//            return null;
//        }
        // L'identité en session doit ressembler à un EPPN.
        if (! ShibUser::isEppn($username)) {
            return null;
        }

        return $this->shibService->getAuthenticatedUser();
    }
+39 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace UnicaenAuth\Entity\Shibboleth;

use UnicaenAuth\Entity\Db\AbstractUser;
use Webmozart\Assert\Assert;
use ZfcUser\Entity\UserInterface;

class ShibUser implements UserInterface
@@ -47,6 +48,41 @@ class ShibUser implements UserInterface
     */
    protected $state = 1;

    /**
     * Teste si une chaîne ressemble à un EPPN.
     *
     * @param string $username
     * @return bool
     */
    static public function isEppn(string $username)
    {
        if (($pos = strpos($username, '@')) === false) {
            return false;
        }

        $domain = substr($username, $pos + 1);
        if (filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) {
            return false;
        }

        return true;
    }

    /**
     * Extrait le domaine de l'EPPN spécifié.
     *
     * @param string $eppn
     * @return string
     */
    static public function extractDomainFromEppn(string $eppn)
    {
        Assert::true(static::isEppn($eppn), "La chaîne suivante n'est pas un EPPN valide : " . $eppn);

        $parts = explode('@', $eppn);

        return $parts[1];
    }

    /**
     * Retourne la partie domaine DNS de l'EPPN.
     * Retourne par exemple "unicaen.fr" lorsque l'EPPN est "tartempion@unicaen.fr"
@@ -55,9 +91,7 @@ class ShibUser implements UserInterface
     */
    public function getEppnDomain()
    {
        $parts = explode('@', $this->getEppn());

        return $parts[1];
        return static::extractDomainFromEppn($this->getEppn());
    }

    /**
@@ -75,6 +109,8 @@ class ShibUser implements UserInterface
     */
    public function setEppn($eppn)
    {
        Assert::true(static::isEppn($eppn), "La chaîne suivante n'est pas un EPPN valide : " . $eppn);

        $this->setUsername($eppn);
    }

+5 −0
Original line number Diff line number Diff line
@@ -319,6 +319,11 @@ EOS;
     */
    public function activateUsurpation(ShibUser $currentShibUser, AbstractUser $utilisateurUsurpe): self
    {
        if (! ShibUser::isEppn($utilisateurUsurpe->getUsername())) {
            // cas d'usurpation d'un compte local (db) depuis une authentification shib
            return $this;
        }

        $toShibUser = new ShibUser();
        $toShibUser->setEppn($utilisateurUsurpe->getUsername());
        $toShibUser->setId(uniqid()); // peut pas mieux faire pour l'instant