Skip to content

Commit 5da8161

Browse files
committed
refactor: invalid references to schema package
we renamed the schema package to dto and clearly some of the package references weren't changed by vscode this change the references manually
1 parent 0601eba commit 5da8161

File tree

9 files changed

+201
-192
lines changed

9 files changed

+201
-192
lines changed

src/labs/routers/auth/__init__.py

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
from uuid import UUID
1010

1111
from fastapi import APIRouter, Request, Depends,\
12-
HTTPException, status
12+
HTTPException, status
1313
from fastapi.security import OAuth2PasswordRequestForm
1414
from sqlalchemy.ext.asyncio import AsyncSession
1515

1616
from ...db import get_async_session
1717
from ...models import User
18-
from ...schema import UserResponse, Token
18+
from ...dto import UserResponse, Token
1919
from ...utils.auth import create_access_token
2020
from ..utils import get_current_user
2121

@@ -32,87 +32,90 @@
3232
router.include_router(router_initiate, prefix="/initiate")
3333
router.include_router(router_verify, prefix="/verify")
3434

35+
3536
@router.post(
36-
"/token",
37-
summary="Provides an endpoint for login via email and password",
37+
"/token",
38+
summary="Provides an endpoint for login via email and password",
3839
)
3940
async def login_for_auth_token(
40-
form_data: OAuth2PasswordRequestForm = Depends(),
41-
session: AsyncSession = Depends(get_async_session)
41+
form_data: OAuth2PasswordRequestForm = Depends(),
42+
session: AsyncSession = Depends(get_async_session)
4243
) -> Token:
43-
""" Attempt to authenticate a user and issue JWT token
44-
45-
If the user does not exists or the password is incorrect
46-
then an exception is raised which returns a 4xx response.
47-
48-
"""
49-
user = await User.get_by_email(
50-
session,
51-
form_data.username
52-
)
53-
54-
if user is None or not user.check_password(form_data.password):
55-
raise HTTPException(
56-
status_code=status.HTTP_401_UNAUTHORIZED,
57-
detail="Incorrect username or password",
58-
headers={"WWW-Authenticate": "Bearer"},
44+
""" Attempt to authenticate a user and issue JWT token
45+
46+
If the user does not exists or the password is incorrect
47+
then an exception is raised which returns a 4xx response.
48+
49+
"""
50+
user = await User.get_by_email(
51+
session,
52+
form_data.username
53+
)
54+
55+
if user is None or not user.check_password(form_data.password):
56+
raise HTTPException(
57+
status_code=status.HTTP_401_UNAUTHORIZED,
58+
detail="Incorrect username or password",
59+
headers={"WWW-Authenticate": "Bearer"},
60+
)
61+
62+
access_token = create_access_token(
63+
subject=str(user.id),
64+
fresh=True
65+
)
66+
67+
return Token(
68+
access_token=access_token,
69+
token_type="bearer"
5970
)
6071

61-
access_token = create_access_token(
62-
subject=str(user.id),
63-
fresh=True
64-
)
65-
66-
return Token(
67-
access_token=access_token,
68-
token_type="bearer"
69-
)
7072

7173
@router.post(
72-
"/refresh",
73-
summary=""" Provides an endpoint for refreshing the JWT token""",
74+
"/refresh",
75+
summary=""" Provides an endpoint for refreshing the JWT token""",
7476
)
7577
async def refresh_jwt_token(request: Request,
76-
current_user: User = Depends(get_current_user)
77-
) -> Token:
78-
""" Provides a refresh token for the JWT session.
79-
80-
There must be a currently authenticated user for the refresh
81-
to work, otherwise an exception is raised.
82-
"""
83-
access_token = create_access_token(
84-
subject=str(current_user.id),
85-
)
86-
87-
return Token(
88-
access_token=access_token,
89-
token_type="bearer"
90-
)
78+
current_user: User = Depends(get_current_user)
79+
) -> Token:
80+
""" Provides a refresh token for the JWT session.
81+
82+
There must be a currently authenticated user for the refresh
83+
to work, otherwise an exception is raised.
84+
"""
85+
access_token = create_access_token(
86+
subject=str(current_user.id),
87+
)
88+
89+
return Token(
90+
access_token=access_token,
91+
token_type="bearer"
92+
)
93+
9194

9295
@router.post(
93-
"/logout",
94-
summary=""" Provides an endpoint for logging out the user""",
96+
"/logout",
97+
summary=""" Provides an endpoint for logging out the user""",
9598
)
9699
async def logout_user(
97-
session: AsyncSession = Depends(get_async_session)
100+
session: AsyncSession = Depends(get_async_session)
98101
):
99-
""" Ends a users session
102+
""" Ends a users session
103+
104+
Essentially invalidates a JWT token and then proceeds returns
105+
a success response.
106+
"""
107+
return {}
100108

101-
Essentially invalidates a JWT token and then proceeds returns
102-
a success response.
103-
"""
104-
return {}
105109

106110
@router.get(
107-
"/me",
111+
"/me",
108112
)
109113
async def get_me(
110-
current_user: User = Depends(get_current_user)
114+
current_user: User = Depends(get_current_user)
111115
) -> UserResponse:
112-
"""Get the currently logged in user or myself
113-
114-
This endpoint will return the currently logged in user or raise
115-
and exception if the user is not logged in.
116-
"""
117-
return current_user
116+
"""Get the currently logged in user or myself
118117
118+
This endpoint will return the currently logged in user or raise
119+
and exception if the user is not logged in.
120+
"""
121+
return current_user

src/labs/routers/auth/create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from ...db import get_async_session
1212
from ...models.user import User
13-
from ...schema.auth import SignupRequest, SignupResponse
13+
from ...dto.auth import SignupRequest, SignupResponse
1414

1515
from .tasks import send_account_verification_email
1616

src/labs/routers/auth/initiate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...models import User
99
from ...settings import settings
1010

11-
from ...schema.auth import OTPTriggerEmailRequest, \
11+
from ...dto.auth import OTPTriggerEmailRequest, \
1212
OTPTriggerSMSRequest, InitiateResetPasswordRequest
1313

1414
from .tasks import send_reset_password_email,\

src/labs/routers/auth/manage.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,38 @@
44

55
from ...db import get_async_session
66
from ...models.user import User
7-
from ...schema.auth import ResetPasswordRequest
7+
from ...dto.auth import ResetPasswordRequest
88

99
router = APIRouter()
1010

11+
1112
@router.post(
1213
"/reset",
1314
)
1415
async def reset_password(
1516
request: ResetPasswordRequest,
16-
session: AsyncSession = Depends(get_async_session),
17+
session: AsyncSession = Depends(get_async_session),
1718
):
1819
user = await User.get_by_email(
19-
session,
20-
request.email
20+
session,
21+
request.email
2122
)
2223

23-
# Even if there's an error we aren't going to reveal the
24-
# fact that the user exists or not
24+
# Even if there's an error we aren't going to reveal the
25+
# fact that the user exists or not
2526
if not user:
26-
raise HTTPException(
27-
status_code=status.HTTP_204_NO_CONTENT,
28-
)
29-
27+
raise HTTPException(
28+
status_code=status.HTTP_204_NO_CONTENT,
29+
)
30+
3031
reset_password_outcome = await user.reset_password(
3132
session,
3233
request.reset_token,
3334
request.password
3435
)
3536

3637
if not reset_password_outcome:
37-
raise HTTPException(
38-
status_code=status.HTTP_406_NOT_ACCEPTABLE,
39-
detail="Reset password failed"
40-
)
41-
42-
38+
raise HTTPException(
39+
status_code=status.HTTP_406_NOT_ACCEPTABLE,
40+
detail="Reset password failed"
41+
)

src/labs/routers/auth/verify.py

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
"""
55
from fastapi import APIRouter, Depends,\
6-
HTTPException, status
6+
HTTPException, status
77
from sqlalchemy.ext.asyncio import AsyncSession
88

99
from ...utils.auth import create_access_token
@@ -12,19 +12,20 @@
1212
from ...settings import settings
1313

1414
from ...models.user import User
15-
from ...schema.auth import VerifyAccountRequest, OTPVerifyRequest,\
16-
Token
15+
from ...dto.auth import VerifyAccountRequest, OTPVerifyRequest,\
16+
Token
1717

1818

1919
router = APIRouter()
2020

21+
2122
@router.post(
22-
"/account",
23-
status_code=status.HTTP_202_ACCEPTED
23+
"/account",
24+
status_code=status.HTTP_202_ACCEPTED
2425
)
2526
async def verify_user(
26-
request: VerifyAccountRequest,
27-
session: AsyncSession = Depends(get_async_session),
27+
request: VerifyAccountRequest,
28+
session: AsyncSession = Depends(get_async_session),
2829
):
2930
"""
3031
Verify an account using a one time token
@@ -38,59 +39,60 @@ async def verify_user(
3839
the token or accounts status is valid
3940
"""
4041
user = await User.get_by_email(
41-
session,
42-
request.email
42+
session,
43+
request.email
4344
)
4445

45-
# Even if there's an error we aren't going to reveal the
46-
# fact that the user exists or not
46+
# Even if there's an error we aren't going to reveal the
47+
# fact that the user exists or not
4748
if not user:
48-
raise HTTPException(
49-
status_code=status.HTTP_204_NO_CONTENT,
50-
)
51-
49+
raise HTTPException(
50+
status_code=status.HTTP_204_NO_CONTENT,
51+
)
52+
5253
verification_outcome = await user.verify_user_account(
53-
session,
54-
request.token
54+
session,
55+
request.token
5556
)
5657

5758
if not verification_outcome:
58-
raise HTTPException(
59-
status_code=status.HTTP_406_NOT_ACCEPTABLE,
60-
detail="Verification failed"
61-
)
59+
raise HTTPException(
60+
status_code=status.HTTP_406_NOT_ACCEPTABLE,
61+
detail="Verification failed"
62+
)
63+
6264

6365
@router.post("/otp")
6466
async def verify_otp(
65-
request: OTPVerifyRequest,
66-
session: AsyncSession = Depends(get_async_session)
67+
request: OTPVerifyRequest,
68+
session: AsyncSession = Depends(get_async_session)
6769
):
68-
""" Attempt to authenticate a user and issue JWT token
69-
70-
"""
71-
# Get the user account
72-
user = await User.get_by_phone(session, request.mobile_number)
73-
74-
if not user:
75-
raise HTTPException(status_code=401, detail="Invalid mobile number")
76-
77-
if not user.verify_otp(
78-
settings.lifetime.totp_token,
79-
settings.lifetime.totp_drift_window,
80-
request.otp
81-
):
82-
raise HTTPException(
83-
status_code=status.HTTP_401_UNAUTHORIZED,
84-
detail="Incorrect OTP",
85-
headers={"WWW-Authenticate": "Bearer"},
70+
""" Attempt to authenticate a user and issue JWT token
71+
72+
"""
73+
# Get the user account
74+
user = await User.get_by_phone(session, request.mobile_number)
75+
76+
if not user:
77+
raise HTTPException(status_code=401, detail="Invalid mobile number")
78+
79+
if not user.verify_otp(
80+
settings.lifetime.totp_token,
81+
settings.lifetime.totp_drift_window,
82+
request.otp
83+
):
84+
raise HTTPException(
85+
status_code=status.HTTP_401_UNAUTHORIZED,
86+
detail="Incorrect OTP",
87+
headers={"WWW-Authenticate": "Bearer"},
88+
)
89+
90+
access_token = create_access_token(
91+
subject=str(user.id),
92+
fresh=True
8693
)
8794

88-
access_token = create_access_token(
89-
subject=str(user.id),
90-
fresh=True
91-
)
92-
93-
return Token(
94-
access_token=access_token,
95-
token_type="bearer"
96-
)
95+
return Token(
96+
access_token=access_token,
97+
token_type="bearer"
98+
)

0 commit comments

Comments
 (0)