Skip to content

Commit e556dc5

Browse files
Merge pull request ecamp#5382 from carlobeltrame/fix-expired-oauth-state-cookie
Fix expired oauth state cookie
2 parents 347c5a7 + bc10e82 commit e556dc5

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

api/src/OAuth/JWTStateOAuth2Client.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* longer-living token and with parts of the cookie available to JavaScript.
3232
*/
3333
class JWTStateOAuth2Client extends OAuth2Client implements OAuth2ClientInterface {
34-
public const JWT_TTL = 300; // seconds, i.e. 5 minutes of validity for the JWT token
34+
public const JWT_TTL = 900; // seconds, i.e. 15 minutes of validity for the JWT token
3535

3636
public function __construct(
3737
AbstractProvider $provider,
@@ -101,14 +101,18 @@ public function redirect(array $scopes = [], array $options = []): RedirectRespo
101101
/**
102102
* Checks the validity of the temporary JWT cookie, and checks that the state parameter is correct.
103103
* Any irregularities would indicate someone tampering with the login system (or someone taking longer
104-
* than 5 minutes to authenticate with the external service...)
104+
* than 15 minutes to authenticate with the external service...)
105105
* After this custom state parameter check, we delegate to the original implementation to finish the OAuth
106106
* flow.
107107
*
108108
* @throws IdentityProviderException
109109
*/
110110
public function getAccessToken(array $options = []): AccessTokenInterface {
111111
$jwt = $this->getCurrentRequest()->cookies->get($this->getCookieName($this->cookiePrefix));
112+
if (null === $jwt) {
113+
throw new InvalidStateException('Expired state');
114+
}
115+
112116
$actualState = $this->getCurrentRequest()->get('state');
113117

114118
try {

api/src/Security/OAuth/GoogleAuthenticator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
1313
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
1414
use Symfony\Bundle\SecurityBundle\Security;
15+
use Symfony\Component\HttpFoundation\JsonResponse;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
@@ -100,6 +101,6 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
100101
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response {
101102
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
102103

103-
return new Response($message, Response::HTTP_FORBIDDEN);
104+
return new JsonResponse(['message' => $message, 'code' => Response::HTTP_FORBIDDEN], Response::HTTP_FORBIDDEN);
104105
}
105106
}

api/src/Security/OAuth/HitobitoAuthenticator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
1414
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;
1515
use Symfony\Bundle\SecurityBundle\Security;
16+
use Symfony\Component\HttpFoundation\JsonResponse;
1617
use Symfony\Component\HttpFoundation\RedirectResponse;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
@@ -107,6 +108,6 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
107108
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response {
108109
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
109110

110-
return new Response($message, Response::HTTP_FORBIDDEN);
111+
return new JsonResponse(['message' => $message, 'code' => Response::HTTP_FORBIDDEN], Response::HTTP_FORBIDDEN);
111112
}
112113
}

0 commit comments

Comments
 (0)