fix: preserve oauth refresh auth through runtime bridge
This commit is contained in:
@@ -136,6 +136,32 @@ def auth_record_from_compat(
|
|||||||
else {}
|
else {}
|
||||||
)
|
)
|
||||||
auth = EnvAuth(env=env)
|
auth = EnvAuth(env=env)
|
||||||
|
case "oauth_refresh_token":
|
||||||
|
client_id = payload_dict.get("client_id")
|
||||||
|
client_secret = payload_dict.get("client_secret")
|
||||||
|
refresh_token = payload_dict.get("refresh_token")
|
||||||
|
token_url = payload_dict.get("token_url")
|
||||||
|
raw_scopes = payload_dict.get("scopes", ())
|
||||||
|
scopes = (
|
||||||
|
tuple(str(scope) for scope in raw_scopes)
|
||||||
|
if isinstance(raw_scopes, list | tuple)
|
||||||
|
else ()
|
||||||
|
)
|
||||||
|
if not isinstance(client_id, str) or not client_id:
|
||||||
|
raise ValueError("oauth_refresh_token client_id is required")
|
||||||
|
if not isinstance(client_secret, str):
|
||||||
|
raise ValueError("oauth_refresh_token client_secret is required")
|
||||||
|
if not isinstance(refresh_token, str) or not refresh_token:
|
||||||
|
raise ValueError("oauth_refresh_token refresh_token is required")
|
||||||
|
if not isinstance(token_url, str) or not token_url:
|
||||||
|
raise ValueError("oauth_refresh_token token_url is required")
|
||||||
|
auth = OAuthRefreshTokenAuth(
|
||||||
|
client_id=client_id,
|
||||||
|
client_secret=client_secret,
|
||||||
|
refresh_token=refresh_token,
|
||||||
|
token_url=AnyUrl(token_url),
|
||||||
|
scopes=scopes,
|
||||||
|
)
|
||||||
case _:
|
case _:
|
||||||
auth = OpaqueAuth(scheme=scheme, payload=payload_dict)
|
auth = OpaqueAuth(scheme=scheme, payload=payload_dict)
|
||||||
return StoredAuthRecord(id=id, auth=auth, metadata=metadata_dict)
|
return StoredAuthRecord(id=id, auth=auth, metadata=metadata_dict)
|
||||||
|
|||||||
@@ -129,3 +129,25 @@ def test_typed_auth_rejects_missing_bearer_token() -> None:
|
|||||||
payload={},
|
payload={},
|
||||||
metadata={},
|
metadata={},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_auth_record_from_compat_maps_oauth_refresh_token() -> None:
|
||||||
|
from wf_api.auth import OAuthRefreshTokenAuth, auth_record_from_compat
|
||||||
|
|
||||||
|
record = auth_record_from_compat(
|
||||||
|
id="google.drive.personal",
|
||||||
|
scheme="oauth_refresh_token",
|
||||||
|
payload={
|
||||||
|
"client_id": "client",
|
||||||
|
"client_secret": "secret",
|
||||||
|
"refresh_token": "refresh",
|
||||||
|
"token_url": "https://oauth2.googleapis.com/token",
|
||||||
|
"scopes": ["https://www.googleapis.com/auth/drive.readonly"],
|
||||||
|
},
|
||||||
|
metadata={"provider": "google"},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(record.auth, OAuthRefreshTokenAuth)
|
||||||
|
assert record.auth.client_id == "client"
|
||||||
|
assert str(record.auth.token_url) == "https://oauth2.googleapis.com/token"
|
||||||
|
assert record.auth.scopes == ("https://www.googleapis.com/auth/drive.readonly",)
|
||||||
|
|||||||
@@ -171,3 +171,36 @@ def test_file_auth_store_loads_new_format_through_load_auth(tmp_path: Path) -> N
|
|||||||
assert legacy.connection_id == "google.drive.personal"
|
assert legacy.connection_id == "google.drive.personal"
|
||||||
assert legacy.scheme == "bearer"
|
assert legacy.scheme == "bearer"
|
||||||
assert legacy.payload["token"] == "token"
|
assert legacy.payload["token"] == "token"
|
||||||
|
|
||||||
|
|
||||||
|
def test_file_auth_store_oauth_refresh_token_survives_legacy_load_auth_bridge(
|
||||||
|
tmp_path: Path,
|
||||||
|
) -> None:
|
||||||
|
from pydantic import AnyUrl
|
||||||
|
|
||||||
|
from wf_api.auth import OAuthRefreshTokenAuth, auth_record_from_compat
|
||||||
|
|
||||||
|
store = FileAuthStore(tmp_path)
|
||||||
|
record = StoredAuthRecord(
|
||||||
|
id="google.drive.personal",
|
||||||
|
auth=OAuthRefreshTokenAuth(
|
||||||
|
client_id="client",
|
||||||
|
client_secret="secret",
|
||||||
|
refresh_token="refresh",
|
||||||
|
token_url=AnyUrl("https://oauth2.googleapis.com/token"),
|
||||||
|
scopes=("https://www.googleapis.com/auth/drive.readonly",),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
store.save_auth_record(record)
|
||||||
|
|
||||||
|
legacy = store.load_auth("google.drive.personal")
|
||||||
|
assert legacy is not None
|
||||||
|
restored = auth_record_from_compat(
|
||||||
|
id=legacy.connection_id,
|
||||||
|
scheme=legacy.scheme,
|
||||||
|
payload=legacy.payload,
|
||||||
|
metadata={},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert isinstance(restored.auth, OAuthRefreshTokenAuth)
|
||||||
|
assert restored.auth.refresh_token == "refresh"
|
||||||
|
|||||||
Reference in New Issue
Block a user