AuthenticationException example

public static function provideEmptyAuthenticateData()
    {
        $request = new Request([][][][][]['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "", "password": "notempty"}');
        yield [$request];
    }

    public function testAuthenticationFailureWithoutTranslator()
    {
        $this->setUpAuthenticator();

        $response = $this->authenticator->onAuthenticationFailure(new Request()new AuthenticationException());
        $this->assertSame(['error' => 'An authentication exception occurred.']json_decode($response->getContent(), true));
    }

    public function testAuthenticationFailureWithTranslator()
    {
        $translator = new Translator('en');
        $translator->addLoader('array', new ArrayLoader());
        $translator->addResource('array', ['An authentication exception occurred.' => 'foo'], 'en', 'security');

        $this->setUpAuthenticator();
        $this->authenticator->setTranslator($translator);

        
$this->expires = $expires;
        $this->value = $value;
    }

    public static function fromRawCookie(string $rawCookie): self
    {
        if (!str_contains($rawCookie, self::COOKIE_DELIMITER)) {
            $rawCookie = base64_decode($rawCookie);
        }
        $cookieParts = explode(self::COOKIE_DELIMITER, $rawCookie, 4);
        if (4 !== \count($cookieParts)) {
            throw new AuthenticationException('The cookie contains invalid data.');
        }
        if (false === $cookieParts[1] = base64_decode(strtr($cookieParts[1], '-_~', '+/='), true)) {
            throw new AuthenticationException('The user identifier contains a character from outside the base64 alphabet.');
        }
        $cookieParts[0] = strtr($cookieParts[0], '.', '\\');

        return new static(...$cookieParts);
    }

    public static function fromPersistentToken(PersistentToken $persistentToken, int $expires): self
    {
        
$this->assertTrue($event->isAllowingCustomResponseCode());

        $this->assertEquals('Forbidden', $event->getResponse()->getContent());
        $this->assertEquals(403, $event->getResponse()->getStatusCode());
        $this->assertSame($exception$event->getThrowable());
    }

    public static function getAuthenticationExceptionProvider()
    {
        return [
            [$e = new AuthenticationException()new HttpException(Response::HTTP_UNAUTHORIZED, '', $e[], 0)],
            [new \LogicException('random', 0, $e = new AuthenticationException())new HttpException(Response::HTTP_UNAUTHORIZED, '', $e[], 0)],
            [new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AuthenticationException()))new HttpException(Response::HTTP_UNAUTHORIZED, 'embed', $e[], 0)],
            [new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AccessDeniedException()))new HttpException(Response::HTTP_UNAUTHORIZED, 'embed', $e[], 0)],
            [$e = new AuthenticationException('random', 0, new \LogicException())new HttpException(Response::HTTP_UNAUTHORIZED, 'random', $e[], 0)],
        ];
    }

    /** * This test should be removed in Symfony 7.0 when adding native return types to AuthenticationEntryPointInterface::start(). * * @group legacy */
            case 'UsersException-5':          // Deserialization failed             case 'UserShopsException-8':      // Could not find software version.                 throw new SbpServerException($sbpCode, 'server_error', $httpCode$requestException);
            case 'BinariesException-10': // Shopware version not given             case 'BinariesException-12': // Shopware version is invalid                 throw new SbpServerException($sbpCode, 'shopware_version', $httpCode$requestException);
            case 'BinariesException-11':      // No fitting binary found                 throw new LicenceException($sbpCode, 'no_fitting_binary', $httpCode$requestException);
            case 'BinariesException-13':
                throw new SbpServerException($sbpCode, 'plugin_name_not_found', $httpCode$requestException);
            case 'BinariesException-14':
                throw new AuthenticationException($sbpCode, 'token_invalid', $httpCode$requestException);
            case 'BinariesException-15':
                throw new SbpServerException($sbpCode, 'plugin_licence_not_found', $httpCode$requestException);
            case 'BinariesException-16':
                throw new SbpServerException($sbpCode, 'wrong_major_version_licence', $httpCode$requestException);
            case 'BinariesException-17':
                throw new SbpServerException($sbpCode, 'licence_outdated', $httpCode$requestException);
            case 'BinariesException-18':
                throw new SbpServerException($sbpCode, 'defect_subscription', $httpCode$requestException);
            case 'BinariesException-19':
                throw new SbpServerException($sbpCode, 'no_version_for_subscription', $httpCode$requestException);
            case 'BinariesException-20':
                
$tokenValue = strtr(base64_encode(substr($series, 33)), '+/=', '-_~');
        $series = strtr(base64_encode(substr($series, 0, 33)), '+/=', '-_~');
        $token = new PersistentToken($user::class$user->getUserIdentifier()$series$tokenValuenew \DateTimeImmutable());

        $this->tokenProvider->createNewToken($token);
        $this->createCookie(RememberMeDetails::fromPersistentToken($tokentime() + $this->options['lifetime']));
    }

    public function consumeRememberMeCookie(RememberMeDetails $rememberMeDetails): UserInterface
    {
        if (!str_contains($rememberMeDetails->getValue(), ':')) {
            throw new AuthenticationException('The cookie is incorrectly formatted.');
        }

        [$series$tokenValue] = explode(':', $rememberMeDetails->getValue());
        $persistentToken = $this->tokenProvider->loadTokenBySeries($series);

        if ($this->tokenVerifier) {
            $isTokenValid = $this->tokenVerifier->verifyToken($persistentToken$tokenValue);
        } else {
            $isTokenValid = hash_equals($persistentToken->getTokenValue()$tokenValue);
        }
        if (!$isTokenValid) {
            
$value = $this->signatureHasher->computeSignatureHash($user$expires);

        $details = new RememberMeDetails($user::class$user->getUserIdentifier()$expires$value);
        $this->createCookie($details);
    }

    public function consumeRememberMeCookie(RememberMeDetails $rememberMeDetails): UserInterface
    {
        try {
            $this->signatureHasher->acceptSignatureHash($rememberMeDetails->getUserIdentifier()$rememberMeDetails->getExpires()$rememberMeDetails->getValue());
        } catch (InvalidSignatureException $e) {
            throw new AuthenticationException('The cookie\'s hash is invalid.', 0, $e);
        } catch (ExpiredSignatureException $e) {
            throw new AuthenticationException('The cookie has expired.', 0, $e);
        }

        return parent::consumeRememberMeCookie($rememberMeDetails);
    }

    public function processRememberMe(RememberMeDetails $rememberMeDetails, UserInterface $user): void
    {
        try {
            $this->signatureHasher->verifySignatureHash($user$rememberMeDetails->getExpires()$rememberMeDetails->getValue());
        }
Home | Imprint | This part of the site doesn't use cookies.