Skip to content

Commit f10dda7

Browse files
julien-ncbackportbot[bot]
authored andcommitted
fix(oauth): rotate the auth token only if the access token rotation was successful
fix(oauth): rotate the auth token only if the access token rotation was successful Signed-off-by: Julien Veyssier <julien-nc@posteo.net> [skip ci]
1 parent fbba9a3 commit f10dda7

2 files changed

Lines changed: 19 additions & 36 deletions

File tree

apps/oauth2/lib/Controller/OauthApiController.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,9 @@ public function getToken(
200200
$redeemedThrottleReason = $grant_type === 'authorization_code'
201201
? 'authorization_code_already_redeemed'
202202
: 'refresh_token_already_redeemed';
203-
$tokenRotated = false;
204203

205204
$this->db->beginTransaction();
206205
try {
207-
$appToken = $this->tokenProvider->rotate(
208-
$appToken,
209-
$decryptedToken,
210-
$newToken
211-
);
212-
$tokenRotated = true;
213-
214-
// Expiration is in 1 hour again
215-
$appToken->setExpires($this->time->getTime() + 3600);
216-
$this->tokenProvider->updateToken($appToken);
217-
218206
$updatedRows = $this->accessTokenMapper->rotateToken(
219207
$accessToken->getId(),
220208
$code,
@@ -225,25 +213,31 @@ public function getToken(
225213

226214
if ($updatedRows !== 1) {
227215
$this->db->rollBack();
228-
// tokenProvider->rotate() updates the auth token cache, so we have to clear the new token on rollback
229-
$this->tokenProvider->invalidateToken($newToken);
230216
$response = new JSONResponse([
231217
'error' => 'invalid_request',
232218
], Http::STATUS_BAD_REQUEST);
233219
$response->throttle(['invalid_request' => $redeemedThrottleReason]);
234220
return $response;
235221
}
236222

223+
$appToken = $this->tokenProvider->rotate(
224+
$appToken,
225+
$decryptedToken,
226+
$newToken
227+
);
228+
229+
// Expiration is in 1 hour again
230+
$appToken->setExpires($this->time->getTime() + 3600);
231+
$this->tokenProvider->updateToken($appToken);
232+
237233
$this->db->commit();
238234
} catch (\Throwable $e) {
239235
if ($this->db->inTransaction()) {
240236
$this->db->rollBack();
241237
}
242-
243-
if ($tokenRotated) {
244-
// tokenProvider->rotate() updates the auth token cache, so we have to clear the new token on rollback
245-
$this->tokenProvider->invalidateToken($newToken);
246-
}
238+
// rotate() and updateToken() write the auth token to the cache,
239+
// so if we are past rotate() we must invalidate the new token
240+
$this->tokenProvider->invalidateToken($newToken);
247241

248242
throw $e;
249243
}

apps/oauth2/tests/Controller/OauthApiControllerTest.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -712,20 +712,14 @@ public function testRefreshTokenRedeemedConcurrently(): void {
712712
return 'random' . $len;
713713
});
714714

715-
$this->tokenProvider->expects($this->once())
716-
->method('rotate')
717-
->with(
718-
$appToken,
719-
'decryptedToken',
720-
'random72'
721-
)->willReturn($appToken);
715+
$this->tokenProvider->expects($this->never())
716+
->method('rotate');
722717

723718
$this->time->method('getTime')
724719
->willReturn(1000);
725720

726-
$this->tokenProvider->expects($this->once())
727-
->method('updateToken')
728-
->with($this->isInstanceOf(PublicKeyToken::class));
721+
$this->tokenProvider->expects($this->never())
722+
->method('updateToken');
729723

730724
$this->crypto->method('encrypt')
731725
->with('random72', 'random128')
@@ -737,16 +731,11 @@ public function testRefreshTokenRedeemedConcurrently(): void {
737731
$this->db->expects($this->never())
738732
->method('commit');
739733

740-
$this->db->expects($this->exactly(2))
741-
->method('inTransaction')
742-
->willReturnOnConsecutiveCalls(true, false);
743-
744734
$this->db->expects($this->once())
745735
->method('rollBack');
746736

747-
$this->tokenProvider->expects($this->once())
748-
->method('invalidateToken')
749-
->with('random72');
737+
$this->tokenProvider->expects($this->never())
738+
->method('invalidateToken');
750739

751740
$this->accessTokenMapper->expects($this->once())
752741
->method('rotateToken')

0 commit comments

Comments
 (0)