Skip to content

Commit d0bf075

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add credential service backed by session state
PiperOrigin-RevId: 771505611
1 parent d1bda9d commit d0bf075

File tree

4 files changed

+461
-0
lines changed

4 files changed

+461
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 ..auth_credential import AuthCredential
23+
from ..auth_tool import AuthConfig
24+
from .base_credential_service import BaseCredentialService
25+
26+
27+
class SessionStateCredentialService(BaseCredentialService):
28+
"""Class for implementation of credential service using session state as the
29+
store.
30+
"""
31+
32+
@override
33+
async def load_credential(
34+
self,
35+
auth_config: AuthConfig,
36+
tool_context: ToolContext,
37+
) -> Optional[AuthCredential]:
38+
"""
39+
Loads the credential by auth config and current tool context from the
40+
backend credential store.
41+
42+
Args:
43+
auth_config: The auth config which contains the auth scheme and auth
44+
credential information. auth_config.get_credential_key will be used to
45+
build the key to load the credential.
46+
47+
tool_context: The context of the current invocation when the tool is
48+
trying to load the credential.
49+
50+
Returns:
51+
Optional[AuthCredential]: the credential saved in the store.
52+
53+
"""
54+
return tool_context.state.get(auth_config.credential_key)
55+
56+
@override
57+
async def save_credential(
58+
self,
59+
auth_config: AuthConfig,
60+
tool_context: ToolContext,
61+
) -> None:
62+
"""
63+
Saves the exchanged_auth_credential in auth config to the backend credential
64+
store.
65+
66+
Args:
67+
auth_config: The auth config which contains the auth scheme and auth
68+
credential information. auth_config.get_credential_key will be used to
69+
build the key to save the credential.
70+
71+
tool_context: The context of the current invocation when the tool is
72+
trying to save the credential.
73+
74+
Returns:
75+
None
76+
"""
77+
78+
tool_context.state[auth_config.credential_key] = (
79+
auth_config.exchanged_auth_credential
80+
)

tests/unittests/auth/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.

0 commit comments

Comments
 (0)