Skip to content

Commit ea41d93

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

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

@@ -238,6 +239,7 @@ public function getTokenAndUser(): array
238239
Debugger::log('ReCodEx::getTokenAndUser()', Debugger::DEBUG);
239240
$body = $this->post('extensions/' . $this->extensionId);
240241
if (!is_array($body) || empty($body['accessToken']) || empty($body['user'])) {
242+
Debugger::log($body, Debugger::DEBUG);
241243
throw new RecodexApiException("Unexpected ReCodEx API response from extension token endpoint.");
242244
}
243245

@@ -255,6 +257,7 @@ public function refreshToken(): array
255257
Debugger::log('ReCodEx::refreshToken()', Debugger::DEBUG);
256258
$body = $this->post('login/refresh');
257259
if (!is_array($body) || empty($body['accessToken']) || empty($body['user'])) {
260+
Debugger::log($body, Debugger::DEBUG);
258261
throw new RecodexApiException("Unexpected ReCodEx API response from token refresh endpoint.");
259262
}
260263

app/presenters/LoginPresenter.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private function finalizeLogin(User $user, string $token): void
5454
{
5555
// part of the token is stored in the database, suffix goes into our token (payload)
5656
$tokenSuffix = $user->setRecodexToken($token);
57-
$user->updatedNow();
5857
$this->users->persist($user);
5958

6059
// generate our token for our frontend
@@ -100,6 +99,7 @@ public function actionDefault()
10099
} else {
101100
$recodexUser->updateUser($user);
102101
}
102+
$user->updatedNow();
103103

104104
$this->finalizeLogin($user, $recodexResponse['accessToken']);
105105
}
@@ -127,13 +127,27 @@ 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());
136-
$recodexUser->updateUser($user);
145+
// Update the user entity if the token uses the same identity as the user
146+
// (token may use identity override, in which case we do not want to update the user)
147+
if ($recodexUser->getId() === $user->getId()) {
148+
$recodexUser->updateUser($user);
149+
$user->updatedNow();
150+
}
137151

138152
$this->finalizeLogin($user, $recodexResponse['accessToken']);
139153
}

0 commit comments

Comments
 (0)