Skip to content

Commit 3b2e94d

Browse files
committed
Fixing bug - the refresh endpoint needs ReCodEx token properly injected.
1 parent 8dcef66 commit 3b2e94d

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

app/helpers/Recodex/RecodexApiHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private function processJsonBody($response)
136136
{
137137
$code = $response->getStatusCode();
138138
if ($code === 401) { // unauthorized, token is probably invalid
139+
Debugger::log("HTTP request to ReCodEx API failed (response $code).", Debugger::DEBUG);
139140
throw new InvalidAccessTokenException("Unauthorized request to ReCodEx API. Token is probably invalid.");
140141
}
141142

@@ -237,6 +238,7 @@ public function getTokenAndUser(): array
237238
{
238239
Debugger::log('ReCodEx::getTokenAndUser()', Debugger::DEBUG);
239240
$body = $this->post('extensions/' . $this->extensionId);
241+
Debugger::log($body, Debugger::DEBUG);
240242
if (!is_array($body) || empty($body['accessToken']) || empty($body['user'])) {
241243
throw new RecodexApiException("Unexpected ReCodEx API response from extension token endpoint.");
242244
}
@@ -254,6 +256,7 @@ public function refreshToken(): array
254256
{
255257
Debugger::log('ReCodEx::refreshToken()', Debugger::DEBUG);
256258
$body = $this->post('login/refresh');
259+
Debugger::log($body, Debugger::DEBUG);
257260
if (!is_array($body) || empty($body['accessToken']) || empty($body['user'])) {
258261
throw new RecodexApiException("Unexpected ReCodEx API response from token refresh endpoint.");
259262
}

app/presenters/LoginPresenter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,25 @@ public function checkRefresh()
127127
*/
128128
public function actionRefresh()
129129
{
130+
// We need to inject the token manually here (this class is not derived from BasePresenterWithApi)
131+
$user = $this->getCurrentUser();
132+
$prefix = $user->getRecodexToken();
133+
$suffix = $this->getAccessToken()->getPayloadOrDefault('suffix', null);
134+
135+
if (!$prefix || !$suffix) {
136+
throw new ForbiddenRequestException("Cannot refresh token - user does not have a ReCodEx token.");
137+
}
138+
139+
// Call ReCodEx API to refresh the token
140+
$this->recodexApi->setAuthToken($prefix . $suffix);
130141
$recodexResponse = $this->recodexApi->refreshToken();
131142
/** @var RecodexUser */
132143
$recodexUser = $recodexResponse['user'];
133144

134-
// Update the user entity with new info from ReCodEx.
135-
$user = $this->users->findOrThrow($recodexUser->getId());
145+
// Update the user entity with new info from ReCodEx
146+
if ($recodexUser->getId() !== $user->getId()) {
147+
throw new AuthenticationException("Token refresh failed - user ID mismatch.");
148+
}
136149
$recodexUser->updateUser($user);
137150

138151
$this->finalizeLogin($user, $recodexResponse['accessToken']);

0 commit comments

Comments
 (0)