@@ -182,21 +182,9 @@ public function getToken(
182182 $ redeemedThrottleReason = $ grant_type === 'authorization_code '
183183 ? 'authorization_code_already_redeemed '
184184 : 'refresh_token_already_redeemed ' ;
185- $ tokenRotated = false ;
186185
187186 $ this ->db ->beginTransaction ();
188187 try {
189- $ appToken = $ this ->tokenProvider ->rotate (
190- $ appToken ,
191- $ decryptedToken ,
192- $ newToken
193- );
194- $ tokenRotated = true ;
195-
196- // Expiration is in 1 hour again
197- $ appToken ->setExpires ($ this ->time ->getTime () + 3600 );
198- $ this ->tokenProvider ->updateToken ($ appToken );
199-
200188 $ updatedRows = $ this ->accessTokenMapper ->rotateToken (
201189 $ accessToken ->getId (),
202190 $ code ,
@@ -207,25 +195,31 @@ public function getToken(
207195
208196 if ($ updatedRows !== 1 ) {
209197 $ this ->db ->rollBack ();
210- // tokenProvider->rotate() updates the auth token cache, so we have to clear the new token on rollback
211- $ this ->tokenProvider ->invalidateToken ($ newToken );
212198 $ response = new JSONResponse ([
213199 'error ' => 'invalid_request ' ,
214200 ], Http::STATUS_BAD_REQUEST );
215201 $ response ->throttle (['invalid_request ' => $ redeemedThrottleReason ]);
216202 return $ response ;
217203 }
218204
205+ $ appToken = $ this ->tokenProvider ->rotate (
206+ $ appToken ,
207+ $ decryptedToken ,
208+ $ newToken
209+ );
210+
211+ // Expiration is in 1 hour again
212+ $ appToken ->setExpires ($ this ->time ->getTime () + 3600 );
213+ $ this ->tokenProvider ->updateToken ($ appToken );
214+
219215 $ this ->db ->commit ();
220216 } catch (\Throwable $ e ) {
221217 if ($ this ->db ->inTransaction ()) {
222218 $ this ->db ->rollBack ();
223219 }
224-
225- if ($ tokenRotated ) {
226- // tokenProvider->rotate() updates the auth token cache, so we have to clear the new token on rollback
227- $ this ->tokenProvider ->invalidateToken ($ newToken );
228- }
220+ // rotate() and updateToken() write the auth token to the cache,
221+ // so if we are past rotate() we must invalidate the new token
222+ $ this ->tokenProvider ->invalidateToken ($ newToken );
229223
230224 throw $ e ;
231225 }
0 commit comments