8000 chore: Add in memory credential service (Experimental) · google/adk-python@ae99d07 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae99d07

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add in memory credential service (Experimental)
PiperOrigin-RevId: 771468462
1 parent a19d617 commit ae99d07

File tree

3 files changed

+417
-0
lines changed

3 files changed

+417
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
from __future__ import annotations
16+
17+
from typing import Optional
18+
19+
from typing_extensions import override
20+
21+
from ...tools.tool_context import ToolContext
22+
from ...utils.feature_decorator import experimental
23+
from ..auth_credential import AuthCredential
24+
from ..auth_tool import AuthConfig
25+
from .base_credential_service import BaseCredentialService
26+
27+
28+
@experimental
29+
class InMemoryCredentialService(BaseCredentialService):
30+
"""Class for in memory implementation of credential service(Experimental)"""
31+
32+
def __init__(self):
33+
super().__init__()
34+
self._store: dict[str, AuthCredential] = {}
35+
36+
@override
37+
async def load_credential(
38+
self,
39+
auth_config: AuthConfig,
40+
tool_context: ToolContext,
41+
) -> Optional[AuthCredential]:
42+
"""
43+
Loads the credential by auth config and current tool context from the
44+
backend credential store.
45+
46+
Args:
47+
auth_config: The auth config which contains the auth scheme and auth
48+
credential information. auth_config.get_credential_key will be used to
49+
build the key to load the credential.
50+
51+
tool_context: The context of the current invocation when the tool is
52+
trying to load the credential.
53+
54+
Returns:
55+
Optional[AuthCredential]: the credential saved in the store.
56+
57+
"""
58+
storage = self._get_storage_for_current_context(tool_context)
59+
return storage.get(auth_config.credential_key)
60+
61+
@override
62+
async def save_credential(
63+
self,
64+
auth_config: AuthConfig,
65+
tool_context: ToolContext,
66+
) -> None:
67+
"""
68+
Saves the exchanged_auth_credential in auth config to the backend credential
69+
store.
70+
71+
Args:
72+
auth_config: The auth config which contains the auth scheme and auth
73+
credential information. auth_config.get_credential_key will be used to
74+
build the key to save the credential.
75+
76+
tool_context: The context of the current invocation when the tool is
77+
trying to save the credential.
78+
79+
Returns:
80+
None
81+
"""
82+
storage = self._get_storage_for_current_context(tool_context)
83+
storage[auth_config.credential_key] = auth_config.exchanged_auth_credential
84+
85+
def _get_storage_for_current_context(self, tool_context: ToolContext) -> str:
86+
app_name = tool_context._invocation_context.app_name
87+
user_id = tool_context._invocation_context.user_id
88+
89+
if app_name not in self._store:
90+
self._store[app_name] = {}
91+
if user_id not in self._store[app_name]:
92+
self._store[app_name][user_id] = {}
93+
return self._store[app_name][user_id]

0 commit comments

Comments
 (0)
0