From 9f27ebdea1033c869dda7a46582ef1ecc320a639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Thu, 20 Feb 2025 17:50:47 +0800 Subject: [PATCH] first commit --- .idea/.gitignore | 8 + .idea/MarsCodeWorkspaceAppSettings.xml | 6 + .idea/inspectionProfiles/Project_Default.xml | 88 ++++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/libroro-server.iml | 18 ++ .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + READNE.md | 3 + __pycache__/app.cpython-311.pyc | Bin 0 -> 542 bytes __pycache__/test.cpython-311.pyc | Bin 0 -> 1600 bytes apis/__init__.py | 9 + apis/__pycache__/__init__.cpython-311.pyc | Bin 0 -> 468 bytes apis/__pycache__/api_user.cpython-311.pyc | Bin 0 -> 7784 bytes apis/api_user.py | 201 +++++++++++++++++ app.py | 30 +++ database/__init__.py | 0 old.utils/__init__.py | 0 old.utils/cookie_utils.py | 14 ++ old.utils/jwt_utils.py | 52 +++++ old.utils/response_utils.py | 42 ++++ old_routes/__init__.py | 5 + .../__pycache__/__init__.cpython-311.pyc | Bin 0 -> 406 bytes old_routes/auth_routes.py | 52 +++++ old_routes/mock_data.py | 181 +++++++++++++++ old_routes/other_routes.py | 82 +++++++ old_routes/user_routes.py | 208 ++++++++++++++++++ test.py | 25 +++ 28 files changed, 1051 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/MarsCodeWorkspaceAppSettings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/libroro-server.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 READNE.md create mode 100644 __pycache__/app.cpython-311.pyc create mode 100644 __pycache__/test.cpython-311.pyc create mode 100644 apis/__init__.py create mode 100644 apis/__pycache__/__init__.cpython-311.pyc create mode 100644 apis/__pycache__/api_user.cpython-311.pyc create mode 100644 apis/api_user.py create mode 100644 app.py create mode 100644 database/__init__.py create mode 100644 old.utils/__init__.py create mode 100644 old.utils/cookie_utils.py create mode 100644 old.utils/jwt_utils.py create mode 100644 old.utils/response_utils.py create mode 100644 old_routes/__init__.py create mode 100644 old_routes/__pycache__/__init__.cpython-311.pyc create mode 100644 old_routes/auth_routes.py create mode 100644 old_routes/mock_data.py create mode 100644 old_routes/other_routes.py create mode 100644 old_routes/user_routes.py create mode 100644 test.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/MarsCodeWorkspaceAppSettings.xml b/.idea/MarsCodeWorkspaceAppSettings.xml new file mode 100644 index 0000000..05ed8ba --- /dev/null +++ b/.idea/MarsCodeWorkspaceAppSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..051acb4 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,88 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/libroro-server.iml b/.idea/libroro-server.iml new file mode 100644 index 0000000..43916a1 --- /dev/null +++ b/.idea/libroro-server.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0d614f4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1a3563e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/READNE.md b/READNE.md new file mode 100644 index 0000000..58458ed --- /dev/null +++ b/READNE.md @@ -0,0 +1,3 @@ +接口文档 + +pip install --upgrade \ No newline at end of file diff --git a/__pycache__/app.cpython-311.pyc b/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..437f0848a4962c80d5ab57d6b322d936b3792e3f GIT binary patch literal 542 zcmZ`#ze~eF6n>XW(pV{iL;VL_GH9JFRdG?oL0rTo1VT!#ZD`VjOHym63J#7Af~!-l z=)t3r}7qp)U_{Y3G?8lfy}&lODHF7Q4hW+UntJ|z{q)y_@^6W*b! zBvOTIP|Oq)nR-!30?JQ7CEigCqhY$lFcNI~ezHhO)eb_w!HI}Z?M8S9BSwVr`p$-a z91t4lEowVvqpI(cpdNa@?$~AOQ7?@vjMOAlXL_st=~&rvJu7m^HeFyc{ER`9JwuET z8o*Ng(+02{|7CCTe1}}@pLyqA2M=XH;tS-TA%A(&+l(Rq1jRlSV<-*)&jzjD!DLYC LLn%&;=Gf{lcPxxM literal 0 HcmV?d00001 diff --git a/__pycache__/test.cpython-311.pyc b/__pycache__/test.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f3985bdbb43a6530931065071254f096aa7fc76c GIT binary patch literal 1600 zcmZ`(-ESL35TCuh^9Qym4qr(sAvblUo=PI}K}(~=#pDZA5fBa!iHs~acMWbFpLKU8 zA##u@m9Q#MT~U;@C8{4#3o1|~q$m5ycq=~D^JXxYdcQc+1Z)d z+4=b0-tFD-c%A{Ij_sAHZ#aOz*rH1OBV+e_G!{VuiNs+7B<>PS5DsJ_QHj5eS$A<( zh=Ao69`|FmEzZr*32MbqnZHdi6K91c1eurY(^e(5MHce5>dt=6I{xH1*i#}Q#1;b4 zxx<1-Cu?|c<-|#G?q;#qA};k>Pk;-!Q6ar*puSLxXzdk>8-k+`|a$__5t8b6=rWp9T#|rapEv{@Wf3b5t_xI}8+bh4NnlOer zH?LfMcpW?)CaayJ^#u$!S?P?ZM&5JEKEmrYrQ`z}ZM>?1Q1;yC#1wmXJ&# zsm2nzdFXvvor!5$Oi2u>s-l|Cp}9#pspDGYP5xs=(@j39sJclslgJvo4nCy4Qv z4>eiU!gFdY9!*Sz2W4$qSCZj)>>L(Ss=6J6K9SY1E^B&6@>A2xj0h_kVL)PuDbQL*E&&mG>te&D40@ zU2p`7j)3tbMy%x$#agJfH}Oy}ioq_p+a5ed*P2~ut{vlpqJ2%I%zl+HyWO(8d;^5u zf6>Y0E6s29(ffS@+VjrNu2UVa;pmL5x&Y?w%*G)SQy7j%wQ184iOfV}iAY4`RCZ%j z4+_z)3e09N*A&jPK$U&5EIJXLks}e4Mw3aCs;R_}u!}pOvfHrd=$lcbR9p_It+-@= zA?;ff_)vsA08hpmCEh{sxN71hAWa5%b0?Pc0{DyIH^9FRUgi>)#tP6@gf;`(N;V+P z2E4qVAJ|+OIs5kK$|WU3*QqNz|0iuRXv;cp|8nHwNbYdHCo@vuj~DslMwLqx_*RTj HTM*g*5$%Y1 literal 0 HcmV?d00001 diff --git a/apis/__init__.py b/apis/__init__.py new file mode 100644 index 0000000..e267c18 --- /dev/null +++ b/apis/__init__.py @@ -0,0 +1,9 @@ +from flask import Blueprint +# 系统相关接口 例如对系统的一些设置 +api_system = Blueprint('system', __name__) +# 权限相关接口 例如菜单 +api_auth = Blueprint('auth', __name__) +# 用户相关接口 例如用户信息 +api_user = Blueprint('user', __name__) +# 其他一些不便于分类的接口 +api_other = Blueprint('other', __name__) \ No newline at end of file diff --git a/apis/__pycache__/__init__.cpython-311.pyc b/apis/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ae423cf4be950d0e37c430a4304427a223146949 GIT binary patch literal 468 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 F`(JT>&s+ci literal 0 HcmV?d00001 diff --git a/apis/api_user.py b/apis/api_user.py new file mode 100644 index 0000000..b3dca92 --- /dev/null +++ b/apis/api_user.py @@ -0,0 +1,201 @@ +import jwt +from flask import request, jsonify +from typing import Optional, Dict + +from apis import api_user + +# TODO: Replace with your own secret key +ACCESS_TOKEN_SECRET = 'access_token_secret' +REFRESH_TOKEN_SECRET = 'refresh_token_secret' + +# 模拟用户数据 +MOCK_USERS = [ + { + "username": "test_user", + "password": "test_password" + } +] + + +def generate_access_token(user: Dict) -> str: + """ + 生成访问令牌 + :param user: 用户信息 + :return: 访问令牌 + """ + return jwt.encode(user, ACCESS_TOKEN_SECRET, algorithm='HS256') + + +def generate_refresh_token(user: Dict) -> str: + """ + 生成刷新令牌 + :param user: 用户信息 + :return: 刷新令牌 + """ + return jwt.encode(user, REFRESH_TOKEN_SECRET, algorithm='HS256') + + +def verify_access_token() -> Optional[Dict]: + """ + 验证访问令牌 + :return: 用户信息(不包含密码)或 None + """ + auth_header = request.headers.get('Authorization') + if not auth_header or not auth_header.startswith('Bearer '): + return None + + token = auth_header.split(' ')[1] + try: + decoded = jwt.decode(token, ACCESS_TOKEN_SECRET, algorithms=['HS256']) + username = decoded.get('username') + user = next((item for item in MOCK_USERS if item['username'] == username), None) + if user: + userinfo = {k: v for k, v in user.items() if k != 'password'} + return userinfo + return None + except jwt.exceptions.InvalidTokenError: + return None + + +def verify_refresh_token(token: str) -> Optional[Dict]: + """ + 验证刷新令牌 + :param token: 刷新令牌 + :return: 用户信息(不包含密码)或 None + """ + try: + decoded = jwt.decode(token, REFRESH_TOKEN_SECRET, algorithms=['HS256']) + username = decoded.get('username') + user = next((item for item in MOCK_USERS if item['username'] == username), None) + if user: + userinfo = {k: v for k, v in user.items() if k != 'password'} + return userinfo + return None + except jwt.exceptions.InvalidTokenError: + return None + + +##======================================== +from flask import jsonify + + +def use_response_success(data=None): + """ + 成功响应 + :param data: 响应数据 + :return: 成功响应 JSON + """ + return { + "code": 0, + "data": data, + "error": None, + "message": "ok" + } + + +def use_page_response_success(page, page_size, list_data, message="ok"): + """ + 分页成功响应 + :param page: 页码 + :param page_size: 每页数量 + :param list_data: 数据列表 + :param message: 消息 + :return: 分页成功响应 JSON + """ + page = int(page) + page_size = int(page_size) + offset = (page - 1) * page_size + page_data = list_data[offset:offset + page_size] + return { + **use_response_success({ + "items": page_data, + "total": len(list_data) + }), + "message": message + } + + +def use_response_error(message, error=None): + """ + 错误响应 + :param message: 错误消息 + :param error: 错误信息 + :return: 错误响应 JSON + """ + return { + "code": -1, + "data": None, + "error": error, + "message": message + } + + +def forbidden_response(message="Forbidden Exception"): + """ + 禁止访问响应 + :param message: 错误消息 + :return: 禁止访问响应 JSON + """ + return jsonify(use_response_error(message, message)), 403 + + +def unAuthorizedResponse(): + """ + 未授权响应 + :return: 未授权响应 JSON + """ + return jsonify(use_response_error("Unauthorized Exception", "Unauthorized Exception")), 401 + + +def sleep(ms): + """ + 睡眠函数 + :param ms: 睡眠时间(毫秒) + :return: 无 + """ + import time + time.sleep(ms / 1000) + + +def pagination(page_no, page_size, array): + """ + 分页函数 + :param page_no: 页码 + :param page_size: 每页数量 + :param array: 数据列表 + :return: 分页后的数据列表 + """ + page_no = int(page_no) + page_size = int(page_size) + offset = (page_no - 1) * page_size + if offset + page_size >= len(array): + return array[offset:] + return array[offset:offset + page_size] + + +##======================================== + +def info(): + """ + 获取用户信息 + :return: 用户信息或未授权响应 + """ + userinfo = verify_access_token() + if not userinfo: + return jsonify({ + "code": -1, + "data": None, + "error": "Unauthorized Exception", + "message": "Unauthorized Exception" + }), 401 + return jsonify({ + "code": 0, + "data": userinfo, + "error": None, + "message": "ok" + }) + + +@api_user.route('/info', methods=['GET']) +def get_info(): + return info() diff --git a/app.py b/app.py new file mode 100644 index 0000000..98a465a --- /dev/null +++ b/app.py @@ -0,0 +1,30 @@ +from flask import Flask, jsonify + +from apis import api_system, api_auth, api_user, api_other +from apis.api_user import use_response_error + +app = Flask(__name__) + + + +# 注册蓝图 +app.register_blueprint(api_system, url_prefix='/system') +app.register_blueprint(api_auth, url_prefix='/auth') +app.register_blueprint(api_user, url_prefix='/api/user') +app.register_blueprint(api_other, url_prefix='/other') +import logging + +# 配置日志记录 +logging.basicConfig(level=logging.DEBUG) + +@app.errorhandler(404) +def page_not_found(e): + logging.error(f"404 Error: {e}") + return jsonify(use_response_error("Page not found", str(e))), 404 + +@app.errorhandler(Exception) +def internal_server_error(e): + logging.error(f"Internal Server Error: {e}", exc_info=True) + return jsonify(use_response_error("Internal Server Error", str(e))), 500 +if __name__ == '__main__': + app.run(host="0.0.0.0",port=5566,debug=True) \ No newline at end of file diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/old.utils/__init__.py b/old.utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/old.utils/cookie_utils.py b/old.utils/cookie_utils.py new file mode 100644 index 0000000..1312237 --- /dev/null +++ b/old.utils/cookie_utils.py @@ -0,0 +1,14 @@ +from flask import request, make_response + +def clear_refresh_token_cookie(): + resp = make_response() + resp.delete_cookie('jwt', httponly=True, samesite='None', secure=True) + return resp + +def set_refresh_token_cookie(refresh_token): + resp = make_response() + resp.set_cookie('jwt', refresh_token, httponly=True, max_age=24 * 60 * 60 * 1000, samesite='None', secure=True) + return resp + +def get_refresh_token_from_cookie(): + return request.cookies.get('jwt') \ No newline at end of file diff --git a/old.utils/jwt_utils.py b/old.utils/jwt_utils.py new file mode 100644 index 0000000..8e2abd6 --- /dev/null +++ b/old.utils/jwt_utils.py @@ -0,0 +1,52 @@ +import jwt +from datetime import datetime, timedelta +from flask import request +from mock_data import MOCK_USERS, UserInfo + +# TODO: Replace with your own secret key +ACCESS_TOKEN_SECRET = 'access_token_secret' +REFRESH_TOKEN_SECRET = 'refresh_token_secret' + +def generate_access_token(user: UserInfo): + payload = { + 'user': user, + 'iat': datetime.utcnow(), + 'exp': datetime.utcnow() + timedelta(days=7) + } + return jwt.encode(payload, ACCESS_TOKEN_SECRET, algorithm='HS256') + +def generate_refresh_token(user: UserInfo): + payload = { + 'user': user, + 'iat': datetime.utcnow(), + 'exp': datetime.utcnow() + timedelta(days=30) + } + return jwt.encode(payload, REFRESH_TOKEN_SECRET, algorithm='HS256') + +def verify_access_token(): + auth_header = request.headers.get('Authorization') + if not auth_header or not auth_header.startswith('Bearer '): + return None + token = auth_header.split(' ')[1] + try: + decoded = jwt.decode(token, ACCESS_TOKEN_SECRET, algorithms=['HS256']) + username = decoded['user']['username'] + user = next((u for u in MOCK_USERS if u['username'] == username), None) + if user: + user_info = {k: v for k, v in user.items() if k != 'password'} + return user_info + return None + except jwt.PyJWTError: + return None + +def verify_refresh_token(token): + try: + decoded = jwt.decode(token, REFRESH_TOKEN_SECRET, algorithms=['HS256']) + username = decoded['user']['username'] + user = next((u for u in MOCK_USERS if u['username'] == username), None) + if user: + user_info = {k: v for k, v in user.items() if k != 'password'} + return user_info + return None + except jwt.PyJWTError: + return None \ No newline at end of file diff --git a/old.utils/response_utils.py b/old.utils/response_utils.py new file mode 100644 index 0000000..423c3f4 --- /dev/null +++ b/old.utils/response_utils.py @@ -0,0 +1,42 @@ +from flask import jsonify + +def use_response_success(data=None): + return jsonify({ + 'code': 0, + 'data': data, + 'error': None, + 'message': 'ok' + }) + +def use_page_response_success(page, page_size, list_data, message='ok'): + page = int(page) + page_size = int(page_size) + offset = (page - 1) * page_size + page_data = list_data[offset:offset + page_size] + return jsonify({ + 'code': 0, + 'data': { + 'items': page_data, + 'total': len(list_data) + }, + 'error': None, + 'message': message + }) + +def use_response_error(message, error=None): + return jsonify({ + 'code': -1, + 'data': None, + 'error': error, + 'message': message + }) + +def forbidden_response(message='Forbidden Exception'): + return use_response_error(message, message), 403 + +def un_authorized_response(): + return use_response_error('Unauthorized Exception', 'Unauthorized Exception'), 401 + +def sleep(ms): + import time + time.sleep(ms / 1000) \ No newline at end of file diff --git a/old_routes/__init__.py b/old_routes/__init__.py new file mode 100644 index 0000000..348889a --- /dev/null +++ b/old_routes/__init__.py @@ -0,0 +1,5 @@ +from flask import Blueprint + +auth_bp = Blueprint('auth', __name__) +other_bp = Blueprint('other', __name__) +user_bp = Blueprint('user', __name__) \ No newline at end of file diff --git a/old_routes/__pycache__/__init__.cpython-311.pyc b/old_routes/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e465dd30a75938faf54dc05944a5f25d9c000489 GIT binary patch literal 406 zcmZ3^%ge<81X>@qrB4CUk3k$5V1Y6|M*$hr8B!Qh7;_kM8KW2(8JHMS8B>^2nU*mz zFsue*2#8_^i!)=0vw+1}FvM9?Sc4ff*Ci#5L_ zBem!jOKEXxk)I~pE!MQ0#NzBCW}xz09P#maiMgrq@weDPYU7g%ZgGH>gIMe!9YDrP zhR-0ge>pi@#e@QdieoB@GIJ91(qmjwi?d7e3u1CIlZx_-@?o?tP*GWGQA|;OX-R5v zOniK1US>&ryk0@&FAkgB{FKt1RJ$U6pplF~Tyy qagj^)3YTgF`wec94$BGQGcqo6t6$+(Z{WCruA+gxfxU