1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::{Mutex, MutexGuard, Notify};
use crate::clock::{system_clock, SharedClock};
use crate::refresher::Refresher;
use crate::token_store::{NoStore, TokenStore};
use crate::{ServiceToken, Token};
/// Internal errors from [`AutoRefresh::get_token`].
///
/// Strategy wrappers convert these into [`AuthError`](crate::AuthError) for the
/// public API.
#[derive(Debug, thiserror::Error)]
pub(crate) enum AutoRefreshError {
/// No token is cached and the strategy cannot self-authenticate.
#[error("No token found")]
NotFound,
/// The token has expired and refresh failed or is unavailable.
#[error("Token has expired")]
Expired,
/// The refresh/auth HTTP call failed.
#[error("Auth error: {0}")]
Auth(#[from] crate::AuthError),
}
impl From<AutoRefreshError> for crate::AuthError {
fn from(err: AutoRefreshError) -> Self {
match err {
AutoRefreshError::NotFound => {
crate::AuthError::NotAuthenticated(crate::error::NotAuthenticated)
}
AutoRefreshError::Expired => crate::AuthError::TokenExpired(crate::error::TokenExpired),
AutoRefreshError::Auth(e) => e,
}
}
}
/// Caches a token in memory and uses a [`Refresher`] to re-authenticate
/// or refresh before expiry, optionally backed by an external [`TokenStore`]
/// for persistence across short-lived strategy instances.
///
/// See the [crate-level documentation](crate#token-refresh) for a full
/// description of the concurrency model and flow diagram.
pub(crate) struct AutoRefresh<R, S = NoStore> {
refresher: R,
state: Mutex<State>,
store: S,
/// Set to `true` while a refresh HTTP call is in-flight.
///
/// Stored as an [`AtomicBool`] rather than inside [`State`] so that
/// [`CancelGuard`] can reset it on future cancellation without acquiring
/// the mutex.
refresh_in_progress: AtomicBool,
refresh_notify: Notify,
/// Source of "now" for token-expiry checks. [`SystemClock`](crate::clock::SystemClock)
/// in production; an injected clock in tests so expiry is deterministic.
clock: SharedClock,
}
struct State {
token: Option<Token>,
}
/// Ensures [`AutoRefresh::refresh_in_progress`] is cleared and waiters are
/// notified if the refresh future is cancelled (dropped) before completing.
///
/// On the normal path (success or handled error), the guard is defused before
/// drop so that the regular cleanup code runs instead.
struct CancelGuard<'a> {
in_progress: &'a AtomicBool,
notify: &'a Notify,
defused: bool,
}
impl Drop for CancelGuard<'_> {
fn drop(&mut self) {
if !self.defused {
self.in_progress.store(false, Ordering::Release);
self.notify.notify_waiters();
}
}
}
impl CancelGuard<'_> {
fn defuse(&mut self) {
self.defused = true;
}
}
impl State {
fn service_token(&self) -> Result<ServiceToken, AutoRefreshError> {
let token = self.token.as_ref().ok_or(AutoRefreshError::NotFound)?;
Ok(ServiceToken::new(token.access_token().clone()))
}
fn require_usable_token(&self, now: u64) -> Result<ServiceToken, AutoRefreshError> {
let token = self.token.as_ref().ok_or(AutoRefreshError::NotFound)?;
if token.is_usable_at(now) {
Ok(ServiceToken::new(token.access_token().clone()))
} else {
Err(AutoRefreshError::Expired)
}
}
}
impl<R> AutoRefresh<R, NoStore> {
/// Create a new `AutoRefresh` with a pre-loaded token and no external store.
///
/// Use this for refreshers that cannot self-authenticate (e.g. OAuth,
/// which needs a refresh token from a prior device code flow).
pub(crate) fn with_token(refresher: R, token: Token) -> Self {
Self {
refresher,
state: Mutex::new(State { token: Some(token) }),
store: NoStore,
refresh_in_progress: AtomicBool::new(false),
refresh_notify: Notify::new(),
clock: system_clock(),
}
}
/// Like [`with_token`](Self::with_token) but with an injected clock, so tests
/// can drive token expiry deterministically.
#[cfg(test)]
pub(crate) fn with_token_and_clock(refresher: R, token: Token, clock: SharedClock) -> Self {
Self {
refresher,
state: Mutex::new(State { token: Some(token) }),
store: NoStore,
refresh_in_progress: AtomicBool::new(false),
refresh_notify: Notify::new(),
clock,
}
}
}
impl<R, S: TokenStore> AutoRefresh<R, S> {
/// Create a new `AutoRefresh` backed by `store` and no in-memory token.
///
/// On the first `get_token` call the store is consulted before falling
/// through to initial authentication via `try_credential(None)`; every
/// successful refresh writes the new token back via `store.save()`. Pass
/// [`NoStore`] for the no-external-cache case — it's the default and
/// elides to a zero-cost no-op.
pub(crate) fn with_store(refresher: R, store: S) -> Self {
Self {
refresher,
state: Mutex::new(State { token: None }),
store,
refresh_in_progress: AtomicBool::new(false),
refresh_notify: Notify::new(),
clock: system_clock(),
}
}
}
impl<R: Refresher, S: TokenStore> AutoRefresh<R, S> {
/// Retrieve a valid access token, refreshing or re-authenticating as needed.
pub(crate) async fn get_token(&self) -> Result<ServiceToken, AutoRefreshError> {
let mut state = self.state.lock().await;
if state.token.is_none() {
// Drop the lock for the store read so a slow user-supplied backend
// (cookie, KV, Redis) doesn't serialise concurrent `get_token`
// callers. Re-acquire and double-check `state.token.is_none()` in
// case another caller populated it while we awaited.
drop(state);
let loaded = self.store.load().await;
state = self.state.lock().await;
if state.token.is_none() {
state.token = loaded;
}
}
if state.token.is_none() {
return self.initial_auth(&mut state).await;
}
// Read "now" once from the injected clock and use it for every expiry
// decision in this call, so the checks are mutually consistent and
// deterministic under test.
let now = self.clock.now_unix_secs();
if !state.token.as_ref().is_some_and(|t| t.is_expired_at(now)) {
return state.service_token();
}
if self.refresh_in_progress.load(Ordering::Acquire) {
return self.wait_for_in_flight_refresh(state, now).await;
}
let Some(credential) = self.refresher.try_credential(state.token.as_mut()) else {
return state.require_usable_token(now);
};
self.refresh_in_progress.store(true, Ordering::Release);
if state.token.as_ref().is_some_and(|t| t.is_usable_at(now)) {
self.refresh_non_blocking(state, credential).await
} else {
self.refresh_blocking(&mut state, credential).await
}
}
/// No cached token — authenticate via `try_credential(None)`.
///
/// The lock is held throughout to prevent concurrent initial-auth attempts.
async fn initial_auth(&self, state: &mut State) -> Result<ServiceToken, AutoRefreshError> {
let Some(credential) = self.refresher.try_credential(None) else {
return Err(AutoRefreshError::NotFound);
};
self.refresh_in_progress.store(true, Ordering::Release);
let mut guard = CancelGuard {
in_progress: &self.refresh_in_progress,
notify: &self.refresh_notify,
defused: false,
};
match self.refresher.refresh(&credential).await {
Ok(new_token) => {
self.save_refreshed_token(&new_token).await;
let token = self.install_refreshed_token(state, new_token);
guard.defuse();
Ok(token)
}
Err(err) => {
guard.defuse();
self.refresh_in_progress.store(false, Ordering::Release);
Err(AutoRefreshError::Auth(err))
}
}
}
/// Persist a freshly refreshed token to the per-refresher sink and the
/// user-supplied `TokenStore`. Awaits the store write, so callers should
/// drop the state lock before invoking this where possible (the
/// non-blocking refresh path does; the blocking/initial paths hold the
/// state lock throughout by design).
async fn save_refreshed_token(&self, new_token: &Token) {
self.refresher.save(new_token);
self.store.save(new_token).await;
}
/// Install a freshly refreshed token in `state`, clear the in-progress
/// flag, and return the corresponding [`ServiceToken`]. Pure in-lock
/// work; caller is responsible for having already persisted the token via
/// [`save_refreshed_token`](Self::save_refreshed_token).
fn install_refreshed_token(&self, state: &mut State, new_token: Token) -> ServiceToken {
let service_token = ServiceToken::new(new_token.access_token().clone());
state.token = Some(new_token);
self.refresh_in_progress.store(false, Ordering::Release);
service_token
}
/// Another caller is already refreshing — return the current token if still
/// usable, otherwise wait for the in-flight refresh to complete via `Notify`.
///
/// Takes `MutexGuard` by value because the lock is dropped before awaiting
/// the notification.
async fn wait_for_in_flight_refresh(
&self,
state: MutexGuard<'_, State>,
now: u64,
) -> Result<ServiceToken, AutoRefreshError> {
if let Ok(token) = state.service_token() {
if state.token.as_ref().is_some_and(|t| t.is_usable_at(now)) {
return Ok(token);
}
}
// Token crossed real expiry during in-flight refresh. Wait for the
// refresh to complete rather than returning Expired.
let notified = self.refresh_notify.notified();
drop(state);
notified.await;
// Re-check after wake — refresh may have failed. Re-read the clock: an
// arbitrary amount of time may have passed while awaiting the refresh.
let now = self.clock.now_unix_secs();
let state = self.state.lock().await;
state.require_usable_token(now)
}
/// Token is expiring but still usable — drop the lock, refresh in the
/// background of this call, and return the old (still-valid) token.
///
/// Takes `MutexGuard` by value because the lock is dropped before the HTTP
/// request. Notifies waiters after the refresh completes (success or error).
///
/// A [`CancelGuard`] ensures that if this future is cancelled at any point
/// before the new token is installed — including the post-HTTP save +
/// install window — `refresh_in_progress` is cleared and waiters are
/// notified, so subsequent callers don't hang in
/// [`wait_for_in_flight_refresh`](Self::wait_for_in_flight_refresh).
/// The credential is not restored on cancellation (it's already gone from
/// `state.token`), so the next caller will get whatever the cached token
/// offers — usable, expired, or absent.
async fn refresh_non_blocking(
&self,
state: MutexGuard<'_, State>,
credential: R::Credential,
) -> Result<ServiceToken, AutoRefreshError> {
let current_service_token = state.service_token()?;
drop(state);
let mut guard = CancelGuard {
in_progress: &self.refresh_in_progress,
notify: &self.refresh_notify,
defused: false,
};
match self.refresher.refresh(&credential).await {
Ok(new_token) => {
self.save_refreshed_token(&new_token).await;
let mut state = self.state.lock().await;
let _ = self.install_refreshed_token(&mut state, new_token);
guard.defuse();
}
Err(err) => {
tracing::warn!(%err, "token refresh failed (token still usable)");
// Defer `defuse()` until after the lock acquire so the
// CancelGuard's Drop still fires if cancellation lands on
// `state.lock().await`. Without this the in-progress flag
// would stay set with no `notify_waiters`, wedging every
// subsequent caller exactly like the Ok-path bug fixed
// earlier in this file.
let mut state = self.state.lock().await;
if let Some(token) = state.token.as_mut() {
self.refresher.restore(token, credential);
}
self.refresh_in_progress.store(false, Ordering::Release);
guard.defuse();
}
}
self.refresh_notify.notify_waiters();
Ok(current_service_token)
}
/// Token is fully expired — refresh while holding the lock so concurrent
/// callers block on `lock().await` until the new token is available.
///
/// A [`CancelGuard`] ensures that if this future is cancelled at any point
/// before the new token is installed — including the post-HTTP save
/// window — `refresh_in_progress` is cleared and waiters are notified so
/// they don't hang indefinitely. (The credential is lost on cancel —
/// see [`CancelGuard`] docs — but subsequent callers will get `Expired`
/// rather than blocking forever.)
async fn refresh_blocking(
&self,
state: &mut State,
credential: R::Credential,
) -> Result<ServiceToken, AutoRefreshError> {
let mut guard = CancelGuard {
in_progress: &self.refresh_in_progress,
notify: &self.refresh_notify,
defused: false,
};
match self.refresher.refresh(&credential).await {
Ok(new_token) => {
self.save_refreshed_token(&new_token).await;
let token = self.install_refreshed_token(state, new_token);
guard.defuse();
Ok(token)
}
Err(err) => {
guard.defuse();
tracing::warn!(%err, "token refresh failed");
if let Some(token) = state.token.as_mut() {
self.refresher.restore(token, credential);
}
self.refresh_in_progress.store(false, Ordering::Release);
Err(AutoRefreshError::Expired)
}
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::device_session_refresher::DeviceSessionRefresher;
use crate::SecretToken;
use mocktail::prelude::*;
use stack_profile::ProfileStore;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
#[test]
fn auto_refresh_error_maps_to_public_auth_error() {
use crate::AuthError;
assert!(matches!(
AuthError::from(AutoRefreshError::NotFound),
AuthError::NotAuthenticated(_)
));
assert!(matches!(
AuthError::from(AutoRefreshError::Expired),
AuthError::TokenExpired(_)
));
// The `Auth` variant passes the inner error through unchanged.
assert!(matches!(
AuthError::from(AutoRefreshError::Auth(AuthError::AccessDenied(
crate::error::AccessDenied
))),
AuthError::AccessDenied(_)
));
}
fn make_token(access: &str, expires_in: u64, refresh: bool) -> Token {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
Token {
access_token: SecretToken::new(access),
token_type: "Bearer".to_string(),
expires_at: now + expires_in,
refresh_token: if refresh {
Some(SecretToken::new("test-refresh-token"))
} else {
None
},
region: None,
client_id: None,
device_instance_id: None,
}
}
fn refresh_response_json(access: &str) -> serde_json::Value {
serde_json::json!({
"access_token": access,
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "new-refresh-token"
})
}
fn error_json(error: &str) -> serde_json::Value {
serde_json::json!({
"error": error,
"error_description": format!("{error} occurred")
})
}
async fn start_server(mocks: MockSet) -> MockServer {
let server = MockServer::new_http("auto-refresh-test").with_mocks(mocks);
server.start().await.unwrap();
server
}
fn auto_refresh_with_token(
dir: &tempfile::TempDir,
server: &MockServer,
token: Token,
) -> AutoRefresh<DeviceSessionRefresher> {
let store = ProfileStore::new(dir.path());
store.init_workspace("ZVATKW3VHMFG27DY").unwrap();
let ws_store = store.current_workspace_store().unwrap();
ws_store.save_profile(&token).unwrap();
let refresher = DeviceSessionRefresher::new(
Some(ws_store),
server.url(""),
"cli",
"ap-southeast-2.aws",
None,
);
AutoRefresh::with_token(refresher, token)
}
mod given_no_cached_token {
use super::*;
#[tokio::test]
async fn returns_not_found_for_oauth() {
let server = start_server(MockSet::new()).await;
let store = ProfileStore::new("/tmp/nonexistent");
let refresher = DeviceSessionRefresher::new(
Some(store),
server.url(""),
"cli",
"ap-southeast-2.aws",
None,
);
let strategy = AutoRefresh::with_store(refresher, NoStore);
let err = strategy.get_token().await.unwrap_err();
assert!(
matches!(err, AutoRefreshError::NotFound),
"expected NotFound, got: {err:?}"
);
}
}
mod given_fresh_token {
use super::*;
#[tokio::test]
async fn returns_cached_token() {
let dir = tempfile::tempdir().unwrap();
let server = start_server(MockSet::new()).await;
let strategy =
auto_refresh_with_token(&dir, &server, make_token("my-access-token", 3600, false));
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"my-access-token",
"should return the cached access token"
);
}
#[tokio::test]
async fn caches_across_calls() {
let dir = tempfile::tempdir().unwrap();
let server = start_server(MockSet::new()).await;
let strategy =
auto_refresh_with_token(&dir, &server, make_token("my-access-token", 3600, false));
let token1 = strategy.get_token().await.unwrap();
assert_eq!(
token1.as_str(),
"my-access-token",
"first call should return the cached token"
);
// Delete the file — second call should still return the cached token.
std::fs::remove_file(
dir.path()
.join("workspaces")
.join("ZVATKW3VHMFG27DY")
.join("auth.json"),
)
.unwrap();
let token2 = strategy.get_token().await.unwrap();
assert_eq!(
token2.as_str(),
"my-access-token",
"second call should return the cached token even after file deletion"
);
}
#[tokio::test]
async fn does_not_trigger_refresh() {
// Mock that would fail if hit — proves no refresh request is made.
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.internal_server_error()
.json(error_json("should_not_be_called"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("fresh-token", 3600, true));
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"fresh-token",
"should return fresh token without triggering refresh"
);
}
}
mod given_fully_expired_token {
use super::*;
mod without_refresh_token {
use super::*;
#[tokio::test]
async fn returns_expired() {
let dir = tempfile::tempdir().unwrap();
let server = start_server(MockSet::new()).await;
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, false));
let err = strategy.get_token().await.unwrap_err();
assert!(
matches!(err, AutoRefreshError::Expired),
"expected Expired, got: {err:?}"
);
}
}
mod with_refresh_token {
use super::*;
#[tokio::test]
async fn refreshes_and_returns_new_token() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-token",
"should return the refreshed token"
);
}
#[tokio::test]
async fn persists_refreshed_token_to_disk() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
let _ = strategy.get_token().await.unwrap();
// Verify the refreshed token was saved to the workspace directory.
let store = ProfileStore::new(dir.path());
let ws_store = store.current_workspace_store().unwrap();
let on_disk: Token = ws_store.load_profile().unwrap();
assert_eq!(
on_disk.access_token().as_str(),
"refreshed-token",
"refreshed token should be persisted to disk"
);
}
#[tokio::test]
async fn returns_expired_on_refresh_failure() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.bad_request().json(error_json("invalid_grant"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
let err = strategy.get_token().await.unwrap_err();
assert!(
matches!(err, AutoRefreshError::Expired),
"expected Expired after failed refresh, got: {err:?}"
);
}
#[tokio::test]
async fn restores_refresh_token_after_failure() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.bad_request().json(error_json("invalid_grant"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
// First call: refresh fails, returns Expired.
let err = strategy.get_token().await.unwrap_err();
assert!(
matches!(err, AutoRefreshError::Expired),
"expected Expired on first attempt, got: {err:?}"
);
// Verify the refresh token was restored so a retry is possible.
let state = strategy.state.lock().await;
assert!(
state.token.is_some(),
"token should still be cached after failed refresh"
);
assert!(
state.token.as_ref().unwrap().refresh_token().is_some(),
"refresh token should be restored for retry"
);
drop(state);
// Replace mock with a success response.
server.mocks().clear();
server.mocks().mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
// Second call: refresh token is available → retry succeeds.
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-token",
"retry should succeed with restored refresh token"
);
}
#[tokio::test]
async fn sequential_calls_only_refresh_once() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-once"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
// First call triggers refresh.
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-once",
"first call should trigger refresh"
);
// Swap mock to track if another refresh is attempted.
server.mocks().clear();
server.mocks().mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-twice"));
});
// Calls 2-5: the refreshed token is fresh, so no further refresh.
for _ in 0..4 {
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-once",
"should return cached refreshed token, not trigger another refresh"
);
}
}
#[tokio::test]
async fn prevents_second_refresh_after_success() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy =
auto_refresh_with_token(&dir, &server, make_token("old-token", 0, true));
// First call refreshes successfully.
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-token",
"first call should refresh the token"
);
// Replace the mock with one that errors.
server.mocks().clear();
server.mocks().mock(|when, then| {
when.post().path("/oauth/token");
then.bad_request().json(error_json("should_not_be_called"));
});
// Second call should return the refreshed token without hitting
// the server again (the new token has a fresh expiry).
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"refreshed-token",
"second call should return cached refreshed token"
);
}
}
}
mod given_expiring_but_usable_token {
use super::*;
mod when_refresh_fails {
use super::*;
#[tokio::test]
async fn returns_current_token() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.bad_request().json(error_json("server_error"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
// Token expires in 30s (within the 90s leeway so is_expired() = true),
// but the access token is still technically usable.
let strategy =
auto_refresh_with_token(&dir, &server, make_token("still-usable", 30, true));
// The refresh fails, but the access token should still be returned
// because it's still usable (30s remaining > 0).
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"still-usable",
"should return still-usable token despite failed refresh"
);
// Verify the access token and refresh token are still present.
let state = strategy.state.lock().await;
assert!(state.token.is_some(), "token should still be cached");
assert_eq!(
state.token.as_ref().unwrap().access_token().as_str(),
"still-usable",
"access token should be unchanged after failed refresh"
);
assert!(
state.token.as_ref().unwrap().refresh_token().is_some(),
"refresh token should be restored after failed refresh"
);
}
#[tokio::test]
async fn restores_refresh_token_for_retry() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.bad_request().json(error_json("server_error"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
// Token expires in 30s — is_expired() = true, is_usable() = true.
let strategy =
auto_refresh_with_token(&dir, &server, make_token("still-usable", 30, true));
// First call: refresh fails, but the still-usable token is returned.
let token = strategy.get_token().await.unwrap();
assert_eq!(
token.as_str(),
"still-usable",
"first call should return still-usable token"
);
// Replace mock with a success response.
server.mocks().clear();
server.mocks().mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
// Second call: refresh token was restored, so the retry succeeds.
let token = strategy.get_token().await.unwrap();
assert!(
token.as_str() == "still-usable" || token.as_str() == "refreshed-token",
"expected old or refreshed token, got: {}",
token.as_str()
);
// Verify the cache now holds the refreshed token.
let state = strategy.state.lock().await;
assert_eq!(
state.token.as_ref().unwrap().access_token().as_str(),
"refreshed-token",
"cache should hold the refreshed token after retry"
);
}
}
}
mod given_concurrent_callers {
use super::*;
#[tokio::test]
async fn returns_usable_token_while_refreshing() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&server,
make_token("still-usable", 30, true),
));
let s1 = Arc::clone(&strategy);
let handle_a = tokio::spawn(async move { s1.get_token().await.unwrap() });
let s2 = Arc::clone(&strategy);
let handle_b = tokio::spawn(async move { s2.get_token().await.unwrap() });
let (result_a, result_b) = tokio::join!(handle_a, handle_b);
let token_a = result_a.unwrap();
let token_b = result_b.unwrap();
assert!(
token_a.as_str() == "still-usable" || token_a.as_str() == "refreshed-token",
"unexpected token_a: {}",
token_a.as_str()
);
assert!(
token_b.as_str() == "still-usable" || token_b.as_str() == "refreshed-token",
"unexpected token_b: {}",
token_b.as_str()
);
}
#[tokio::test]
async fn blocks_until_refresh_completes() {
let mut mocks = MockSet::new();
mocks.mock(|when, then| {
when.post().path("/oauth/token");
then.json(refresh_response_json("refreshed-token"));
});
let server = start_server(mocks).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&server,
make_token("expired-token", 0, true),
));
let s1 = Arc::clone(&strategy);
let handle_a = tokio::spawn(async move { s1.get_token().await.unwrap() });
let s2 = Arc::clone(&strategy);
let handle_b = tokio::spawn(async move { s2.get_token().await.unwrap() });
let (result_a, result_b) = tokio::join!(handle_a, handle_b);
let token_a = result_a.unwrap();
let token_b = result_b.unwrap();
assert_eq!(
token_a.as_str(),
"refreshed-token",
"caller a should receive refreshed token"
);
assert_eq!(
token_b.as_str(),
"refreshed-token",
"caller b should receive refreshed token"
);
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod stress_tests {
use super::*;
use crate::device_session_refresher::DeviceSessionRefresher;
use crate::SecretToken;
use stack_profile::ProfileStore;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
/// Tracks in-flight and peak concurrency for test assertions.
#[derive(Clone)]
struct CountingState {
total: Arc<AtomicUsize>,
current: Arc<AtomicUsize>,
peak: Arc<AtomicUsize>,
}
impl CountingState {
fn new() -> Self {
Self {
total: Arc::new(AtomicUsize::new(0)),
current: Arc::new(AtomicUsize::new(0)),
peak: Arc::new(AtomicUsize::new(0)),
}
}
fn enter(&self) {
self.total.fetch_add(1, Ordering::SeqCst);
let prev = self.current.fetch_add(1, Ordering::SeqCst);
self.peak.fetch_max(prev + 1, Ordering::SeqCst);
}
fn exit(&self) {
self.current.fetch_sub(1, Ordering::SeqCst);
}
fn peak(&self) -> usize {
self.peak.load(Ordering::SeqCst)
}
fn total(&self) -> usize {
self.total.load(Ordering::SeqCst)
}
}
#[derive(Clone)]
struct DelayedRefreshState {
counting: CountingState,
delay: Duration,
}
async fn delayed_refresh_handler(
axum::extract::State(state): axum::extract::State<DelayedRefreshState>,
) -> axum::Json<serde_json::Value> {
state.counting.enter();
tokio::time::sleep(state.delay).await;
state.counting.exit();
axum::Json(serde_json::json!({
"access_token": "refreshed-token",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "new-refresh-token"
}))
}
async fn delayed_error_handler(
axum::extract::State(state): axum::extract::State<DelayedRefreshState>,
) -> (axum::http::StatusCode, axum::Json<serde_json::Value>) {
state.counting.enter();
tokio::time::sleep(state.delay).await;
state.counting.exit();
(
axum::http::StatusCode::BAD_REQUEST,
axum::Json(serde_json::json!({
"error": "invalid_grant",
"error_description": "invalid_grant occurred"
})),
)
}
async fn start_axum_server<H, T>(
handler: H,
state: DelayedRefreshState,
) -> (url::Url, CountingState)
where
H: axum::handler::Handler<T, DelayedRefreshState> + Clone + Send + 'static,
T: 'static,
{
let counting = state.counting.clone();
let app = axum::Router::new()
.route("/oauth/token", axum::routing::post(handler))
.with_state(state);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
let base_url = url::Url::parse(&format!("http://{addr}")).unwrap();
(base_url, counting)
}
fn make_token(access: &str, expires_in: u64, refresh: bool) -> Token {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
Token {
access_token: SecretToken::new(access),
token_type: "Bearer".to_string(),
expires_at: now + expires_in,
refresh_token: if refresh {
Some(SecretToken::new("test-refresh-token"))
} else {
None
},
region: None,
client_id: None,
device_instance_id: None,
}
}
fn auto_refresh_with_token(
dir: &tempfile::TempDir,
base_url: &url::Url,
token: Token,
) -> AutoRefresh<DeviceSessionRefresher> {
let store = ProfileStore::new(dir.path());
store.init_workspace("ZVATKW3VHMFG27DY").unwrap();
let ws_store = store.current_workspace_store().unwrap();
ws_store.save_profile(&token).unwrap();
let refresher = DeviceSessionRefresher::new(
Some(ws_store),
base_url.clone(),
"cli",
"ap-southeast-2.aws",
None,
);
AutoRefresh::with_token(refresher, token)
}
const CONCURRENCY: usize = 50;
mod given_fresh_token {
use super::*;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn all_callers_return_immediately() {
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_millis(500),
};
let (base_url, stats) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("fresh-token", 3600, true),
));
let start = Instant::now();
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy);
handles.push(tokio::spawn(async move { s.get_token().await.unwrap() }));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
let elapsed = start.elapsed();
for token in &results {
assert_eq!(
token.as_str(),
"fresh-token",
"all callers should receive the fresh token"
);
}
assert!(
elapsed < Duration::from_millis(200),
"expected < 200ms for fresh tokens, got {:?}",
elapsed
);
assert_eq!(stats.total(), 0, "no refresh requests should be made");
}
}
mod given_expiring_but_usable_token {
use super::*;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn non_blocking_reads_during_refresh() {
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_millis(500),
};
let (base_url, stats) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("still-usable", 30, true),
));
let start = Instant::now();
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy);
handles.push(tokio::spawn(async move {
let call_start = Instant::now();
let token = s.get_token().await.unwrap();
(token, call_start.elapsed())
}));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
let elapsed = start.elapsed();
for (token, _) in &results {
assert!(
token.as_str() == "still-usable" || token.as_str() == "refreshed-token",
"unexpected token: {}",
token.as_str()
);
}
let fast_callers = results
.iter()
.filter(|(_, dur)| *dur < Duration::from_millis(100))
.count();
assert!(
fast_callers >= CONCURRENCY - 1,
"expected at least {} fast callers, got {} (total elapsed: {:?})",
CONCURRENCY - 1,
fast_callers,
elapsed
);
assert_eq!(stats.peak(), 1, "peak concurrency to refresh endpoint");
assert_eq!(stats.total(), 1, "total refresh requests");
}
}
mod given_fully_expired_token {
use super::*;
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn all_callers_block_until_refresh() {
let refresh_delay = Duration::from_millis(200);
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: refresh_delay,
};
let (base_url, stats) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("expired-token", 0, true),
));
let start = Instant::now();
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy);
handles.push(tokio::spawn(async move { s.get_token().await.unwrap() }));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
let elapsed = start.elapsed();
for token in &results {
assert_eq!(
token.as_str(),
"refreshed-token",
"all callers should receive refreshed token"
);
}
assert!(
elapsed < refresh_delay + Duration::from_millis(200),
"expected < {:?} for blocked callers, got {:?}",
refresh_delay + Duration::from_millis(200),
elapsed
);
assert_eq!(stats.peak(), 1, "peak concurrency to refresh endpoint");
assert_eq!(stats.total(), 1, "total refresh requests");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn all_callers_receive_expired_on_failure() {
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_millis(10),
};
let (base_url, stats) = start_axum_server(delayed_error_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("expired-token", 0, true),
));
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy);
handles.push(tokio::spawn(async move { s.get_token().await }));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
for result in &results {
assert!(result.is_err(), "expected Expired error, got Ok");
let err = result.as_ref().unwrap_err();
assert!(
matches!(err, AutoRefreshError::Expired),
"expected Expired, got: {err:?}"
);
}
let state = strategy.state.lock().await;
assert!(
state.token.as_ref().unwrap().refresh_token().is_some(),
"refresh token should be restored after failed refresh"
);
drop(state);
assert_eq!(stats.peak(), 1, "peak concurrency to refresh endpoint");
assert!(
stats.total() >= 1,
"at least one refresh attempt should be made"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn retry_succeeds_after_failure() {
// Phase 1: Server returns errors.
let counting1 = CountingState::new();
let state1 = DelayedRefreshState {
counting: counting1.clone(),
delay: Duration::from_millis(50),
};
let (base_url, _) = start_axum_server(delayed_error_handler, state1).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("expired-token", 0, true),
));
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy);
handles.push(tokio::spawn(async move { s.get_token().await }));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
for result in &results {
assert!(
result.is_err(),
"first wave: expected Expired, got Ok({})",
result.as_ref().unwrap().as_str()
);
}
// Phase 2: New server that returns success.
let counting2 = CountingState::new();
let state2 = DelayedRefreshState {
counting: counting2.clone(),
delay: Duration::from_millis(50),
};
let (base_url2, stats2) = start_axum_server(delayed_refresh_handler, state2).await;
let strategy2 = Arc::new(auto_refresh_with_token(
&dir,
&base_url2,
make_token("expired-token", 0, true),
));
let mut handles = Vec::with_capacity(CONCURRENCY);
for _ in 0..CONCURRENCY {
let s = Arc::clone(&strategy2);
handles.push(tokio::spawn(async move { s.get_token().await.unwrap() }));
}
let results: Vec<_> = {
let mut results = Vec::with_capacity(handles.len());
for handle in handles {
results.push(handle.await.unwrap());
}
results
};
for token in &results {
assert_eq!(
token.as_str(),
"refreshed-token",
"retry callers should receive refreshed token"
);
}
assert_eq!(stats2.total(), 1, "only one retry refresh should be made");
}
}
mod given_cancelled_refresh {
use super::*;
/// If a blocking refresh (fully expired token) is cancelled mid-flight,
/// the `CancelGuard` must reset `refresh_in_progress` and notify waiters
/// so the next caller doesn't hang in `wait_for_in_flight_refresh`.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn blocked_callers_recover_after_cancellation() {
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_secs(10), // Very slow — will be cancelled
};
let (base_url, _) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("expired-token", 0, true),
));
// Spawn get_token and let the blocking refresh start.
let s = Arc::clone(&strategy);
let handle = tokio::spawn(async move { s.get_token().await });
tokio::time::sleep(Duration::from_millis(100)).await;
// Cancel the refresh mid-flight.
handle.abort();
let _ = handle.await;
// The next caller must not hang. The credential is lost (refresh
// token was taken before the HTTP call), so the result is Expired,
// but the important thing is that it completes promptly.
let s = Arc::clone(&strategy);
let result = tokio::time::timeout(Duration::from_secs(2), s.get_token()).await;
assert!(
result.is_ok(),
"get_token() should not hang after cancelled blocking refresh"
);
}
/// Regression test: cancellation in the window *after* the upstream
/// HTTP refresh succeeds but *before* the new token is installed must
/// still clear `refresh_in_progress` and notify waiters. The previous
/// implementation defused the [`CancelGuard`] before
/// `save_refreshed_token`, so a drop during the (async) store-save or
/// the subsequent state-lock acquire would strand the flag — wedging
/// any caller that later hit `wait_for_in_flight_refresh`.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn save_phase_cancellation_does_not_strand_in_progress_flag() {
use crate::token_store::TokenStore;
/// Store that returns a single pre-loaded token from `load()` and
/// delays inside `save()` long enough for a test to cancel.
struct SlowSaveStore {
initial: tokio::sync::Mutex<Option<Token>>,
delay: Duration,
}
impl TokenStore for SlowSaveStore {
async fn load(&self) -> Option<Token> {
self.initial.lock().await.take()
}
async fn save(&self, _token: &Token) {
tokio::time::sleep(self.delay).await;
}
}
// Fast upstream HTTP — refresh succeeds in <50ms.
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_millis(10),
};
let (base_url, _) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
let store = ProfileStore::new(dir.path());
store.init_workspace("ZVATKW3VHMFG27DY").unwrap();
let ws_store = store.current_workspace_store().unwrap();
let refresher = DeviceSessionRefresher::new(
Some(ws_store),
base_url,
"cli",
"ap-southeast-2.aws",
None,
);
// Slow async save — cancellation reliably lands here, in the
// post-HTTP / pre-install window.
let slow_store = SlowSaveStore {
initial: tokio::sync::Mutex::new(Some(make_token("expired-token", 0, true))),
delay: Duration::from_secs(10),
};
let strategy = Arc::new(AutoRefresh::with_store(refresher, slow_store));
// Trigger refresh; the task will complete the HTTP exchange and
// then block inside store.save (the slow async path).
let s = Arc::clone(&strategy);
let handle = tokio::spawn(async move { s.get_token().await });
// 200ms is comfortably past the 10ms HTTP delay but well inside
// the 10s save delay — so abort() lands during save_refreshed_token.
tokio::time::sleep(Duration::from_millis(200)).await;
handle.abort();
let _ = handle.await;
// The CancelGuard must have cleared refresh_in_progress on drop.
// If the old (pre-fix) code regresses, the flag stays true and a
// subsequent caller wedges on wait_for_in_flight_refresh waiting
// for a notify that will never come — the timeout below catches it.
let s = Arc::clone(&strategy);
let result = tokio::time::timeout(Duration::from_secs(2), s.get_token()).await;
assert!(
result.is_ok(),
"get_token() should not hang after cancellation in the save/install window"
);
}
/// If a non-blocking refresh (expiring-but-usable token) is cancelled
/// mid-flight, the `CancelGuard` must reset `refresh_in_progress` and
/// notify waiters so they don't hang once the token crosses real expiry.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn non_blocking_callers_recover_after_cancellation() {
let counting = CountingState::new();
let state = DelayedRefreshState {
counting: counting.clone(),
delay: Duration::from_secs(10), // Very slow — will be cancelled
};
let (base_url, _) = start_axum_server(delayed_refresh_handler, state).await;
let dir = tempfile::tempdir().unwrap();
// Token expires in 30s — is_expired() = true, is_usable() = true.
let strategy = Arc::new(auto_refresh_with_token(
&dir,
&base_url,
make_token("still-usable", 30, true),
));
// Spawn get_token — triggers non-blocking refresh, drops lock, then
// blocks on the slow HTTP call.
let s = Arc::clone(&strategy);
let handle = tokio::spawn(async move { s.get_token().await });
tokio::time::sleep(Duration::from_millis(100)).await;
// Cancel the refresh mid-flight.
handle.abort();
let _ = handle.await;
// The next caller must not hang. The token is still usable so it
// should be returned even though the refresh was cancelled.
let s = Arc::clone(&strategy);
let result = tokio::time::timeout(Duration::from_secs(2), s.get_token()).await;
assert!(
result.is_ok(),
"get_token() should not hang after cancelled non-blocking refresh"
);
let result = result.unwrap();
assert!(
result.is_ok(),
"expected Ok with still-usable token, got: {:?}",
result.unwrap_err()
);
}
}
}
/// Deterministic regression test for the "token crosses real expiry while a
/// non-blocking refresh is in flight" race.
///
/// Before the fix, late-arriving callers saw `refresh_in_progress = true` +
/// `!is_usable()` and returned `Err(Expired)` instead of waiting for the
/// in-flight refresh. The original reproduction (a wall-clock stress test) hung
/// the outcome on a ~1-second window — token expiry has whole-second
/// granularity — which made it latently flaky and broke outright under coverage
/// instrumentation. This version drives expiry with a [`TestClock`] and gates
/// the refresh with a [`Notify`], so it is fully deterministic: no real sleeps,
/// no network.
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod expiry_crossing_regression {
use super::*;
use crate::clock::TestClock;
use crate::{AuthError, SecretToken};
use std::future::Future;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
/// Number of callers that arrive after the token crosses real expiry.
const WAITERS: usize = 8;
/// A [`Refresher`] whose `refresh` blocks on a test-controlled gate, so the
/// test can hold a refresh "in flight" while it advances the clock and
/// launches waiters — with no wall-clock timing involved.
struct GatedRefresher {
/// Notified once `refresh` is entered (the refresh is now in flight).
started: Arc<Notify>,
/// `refresh` awaits this; the test releases it to complete the refresh.
gate: Arc<Notify>,
/// Counts `refresh` invocations — asserts exactly one refresh happens.
calls: Arc<AtomicUsize>,
/// Absolute expiry stamped on the refreshed token.
refreshed_expires_at: u64,
}
impl Refresher for GatedRefresher {
type Credential = ();
fn save(&self, _token: &Token) {}
fn try_credential(&self, token: Option<&mut Token>) -> Option<Self::Credential> {
// Refresh only when there's a token to refresh, matching the real
// refreshers' "needs a prior token" contract.
token.map(|_| ())
}
fn restore(&self, _token: &mut Token, _credential: Self::Credential) {}
fn refresh(
&self,
_credential: &Self::Credential,
) -> impl Future<Output = Result<Token, AuthError>> + Send {
let started = Arc::clone(&self.started);
let gate = Arc::clone(&self.gate);
let calls = Arc::clone(&self.calls);
let refreshed_expires_at = self.refreshed_expires_at;
async move {
calls.fetch_add(1, Ordering::SeqCst);
started.notify_one();
gate.notified().await;
Ok(make_token("refreshed-token", refreshed_expires_at))
}
}
}
fn make_token(access: &str, expires_at: u64) -> Token {
Token {
access_token: SecretToken::new(access),
refresh_token: Some(SecretToken::new("refresh-token")),
token_type: "Bearer".to_string(),
expires_at,
region: None,
client_id: None,
device_instance_id: None,
}
}
#[tokio::test]
async fn waiters_wait_for_refresh_when_token_crosses_expiry() {
let clock = TestClock::new(1_000_000);
let started = Arc::new(Notify::new());
let gate = Arc::new(Notify::new());
let calls = Arc::new(AtomicUsize::new(0));
let refresher = GatedRefresher {
started: Arc::clone(&started),
gate: Arc::clone(&gate),
calls: Arc::clone(&calls),
refreshed_expires_at: clock.now() + 3600,
};
// Within the 90s leeway (so a refresh is triggered) but still usable at
// the current clock value (so the first caller takes the non-blocking
// path and gets the old token).
let token = make_token("expiring-soon", clock.now() + 10);
let strategy = Arc::new(AutoRefresh::with_token_and_clock(
refresher,
token,
clock.shared(),
));
// 1. First caller starts the non-blocking refresh.
let first = {
let s = Arc::clone(&strategy);
tokio::spawn(async move { s.get_token().await })
};
// Wait until the refresh is actually in flight (gated; won't complete yet).
started.notified().await;
// 2. Advance the clock past real expiry while the refresh is still gated.
// The token is now both expired and unusable.
clock.advance(20);
// 3. Launch waiters. They observe refresh_in_progress + !is_usable, so they
// must wait for the in-flight refresh rather than returning Expired.
let waiters: Vec<_> = (0..WAITERS)
.map(|_| {
let s = Arc::clone(&strategy);
tokio::spawn(async move { s.get_token().await })
})
.collect();
// Let the waiters reach their wait point. This is cooperative scheduling
// on the current-thread runtime (yielding lets the spawned waiters run
// until they park on the refresh notification), not a wall-clock delay.
for _ in 0..32 {
tokio::task::yield_now().await;
}
// 4. Release the refresh. The first caller installs the new token and
// notifies the waiters, which then return the refreshed token.
gate.notify_one();
let first = first.await.unwrap().unwrap();
assert_eq!(
first.as_str(),
"expiring-soon",
"first caller receives the old token (still usable when it was called)"
);
for (i, waiter) in waiters.into_iter().enumerate() {
let token = waiter.await.unwrap().unwrap_or_else(|e| {
panic!("waiter {i} returned Err({e:?}), expected the refreshed token")
});
assert_eq!(
token.as_str(),
"refreshed-token",
"waiter {i} should receive the refreshed token, not Expired"
);
}
assert_eq!(
calls.load(Ordering::SeqCst),
1,
"exactly one refresh should occur for all callers combined"
);
}
/// A [`Refresher`] like [`GatedRefresher`], but whose gated `refresh`
/// resolves to `Err` once released — so the test can exercise the *failure*
/// axis of the in-flight-refresh race.
struct FailingGatedRefresher {
started: Arc<Notify>,
gate: Arc<Notify>,
calls: Arc<AtomicUsize>,
}
impl Refresher for FailingGatedRefresher {
type Credential = ();
fn save(&self, _token: &Token) {}
fn try_credential(&self, token: Option<&mut Token>) -> Option<Self::Credential> {
token.map(|_| ())
}
fn restore(&self, _token: &mut Token, _credential: Self::Credential) {}
fn refresh(
&self,
_credential: &Self::Credential,
) -> impl Future<Output = Result<Token, AuthError>> + Send {
let started = Arc::clone(&self.started);
let gate = Arc::clone(&self.gate);
let calls = Arc::clone(&self.calls);
async move {
calls.fetch_add(1, Ordering::SeqCst);
started.notify_one();
gate.notified().await;
Err(AuthError::TokenExpired(crate::error::TokenExpired))
}
}
}
/// The failure counterpart to
/// [`waiters_wait_for_refresh_when_token_crosses_expiry`]: when the in-flight
/// refresh *fails* and the clock has crossed real expiry, waiters waking in
/// [`AutoRefresh::wait_for_in_flight_refresh`] must re-read the clock, find
/// the cached token unusable via `require_usable_token(now)`, and return
/// `Expired` — they must not hang, and must not hand back a stale token. This
/// is exactly the branch the post-wake clock re-read (the `now` re-read after
/// `notified().await`) exists to make correct.
#[tokio::test]
async fn waiters_get_expired_when_in_flight_refresh_fails() {
let clock = TestClock::new(1_000_000);
let started = Arc::new(Notify::new());
let gate = Arc::new(Notify::new());
let calls = Arc::new(AtomicUsize::new(0));
let refresher = FailingGatedRefresher {
started: Arc::clone(&started),
gate: Arc::clone(&gate),
calls: Arc::clone(&calls),
};
// Within the 90s leeway (triggers a refresh) but still usable now, so the
// first caller takes the non-blocking path and captures the old token.
let token = make_token("expiring-soon", clock.now() + 10);
let strategy = Arc::new(AutoRefresh::with_token_and_clock(
refresher,
token,
clock.shared(),
));
// 1. First caller starts the (gated) non-blocking refresh.
let first = {
let s = Arc::clone(&strategy);
tokio::spawn(async move { s.get_token().await })
};
started.notified().await;
// 2. Advance the clock past real expiry while the refresh is still gated.
// The cached token is now both expired and unusable.
clock.advance(20);
// 3. Launch waiters. They observe refresh_in_progress + !is_usable, so
// they park in wait_for_in_flight_refresh rather than returning early.
let waiters: Vec<_> = (0..WAITERS)
.map(|_| {
let s = Arc::clone(&strategy);
tokio::spawn(async move { s.get_token().await })
})
.collect();
for _ in 0..32 {
tokio::task::yield_now().await;
}
// 4. Release the refresh → it returns Err. The first caller still returns
// the old token it captured while it was usable; the waiters re-read
// the now-advanced clock, find the token unusable, and get Expired.
gate.notify_one();
let first = first.await.unwrap();
assert_eq!(
first.unwrap().as_str(),
"expiring-soon",
"first caller keeps the old token it captured before the refresh failed"
);
for (i, waiter) in waiters.into_iter().enumerate() {
let result = waiter.await.unwrap();
assert!(
matches!(result, Err(AutoRefreshError::Expired)),
"waiter {i} should get Expired after the failed refresh, got: {result:?}"
);
}
assert_eq!(
calls.load(Ordering::SeqCst),
1,
"exactly one refresh attempt for all callers combined"
);
}
/// A [`Refresher`] whose `refresh` panics if it is ever called, so a test can
/// assert that no refresh is triggered.
struct NeverRefresher;
impl Refresher for NeverRefresher {
type Credential = ();
fn save(&self, _token: &Token) {}
fn try_credential(&self, token: Option<&mut Token>) -> Option<Self::Credential> {
token.map(|_| ())
}
fn restore(&self, _token: &mut Token, _credential: Self::Credential) {}
// Keep the explicit `impl Future + Send` form to match the sibling test
// refreshers; the trivial body would otherwise trip `manual_async_fn`.
#[allow(clippy::manual_async_fn)]
fn refresh(
&self,
_credential: &Self::Credential,
) -> impl Future<Output = Result<Token, AuthError>> + Send {
async { panic!("refresh must not be called while the token reads as fresh") }
}
}
/// A wall clock running *backwards* (NTP step, VM snapshot restore) must not
/// panic or spuriously force a refresh. Each `get_token` call samples `now`
/// once and re-evaluates the pure `is_expired_at`/`is_usable_at` predicates,
/// and the only time subtraction in the crate (`Token::expires_in`) saturates
/// — so there is no cross-call delta to underflow. A rewind simply makes the
/// token read as fresh again.
#[tokio::test]
async fn backwards_clock_does_not_panic_or_force_refresh() {
let clock = TestClock::new(1_000_000);
// Fresh token: expires well beyond the 90s leeway.
let token = make_token("fresh", clock.now() + 3600);
let strategy = AutoRefresh::with_token_and_clock(NeverRefresher, token, clock.shared());
// Forward reading returns the cached token without refreshing.
assert_eq!(strategy.get_token().await.unwrap().as_str(), "fresh");
// The wall clock jumps 100_000s into the past.
clock.set(900_000);
// Still fresh, still no refresh (NeverRefresher would panic), no hang.
assert_eq!(strategy.get_token().await.unwrap().as_str(), "fresh");
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod regression_cip_3159 {
use super::*;
use crate::access_key_refresher::AccessKeyRefresher;
use crate::SecretToken;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// `/api/authorise` handler that sleeps `delay` before returning a valid
/// access-key token response (with an ABSOLUTE-epoch `expiry`, as CTS
/// returns), giving the test a window to cancel in.
async fn delayed_authorise_handler(
axum::extract::State(delay): axum::extract::State<Duration>,
) -> axum::Json<serde_json::Value> {
tokio::time::sleep(delay).await;
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
axum::Json(serde_json::json!({
"accessToken": "refreshed-token",
"expiry": now + 3600
}))
}
async fn start_authorise_server(delay: Duration) -> url::Url {
let app = axum::Router::new()
.route(
"/api/authorise",
axum::routing::post(delayed_authorise_handler),
)
.with_state(delay);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
url::Url::parse(&format!("http://{addr}")).unwrap()
}
/// is_expired() == true (within the 90s leeway, so `get_token` refreshes),
/// but is_usable() == true for `secs_until_expiry` (so it takes the
/// non-blocking path).
fn expiring_but_usable_token(access: &str, secs_until_expiry: u64) -> Token {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
Token {
access_token: SecretToken::new(access),
token_type: "Bearer".to_string(),
expires_at: now + secs_until_expiry,
refresh_token: None,
region: None,
client_id: None,
device_instance_id: None,
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn cancellation_in_relock_window_does_not_strand_refresh() {
let http_delay = Duration::from_millis(400);
let base_url = start_authorise_server(http_delay).await;
let strategy = Arc::new(AutoRefresh::with_token(
AccessKeyRefresher::new(
SecretToken::new("CSAKtestKeyId.testKeySecret"),
base_url,
None,
),
expiring_but_usable_token("old-usable", 2),
));
// Caller A drives the refresh: it locks state, sets the in-progress
// flag, drops the lock, then awaits the (slow) HTTP authorise call.
let a = Arc::clone(&strategy);
let handle = tokio::spawn(async move { a.get_token().await });
// Let A reach the HTTP await, then take the state lock so that when A's
// request completes it parks on its post-HTTP `state.lock().await`
// instead of installing the new token.
tokio::time::sleep(Duration::from_millis(100)).await;
let held = strategy.state.lock().await;
// A's HTTP completes (~400ms) and blocks on the lock we hold.
tokio::time::sleep(http_delay + Duration::from_millis(200)).await;
assert!(
strategy.refresh_in_progress.load(Ordering::Acquire),
"precondition: a refresh should be in flight while caller A is parked",
);
// Cancel A precisely in the post-HTTP, pre-install window.
handle.abort();
let _ = handle.await;
drop(held);
// The CancelGuard's Drop must have cleared the flag on cancellation.
// Pre-fix, defuse() ran before the re-lock, so this stays `true`.
assert!(
!strategy.refresh_in_progress.load(Ordering::Acquire),
"refresh_in_progress stranded `true` after cancellation in the re-lock window (CIP-3159)",
);
// End-to-end: once the cached token crosses real expiry, a stranded flag
// would route the next caller into wait_for_in_flight_refresh and hang on
// a notify that never comes. With the fix, the caller re-authenticates.
tokio::time::sleep(Duration::from_millis(2100)).await;
let b = Arc::clone(&strategy);
let result =
tokio::time::timeout(Duration::from_secs(3), async move { b.get_token().await }).await;
assert!(
matches!(result, Ok(Ok(_))),
"get_token() hung or failed after cancellation — refresh wedged (CIP-3159): {result:?}",
);
}
}