Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix oauth2 state encoding #2

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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