56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
import sqlite3
|
|
from datetime import datetime, timedelta
|
|
|
|
class otp:
|
|
|
|
def connection():
|
|
try:
|
|
conn = sqlite3.connect('otp.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute('''CREATE TABLE IF NOT EXISTS sessions (email text UNIQUE, code integer, datedel text)''')
|
|
conn.commit()
|
|
conn.close()
|
|
return True
|
|
except:
|
|
return False
|
|
|
|
|
|
def check_code(email):
|
|
if (otp.connection()):
|
|
exists = '''SELECT email FROM sessions WHERE email = ? LIMIT 1'''
|
|
conn = sqlite3.connect('otp.db')
|
|
cursor = conn.cursor()
|
|
cursor.execute(exists, (email, ))
|
|
result = cursor.fetchone()
|
|
if (result == None):
|
|
conn.close()
|
|
else:
|
|
past_sql = '''SELECT datedel FROM sessions WHERE email = ? LIMIT 1'''
|
|
cursor.execute(past_sql, (email, ))
|
|
fetch_past = cursor.fetchone()
|
|
past = fetch_past[0]
|
|
present = datetime.now()
|
|
if (str(present) > past):
|
|
del_sql = '''DELETE FROM sessions WHERE email = ?'''
|
|
cursor.execute(del_sql, (email, ))
|
|
conn.commit()
|
|
conn.close()
|
|
return False
|
|
return True
|
|
|
|
def authenticate(email, user_otp):
|
|
if (otp.check_code(email)):
|
|
conn = sqlite3.connect('otp.db')
|
|
cursor = conn.cursor()
|
|
match_sql = '''SELECT code FROM sessions WHERE email = ? LIMIT 1'''
|
|
cursor.execute(match_sql, (email, ))
|
|
fetch_otp = cursor.fetchone()
|
|
print(fetch_otp[0])
|
|
print(user_otp)
|
|
if (str(fetch_otp[0]) == str(user_otp)):
|
|
conn.close()
|
|
return True
|
|
else:
|
|
conn.close()
|
|
return False
|