def __send_mail(self, username, email): """ Sends mail to the passed email """ try: s = smtplib.SMTP("smtp.gmail.com", 587) s.starttls() s.login(vars.email["username"], vars.email["password"]) otp = random.randint(111111, 999999) message = MIMEMultipart() message["From"] = vars.email["username"] message["To"] = email message["Subject"] = "otp" body = MIMEText("otp :- " + str(otp)) message.attach(body) if self.__save_otp(username, otp): s.sendmail(vars.email["username"], email, message.as_string()) s.quit() return "1" else: return error(1065) except Exception as e: print(vars.email) print(e) return error(1067)
def store(self, username, email, password): """ saves a user in the database """ if len(password) < 10: return error(1064) password = hashlib.md5(password.encode()).hexdigest() if not self.__validate_email(email): return error(1063) sql = f"INSERT INTO `users`(`Username`,`Email`,`Password`) VALUES ('{username}','{email}','{password}');" with self.__connection.cursor() as cursor: try: stmt = cursor.execute(sql) if stmt: self.__connection.commit() cursor.close() return "1" return "0" except pymysql.Error as e: if e.args[0] == 1062: return error(1062) return error(1071) def __del__(self): self.__connection.close()
def authenticate(self, username, password): """ authenticates a user in the database """ password = hashlib.md5(password.encode()).hexdigest() sql = f"SELECT * FROM `users` WHERE `Username`='{username}';" with self.__connection.cursor() as cursor: try: stmt = cursor.execute(sql) if stmt: c = cursor.fetchone() cursor.close() print(c) username = c["Username"] email = c["Email"] pwd = c["Password"] data = {"username": username, "email": email} if pwd == password: return json.dumps(data) else: return "0" else: return "2" except pymysql.Error as e: print(e.args[1]) return error(1071)
def __change_pass(self, username, new_pass): password = hashlib.md5(new_pass.encode()).hexdigest() sql = f"UPDATE `users` SET `Password`='{password}' WHERE `users`.`Username`='{username}';" with self.__connection.cursor() as cursor: try: stmt = cursor.execute(sql) k = self.__del_otp(username) if stmt and k: self.__connection.commit() cursor.close() return "1" elif not stmt and k: return "1" return error(1068) except pymysql.Error as e: print(e.args[1]) return error(1068)
def reset_pass(self, username, otp, new_pass): u = self.__verify_otp(username, otp) if u: return self.__change_pass(username, new_pass) return error(1069)
def forgot(self, username): email = self.__fetch_email(username) if email: return self.__send_mail(username, email) else: return error(1070)