|
| 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