Skip to content

Commit e37f09a

Browse files
Merge pull request #135 from ISE-GenAI-TechX25-Section-D/hashingPasswords
Added hashing
2 parents d8833a7 + cccb4d6 commit e37f09a

5 files changed

Lines changed: 71 additions & 13 deletions

File tree

bq_scripts/createUsersbackup.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from google.cloud import bigquery
2+
3+
def backup_users_table():
4+
client = bigquery.Client()
5+
6+
source_table = "diegoperez16techx25.Committees.Users"
7+
backup_table = "diegoperez16techx25.Committees.Users_backup"
8+
9+
job = client.copy_table(source_table, backup_table)
10+
11+
job.result() # Wait for the job to complete
12+
13+
print("✅ Backup complete: Users → Users_backup")
14+
15+
backup_users_table()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from google.cloud import bigquery
2+
import bcrypt
3+
4+
def hash_existing_passwords():
5+
client = bigquery.Client()
6+
7+
# Step 1: Fetch users with unhashed passwords (assumes hash is > 60 chars)
8+
query = """
9+
SELECT UserId, Password
10+
FROM `diegoperez16techx25.Committees.Users`
11+
WHERE LENGTH(Password) < 60
12+
"""
13+
results = client.query(query).result()
14+
15+
updated_count = 0
16+
17+
for row in results:
18+
user_id = row.UserId
19+
plain_pw = row.Password
20+
21+
# Step 2: Hash password
22+
hashed_pw = bcrypt.hashpw(plain_pw.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
23+
24+
# Step 3: Update the user's password
25+
update_query = f"""
26+
UPDATE `diegoperez16techx25.Committees.Users`
27+
SET Password = '{hashed_pw}'
28+
WHERE UserId = '{user_id}'
29+
"""
30+
client.query(update_query).result()
31+
updated_count += 1
32+
print(f"✅ Hashed password for {user_id}")
33+
34+
print(f"🔐 Done! {updated_count} user passwords were hashed.")
35+
36+
if __name__ == "__main__":
37+
hash_existing_passwords()
3.35 MB
Loading

modules.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import pandas as pd
1515
from google.cloud import bigquery
1616
from datetime import datetime, date
17+
import bcrypt
1718

1819
# This one has been written for you as an example. You may change it as wanted.
1920
def display_my_custom_component(value):
@@ -226,15 +227,19 @@ def display_genai_advice(
226227

227228
streamlit_module.caption(f"Last updated: {timestamp}")
228229

230+
import bcrypt # 🔐 Add this at the top of your file
231+
229232
def login_box():
233+
def check_password(plain_password, hashed_password):
234+
return bcrypt.checkpw(plain_password.encode(), hashed_password.encode())
235+
230236
sl.subheader("🔐 Login")
231237

232238
username = sl.text_input("Username")
233239
password = sl.text_input("Password", type="password")
234240

235241
login_button = sl.button("Log In")
236242

237-
238243
if login_button:
239244
if not username or not password:
240245
sl.warning("Please enter both username and password.")
@@ -248,25 +253,24 @@ def login_box():
248253
sl.error("User not found.")
249254
return False
250255

251-
if password != expected_password:
256+
if not check_password(password, expected_password):
252257
sl.error("Incorrect password.")
253258
return False
254259

255260
sl.success(f"Welcome back, {user_info['full_name']}!")
256261
sl.session_state.userId = userID
257262
sl.rerun()
258-
return True # Can be used to set session state or display more info
263+
return True
259264

260265
return None
261266

267+
262268
def signup_box():
263269
sl.subheader("📝 Sign Up")
264270

265-
# Check for submitted state
266271
if "signup_submitted" not in sl.session_state:
267272
sl.session_state.signup_submitted = False
268273

269-
# If already submitted, show success and login button
270274
if sl.session_state.signup_submitted:
271275
sl.success("✅ Account created successfully!")
272276
if sl.button("Go to Login"):
@@ -278,10 +282,10 @@ def signup_box():
278282
first_name = sl.text_input("First Name")
279283
last_name = sl.text_input("Last Name")
280284
dob = sl.date_input(
281-
"Date of Birth",
282-
value=date(2000, 1, 1),
283-
min_value=date(1900, 1, 1),
284-
max_value=datetime.today().date()
285+
"Date of Birth",
286+
value=date(2000, 1, 1),
287+
min_value=date(1900, 1, 1),
288+
max_value=datetime.today().date()
285289
)
286290
username = sl.text_input("Username")
287291
image_url = sl.text_input("Profile Image URL (optional)")
@@ -291,7 +295,6 @@ def signup_box():
291295
signup_button = sl.button("Create Account")
292296

293297
if signup_button:
294-
# Validate required fields
295298
if not all([first_name, last_name, dob, username, password, confirm_password]):
296299
sl.warning("Please fill in all required fields.")
297300
return None
@@ -304,15 +307,17 @@ def signup_box():
304307
sl.error("Username already taken.")
305308
return None
306309

310+
311+
hashed_password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode('utf-8')
312+
307313
full_name = f"{first_name} {last_name}"
308314

309315
create_new_user(
310316
username=username,
311317
name=full_name,
312318
image_url=image_url,
313319
date_of_birth=str(dob),
314-
password=password
315-
320+
password=hashed_password
316321
)
317322

318323
sl.success("Account created! You can now log in.")

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ datetime
66
google-cloud-aiplatform==1.69.0
77
python-dotenv==1.0.1
88
Pillow==11.1.0
9-
streamlit-option-menu
9+
streamlit-option-menu
10+
bcrypt

0 commit comments

Comments
 (0)