1 Star 0 Fork 0

枫Feng/ai-summary-for-blog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
security.py 962 Bytes
一键复制 编辑 原始数据 按行查看 历史
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
from fastapi import HTTPException, status
# 配置
SECRET_KEY = "your-secret-key-here" # 在生产环境中应该使用环境变量
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_token(token: str) -> Optional[str]:
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
if username is None:
return None
return username
except JWTError:
return None
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/canfeng_plaeir/ai-summary-for-blog.git
git@gitee.com:canfeng_plaeir/ai-summary-for-blog.git
canfeng_plaeir
ai-summary-for-blog
ai-summary-for-blog
main

搜索帮助