Files
ToNav/utils/auth.py
OpenClaw Agent 872526505e Initial commit: ToNav Personal Navigation Page
- Flask + SQLite 个人导航页系统
- 前台导航页(分类Tab、卡片展示)
- 管理后台(服务管理、分类管理、健康检测)
- 响应式设计
- Systemd 服务配置
2026-02-12 21:57:15 +08:00

39 lines
911 B
Python
Raw 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.
# -*- coding: utf-8 -*-
"""认证工具"""
import hashlib
import sqlite3
from config import Config
def hash_password(password):
"""密码哈希SHA256简单版"""
return hashlib.sha256(password.encode()).hexdigest()
def verify_password(password, stored_hash):
"""验证密码"""
return hash_password(password) == stored_hash
def authenticate(username, password):
"""用户认证"""
conn = sqlite3.connect(Config.DATABASE_PATH)
cursor = conn.cursor()
cursor.execute('''
SELECT id, username, password_hash FROM users
WHERE username = ?
''', (username,))
user = cursor.fetchone()
conn.close()
if user and verify_password(password, user[2]):
return {
'id': user[0],
'username': user[1]
}
return None
def is_logged_in(session):
"""检查是否已登录"""
return 'user_id' in session