-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathClientAuth.py
161 lines (133 loc) · 3.65 KB
/
ClientAuth.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#! /usr/bin/env python3
# Client-side user authentication functions
# modules
import re
import hashlib
# params for user auth from dfc.conf
def user_auth():
# get usernames
fh = open('dfc.conf', mode='r', encoding='cp1252')
users=re.findall(r'Username: .*', fh.read())
usernames=list()
for i in range(0, len(users)):
usernames.append(str(users[i]).split()[1])
fh.close()
# get passwords
fh = open('dfc.conf', mode='r', encoding='cp1252')
passes=re.findall(r'Password: .*', fh.read())
passwords=list()
for i in range(0, len(passes)):
passwords.append(str(passes[i]).split()[1])
fh.close()
# dict with usernames:passwords
global auth_dict
auth_dict = {}
for i in range(0, len(users)):
entry={usernames[i]:passwords[i]}
auth_dict.update(entry)
return auth_dict
# client-side auth
def authenticate():
# config params
user_auth()
# authenticate username
# initialize an auth status
auth_status = ''
# give user 4 attempts
for i in range(0, 4):
if auth_status == 'Correct username.':
# go to password auth
pass
else:
# get username
username = input('username: ')
# initialize username auth
username_auth = []
ct = 0
for key, value in auth_dict.items():
ct += 1
if username == key:
# get specific username index in dictionary
username_auth.append(ct)
else:
username_auth.append(0)
if i < 2:
if sum(username_auth) > 0:
auth_status = 'Correct username.'
continue
else:
print('Wrong username. You have ' +str(3-i) + ' attempts left.')
continue
elif i == 2:
if sum(username_auth) > 0:
auth_status = 'Correct username.'
continue
else:
print('Wrong username. You have ' +str(3-i) + ' attempt left.')
continue
else:
if sum(username_auth) > 0:
auth_status = 'Correct username.'
continue
else:
print('Wrong username. You have no more attempts.\nExiting now....')
continue
# authenticate password
# get the index of the user in the auth_dict to check password in that index
user_index = sum(username_auth)
# re-initialize auth status
auth_status = ''
for i in range(0, 4):
if auth_status == 'Existing password.':
# pass authentication
pass
else:
# get password
password = input('password: ')
hash=hashlib.md5()
hash.update(password.encode())
password = hash.hexdigest()
# initialize password auth
password_auth = []
ct = 0
for key, value in auth_dict.items():
ct += 1
if password == value:
password_auth.append(ct)
else:
password_auth.append(0)
if i < 2:
if sum(password_auth) > 0:
auth_status = 'Existing password.'
continue
else:
print('Wrong password. You have ' +str(3-i) + ' attempts left.')
continue
elif i == 2:
if sum(password_auth) > 0:
auth_status = 'Existing password.'
continue
else:
print('Wrong password. You have ' +str(3-i) + ' attempt left.')
continue
else:
if sum(password_auth) > 0:
auth_status = 'Existing password.'
continue
else:
print('Wrong password. You have no more attempts.\nExiting now....')
sys.exit()
# Check that specific user used that password (auth username:password combination)
# get the index of the password in the auth_dict
pass_index = sum(password_auth)
if user_index == pass_index:
pass
else:
print('Wrong password. You have no more attempts.\nExiting now....')
sys.exit()
# Final auth after passing all checks
print('Authorization Granted.')
global final_authorization
final_authorization = (username, password)
return final_authorization
authenticate()