Files
wechat_backend/security.py
王宏豪 3f1a13936d feat(api): 重构图片地址管理与上传机制
- 替换开发环境API基础URL为本地服务地址
- 实现签名URL本地缓存机制,解决微信按URL缓存图片导致重复下载问题
- 添加缓存清理功能,包括过期清理和按需删除特定缓存项
- 集成阿里云OSS上传功能,支持直传减少服务器带宽占用
- 优化头像地址处理逻辑,统一使用签名缓存避免地址频繁变化
- 移除过时的OSS上传示例文件oss-upload.js
- 更新课程页面实现跳转第三方小程序功能
- 调整打卡记录图片地址获取方式,统一使用新的签名缓存逻辑
- 优化图片上传流程,增加压缩处理和批量上传支持
- 完善认证模块中的缓存清理逻辑,切换账号时清除相关图片缓存
- 添加后端环境变量配置模板和代理文档说明
2026-09-23 17:46:42 +08:00

47 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import uuid
import hmac
import hashlib
from . import config
def encrypt_phone(phone):
return hmac.new(
config.PHONE_HMAC_KEY.encode("utf-8"),
phone.encode("utf-8"),
hashlib.sha256,
).hexdigest()
def create_session_token():
"""生成写入 auth_sessions 的会话 token(长度适合 token VARCHAR(64))。"""
return uuid.uuid4().hex + uuid.uuid4().hex[:16]
# ── 密码哈希(手机号+密码登录用,2026-09-22 新增)─────────────────────
# 统一用纯 SHA-256:hashlib.sha256(p.encode()).hexdigest(),
# 与遗留鉴权代码 routes/auth_api.py / routes/auth_app.py 的 hash_pwd() 完全一致。
# 刻意不引入 bcrypt / passlib / PBKDF2,也不加盐(保持与存量数据格式一致)。
# 存储格式:64 位十六进制 hex 串。
def hash_password(password):
"""把明文密码变成可入库的哈希串(纯 SHA-256 hex,无盐)。"""
if not password:
raise ValueError("password 不能为空")
return hashlib.sha256(password.encode()).hexdigest()
def verify_password(password, stored):
"""校验明文密码是否匹配存储串(恒时比较,防时序侧信道)。
⚠️ stored 为 None / 空串一律返回 False,**绝不把异常抛给调用方**。
典型场景:006 迁移还没执行时 SELECT * 取不到 password_hash 列,这里是 None。
"""
if not password or not stored:
return False
return hmac.compare_digest(
hashlib.sha256(password.encode()).hexdigest(),
stored,
)