ZHIWEI666 commited on
Commit
9d4b060
·
verified ·
1 Parent(s): 189dfb4

Delete 密码迁移.py

Browse files
Files changed (1) hide show
  1. 密码迁移.py +0 -131
密码迁移.py DELETED
@@ -1,131 +0,0 @@
1
- # 密码迁移.py
2
- # ==========================================
3
- # 🔐 密码迁移脚本 - 一次性执行
4
- # ==========================================
5
- # 作用:将现有用户的明文密码批量转换为 SHA256 哈希
6
- # 执行时机:部署安全更新后,首次运行此脚本
7
- # 执行方式:python 密码迁移.py
8
- # ==========================================
9
- # ⚠️ 注意事项:
10
- # 1. 此脚本会直接修改 users.json
11
- # 2. 执行前请备份数据
12
- # 3. 只需执行一次,执行后可删除此脚本
13
- # 4. 登录接口已内置自动升级逻辑,此脚本为可选加速方案
14
- # ==========================================
15
-
16
- import os
17
- import json
18
- import hashlib
19
-
20
- # 密码哈希配置(必须与 安全认证.py 保持一致)
21
- PASSWORD_SALT = os.environ.get("PASSWORD_SALT", "ComfyUI-Ranking-Salt-v1")
22
-
23
- # 数据库路径配置
24
- if os.environ.get("SPACE_ID"):
25
- LOCAL_DB_DIR = "/tmp/local_db_data"
26
- else:
27
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
28
- LOCAL_DB_DIR = os.path.join(BASE_DIR, "cache")
29
-
30
-
31
- def hash_password(password: str) -> str:
32
- """将明文密码转换为 SHA256 哈希值"""
33
- salted_password = f"{PASSWORD_SALT}{password}"
34
- return hashlib.sha256(salted_password.encode("utf-8")).hexdigest()
35
-
36
-
37
- def is_hashed(password: str) -> bool:
38
- """判断密码是否已经是哈希格式(64位十六进制)"""
39
- if len(password) != 64:
40
- return False
41
- try:
42
- int(password, 16)
43
- return True
44
- except ValueError:
45
- return False
46
-
47
-
48
- def migrate_passwords():
49
- """批量迁移所有用户的密码为哈希格式"""
50
- users_file = os.path.join(LOCAL_DB_DIR, "users.json")
51
-
52
- # 检查文件是否存在
53
- if not os.path.exists(users_file):
54
- print(f"❌ 用户数据文件不存在: {users_file}")
55
- print(" 请确认数据库路径配置正确")
56
- return
57
-
58
- # 读取用户数据
59
- try:
60
- with open(users_file, "r", encoding="utf-8") as f:
61
- users_db = json.load(f)
62
- except Exception as e:
63
- print(f"❌ 读取用户数据失败: {e}")
64
- return
65
-
66
- # 统计信息
67
- total_users = len(users_db)
68
- migrated_count = 0
69
- already_hashed_count = 0
70
- skipped_count = 0
71
-
72
- print("=" * 50)
73
- print("🔐 开始密码迁移...")
74
- print(f" 总用户数: {total_users}")
75
- print("=" * 50)
76
-
77
- # 遍历所有用户
78
- for account, user_data in users_db.items():
79
- password = user_data.get("password", "")
80
-
81
- if not password:
82
- print(f"⚠️ 跳过: {account} (无密码字段)")
83
- skipped_count += 1
84
- continue
85
-
86
- if is_hashed(password):
87
- already_hashed_count += 1
88
- continue
89
-
90
- # 执行哈希迁移
91
- user_data["password"] = hash_password(password)
92
- migrated_count += 1
93
- print(f"✅ 已迁移: {account}")
94
-
95
- # 保存更新后的数据
96
- if migrated_count > 0:
97
- try:
98
- with open(users_file, "w", encoding="utf-8") as f:
99
- json.dump(users_db, f, ensure_ascii=False, indent=2)
100
- print("\n" + "=" * 50)
101
- print("✅ 迁移完成并已保存!")
102
- except Exception as e:
103
- print(f"\n❌ 保存失败: {e}")
104
- return
105
-
106
- # 输出统计结果
107
- print("\n📊 迁移统计:")
108
- print(f" - 总用户数: {total_users}")
109
- print(f" - 本次迁移: {migrated_count}")
110
- print(f" - 已是哈希: {already_hashed_count}")
111
- print(f" - 跳过: {skipped_count}")
112
- print("=" * 50)
113
-
114
- if migrated_count == 0:
115
- print("\n✨ 所有密码均已是哈希格式,无需迁移")
116
- else:
117
- print(f"\n🎉 成功将 {migrated_count} 个用户的密码升级为哈希存储!")
118
-
119
-
120
- if __name__ == "__main__":
121
- print("\n" + "=" * 50)
122
- print("🔐 ComfyUI-Ranking 密码安全迁移工具")
123
- print("=" * 50)
124
- print("\n⚠️ 警告: 此操作将修改 users.json 中的密码字段")
125
- print(" 建议先备份数据文件\n")
126
-
127
- confirm = "yes" # 自动确认,无需手动输入
128
- if confirm.lower() == "yes":
129
- migrate_passwords()
130
- else:
131
- print("\n❌ 已取消迁移")