forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.py
More file actions
55 lines (43 loc) · 1.66 KB
/
credentials.py
File metadata and controls
55 lines (43 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from abc import ABC, abstractmethod
from a2a.client.client import ClientCallContext
class CredentialService(ABC):
"""An abstract service for retrieving credentials."""
@abstractmethod
async def get_credentials(
self,
security_scheme_name: str,
context: ClientCallContext | None,
) -> str | None:
"""
Retrieves a credential (e.g., token) for a security scheme.
"""
class InMemoryContextCredentialStore(CredentialService):
"""A simple in-memory store for session-keyed credentials.
This class uses the 'sessionId' from the ClientCallContext state to
store and retrieve credentials...
"""
def __init__(self) -> None:
self._store: dict[str, dict[str, str]] = {}
async def get_credentials(
self,
security_scheme_name: str,
context: ClientCallContext | None,
) -> str | None:
"""Retrieves credentials from the in-memory store.
Args:
security_scheme_name: The name of the security scheme.
context: The client call context.
Returns:
The credential string, or None if not found.
"""
if not context or 'sessionId' not in context.state:
return None
session_id = context.state['sessionId']
return self._store.get(session_id, {}).get(security_scheme_name)
async def set_credentials(
self, session_id: str, security_scheme_name: str, credential: str
) -> None:
"""Method to populate the store."""
if session_id not in self._store:
self._store[session_id] = {}
self._store[session_id][security_scheme_name] = credential