Skip to content

Commit 647320e

Browse files
committed
feat(auth): Add JWT token lifetime and improve user authentication models
1 parent c82ed8b commit 647320e

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

app/db/dao/dummy_dao.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ async def get_all_dummies(self, limit: int, offset: int) -> list[DummyModel]:
3434
select(DummyModel).limit(limit).offset(offset),
3535
)
3636

37-
return list(raw_dummies.scalars().fetchall())
37+
return list(raw_dummies.scalars().all())
3838

3939
async def filter(self, name: str | None = None) -> list[DummyModel]:
4040
"""
41-
Get specific dummy model.
41+
Get specific dummy models.
4242
4343
:param name: name of dummy instance.
4444
:return: dummy models.
@@ -47,4 +47,4 @@ async def filter(self, name: str | None = None) -> list[DummyModel]:
4747
if name:
4848
query = query.where(DummyModel.name == name)
4949
rows = await self.session.execute(query)
50-
return list(rows.scalars().fetchall())
50+
return list(rows.scalars().all())

app/db/models/jwt_token.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from app.db.base import Base
2+
3+
4+
class Token(Base):
5+
"""
6+
Represents an access token response with its type.
7+
Used for transmitting access tokens to clients after authentication.
8+
"""
9+
10+
access_token: str
11+
token_type: str = "bearer" # # noqa: S105
12+
13+
14+
class TokenPayload(Base):
15+
"""Represents the payload section of a JWT, commonly holding user identification (subject)."""
16+
17+
sub: str | None = None

app/db/models/users.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""Setup User Authentication using FastAPI User
2+
See more: https://fastapi-users.github.io/fastapi-users/latest/ .
3+
"""
4+
15
import uuid
26
from typing import Annotated
37

@@ -70,7 +74,7 @@ def get_jwt_strategy() -> JWTStrategy:
7074
7175
:returns: instance of JWTStrategy with provided settings.
7276
"""
73-
return JWTStrategy(secret=settings.USERS_SECRET, lifetime_seconds=None)
77+
return JWTStrategy(secret=settings.USERS_SECRET, lifetime_seconds=86400) # 1 day
7478

7579

7680
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")

0 commit comments

Comments
 (0)