Re-read disk after an `invalid_grant` and retry once if a peer rotated the refresh_token. `attempted` is the bundle whose refresh_token the IdP just rejected. If disk now holds a different refresh_token, a concurrent process won the rotation race — adopt and reuse it
(
self, attempted: dict, exc: _InvalidGrantError
)
| 1078 | return self._bundle["access_token"] |
| 1079 | |
| 1080 | def _recover_from_invalid_grant( |
| 1081 | self, attempted: dict, exc: _InvalidGrantError |
| 1082 | ) -> dict: |
| 1083 | """Re-read disk after an `invalid_grant` and retry once if a peer |
| 1084 | rotated the refresh_token. |
| 1085 | |
| 1086 | `attempted` is the bundle whose refresh_token the IdP just |
| 1087 | rejected. If disk now holds a different refresh_token, a |
| 1088 | concurrent process won the rotation race — adopt and reuse it |
| 1089 | (returning early if it is already fresh, otherwise refreshing |
| 1090 | with it). If disk offers nothing new, the rejection is genuine: |
| 1091 | re-raise so the caller sees the re-authenticate hint. |
| 1092 | """ |
| 1093 | disk = _read_oidc_token_bundle(self._gateway_dir) |
| 1094 | if disk is None or disk.get("refresh_token") == attempted.get("refresh_token"): |
| 1095 | # No peer rotation — the refresh_token really is dead. |
| 1096 | raise exc |
| 1097 | if _normalize_issuer(disk) != _normalize_issuer(attempted): |
| 1098 | self._token_endpoint = None |
| 1099 | if self._is_fresh(disk): |
| 1100 | return disk |
| 1101 | # Single retry with the peer's rotated token; a second |
| 1102 | # _InvalidGrantError here propagates (no further retry). |
| 1103 | return self._refresh(disk) |
| 1104 | |
| 1105 | @staticmethod |
| 1106 | def _is_fresh(bundle: dict) -> bool: |
no test coverage detected