From 5a0777033f6733c33fbd6119ade812e0c749be44 Mon Sep 17 00:00:00 2001 From: Parker Date: Mon, 4 Nov 2024 21:14:18 -0600 Subject: Work on refresh tokens --- api/util/authentication.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'api/util') diff --git a/api/util/authentication.py b/api/util/authentication.py index 507b806..b8ac6a6 100644 --- a/api/util/authentication.py +++ b/api/util/authentication.py @@ -4,7 +4,7 @@ from fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer from jwt.exceptions import InvalidTokenError from datetime import datetime, timedelta -from typing import Annotated +from typing import Annotated, Optional import jwt from api.util.db_dependency import get_db @@ -59,8 +59,23 @@ def create_access_token(data: dict, expires_delta: timedelta): return encoded_jwt -async def get_current_user( +# Backwards kinda of way to get refresh token support +# 'refresh_get_current_user' is only called from /refresh +# and alerts 'current_user' that it should expect a refresh token +async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): + user = await current_user(token) + return user + + +async def refresh_get_current_user( token: Annotated[str, Depends(oauth2_scheme)], +): + user = await current_user(token, is_refresh=True) + return user + + +async def current_user( + token: str, is_refresh: bool = False, db=Depends(get_db), ): -- cgit v1.2.3-70-g09d2