Skip to content

Commit

Permalink
Fix oauth2 state encoding (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
marforon committed Dec 14, 2022
1 parent 8e98567 commit 5ad6c74
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/Util/HttpUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use AnzuSystems\AuthBundle\Exception\NotFoundAccessTokenException;
use AnzuSystems\AuthBundle\Helper\ConditionHelper;
use AnzuSystems\AuthBundle\Model\RefreshTokenDto;
use DateTimeImmutable;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Token\Parser;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function grabDeviceIdFromRequest(Request $request): string
/**
* @throws InvalidJwtException
*/
public function storeJwtOnResponse(Response $response, Token $token): void
public function storeJwtOnResponse(Response $response, Token $token, DateTimeImmutable $expiresAt = null): void
{
$rawToken = $token->toString();
[$header, $claims, $signature] = explode('.', $rawToken, 3);
Expand All @@ -97,16 +98,17 @@ public function storeJwtOnResponse(Response $response, Token $token): void
throw InvalidJwtException::create($rawToken);
}

$lifetime = $expiresAt?->getTimestamp() ?? $this->jwtConfiguration->getLifetime();
$payloadCookie = $this->createCookie(
$this->cookieConfiguration->getJwtPayloadCookieName(),
$header . '.' . $claims,
$this->jwtConfiguration->getLifetime(),
$lifetime,
false
);
$signatureCookie = $this->createCookie(
$this->cookieConfiguration->getJwtSignatureCookieName(),
$signature,
$this->jwtConfiguration->getLifetime()
$lifetime
);
$refreshTokenExistenceCookie = $this->createCookie(
$this->cookieConfiguration->getRefreshTokenExistenceCookieName(),
Expand Down
4 changes: 2 additions & 2 deletions src/Util/JwtUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
*
* @throws MissingConfigurationException
*/
public function create(string $userIdentifier, DateTimeImmutable $expiresAt = null): Plain
public function create(string $authId, DateTimeImmutable $expiresAt = null): Plain
{
$privateCert = $this->jwtConfiguration->getPrivateCert();

Expand All @@ -46,7 +46,7 @@ public function create(string $userIdentifier, DateTimeImmutable $expiresAt = nu
->issuedAt(new DateTimeImmutable())
->canOnlyBeUsedAfter(new DateTimeImmutable())
->expiresAt($expiresAt ?: new DateTimeImmutable(sprintf('+%d seconds', $this->jwtConfiguration->getLifetime())))
->relatedTo($userIdentifier)
->relatedTo($authId)
->getToken(
$this->jwtConfiguration->getAlgorithm()->signer(),
InMemory::plainText($privateCert)
Expand Down
6 changes: 3 additions & 3 deletions src/Util/StatelessTokenUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function __construct(

public function createForRequest(Request $request): string
{
return base64_encode(PasswordHelper::passwordHash(
return urldecode(base64_encode(PasswordHelper::passwordHash(
$this->createPlainForRequest($request)
));
)));
}

/**
Expand All @@ -28,7 +28,7 @@ public function isValidForRequest(Request $request, string $hash): bool
{
$token = $this->createPlainForRequest($request);

return password_verify($token, base64_decode($hash, strict: true));
return password_verify($token, urldecode(base64_decode($hash, strict: true)));
}

/**
Expand Down

0 comments on commit 5ad6c74

Please sign in to comment.