From 8aa2671e501b1df3c607ac70edfdbf6a12a5b7f0 Mon Sep 17 00:00:00 2001 From: Michael McCamy Date: Wed, 15 Oct 2025 12:35:08 -0400 Subject: [PATCH] Created auth, db, and test file for user authentication --- test.py | 47 ++++++++++++++++++++++++++++++++++++++ utils/auth.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils/db.py | 9 ++++++++ 3 files changed, 118 insertions(+) create mode 100644 test.py create mode 100644 utils/auth.py create mode 100644 utils/db.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..1f6db2d --- /dev/null +++ b/test.py @@ -0,0 +1,47 @@ +from utils.auth import create_volunteer, login_volunteer, create_admin, login_admin + +# --- Test data --- +volunteer_test = { + "name": "Alice", + "email": "alice@test.com", + "password": "volpass123" +} + +admin_test = { + "name": "Bob", + "email": "bob@test.com", + "password": "adminpass123" +} + +ADMIN_KEY = "admin123" + +# --- Volunteer Tests --- +print("=== Volunteer Signup Test ===") +success, msg = create_volunteer(volunteer_test['name'], volunteer_test['email'], volunteer_test['password']) +print("Signup:", msg) + +print("\n=== Volunteer Login Test ===") +success, info = login_volunteer(volunteer_test['email'], volunteer_test['password']) +if success: + print("Login success:", info) +else: + print("Login failed:", info) + +# --- Admin Tests --- +print("\n=== Admin Signup Test ===") +success, msg = create_admin(admin_test['name'], admin_test['email'], admin_test['password']) +print("Admin signup:", msg) + +print("\n=== Admin Login Test ===") +success, info = login_admin(admin_test['email'], admin_test['password'], ADMIN_KEY) +if success: + print("Admin login success:", info) +else: + print("Admin login failed:", info) + +print("\n=== Admin Login Fail Test (wrong key) ===") +success, info = login_admin(admin_test['email'], admin_test['password'], "wrongkey") +if success: + print("Admin login successful (should not happen!)", info) +else: + print("Admin login failed as expected:", info) diff --git a/utils/auth.py b/utils/auth.py new file mode 100644 index 0000000..39c9b11 --- /dev/null +++ b/utils/auth.py @@ -0,0 +1,62 @@ +import hashlib +from utils.db import db + +def hash_password(password: str) -> str: + return hashlib.sha256(password.encode()).hexdigest() + +# Volunteer functions +def create_volunteer(name: str, email: str, password: str): + users = db.child("volunteers").get().val() or {} + if any(u['email'].lower() == email.lower() for u in users.values()): + return False, "Email already registered" + + db.child("volunteers").push({ + "name": name, + "email": email, + "password": hash_password(password), + "role": "volunteer" + }) + return True, "Account created successfully" + +def login_volunteer(email: str, password: str): + users = db.child("volunteers").get().val() or {} + for uid, user in users.items(): + if user['email'].lower() == email.lower() and user['password'] == hash_password(password): + return True, { + "user_id": uid, + "name": user['name'], + "email": user['email'], + "role": "volunteer" + } + return False, "Invalid email or password" + +# Admin functions +ADMIN_KEY = "admin123" # For testing purposes + +def create_admin(name: str, email: str, password: str): + users = db.child("admins").get().val() or {} + if any(u['email'].lower() == email.lower() for u in users.values()): + return False, "Email already registered" + + db.child("admins").push({ + "name": name, + "email": email, + "password": hash_password(password), + "role": "admin" + }) + return True, "Admin account created successfully" + +def login_admin(email: str, password: str, key: str): + if key != ADMIN_KEY: + return False, "Invalid admin key" + + users = db.child("admins").get().val() or {} + for uid, user in users.items(): + if user['email'].lower() == email.lower() and user['password'] == hash_password(password): + return True, { + "user_id": uid, + "name": user['name'], + "email": user['email'], + "role": "admin" + } + return False, "Invalid email or password" diff --git a/utils/db.py b/utils/db.py new file mode 100644 index 0000000..68890f2 --- /dev/null +++ b/utils/db.py @@ -0,0 +1,9 @@ +import json +import pyrebase + +with open("firebase_config.json") as f: + firebase_config = json.load(f) + +firebase = pyrebase.initialize_app(firebase_config) +auth = firebase.auth() # For authentication if needed +db = firebase.database() # Real-time database