huace-aigc-auth-client 1.1.23__py3-none-any.whl → 1.1.25__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- huace_aigc_auth_client/__init__.py +1 -1
- huace_aigc_auth_client/sdk.py +82 -3
- {huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/METADATA +1 -1
- {huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/RECORD +7 -7
- {huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/WHEEL +0 -0
- {huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/licenses/LICENSE +0 -0
- {huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/top_level.txt +0 -0
huace_aigc_auth_client/sdk.py
CHANGED
|
@@ -689,8 +689,13 @@ class AuthMiddleware:
|
|
|
689
689
|
# 只收集表单字段(非文件字段)
|
|
690
690
|
try:
|
|
691
691
|
form = await request.form()
|
|
692
|
-
|
|
693
|
-
|
|
692
|
+
form_params = {}
|
|
693
|
+
for k, v in form.items():
|
|
694
|
+
if hasattr(v, "filename"):
|
|
695
|
+
# 跳过文件字段
|
|
696
|
+
continue
|
|
697
|
+
form_params[k] = v
|
|
698
|
+
params["form_params"] = form_params
|
|
694
699
|
except Exception:
|
|
695
700
|
pass
|
|
696
701
|
elif content_type.startswith('text/'):
|
|
@@ -755,9 +760,83 @@ class AuthMiddleware:
|
|
|
755
760
|
return None
|
|
756
761
|
return authorization[7:]
|
|
757
762
|
|
|
763
|
+
def get_fastapi_middleware_class(self, user_info_callback: Callable = None):
|
|
764
|
+
"""
|
|
765
|
+
获取 FastAPI 中间件类(推荐使用,避免请求体读取问题)
|
|
766
|
+
|
|
767
|
+
使用方法:
|
|
768
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
769
|
+
app.add_middleware(auth_middleware.get_fastapi_middleware_class())
|
|
770
|
+
|
|
771
|
+
Returns:
|
|
772
|
+
继承于 BaseHTTPMiddleware 的中间件类
|
|
773
|
+
"""
|
|
774
|
+
from fastapi.responses import JSONResponse
|
|
775
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
776
|
+
|
|
777
|
+
auth_middleware_instance = self
|
|
778
|
+
|
|
779
|
+
class AuthHTTPMiddleware(BaseHTTPMiddleware):
|
|
780
|
+
async def dispatch(self, request, call_next):
|
|
781
|
+
path = request.url.path
|
|
782
|
+
start_time = time.time()
|
|
783
|
+
|
|
784
|
+
# 收集请求参数
|
|
785
|
+
request_params = await auth_middleware_instance._collect_fastapi_request_params(request) if auth_middleware_instance.enable_stats else None
|
|
786
|
+
|
|
787
|
+
# 检查是否跳过
|
|
788
|
+
if auth_middleware_instance._should_skip(path):
|
|
789
|
+
return await call_next(request)
|
|
790
|
+
|
|
791
|
+
# 获取 Authorization header
|
|
792
|
+
authorization = request.headers.get("Authorization")
|
|
793
|
+
token = auth_middleware_instance._extract_token(authorization)
|
|
794
|
+
|
|
795
|
+
if not token:
|
|
796
|
+
logger.warning("AuthMiddleware未提供认证信息")
|
|
797
|
+
response_time = time.time() - start_time
|
|
798
|
+
auth_middleware_instance._collect_stats(path, request.method, 401, response_time, "", "未提供认证信息", request_params)
|
|
799
|
+
return JSONResponse(
|
|
800
|
+
status_code=401,
|
|
801
|
+
content={"code": 401, "message": "未提供认证信息", "data": None}
|
|
802
|
+
)
|
|
803
|
+
|
|
804
|
+
# 验证 token
|
|
805
|
+
try:
|
|
806
|
+
user_info = auth_middleware_instance.client.get_user_info(token)
|
|
807
|
+
# 将用户信息存储到 request.state
|
|
808
|
+
request.state.user_info = user_info
|
|
809
|
+
# 设置上下文
|
|
810
|
+
set_current_user(dataclasses.asdict(user_info))
|
|
811
|
+
if user_info_callback:
|
|
812
|
+
await user_info_callback(request, user_info)
|
|
813
|
+
except AigcAuthError as e:
|
|
814
|
+
logger.error(f"AuthMiddleware认证失败: {e.message}")
|
|
815
|
+
response_time = time.time() - start_time
|
|
816
|
+
auth_middleware_instance._collect_stats(path, request.method, 401, response_time, token, e.message, request_params)
|
|
817
|
+
return JSONResponse(
|
|
818
|
+
status_code=401,
|
|
819
|
+
content={"code": e.code, "message": e.message, "data": None}
|
|
820
|
+
)
|
|
821
|
+
|
|
822
|
+
# 处理请求
|
|
823
|
+
try:
|
|
824
|
+
response = await call_next(request)
|
|
825
|
+
response_time = time.time() - start_time
|
|
826
|
+
auth_middleware_instance._collect_stats(path, request.method, response.status_code, response_time, token, None, request_params)
|
|
827
|
+
return response
|
|
828
|
+
except Exception as e:
|
|
829
|
+
response_time = time.time() - start_time
|
|
830
|
+
auth_middleware_instance._collect_stats(path, request.method, 500, response_time, token, str(e), request_params)
|
|
831
|
+
raise
|
|
832
|
+
finally:
|
|
833
|
+
clear_current_user()
|
|
834
|
+
|
|
835
|
+
return AuthHTTPMiddleware
|
|
836
|
+
|
|
758
837
|
async def fastapi_middleware(self, request, call_next, user_info_callback: Callable = None):
|
|
759
838
|
"""
|
|
760
|
-
FastAPI
|
|
839
|
+
FastAPI 中间件(旧方法,推荐使用 get_fastapi_middleware_class)
|
|
761
840
|
|
|
762
841
|
使用方法:
|
|
763
842
|
@app.middleware("http")
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
huace_aigc_auth_client/__init__.py,sha256=
|
|
1
|
+
huace_aigc_auth_client/__init__.py,sha256=ryj_DFuA-ARb1FKJCmkw-KAzn0IHsD4gvIGcPRmfsdw,4630
|
|
2
2
|
huace_aigc_auth_client/api_stats_collector.py,sha256=UCUu2CHWsJyiAM58-XLQtfxWK0fjZ8TTdw90gv_Gx04,10347
|
|
3
3
|
huace_aigc_auth_client/legacy_adapter.py,sha256=TVCBAKejE2z2HQFsEwDW8LMiaIkXNfz3Mxv6_E-UJFY,24102
|
|
4
|
-
huace_aigc_auth_client/sdk.py,sha256=
|
|
4
|
+
huace_aigc_auth_client/sdk.py,sha256=7EQUjlvITZ3q9KzfZBqRJp2_0u7PeL-ObgxRDzBbpGQ,36821
|
|
5
5
|
huace_aigc_auth_client/user_context.py,sha256=KzevYLsLv1hv8rlvRw83FT-HugeoBJSJ1Pi56iLWyTE,5592
|
|
6
6
|
huace_aigc_auth_client/webhook.py,sha256=XQZYEbMoqIdqZWCGSTcedeDKJpDbUVSq5g08g-6Qucg,4124
|
|
7
7
|
huace_aigc_auth_client/webhook_flask.py,sha256=Iosu4dBtRhQZM_ytn-bn82MpVsyOiV28FBnt7Tfh31U,7225
|
|
8
|
-
huace_aigc_auth_client-1.1.
|
|
9
|
-
huace_aigc_auth_client-1.1.
|
|
10
|
-
huace_aigc_auth_client-1.1.
|
|
11
|
-
huace_aigc_auth_client-1.1.
|
|
12
|
-
huace_aigc_auth_client-1.1.
|
|
8
|
+
huace_aigc_auth_client-1.1.25.dist-info/licenses/LICENSE,sha256=z7dgC7KljhBLNvKjN15391nMj3aLt0gbud8-Yf1F8EQ,1063
|
|
9
|
+
huace_aigc_auth_client-1.1.25.dist-info/METADATA,sha256=Co9bvEZuwkzQAhyIbBPE5yo54R7WUERGR9LtiiFPCvw,23629
|
|
10
|
+
huace_aigc_auth_client-1.1.25.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
11
|
+
huace_aigc_auth_client-1.1.25.dist-info/top_level.txt,sha256=kbv0nQ6PQ0JVneWPH7O2AbtlJnP7AjvFJ6JjM6ZEBxo,23
|
|
12
|
+
huace_aigc_auth_client-1.1.25.dist-info/RECORD,,
|
|
File without changes
|
{huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{huace_aigc_auth_client-1.1.23.dist-info → huace_aigc_auth_client-1.1.25.dist-info}/top_level.txt
RENAMED
|
File without changes
|