Skip to content
Snippets Groups Projects
Select Git revision
  • 67e96ad4aed3ae929a63bf634bd57918daadb363
  • master default protected
  • release_3.0.0
  • test
  • feature_pre_sql
  • develop
  • 3.0.1
  • 3.0.0
  • 2.3.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
  • 1.3.7
  • 1.3.6
  • 1.3.5
  • 1.3.4
  • 1.3.3
  • 1.3.2
  • 1.3.1
  • 1.3.0
  • 1.2.6
  • 1.2.5
  • 1.2.4
  • 1.2.3
  • 1.2.2
  • 1.2.1
26 results

global-development.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TokenAdapter.php 2.48 KiB
    <?php
    
    namespace UnicaenAuthToken\Authentication\Adapter;
    
    use RuntimeException;
    use UnicaenAuthentification\Authentication\Adapter\AbstractDb;
    use UnicaenAuthToken\Service\TokenServiceAwareTrait;
    use UnicaenAuthToken\Service\TokenServiceException;
    use Laminas\Authentication\Result as AuthenticationResult;
    use ZfcUser\Entity\UserInterface;
    
    /**
     * Adpater d'authentification à partir d'un token.
     *
     * @author Unicaen
     *
     * @property \UnicaenAuthToken\Options\ModuleOptions $moduleOptions
     */
    class TokenAdapter extends AbstractDb
    {
        use TokenServiceAwareTrait;
    
        const TYPE = 'token';
        protected $type = self::TYPE;
    
        /**
         * @inheritDoc
         */
        protected function fetchUserObject(): ?UserInterface
        {
            /** @var \Laminas\Http\Request $request */
            $request = $this->event->getRequest();
            $token = $request->getPost()->get('identity');
    
            $userToken= $this->tokenService->findUserTokenByToken($token);
            if ($userToken === null) {
                $this->event
                    ->setCode(AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND)
                    ->setMessages([]);
                $this->setSatisfied(false);
                return null;
            }
    
            if ($userToken->isExpired()) {
                $this->event
                    ->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED)
                    ->setMessages(["Ce jeton n'est plus utilisable car il a expiré."]);
                $this->setSatisfied(false);
                return null;
            }
            if ($userToken->isActionsMaxCountReached()) {
                $this->event
                    ->setCode(AuthenticationResult::FAILURE_UNCATEGORIZED)
                    ->setMessages(["Ce jeton n'est plus utilisable car il a atteint son nombre maximum d'utilisations."]);
                $this->setSatisfied(false);
                return null;
            }
    
            $userToken->incrementActionsCount();
            $userToken->setLastUsedOn();
            try {
                $this->tokenService->saveUserToken($userToken);
            } catch (TokenServiceException $e) {
                throw new RuntimeException("Impossible d'incrémenter le jeton.", null, $e);
            }
    
            $userObject = $this->mapper->findById($userToken->getUserId());
            if ($userObject === null) {
                throw new RuntimeException("Le jeton fait référence à un utilisateur introuvable.");
            }
    
            return $userObject;
        }
    
        /**
         * @inheritDoc
         */
        protected function authenticateUserObject(UserInterface $userObject): bool
        {
            return true;
        }
    }