Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
webomagic-auth
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Pratik Shrivastava
webomagic-auth
Commits
4d2ece43
Commit
4d2ece43
authored
Jul 20, 2024
by
Pratik Shrivastava
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add forgot password API
parent
2fb82414
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
282 additions
and
3 deletions
+282
-3
app/routes/user/__init__.py
app/routes/user/__init__.py
+122
-3
app/routes/user/user_dao_service.py
app/routes/user/user_dao_service.py
+17
-0
app/routes/user/user_payload.py
app/routes/user/user_payload.py
+20
-0
app/routes/user/user_response.py
app/routes/user/user_response.py
+15
-0
app/routes/user/user_validator.py
app/routes/user/user_validator.py
+3
-0
app/utils/mail_service.py
app/utils/mail_service.py
+105
-0
No files found.
app/routes/user/__init__.py
View file @
4d2ece43
import
os
import
string
import
traceback
import
secrets
from
flask
import
request
from
flask_restx
import
Resource
from
marshmallow
import
ValidationError
...
...
@@ -20,7 +18,7 @@ from app.routes.user.user_dao_service import *
from
app.routes.user.user_util
import
process_valid_loging
,
generate_otp
,
generate_api_key
from
app.routes.user.user_validator
import
*
from
app.utils.api_response
import
prepare_api_response
from
app.utils.mail_service
import
send_otp
from
app.utils.mail_service
import
send_otp
,
send_password_recovery
from
app.utils.redis
import
set_value
,
get_value
...
...
@@ -168,6 +166,127 @@ class LoginOTPVerify(Resource):
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/forgot-password'
,
methods
=
[
'POST'
])
class
ForgotPassword
(
Resource
):
@
ns
.
expect
(
*
forgot_password_payload
())
@
ns
.
response
(
*
forgot_password_response
())
@
sanitized
()
def
post
(
self
):
try
:
validator
=
OtpValidator
()
payload
:
dict
=
validator
.
load
(
request
.
get_json
())
email
:
str
=
payload
.
get
(
'email'
)
user_info
:
ORM
.
User
|
None
=
get_user_by_email
(
email
)
if
(
not
user_info
):
return
prepare_api_response
(
401
,
'Invalid Email'
)
otp
:
int
=
generate_otp
()
redis_key
:
str
=
str
(
user_info
.
id
)
+
'_'
+
'otp'
+
'_'
+
str
(
otp
)
link
:
str
=
'http://127.0.0.1:5000/webomagic-user/v1/user/forgot-password-verify/'
+
str
(
email
)
+
'/'
+
str
(
otp
)
send_password_recovery
(
'shadowtitan3003@gmail.com'
,
[
'pratikkumar441@gmail.com'
],
link
)
set_value
(
redis_key
,
1
,
60
*
5
)
return
prepare_api_response
(
SUCCESS_CODE
,
'OTP generated successfully'
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/forgot-password-verify/<string:email>/<int:otp>'
,
methods
=
[
'GET'
])
class
ForgotPasswordVerify
(
Resource
):
@
ns
.
expect
(
*
forgot_password_payload
())
@
ns
.
response
(
*
forgot_password_response
())
@
sanitized
()
def
get
(
self
,
email
,
otp
):
try
:
user_info
:
ORM
.
User
|
None
=
get_user_by_email
(
email
)
if
(
not
user_info
):
return
prepare_api_response
(
401
,
'Invalid Email'
)
redis_key
:
str
=
str
(
user_info
.
id
)
+
'_'
+
'otp'
+
'_'
+
str
(
otp
)
if
int
(
get_value
(
redis_key
))
!=
1
:
raise
Exception
(
'Invalid'
)
api_key
=
generate_api_key
()
process_valid_loging
(
user_info
,
api_key
)
return
prepare_api_response
(
SUCCESS_CODE
,
'Email Verified Successfully'
,
data
=
{
'apiKey'
:
api_key
}
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/set-new-password'
,
methods
=
[
'POST'
])
class
SetNewPassword
(
Resource
):
@
ns
.
expect
(
*
set_new_password_payload
())
@
ns
.
response
(
*
set_new_password_response
())
@
sanitized
()
@
fetch_user
()
def
post
(
self
,
user_id
:
int
):
try
:
validator
=
SetNewPasswordValidator
()
payload
:
dict
=
validator
.
load
(
request
.
get_json
())
newPassword
:
str
=
payload
.
get
(
'newPassword'
)
updated
:
bool
|
None
=
\
update_password
(
user_id
=
user_id
,
new_password
=
newPassword
)
if
not
updated
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
UPDATE_ERROR_MESSAGE
)
return
prepare_api_response
(
SUCCESS_CODE
,
UPDATE_SUCCESS_MESSAGE
)
except
ValidationError
as
e
:
return
prepare_api_response
(
VALIDATION_ERROR_CODE
,
VALIDATION_ERROR_MESSAGE
,
error
=
e
.
normalized_messages
()
)
except
Exception
as
e
:
logger
.
error
(
traceback
.
format_exc
())
return
prepare_api_response
(
EXCEPTION_CODE
,
str
(
e
))
@
ns
.
route
(
'/profile'
,
methods
=
[
'GET'
])
class
GetProfile
(
Resource
):
...
...
app/routes/user/user_dao_service.py
View file @
4d2ece43
from
typing
import
List
from
app
import
orm
as
ORM
from
app.app_config.constant
import
RoleTypeEnum
from
app
import
db
def
get_user_by_username
(
username
:
str
)
->
ORM
.
User
|
None
:
...
...
@@ -68,3 +69,19 @@ def get_user_list_for_project_admin(admin_id: int) -> List[ORM.User]:
else
:
# return all user data for super admin
return
ORM
.
User
.
query
.
filter
(
ORM
.
User
.
is_deleted
==
False
)
.
all
()
def
update_password
(
user_id
:
int
,
new_password
:
str
)
->
bool
|
None
:
try
:
user_info
:
ORM
.
User
=
get_user_by_id
(
user_id
)
user_info
.
password
=
new_password
db
.
session
.
commit
()
return
True
except
Exception
as
e
:
db
.
session
.
rollback
()
raise
e
app/routes/user/user_payload.py
View file @
4d2ece43
...
...
@@ -28,6 +28,26 @@ def otp_verify_payload() -> list:
return
[
payload
]
def
forgot_password_payload
()
->
list
:
payload
=
api
.
model
(
'forgot Password Payload'
,
{
'email'
:
fields
.
String
}
)
return
[
payload
]
def
set_new_password_payload
()
->
list
:
payload
=
api
.
model
(
'set New Password Payload'
,
{
'newPassword'
:
fields
.
String
}
)
return
[
payload
]
def
get_user_payload
()
->
list
:
parser
=
api
.
parser
()
...
...
app/routes/user/user_response.py
View file @
4d2ece43
...
...
@@ -55,6 +55,21 @@ def otp_verify_response() -> any:
data
=
fields
.
Nested
(
LOGIN_RESPONSE_MODEL
),
)
def
forgot_password_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
SUCCESS_MESSAGE
,
model_name
=
'forgot Password Response'
,
data
=
fields
.
Nested
(
LOGIN_RESPONSE_MODEL
),
)
def
set_new_password_response
()
->
any
:
return
prepare_swagger_response
(
code
=
SUCCESS_CODE
,
message
=
'Password Changed Successfully'
,
model_name
=
'set New Password Response'
,
)
def
get_user_response
()
->
any
:
return
prepare_swagger_response
(
...
...
app/routes/user/user_validator.py
View file @
4d2ece43
...
...
@@ -12,5 +12,8 @@ class OtpVerifyValidator(Schema):
email
=
fields
.
String
(
required
=
True
)
OTP
=
fields
.
Integer
(
required
=
True
)
class
SetNewPasswordValidator
(
Schema
):
password
=
fields
.
String
(
required
=
True
,
validate
=
validate
.
Length
(
min
=
6
))
class
GetUserValidator
(
Schema
):
id
=
fields
.
Integer
(
required
=
True
,
validate
=
validate
.
Range
(
min
=
1
))
\ No newline at end of file
app/utils/mail_service.py
View file @
4d2ece43
...
...
@@ -113,3 +113,108 @@ def send_otp(sender: str, receiver: list, otp: int) -> None:
except
Exception
as
e
:
print
(
e
)
return
None
def
send_password_recovery
(
sender
:
str
,
receiver
:
list
,
recovery_link
:
str
)
->
None
:
email_title
=
'OTP for Login'
msg
=
Message
(
email_title
,
sender
=
sender
,
recipients
=
receiver
)
# Define the OTP template
email_html
=
'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>OTP Template</title>
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap"
rel="stylesheet"
/>
</head>
<body
style="
margin: 0;
font-family: 'Poppins', sans-serif;
background: #ffffff;
font-size: 14px;
"
>
<div
style="
max-width: 680px;
margin: 0 auto;
padding: 45px 30px 60px;
background: #f4f7ff;
font-size: 14px;
color: #434343;
"
>
<main>
<div
style="
margin: 0;
margin-top: 70px;
padding: 92px 30px 115px;
background: #ffffff;
border-radius: 30px;
text-align: center;
"
>
<div style="width: 100
%
; max-width: 489px; margin: 0 auto;">
<h1
style="
margin: 0;
font-size: 24px;
font-weight: 500;
color: #1f1f1f;
"
>
OTP Verification
</h1>
<p
style="
margin: 0;
margin-top: 17px;
font-size: 16px;
font-weight: 500;
"
>
Hi {{ user }},
</p>
<p
style="
margin: 0;
margin-top: 17px;
font-weight: 500;
letter-spacing: 0.56px;
"
>
Use the following link to recover your password. This link is valid for
<span style="font-weight: 600; color: #1f1f1f;">5 minutes</span>.
Do not share this link with others.
</p>
<p>
{{ recovery_link }}
</p>
</div>
</div>
</main>
</div>
</body>
</html>
'''
email_html_rendered
=
render_template_string
(
email_html
,
user
=
receiver
[
0
],
recovery_link
=
recovery_link
)
msg
.
html
=
email_html_rendered
msg
.
subject
=
'Password Recovery'
try
:
mail
.
send
(
msg
)
return
'Email sent!'
except
Exception
as
e
:
print
(
e
)
return
None
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment