Add py files
This commit is contained in:
commit
78410fe209
72
codelistener.py
Normal file
72
codelistener.py
Normal file
@ -0,0 +1,72 @@
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from socket import *
|
||||
import requests
|
||||
from sendmail import mail
|
||||
from verification import otp
|
||||
|
||||
class MyRequestHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html")
|
||||
if self.path == '/':
|
||||
self.path = '/pages/index.html'
|
||||
try:
|
||||
file_to_open = open(self.path[1:]).read()
|
||||
self.send_response(200)
|
||||
except:
|
||||
file_to_open = "File Not Found"
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes(file_to_open, 'utf-8'))
|
||||
|
||||
def do_POST(self):
|
||||
|
||||
# getting the content length from the header information and then rfile is the POST request, with content_length being the number of bytes it needs to grab (optional for if you want to grab less bytes for whatever reason)
|
||||
content_length = int(self.headers['Content-Length'])
|
||||
data_input = bytes.decode(self.rfile.read(content_length))
|
||||
|
||||
if (data_input.startswith("email=")):
|
||||
data_str = str(data_input.replace('%40', '@'))
|
||||
formatted = data_str.replace('email=', '')
|
||||
mail.send(formatted, mail.gen_code(formatted))
|
||||
self.path = '/pages/otp.html'
|
||||
try:
|
||||
file_to_open = open(self.path[1:]).read()
|
||||
self.send_response(200)
|
||||
except:
|
||||
file_to_open = "File Not Found"
|
||||
self.send_response(404)
|
||||
self.end_headers()
|
||||
self.wfile.write(bytes(file_to_open, 'utf-8'))
|
||||
if (data_input.startswith("verifEmail=")):
|
||||
data_str = str(data_input.replace('%40', '@'))
|
||||
|
||||
# get email
|
||||
formatted_verifemail = data_str.replace('verifEmail=', '')
|
||||
formatted_verifnums = formatted_verifemail.replace('&verifOtp=', '')
|
||||
email = formatted_verifnums[:-4]
|
||||
|
||||
#get otp code
|
||||
otp_code = data_str[-4:]
|
||||
|
||||
print(email)
|
||||
print(otp)
|
||||
if (otp.authenticate(email, otp_code)):
|
||||
print("OK!")
|
||||
else:
|
||||
print("BAD!")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Handler = MyRequestHandler
|
||||
|
||||
|
||||
hostName = "localhost"
|
||||
serverPort = 8080
|
||||
|
||||
server = HTTPServer((hostName, serverPort), Handler)
|
||||
|
||||
server.serve_forever()
|
65
sendmail.py
Normal file
65
sendmail.py
Normal file
@ -0,0 +1,65 @@
|
||||
import smtplib
|
||||
from smtplib import *
|
||||
from email.message import EmailMessage
|
||||
from random import randrange
|
||||
import sqlite3
|
||||
from datetime import datetime, timedelta
|
||||
from verification import otp
|
||||
|
||||
class mail:
|
||||
|
||||
def __init__(self, email):
|
||||
self.email = email
|
||||
|
||||
def add_db(email, code):
|
||||
conn = sqlite3.connect('otp.db')
|
||||
cursor = conn.cursor()
|
||||
|
||||
insert_email = "INSERT OR IGNORE INTO sessions (email) VALUES (?)"
|
||||
insert_code = "UPDATE sessions SET code = ? WHERE email = ?"
|
||||
insert_datedel = "UPDATE sessions SET datedel = ? WHERE email = ?"
|
||||
|
||||
cursor.execute(insert_email, (email, ))
|
||||
cursor.execute(insert_code, (code, email, ))
|
||||
cursor.execute(insert_datedel, (datetime.now() + timedelta(minutes=5), email, ))
|
||||
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return True
|
||||
|
||||
def gen_code(email):
|
||||
if (otp.check_code(email)):
|
||||
code = randrange(1000, 9999)
|
||||
mail.add_db(email, code)
|
||||
return code
|
||||
else:
|
||||
print("Code already in progress!")
|
||||
|
||||
def send(email, code):
|
||||
try:
|
||||
smtpObj = smtplib.SMTP_SSL('mail.__server__.com', 465)
|
||||
|
||||
# Identify yourself to an ESMTP server using EHLO
|
||||
smtpObj.ehlo()
|
||||
|
||||
# Login to the server (if required)
|
||||
smtpObj.login('username', 'passwd')
|
||||
|
||||
# Send an email
|
||||
msg = EmailMessage()
|
||||
msg.set_content(f'You have requested a verification code. Your code is {code}. This code expires in 5 minutes.')
|
||||
|
||||
msg['Subject'] = 'UWP Community Verification Code'
|
||||
msg['From'] = ""
|
||||
msg['To'] = email
|
||||
|
||||
smtpObj.send_message(msg)
|
||||
|
||||
# Quit the SMTP session
|
||||
smtpObj.quit()
|
||||
except SMTPResponseException as e:
|
||||
print(e)
|
||||
|
||||
|
||||
|
55
verification.py
Normal file
55
verification.py
Normal file
@ -0,0 +1,55 @@
|
||||
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
|
Loading…
Reference in New Issue
Block a user