11from datetime import timedelta
2- from typing import Annotated , Any
2+ from typing import Annotated
33
44from fastapi import APIRouter , Depends , HTTPException , status
55from fastapi .responses import HTMLResponse
2020
2121@router .post ("/login/access-token" )
2222def login_access_token (
23- session : SessionDep , form_data : Annotated [OAuth2PasswordRequestForm , Depends ()]
23+ session : SessionDep ,
24+ form_data : Annotated [OAuth2PasswordRequestForm , Depends ()],
2425) -> Token :
2526 """
2627 OAuth2 compatible token login, get an access token for future requests
2728 """
2829 user = auth_service .authenticate (
29- session = session , email = form_data .username , password = form_data .password
30+ session = session ,
31+ email = form_data .username ,
32+ password = form_data .password ,
3033 )
3134 if not user :
3235 raise HTTPException (
3336 status_code = status .HTTP_400_BAD_REQUEST ,
3437 detail = "Incorrect email or password" ,
3538 )
36- elif not user .is_active :
39+ if not user .is_active :
3740 raise HTTPException (
38- status_code = status .HTTP_400_BAD_REQUEST , detail = "Inactive user"
41+ status_code = status .HTTP_400_BAD_REQUEST ,
42+ detail = "Inactive user" ,
3943 )
4044 access_token_expires = timedelta (minutes = settings .ACCESS_TOKEN_EXPIRE_MINUTES )
4145 return Token (
42- access_token = create_access_token (user .id , expires_delta = access_token_expires )
46+ access_token = create_access_token (user .id , expires_delta = access_token_expires ),
4347 )
4448
4549
4650@router .post ("/login/test-token" , response_model = UserPublic )
47- def test_token (current_user : CurrentUser ) -> Any :
51+ def test_token (current_user : CurrentUser ) -> CurrentUser :
4852 """
4953 Test access token
5054 """
@@ -63,15 +67,17 @@ def recover_password(email: str, session: SessionDep) -> Message:
6367 if user :
6468 password_reset_token = auth_service .generate_password_reset_token (email = email )
6569 email_data = email_service .generate_reset_password_email (
66- email_to = user .email , email = email , token = password_reset_token
70+ email_to = user .email ,
71+ email = email ,
72+ token = password_reset_token ,
6773 )
6874 email_service .send_email (
6975 email_to = user .email ,
7076 subject = email_data .subject ,
7177 html_content = email_data .html_content ,
7278 )
7379 return Message (
74- message = "If that email is registered, we sent a password recovery link"
80+ message = "If that email is registered, we sent a password recovery link" ,
7581 )
7682
7783
@@ -83,17 +89,20 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8389 email = auth_service .verify_password_reset_token (token = body .token )
8490 if not email :
8591 raise HTTPException (
86- status_code = status .HTTP_400_BAD_REQUEST , detail = "Invalid token"
92+ status_code = status .HTTP_400_BAD_REQUEST ,
93+ detail = "Invalid token" ,
8794 )
8895 user = user_service .get_user_by_email (session = session , email = email )
8996 if not user :
9097 # Don't reveal that the user doesn't exist - use same error as invalid token
9198 raise HTTPException (
92- status_code = status .HTTP_400_BAD_REQUEST , detail = "Invalid token"
99+ status_code = status .HTTP_400_BAD_REQUEST ,
100+ detail = "Invalid token" ,
93101 )
94- elif not user .is_active :
102+ if not user .is_active :
95103 raise HTTPException (
96- status_code = status .HTTP_400_BAD_REQUEST , detail = "Inactive user"
104+ status_code = status .HTTP_400_BAD_REQUEST ,
105+ detail = "Inactive user" ,
97106 )
98107 user_in_update = UserUpdate (password = body .new_password )
99108 user_service .update_user (
@@ -109,7 +118,7 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
109118 dependencies = [Depends (get_current_active_superuser )],
110119 response_class = HTMLResponse ,
111120)
112- def recover_password_html_content (email : str , session : SessionDep ) -> Any :
121+ def recover_password_html_content (email : str , session : SessionDep ) -> HTMLResponse :
113122 """
114123 HTML Content for Password Recovery
115124 """
@@ -122,9 +131,12 @@ def recover_password_html_content(email: str, session: SessionDep) -> Any:
122131 )
123132 password_reset_token = auth_service .generate_password_reset_token (email = email )
124133 email_data = email_service .generate_reset_password_email (
125- email_to = user .email , email = email , token = password_reset_token
134+ email_to = user .email ,
135+ email = email ,
136+ token = password_reset_token ,
126137 )
127138
128139 return HTMLResponse (
129- content = email_data .html_content , headers = {"subject:" : email_data .subject }
140+ content = email_data .html_content ,
141+ headers = {"subject:" : email_data .subject },
130142 )
0 commit comments