Add Password Check and OTP for Extra Security! 🔒
What I Want to Do 🎯
Make the banking app safer by:
- Making sure passwords are strong
- Adding a simple OTP (One-Time Password) when logging in
Simple Password Rules:
- At least 8 characters long
- Has at least one number
- Has at least one capital letter
- Has at least one special character (!@#$%^&*)
Simple OTP Rules:
- 6-digit code
- Sent to user's email
- Valid for 5 minutes
Where to Make Changes:
In secure_banking_system.py, we'll add:
- Password validation to
SecurityManager class
- Simple OTP generation and verification
Example Code:
import random
import time
class SecurityManager:
def __init__(self):
# Existing code...
self.otp_store = {} # Store OTPs temporarily
@staticmethod
def validate_password(password: str) -> bool:
"""Check if password meets security requirements"""
if len(password) < 8:
return False
if not any(char.isdigit() for char in password):
return False
if not any(char.isupper() for char in password):
return False
if not any(char in "!@#$%^&*" for char in password):
return False
return True
def generate_otp(self, email: str) -> str:
"""Generate a 6-digit OTP"""
otp = str(random.randint(100000, 999999))
self.otp_store[email] = {
'otp': otp,
'timestamp': time.time()
}
return otp
def verify_otp(self, email: str, otp: str) -> bool:
"""Verify if OTP is valid"""
if email not in self.otp_store:
return False
stored_data = self.otp_store[email]
# Check if OTP is expired (5 minutes)
if time.time() - stored_data['timestamp'] > 300:
del self.otp_store[email]
return False
if stored_data['otp'] == otp:
del self.otp_store[email]
return True
return False
How to Use It:
-
When user creates account:
if SecurityManager.validate_password(password):
# Create account
else:
print("Password is not strong enough!")
-
When user logs in:
# Generate and send OTP
otp = security_manager.generate_otp(user_email)
# In real app, send this OTP via email
# User enters OTP
if security_manager.verify_otp(user_email, entered_otp):
print("Login successful!")
else:
print("Invalid or expired OTP!")
Testing It Out ✅
Try these scenarios:
-
Password validation:
- "password" (should fail)
- "Password123!" (should work)
-
OTP verification:
- Enter correct OTP (should work)
- Enter wrong OTP (should fail)
- Wait 5 minutes (should expire)
Add Password Check and OTP for Extra Security! 🔒
What I Want to Do 🎯
Make the banking app safer by:
Simple Password Rules:
Simple OTP Rules:
Where to Make Changes:
In
secure_banking_system.py, we'll add:SecurityManagerclassExample Code:
How to Use It:
When user creates account:
When user logs in:
Testing It Out ✅
Try these scenarios:
Password validation:
OTP verification: