Skip to content

Commit 5bda0fe

Browse files
committed
fix(credentials): 小浣熊重导入保留按需登录凭据
重贴 xhx_token 时导入项不含手机号/密码,整份覆盖后 login_secret 被重算为空,按需登录静默失效(2026-12-03 045920 实测)。合并前用 渠道钩子 preserve_on_import 继承缺失字段;显式新值仍优先。
1 parent d2c2d62 commit 5bda0fe

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎src/api/admin_credentials.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,14 @@ async def register_import(provider_id: str, credential_data: dict, tags: list[st
442442
lambda data: login_identities_for(provider_id, data),
443443
target) if target else None
444444
if existing is not None:
445+
# 重新导入 token 时,部分渠道(小浣熊)需要继承已存的手机号/
446+
# 密码:导入框只让贴新 token,整份覆盖会把按需登录凭据清空。
447+
preserve = getattr(registry[provider_id], "preserve_on_import", None)
448+
merged_data = (
449+
preserve(credentials.credential_data(existing), credential_data)
450+
if preserve is not None else credential_data)
445451
merged = credentials.merge_login_credential(
446-
existing, credential_data, nickname)
452+
existing, merged_data, nickname)
447453
if merged:
448454
credential_id = existing
449455
if not merged and provider_id == "zen" and credential_data.get("api_key"):

‎src/provider/raccoon/client.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ def set_login_secret(self, credential_data: dict, phone: str,
658658
data["password"] = password
659659
return data
660660

661+
def preserve_on_import(self, previous: dict | None, incoming: dict) -> dict:
662+
"""重新导入 token(=小浣熊的「重新登录」)时继承已存的手机号/密码。
663+
664+
用户换 token 通常只从浏览器复制 `xhx_token`,导入框里不会带手机号与
665+
密码;若整份覆盖,`login_secret` 会被重算成空,按需登录静默失效
666+
(2026-12-03 045920 就是这样丢的)。只在**新数据没带**对应字段时继承
667+
旧值;要改密码或清除请走 `PUT /api/credentials/{id}/login-secret`。
668+
"""
669+
merged = dict(incoming)
670+
previous = previous or {}
671+
for name in ("phone", "password"):
672+
if not merged.get(name) and previous.get(name):
673+
merged[name] = previous[name]
674+
return merged
675+
661676
def classify(self, status: int, body: bytes) -> ErrKind:
662677
return raccoon_events.classify_status(status, body)
663678

‎tests/api/test_admin_credentials.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,58 @@ async def probe_quota(self, _data):
605605
assert app.state.credentials.credential_data(first["id"])["expires_at"] == 2
606606

607607

608+
def test_raccoon_reimport_preserves_saved_login_secret(admin_client):
609+
"""重贴新 token 时继承已存手机号/密码,`login_secret` 不被清空。
610+
611+
导入框只让贴 token;若整份覆盖,按需登录凭据会静默丢失
612+
(2026-12-03 045920 重导入后 `login_secret` 1 → NULL)。
613+
"""
614+
from src.provider.base import Quota
615+
from src.provider.raccoon.client import RaccoonProvider
616+
from src.provider.raccoon.credential import RaccoonCredential
617+
618+
app, client = admin_client
619+
real = RaccoonProvider()
620+
621+
class Stub:
622+
id = "raccoon"
623+
endpoint = "https://e"
624+
625+
def import_credential(self, raw):
626+
credential = RaccoonCredential.from_import(raw)
627+
if not credential.access_token:
628+
raise ValueError("missing access token")
629+
return credential.to_dict()
630+
631+
def preserve_on_import(self, previous, incoming):
632+
return real.preserve_on_import(previous, incoming)
633+
634+
async def probe_quota(self, _data):
635+
return Quota(remaining=1, total=2, probed_at=1)
636+
637+
app.state.executor._deps.providers["raccoon"] = Stub()
638+
first = client.post("/api/credentials", json={
639+
"provider": "raccoon",
640+
"credential": {"access_token": _jwt({"sub": "pu-1", "exp": 1}),
641+
"phone": "13800000000", "password": "pw12345678"}}).json()
642+
assert first["merged"] is False
643+
credential_id = first["id"]
644+
assert next(row for row in app.state.credentials.list_all()
645+
if row["id"] == credential_id)["login_secret"] == 1
646+
647+
second = client.post("/api/credentials", json={
648+
"provider": "raccoon",
649+
"credential": {"access_token": _jwt({"sub": "pu-1", "exp": 2})}}).json()
650+
651+
assert second["merged"] is True and second["id"] == credential_id
652+
data = app.state.credentials.credential_data(credential_id)
653+
assert data["phone"] == "13800000000"
654+
assert data["password"] == "pw12345678"
655+
assert data["expires_at"] == 2 # 新 token 仍生效
656+
assert next(row for row in app.state.credentials.list_all()
657+
if row["id"] == credential_id)["login_secret"] == 1
658+
659+
608660
def test_raccoon_import_without_uid_does_not_merge(admin_client):
609661
"""解不出 uid(非 JWT / claims 坏)→ 无法安全合并,退回新增(绝不猜)。"""
610662
from src.provider.base import Quota

‎tests/provider/raccoon/test_provider.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ def test_credential_expires_at():
8282
assert provider.credential_expires_at({}) == 0
8383

8484

85+
def test_preserve_on_import_inherits_missing_login_secret():
86+
"""重贴 token 时没带手机号/密码 → 继承旧值;新值优先,旧值为空不补。"""
87+
provider = RaccoonProvider(client=FakeClient())
88+
previous = {"phone": "13800000000", "password": "old-pass", "uid": "u"}
89+
90+
# 两个字段都缺 → 全部继承
91+
kept = provider.preserve_on_import(previous, {"access_token": "new"})
92+
assert kept["phone"] == "13800000000"
93+
assert kept["password"] == "old-pass"
94+
assert kept["access_token"] == "new"
95+
96+
# 只缺密码 → 只补密码,新手机号不被旧值覆盖
97+
partial = provider.preserve_on_import(
98+
previous, {"access_token": "new", "phone": "13900000000"})
99+
assert partial["phone"] == "13900000000"
100+
assert partial["password"] == "old-pass"
101+
102+
# 旧数据为空/None → 原样返回(不凭空造出空字段)
103+
assert provider.preserve_on_import(None, {"access_token": "new"}) == {
104+
"access_token": "new"}
105+
assert provider.preserve_on_import({}, {"access_token": "new"}) == {
106+
"access_token": "new"}
107+
108+
85109
def test_classify_delegates_to_events():
86110
provider = RaccoonProvider(client=FakeClient())
87111
assert provider.classify(401, b"") is ErrKind.DEAD

0 commit comments

Comments
 (0)