huace-aigc-auth-client 1.1.24__py3-none-any.whl → 1.1.26__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 +94 -5
- {huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/METADATA +1 -1
- {huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/RECORD +7 -7
- {huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/WHEEL +0 -0
- {huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/licenses/LICENSE +0 -0
- {huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/top_level.txt +0 -0
huace_aigc_auth_client/sdk.py
CHANGED
|
@@ -687,7 +687,17 @@ class AuthMiddleware:
|
|
|
687
687
|
elif "multipart/form-data" in content_type:
|
|
688
688
|
# 文件上传请求,不读取 body,避免损坏文件流
|
|
689
689
|
# 只收集表单字段(非文件字段)
|
|
690
|
-
|
|
690
|
+
try:
|
|
691
|
+
form = await request.form()
|
|
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
|
|
699
|
+
except Exception:
|
|
700
|
+
pass
|
|
691
701
|
elif content_type.startswith('text/'):
|
|
692
702
|
# 只读取文本类型的请求体,且限制大小避免内存问题
|
|
693
703
|
try:
|
|
@@ -750,9 +760,83 @@ class AuthMiddleware:
|
|
|
750
760
|
return None
|
|
751
761
|
return authorization[7:]
|
|
752
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
|
+
|
|
753
837
|
async def fastapi_middleware(self, request, call_next, user_info_callback: Callable = None):
|
|
754
838
|
"""
|
|
755
|
-
FastAPI
|
|
839
|
+
FastAPI 中间件(旧方法,推荐使用 get_fastapi_middleware_class)
|
|
756
840
|
|
|
757
841
|
使用方法:
|
|
758
842
|
@app.middleware("http")
|
|
@@ -768,9 +852,6 @@ class AuthMiddleware:
|
|
|
768
852
|
|
|
769
853
|
path = request.url.path
|
|
770
854
|
start_time = time.time()
|
|
771
|
-
|
|
772
|
-
# 收集请求参数
|
|
773
|
-
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
774
855
|
|
|
775
856
|
# 检查是否跳过
|
|
776
857
|
if self._should_skip(path):
|
|
@@ -783,6 +864,8 @@ class AuthMiddleware:
|
|
|
783
864
|
if not token:
|
|
784
865
|
logger.warning("AuthMiddleware未提供认证信息")
|
|
785
866
|
response_time = time.time() - start_time
|
|
867
|
+
# 收集请求参数
|
|
868
|
+
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
786
869
|
self._collect_stats(path, request.method, 401, response_time, "", "未提供认证信息", request_params)
|
|
787
870
|
return JSONResponse(
|
|
788
871
|
status_code=401,
|
|
@@ -801,6 +884,8 @@ class AuthMiddleware:
|
|
|
801
884
|
except AigcAuthError as e:
|
|
802
885
|
logger.error(f"AuthMiddleware认证失败: {e.message}")
|
|
803
886
|
response_time = time.time() - start_time
|
|
887
|
+
# 收集请求参数
|
|
888
|
+
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
804
889
|
self._collect_stats(path, request.method, 401, response_time, token, e.message, request_params)
|
|
805
890
|
return JSONResponse(
|
|
806
891
|
status_code=401,
|
|
@@ -811,9 +896,13 @@ class AuthMiddleware:
|
|
|
811
896
|
try:
|
|
812
897
|
response = await call_next(request)
|
|
813
898
|
response_time = time.time() - start_time
|
|
899
|
+
# 收集请求参数
|
|
900
|
+
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
814
901
|
self._collect_stats(path, request.method, response.status_code, response_time, token, None, request_params)
|
|
815
902
|
return response
|
|
816
903
|
except Exception as e:
|
|
904
|
+
# 收集请求参数
|
|
905
|
+
request_params = await self._collect_fastapi_request_params(request) if self.enable_stats else None
|
|
817
906
|
response_time = time.time() - start_time
|
|
818
907
|
self._collect_stats(path, request.method, 500, response_time, token, str(e), request_params)
|
|
819
908
|
raise
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
huace_aigc_auth_client/__init__.py,sha256=
|
|
1
|
+
huace_aigc_auth_client/__init__.py,sha256=UvYxKO3l-WAdwb8Is69IZROn4xJtTzmxOZprp4d_dQg,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=79n-CRyUY6Q7TVS_a5I-U39BB_WA_P_dLiFdAJjZmiE,37255
|
|
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.26.dist-info/licenses/LICENSE,sha256=z7dgC7KljhBLNvKjN15391nMj3aLt0gbud8-Yf1F8EQ,1063
|
|
9
|
+
huace_aigc_auth_client-1.1.26.dist-info/METADATA,sha256=n8tmSxO9LtOYxmNvmrkZShMNTjwi8zM9mPsxk3s9efI,23629
|
|
10
|
+
huace_aigc_auth_client-1.1.26.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
11
|
+
huace_aigc_auth_client-1.1.26.dist-info/top_level.txt,sha256=kbv0nQ6PQ0JVneWPH7O2AbtlJnP7AjvFJ6JjM6ZEBxo,23
|
|
12
|
+
huace_aigc_auth_client-1.1.26.dist-info/RECORD,,
|
|
File without changes
|
{huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{huace_aigc_auth_client-1.1.24.dist-info → huace_aigc_auth_client-1.1.26.dist-info}/top_level.txt
RENAMED
|
File without changes
|