feat(auth): Implement create_refresh_token and add tests

This commit is contained in:
Yunxiao Xu
2026-02-18 13:25:36 -08:00
parent 1aa8faec00
commit 626b644740
2 changed files with 46 additions and 46 deletions

View File

@@ -61,6 +61,34 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -
encoded_jwt = jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm)
return encoded_jwt
def create_refresh_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
"""
Create a JWT refresh token.
Args:
data: The payload data to encode.
expires_delta: Optional expiration time delta.
Returns:
str: The encoded JWT token.
"""
to_encode = data.copy()
now = datetime.now(timezone.utc)
if expires_delta:
expire = now + expires_delta
else:
expire = now + timedelta(days=settings.refresh_token_expire_days)
to_encode.update({
"exp": expire,
"iat": now,
"iss": "ea-chatbot-api",
"type": "refresh"
})
encoded_jwt = jwt.encode(to_encode, settings.secret_key, algorithm=settings.algorithm)
return encoded_jwt
def decode_access_token(token: str) -> Optional[dict]:
"""
Decode a JWT access token.