-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
361 lines (224 loc) · 8.98 KB
/
functions.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import json
from functools import wraps
import re
from datetime import datetime
from datetime import timedelta
import pytz
from flask import (Flask, abort, jsonify, redirect,url_for, render_template, request, session)
from flask_socketio import SocketIO
import requests
import jwt # You need to have PyJWT library installed
import string
import random
import datetime
import pytz
from argon2 import PasswordHasher
import argon2.exceptions
JWT_EXPIRATION_MINUTES = 1440
skc = "kousic"
# Function to create a new JWT token
def create_jwt_token(username,sid,client_ip, skc):
payload = {
'username': username,
'ip': client_ip,
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=JWT_EXPIRATION_MINUTES),
'user_agent': request.headers.get('User-Agent'),
"sid": sid
}
token = jwt.encode(payload, skc, algorithm='HS256')
return token
def hash_password(password):
# Generate a salt
ph = PasswordHasher()
hash = ph.hash(password)
return hash
def read_file(file_path):
with open(file_path, 'r') as f:
return f.read()
def validate_password(password):
pattern = r'^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
return bool(re.match(pattern, password))
def InputValidation(u, p1, p2):
rp1 = r'[a-zA-Z]{5,}' # atleast six characters and minimum five characters should be alphabets
if re.match(rp1, u):
username=u
if (p1 == p2):
if (validate_password(p1) == 1):
p1 = hash_password(p1)
InpData = [username, p1]
return InpData
else:
error="Password must contain atleast 8 characters.<br>Atleast one Letter. <br> Atleast one Digit. <br>Atleast one character from @, $, !, %, ?, &."
return error
else:
error="Both Passwords should match!"
return error
else:
error= "Username should have atleast 6 Alphabets"
return error
#================================================================================================================================#
# Function to format current date and time in IST timezone
def format_date_time():
# Get the current datetime in IST timezone
ist = pytz.timezone('Asia/Kolkata')
current_datetime = datetime.datetime.now(ist)
# Format the datetime as "Mon DD YYYY hh:mmAM/PM"
formatted_datetime = current_datetime.strftime("%b %d %Y %I:%M%p")
return formatted_datetime
# Function to generate a random string of specified length
def generate_random_string(length=random.randint(30, 40)):
characters = string.ascii_letters + string.digits
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
# Function to send sign-up data to the API
def send_sign_up_data_to_db(username, password):
# API endpoint URL
url = "http://localhost/api/postUserData.php"
# JWT payload
payload = {
"username": username,
"password": password,
"datetime": format_date_time(),
"sid": generate_random_string()
}
# Your secret key
secret_key = 'kousic'
# Create JWT token
jwt_token = jwt.encode(payload, secret_key, algorithm='HS256')
# Custom headers including the JWT
headers = {
"Authorization": f"Bearer {jwt_token}"
}
# Send the POST request with custom headers and JWT token
response = requests.post(url, headers=headers)
# Print response content
# print("Response Content:", response.content)
return response.status_code
#==============for password verification==============#
def verify_password(entered_password, stored_hashed_password):
ph = PasswordHasher()
try:
passwords_match = ph.verify(stored_hashed_password, entered_password)
except argon2.exceptions.VerifyMismatchError:
# Password verification failed
return False
return passwords_match
#==============for password verification==============#
#S==============Shit is about to get real==============#
def isPasswordOkay(username, password):
# API endpoint URL
secret_key = "kousic"
url = "http://localhost/api/isPasswordOkay.php"
# Custom headers including the username and password
headers = {
"u": f"{username}",
"p": "kousic111"
}
# Send the GET request with custom headers
responsetoget = requests.get(url, headers=headers)
print("========================")
print(responsetoget)
print("========================")
print(responsetoget.status_code)
print("========================")
if responsetoget.status_code == 200 :
try:
response_json = responsetoget.json()
jwt_token = response_json['message']
print("========================")
print(f"This is the JWT : {jwt_token}" )
except (json.JSONDecodeError, KeyError):
print("Failed to extract JWT token from response")
return False
# Decode the JWT token
try:
decoded_payload = jwt.decode(jwt_token, secret_key, algorithms=['HS256'])
password_hash = decoded_payload['password']
if verify_password(password, password_hash):
client_ip = request.remote_addr
jwt_token1 = create_jwt_token(decoded_payload['username'], decoded_payload['sid'], client_ip, skc)
# API endpoint URL
url = "http://localhost/api/uploadToken.php"
# JWT payload
payload1 = {
"id": decoded_payload['id'],
"username": decoded_payload['username'],
"token": jwt_token1,
"aid": decoded_payload['aid'],
"valid": True
}
# Create JWT token#####Works because skc and secret_key are same####
jwt_token11 = jwt.encode(payload1, skc, algorithm='HS256')
# print("New JWT Token:", jwt_token11)
# Custom headers including the JWT
headers = {
"token": f"Bearer {jwt_token11}"
}
# Send the POST request with custom headers and JWT token
response1 = requests.post(url, headers=headers)
# Print response content
print("Response Content:", response1.content)
return [response1.status_code, jwt_token1]
else:
return False
except jwt.ExpiredSignatureError:
print("Token expired")
return False
except jwt.InvalidTokenError:
print("Invalid token")
return False
else:
return False
####=======================================####
#E==============Shit is about to get real==============#
# Function to send sign-up data to the API
def get_user_data_from_db(username):
# API endpoint URL
url = "http://localhost/api/getUserData.php"
# Your secret key
secret_key = 'kousic'
# Custom headers including the JWT
headers = {
"u": f"{username}",
"p": "kousic111"#Here is where you need to change the key||
}
# Send the POST request with custom headers and JWT token
response = requests.get(url, headers=headers)
try:
response_json = json.loads(response.content)
jwt_token = response_json['message']
except (json.JSONDecodeError, KeyError):
print("Failed to extract JWT token from response")
return
# Decode the JWT token
try:
decoded_payload = jwt.decode(jwt_token, secret_key, algorithms=['HS256'])
return decoded_payload
except jwt.ExpiredSignatureError:
return False
except jwt.InvalidTokenError:
return False
def require_token(f):
@wraps(f)
def decorated_function(*args, **kwargs):
token = session.get('authenticated')
if not token:
return redirect(url_for('LogIn'))
try:
payload = jwt.decode(token, skc, algorithms=['HS256'])
# Verify client IP against the IP in the token payload
client_ip = request.remote_addr
token_ip = payload.get('ip')
if token_ip and token_ip != client_ip:
return redirect(url_for('LogIn'))
# Verify user-agent header against the user-agent in the token payload
client_user_agent = request.headers.get('User-Agent')
token_user_agent = payload.get('user_agent')
if token_user_agent and token_user_agent != client_user_agent:
return redirect(url_for('LogIn'))
return f(*args, **kwargs)
except jwt.ExpiredSignatureError:
return redirect(url_for('LogIn'))
except jwt.InvalidTokenError:
return redirect(url_for('LogIn'))
return decorated_function