fix(auth): 重构登录认证,并支持跳转到关联服务如图纸标注
This commit is contained in:
1 parent
ce1fd59f61
commit
affe139bf9
51 files changed
+1939
-132
No files matched your search
@@ -0,0 +1,53 @@
|
||||
"""
|
||||
backend/tests/test_casdoor_state.py
|
||||
|
||||
用途:验证 Casdoor 授权码回调 state 的格式兼容、签名完整性和有效期边界。
|
||||
依赖:HXOPS Casdoor state 服务;不访问 Flask 路由、数据库或 Casdoor。
|
||||
逻辑:固定密钥和时钟验证 state 可跨实例校验,篡改或过期必须安全失败。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.auth.hxops.casdoor_state import consume_casdoor_state, issue_casdoor_state
|
||||
|
||||
|
||||
def test_casdoor_state_preserves_existing_wire_format_and_redirect_uri():
|
||||
"""新服务签发的 state 保持 v1 前缀,且在同一密钥下可恢复原 redirect_uri。"""
|
||||
state = issue_casdoor_state(
|
||||
'https://hxops.example.com/auth/callback',
|
||||
secret='state-test-secret',
|
||||
now_timestamp=1_700_000_000,
|
||||
nonce_factory=lambda: 'fixed-nonce',
|
||||
)
|
||||
|
||||
assert state.startswith('hxops_casdoor_v1.')
|
||||
assert consume_casdoor_state(
|
||||
state,
|
||||
secret='state-test-secret',
|
||||
now_timestamp=1_700_000_001,
|
||||
) == 'https://hxops.example.com/auth/callback'
|
||||
|
||||
|
||||
def test_casdoor_state_rejects_tampered_signature():
|
||||
"""攻击者篡改 payload 或签名后,服务必须拒绝恢复 callback 地址。"""
|
||||
state = issue_casdoor_state(
|
||||
'https://hxops.example.com/auth/callback',
|
||||
secret='state-test-secret',
|
||||
now_timestamp=1_700_000_000,
|
||||
)
|
||||
prefix, payload, signature = state.split('.')
|
||||
tampered = f'{prefix}.{payload}.{signature[:-1]}x'
|
||||
|
||||
assert consume_casdoor_state(tampered, secret='state-test-secret', now_timestamp=1_700_000_001) is None
|
||||
|
||||
|
||||
def test_casdoor_state_rejects_expired_or_wrong_secret():
|
||||
"""过期 state 或不同实例密钥均不得通过,保持 callback 的 CSRF 防护。"""
|
||||
state = issue_casdoor_state(
|
||||
'https://hxops.example.com/auth/callback',
|
||||
secret='state-test-secret',
|
||||
now_timestamp=1_700_000_000,
|
||||
)
|
||||
|
||||
assert consume_casdoor_state(state, secret='wrong-secret', now_timestamp=1_700_000_001) is None
|
||||
assert consume_casdoor_state(state, secret='state-test-secret', now_timestamp=1_700_000_600) is None
|
||||
Reference in new issue
Block a user