Repository navigation
Commit 9511479
fix(openapi): handle HTTP 401 in RestApiTool and recover stale OAuth2 credentials
Fixes #7085
Credit to Philipp Horstenkamp, who reported the issue and first fixed it in PR
#7087. That fix recovers a conversation started after the token went bad, but
leaves the rejected credential in place for one already in progress, which is
the half this CL adds; three of its negative tests are carried over here.
Previously, when an OAuth2 API call returned HTTP 401 because an access token
expired or was revoked, RestApiTool reported the error but left the stale
credential in session state, permanently blocking subsequent tool calls.
This change introduces automated recovery in ToolAuthHandler:
- Reactive refresh: If a refresh token is present, automatically refreshes the
access token and retries the API call once. Preserves rotated refresh tokens
and prevents clobbering existing tokens during refresh.
- User re-authorization: If refresh fails or is unavailable, evicts the stale
credential and requests re-authorization for interactive OAuth2 flows.
- Concurrency & recursion guard: Added claim_recovery() context manager to
serialize recovery per credential across concurrent calls and bound retry
recursion.
- Budget limits: Caps re-authorization requests and reactive refreshes per
credential to prevent infinite recovery loops when an API persistently
returns 401. Non-401 responses refill the recovery budgets.
PiperOrigin-RevId: 9824980061 parent 0add163 commit 9511479
5 files changed
Lines changed: 2958 additions & 20 deletions
File tree
- src/google/adk/tools/openapi_tool/openapi_spec_parser
- tests/unittests/tools/openapi_tool/openapi_spec_parser
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
| 22 | + | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| |||
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
80 | 78 | | |
81 | 79 | | |
82 | 80 | | |
| |||
522 | 520 | | |
523 | 521 | | |
524 | 522 | | |
525 | | - | |
526 | | - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
527 | 526 | | |
528 | 527 | | |
529 | | - | |
| 528 | + | |
| 529 | + | |
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
| |||
609 | 609 | | |
610 | 610 | | |
611 | 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 | + | |
612 | 638 | | |
613 | 639 | | |
614 | 640 | | |
| |||
621 | 647 | | |
622 | 648 | | |
623 | 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 | + | |
624 | 688 | | |
625 | 689 | | |
626 | 690 | | |
| |||
0 commit comments