From 323533165951a065b13b7f91e694a09129ad245c Mon Sep 17 00:00:00 2001 From: mrcotrmpr Date: Thu, 30 Sep 2021 10:22:47 +0200 Subject: [PATCH] fix existing tests, refactor method --- .../AuthenticationSuccessHandler.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Security/Http/Authentication/AuthenticationSuccessHandler.php b/src/Security/Http/Authentication/AuthenticationSuccessHandler.php index 3b4aa93..afa684a 100644 --- a/src/Security/Http/Authentication/AuthenticationSuccessHandler.php +++ b/src/Security/Http/Authentication/AuthenticationSuccessHandler.php @@ -63,11 +63,10 @@ public function handleAuthenticationSuccess(UserInterface $user, $jwt = null): J public function onAuthenticationSuccess(Request $request, TokenInterface $token): JsonResponse { $response = $this->handleAuthenticationSuccess($token->getUser()); - $username = $request->request->get('username'); - $remember_device_cookie = $request->cookies->get('REMEMBER_DEVICE'); + $username = $request->request->get('username'); if ($this->rememberDeviceResolver->getRememberDeviceStatus()) { - if (is_null($request->cookies) || is_null($remember_device_cookie) || $this->jwtEncoder->decode($remember_device_cookie)['exp'] < time() || $username != $this->jwtEncoder->decode($remember_device_cookie)['user']) { + if ($this->checkForInvalidRememberDeviceCookie($request, $username)) { $expiry_time = time() + $this->rememberDeviceResolver->getRememberDeviceExpiryDays() * 86400; @@ -93,6 +92,18 @@ public function addResponsePayload(string $key, $value): void $this->responsePayload[$key] = $value; } + private function checkForInvalidRememberDeviceCookie($request, $username): bool + { + switch ($request) { + case is_null($request->cookies): + case is_null($request->cookies->get("REMEMBER_DEVICE")): + case $this->jwtEncoder->decode($request->cookies->get("REMEMBER_DEVICE"))['exp'] < time(): + case $username != $this->jwtEncoder->decode($request->cookies->get("REMEMBER_DEVICE"))['user']: + return true; + } + return false; + } + private function addToValidTokens($token, $user): void { $entityManager = $this->doctrine->getManager();