Skip to content

Commit 460c030

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add a base credential refresher interface
PiperOrigin-RevId: 771625930
1 parent 28dfcd2 commit 460c030

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

src/google/adk/auth/exchanger/base_credential_exchanger.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@
2424
from ..auth_schemes import AuthScheme
2525

2626

27+
class CredentialExchangError(Exception):
28+
"""Base exception for credential exchange errors."""
29+
30+
2731
@experimental
2832
class BaseCredentialExchanger(abc.ABC):
29-
"""Base interface for credential exchangers."""
33+
"""Base interface for credential exchangers.
34+
35+
Credential exchangers are responsible for exchanging credentials from
36+
one format or scheme to another.
37+
"""
3038

3139
@abc.abstractmethod
3240
async def exchange(
@@ -44,6 +52,6 @@ async def exchange(
4452
The exchanged credential.
4553
4654
Raises:
47-
ValueError: If credential exchange fails.
55+
CredentialExchangError: If credential exchange fails.
4856
"""
4957
pass
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Credential refresher module."""
16+
17+
from .base_credential_refresher import BaseCredentialRefresher
18+
19+
__all__ = [
20+
"BaseCredentialRefresher",
21+
]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Base credential refresher interface."""
16+
17+
from __future__ import annotations
18+
19+
import abc
20+
from typing import Optional
21+
22+
from google.adk.auth.auth_credential import AuthCredential
23+
from google.adk.auth.auth_schemes import AuthScheme
24+
from google.adk.utils.feature_decorator import experimental
25+
26+
27+
class CredentialRefresherError(Exception):
28+
"""Base exception for credential refresh errors."""
29+
30+
31+
@experimental
32+
class BaseCredentialRefresher(abc.ABC):
33+
"""Base interface for credential refreshers.
34+
35+
Credential refreshers are responsible for checking if a credential is expired
36+
or needs to be refreshed, and for refreshing it if necessary.
37+
"""
38+
39+
@abc.abstractmethod
40+
async def is_refresh_needed(
41+
self,
42+
auth_credential: AuthCredential,
43+
auth_scheme: Optional[AuthScheme] = None,
44+
) -> bool:
45+
"""Checks if a credential needs to be refreshed.
46+
47+
Args:
48+
auth_credential: The credential to check.
49+
auth_scheme: The authentication scheme (optional, some refreshers don't need it).
50+
51+
Returns:
52+
True if the credential needs to be refreshed, False otherwise.
53+
"""
54+
pass
55+
56+
@abc.abstractmethod
57+
async def refresh(
58+
self,
59+
auth_credential: AuthCredential,
60+
auth_scheme: Optional[AuthScheme] = None,
61+
) -> AuthCredential:
62+
"""Refreshes a credential if needed.
63+
64+
Args:
65+
auth_credential: The credential to refresh.
66+
auth_scheme: The authentication scheme (optional, some refreshers don't need it).
67+
68+
Returns:
69+
The refreshed credential.
70+
71+
Raises:
72+
CredentialRefresherError: If credential refresh fails.
73+
"""
74+
pass

0 commit comments

Comments
 (0)