From efbe2b36f3d027477b23bd68558e58fe14e98d5c Mon Sep 17 00:00:00 2001 From: qing <1776177662@qq.com> Date: Sat, 25 Jul 2026 14:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=89=80=E6=9C=89=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/create_sqlite3.sql | 4 +++ dnfs/dnfs.py | 63 ++++++++++++++++++++++++++++++------------- smtp/smtp.py | 0 3 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 smtp/smtp.py diff --git a/db/create_sqlite3.sql b/db/create_sqlite3.sql index 3deacb5..28ba662 100644 --- a/db/create_sqlite3.sql +++ b/db/create_sqlite3.sql @@ -13,6 +13,10 @@ CREATE TABLE IF NOT EXISTS Users ( CREATE TABLE IF NOT EXISTS Notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER NOT NULL, + owner VARCHAR(128) DEFAULT 'Unknow', title VARCHAR(128) NOT NULL, content TEXT NOT NULL ); + +-- 旧版增量 +-- ALTER TABLE Notes ADD COLUMN owner VARCHAR(128) DEFAULT 'Unknow'; diff --git a/dnfs/dnfs.py b/dnfs/dnfs.py index fdae19f..fb89004 100755 --- a/dnfs/dnfs.py +++ b/dnfs/dnfs.py @@ -22,6 +22,7 @@ from flask_cors import CORS from waitress import serve import os import json +import base64 import urllib.request import urllib.error from typing import Dict, Any @@ -157,7 +158,18 @@ CORS(app) # "pid": 0, # "title": "test", # "content": "hello" -#} +#} + +def decode_token(token: str): + idx = token.find('_') + if idx != -1: + raw = token[:idx] + # FIXME: 验证数字签名 + decoded = base64.b64decode(raw) + jj = decoded.decode('utf-8') + return json.loads(jj) + + @app.route('/add_note', methods=['POST']) def add_note(): @@ -174,14 +186,14 @@ def add_note(): return jsonify({"error": "Pid, title and content are required"}), 400 - #new_note = Note(pid=int(pid), title=title, content=content) - - #with app.app_context(): - - # db.session.add(new_note) - - # db.session.commit() + info = None + auth: str = request.headers.get('Authorization', '') + if auth.startswith('Bearer '): + token: str = auth[7:] + info = decode_token(token) + if info is None: + return jsonify({"error": "Warning: no Authorization"}), 401 if pid != 0: @@ -211,6 +223,7 @@ def add_note(): 'table': 'Notes', 'values': { "pid": pid, + 'owner': info['user'], "title": title, 'content': content } @@ -247,17 +260,6 @@ def get_notes(): notes = call_db_service(DB_SERVER_URL, select_payload) - #with app.app_context(): - - # notes = Note.query.filter(Note.pid == int(pid)) - - #result = [{ - # 'id': note['id'], - # 'pid': note['pid'], - # 'title': note['title'], - # 'content':note['content'] - #} for note in notes] - result = notes['data'] return jsonify(result), 200 @@ -276,6 +278,15 @@ def delete_note(): return jsonify({"error": "Note ID is required"}), 400 + info = None + auth: str = request.headers.get('Authorization', '') + if auth.startswith('Bearer '): + token: str = auth[7:] + info = decode_token(token) + + if info is None: + return jsonify({"error": "Warning: no Authorization"}), 401 + #with app.app_context(): # notes = Note.query.filter(Note.pid == int(note_id)) @@ -310,6 +321,20 @@ def delete_note(): return jsonify({'error': 'this node has a child '}), 400 + # 检查所有权 + select_payload = { + 'action': 'select', + 'table': 'Notes', + 'where': { + 'id': note_id + } + } + + data = call_db_service(DB_SERVER_URL, select_payload) + notes = data['data'] + if not info['user'] == 'admin': + if not len(notes) == 1 or not notes[0]['owner'] == info['user']: + return jsonify({"error": 'It\'s not your note'}), 403 delete_payload = { diff --git a/smtp/smtp.py b/smtp/smtp.py new file mode 100644 index 0000000..e69de29