Skip to content

Commit fe32bf9

Browse files
committed
feat: tell a terminal told to slow down which interval to use
A terminal polling for its sign-in sooner than the interval only got slow_down, and had to work out on its own how long to wait from then on: an agent driving it could burn the rate limit guessing. The answer now carries the interval to use, the first one plus the five seconds RFC 8628 adds after a slow_down, and its message says it. 🤖 Generated with Claude Code Claude-Session: https://claude.ai/code/session_01Hh5F6zZBAFFGYpd22oA1PZ
1 parent 7aad500 commit fe32bf9

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎app/Dto/DeviceAuthorization.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
public const int INTERVAL_SECONDS = 5;
2222

23+
/** What RFC 8628 adds to the interval of a terminal told to slow down. */
24+
public const int SLOW_DOWN_SECONDS = 5;
25+
2326
public function __construct(
2427
public string $key,
2528
public string $fingerprint,

‎app/Exceptions/ApiException.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,16 @@ public static function authorizationPending(): self
8787
return new self(ApiErrorCode::AuthorizationPending, __('Waiting for the device to be approved on the web.'));
8888
}
8989

90-
public static function slowDown(): self
90+
/**
91+
* Says the interval to wait between tries from now on, so a terminal doesn't have to guess it.
92+
*/
93+
public static function slowDown(int $interval): self
9194
{
92-
return new self(ApiErrorCode::SlowDown, __('Asking too often. Wait a few seconds more between tries.'));
95+
return new self(
96+
ApiErrorCode::SlowDown,
97+
__('Asking too often. Wait :seconds seconds between tries from now on.', ['seconds' => $interval]),
98+
data: ['interval' => $interval],
99+
);
93100
}
94101

95102
public static function accessDenied(): self

‎app/Http/Requests/CreateApiDeviceRequest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private function approvedDevice(string $deviceCode): Device
131131
if ($authorization->status === DeviceAuthorizationStatus::Pending) {
132132
throw Cache::add("{$authorization->key}:polled", true, DeviceAuthorization::INTERVAL_SECONDS - 1)
133133
? ApiException::authorizationPending()
134-
: ApiException::slowDown();
134+
: ApiException::slowDown(DeviceAuthorization::INTERVAL_SECONDS + DeviceAuthorization::SLOW_DOWN_SECONDS);
135135
}
136136

137137
throw_if($authorization->status === DeviceAuthorizationStatus::Denied, ApiException::accessDenied());

‎tests/Feature/Controllers/Api/DeviceControllerTest.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,16 @@
207207
expect(Device::query()->exists())->toBeFalse();
208208
});
209209

210-
it('tells a terminal asking again sooner than the interval to slow down', function (): void {
210+
it('tells a terminal asking again sooner than the interval to slow down, and the interval to use from then on', function (): void {
211211
signedApiRequest($this->keyPair, 'POST', route('api-device.store'), ['device_code' => $this->deviceCode])
212212
->assertJsonPath('code', 'authorization_pending');
213213

214214
signedApiRequest($this->keyPair, 'POST', route('api-device.store'), ['device_code' => $this->deviceCode])
215215
->assertBadRequest()
216216
->assertExactJson([
217-
'message' => 'Asking too often. Wait a few seconds more between tries.',
217+
'message' => 'Asking too often. Wait 10 seconds between tries from now on.',
218218
'code' => 'slow_down',
219+
'interval' => 10,
219220
]);
220221

221222
$this->travel(3)->seconds();

0 commit comments

Comments
 (0)