This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
User_Authenticator.py
85 lines (59 loc) · 2.55 KB
/
User_Authenticator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#user authentication Engine using MySQL datase
import mysql.connector as conn
from random import randint
def pass_gen() -> str:
passwd = ''.join(chr(randint(33, 126)) for _ in range(6))
return passwd
# Authentication class used for both server and client side
class user_db(object):
def __init__(self, host, username, password) -> None:
self.mydb = conn.connect(
host=host, # Specify the IP address or hostname here
user=username,
password=password
)
self.cursor = self.mydb.cursor(buffered=True)
#used in server script
def server_side(self,user):
try:
self.cursor.execute("CREATE DATABASE IF NOT EXISTS Authorization")
self.mydb.commit()
self.cursor.execute("USE Authorization")
self.mydb.commit()
self.cursor.execute("CREATE TABLE IF NOT EXISTS Auth (user VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL)")
self.mydb.commit()
except conn.Error as e:
print('Check your Mysql Server')
finally:
passwd = pass_gen()
try :
self.cursor.execute('Insert into Auth(user,password) values(%s,%s)',(user,passwd))
print('The password is : '+passwd)
print('Share this password with your user')
self.mydb.commit()
except :
self.cursor.execute('Select password from Auth where user = %s',(user,))
self.mydb.commit()
query = self.cursor.fetchone()
self.mydb.commit()
print('User already Exists with password : ' + query[0])
print('Share this password with your user')
self.cursor.close()
self.mydb.close()
#create a new client object in client script to execute it
def client_side(self,user,password):
self.cursor.execute('USE Authorization')
self.mydb.commit()
self.cursor.execute('Select password from Auth where user = %s',(user,))
self.mydb.commit()
query = self.cursor.fetchone()
if query[0] != password :
print('Wrong Password')
self.cursor.close()
self.mydb.close()
return 0
else :
print('User Authentication Sucessfull')
self.cursor.close()
self.mydb.close()
return 1