feat(init): 添加完整的认证和业务路由模块
- 实现微信登录、手机号绑定、token验证和登出功能 - 添加打卡系统,支持正常、迟到、请假等状态记录 - 集成课程投票功能,支持多课程选择和评分评论 - 开发反馈系统,支持图文反馈提交和管理员处理 - 构建首页页面模块,动态展示学习资源和个人信息 - 设计计划管理系统,支持周/月/年计划制定和跟踪 - 实现小组计划统计和历史记录查询功能 - 完善用户认证会话管理和数据库交互逻辑
This commit is contained in:
32
security.py
Normal file
32
security.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import logging
|
||||
import uuid
|
||||
import hmac
|
||||
import hashlib
|
||||
import time
|
||||
|
||||
import jwt as pyjwt
|
||||
|
||||
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_access_token(user_id):
|
||||
now = int(time.time())
|
||||
payload = {"sub": str(user_id), "iat": now, "exp": now + config.JWT_TTL_SECONDS}
|
||||
return pyjwt.encode(payload, config.JWT_SECRET, algorithm=config.JWT_ALGORITHM)
|
||||
|
||||
|
||||
def decode_access_token(token):
|
||||
return pyjwt.decode(token, config.JWT_SECRET, algorithms=[config.JWT_ALGORITHM])
|
||||
|
||||
|
||||
def create_session_token():
|
||||
"""生成写入 auth_sessions 的会话 token(长度适合 token VARCHAR(64))。"""
|
||||
return uuid.uuid4().hex + uuid.uuid4().hex[:16]
|
||||
Reference in New Issue
Block a user