Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d784c994a | |||
| 49ed92d37e | |||
| 697b074ee3 | |||
| 2849fffcde |
@@ -0,0 +1,10 @@
|
|||||||
|
config:
|
||||||
|
path_mode: relative
|
||||||
|
|
||||||
|
prompts:
|
||||||
|
preset: "下面的代码来自项目相对路径 {{path}}:\n---\n{{content}}"
|
||||||
|
|
||||||
|
preset:
|
||||||
|
auth:
|
||||||
|
- "app/auth.py"
|
||||||
|
- "app/auth/*"
|
||||||
+5
-1
@@ -12,4 +12,8 @@ wheels/
|
|||||||
.env
|
.env
|
||||||
|
|
||||||
*.json
|
*.json
|
||||||
*.log
|
*.log
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
.git
|
||||||
+3
-3
@@ -6,7 +6,7 @@ import secrets
|
|||||||
from app.auth.deps import get_current_user
|
from app.auth.deps import get_current_user
|
||||||
from app.db import SessionLocal
|
from app.db import SessionLocal
|
||||||
from app.models import User
|
from app.models import User
|
||||||
from app.auth.jwt import create_access_token, create_refresh_token, decode_token
|
from app.auth.jwt import create_jwt_token, create_refresh_token, decode_token
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ async def auth_callback(request: Request, code: str, state: str):
|
|||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
jwt_token = create_access_token({
|
jwt_token = create_jwt_token({
|
||||||
"sub": user.id,
|
"sub": user.id,
|
||||||
"email": user.email,
|
"email": user.email,
|
||||||
})
|
})
|
||||||
@@ -125,7 +125,7 @@ def refresh_token(refresh_token: str):
|
|||||||
|
|
||||||
user_id = payload.get("sub")
|
user_id = payload.get("sub")
|
||||||
|
|
||||||
new_access = create_access_token({"sub": user_id})
|
new_access = create_jwt_token({"sub": user_id})
|
||||||
|
|
||||||
return {"access_token": new_access}
|
return {"access_token": new_access}
|
||||||
|
|
||||||
|
|||||||
+6
-3
@@ -6,14 +6,17 @@ from fastapi import HTTPException, status
|
|||||||
JWT_SECRET = os.getenv("JWT_SECRET")
|
JWT_SECRET = os.getenv("JWT_SECRET")
|
||||||
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256")
|
JWT_ALGORITHM = os.getenv("JWT_ALGORITHM", "HS256")
|
||||||
|
|
||||||
|
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60"))
|
||||||
|
REFRESH_TOKEN_EXPIRE_DAYS = int(os.getenv("REFRESH_TOKEN_EXPIRE_DAYS", "7"))
|
||||||
|
|
||||||
def now():
|
def now():
|
||||||
return datetime.now(timezone.utc)
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
def create_access_token(data: dict):
|
def create_jwt_token(data: dict):
|
||||||
payload = {
|
payload = {
|
||||||
**data,
|
**data,
|
||||||
"type": "access",
|
"type": "access",
|
||||||
"exp": now() + timedelta(minutes=60),
|
"exp": now() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
|
||||||
"iat": now(),
|
"iat": now(),
|
||||||
}
|
}
|
||||||
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
||||||
@@ -22,7 +25,7 @@ def create_refresh_token(data: dict):
|
|||||||
payload = {
|
payload = {
|
||||||
**data,
|
**data,
|
||||||
"type": "refresh",
|
"type": "refresh",
|
||||||
"exp": now() + timedelta(days=7),
|
"exp": now() + timedelta(days=REFRESH_TOKEN_EXPIRE_DAYS),
|
||||||
"iat": now(),
|
"iat": now(),
|
||||||
}
|
}
|
||||||
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
|
||||||
|
|||||||
Reference in New Issue
Block a user