Skip to content

Commit 29cd183

Browse files
seanzhougooglecopybara-github
authored andcommitted
chore: Add credential service backed by session state
PiperOrigin-RevId: 774878336
1 parent bd67e84 commit 29cd183

File tree

4 files changed

+464
-0
lines changed

4 files changed

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

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)