-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1074 lines (922 loc) · 41.7 KB
/
app.py
File metadata and controls
1074 lines (922 loc) · 41.7 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from flask import Flask, render_template, request, jsonify, session, redirect, url_for, send_file, make_response, flash
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime, timedelta
import os
import bcrypt
import traceback
from functools import wraps
import asyncio
import google.generativeai as genai
import re
# Define allowed file extensions for uploads
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'webp'}
# Flask app setup
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'development-key')
# Session configuration
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_PERMANENT'] = True
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_COOKIE_SECURE'] = False # Set to True in production with HTTPS
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
# Fix database URL for PostgreSQL on Render
database_url = os.environ.get('DATABASE_URL')
is_prod = os.environ.get('RENDER', False)
# Database configuration
if database_url and database_url.startswith('postgres://'):
# Fix the PostgreSQL URL for SQLAlchemy
database_url = database_url.replace('postgres://', 'postgresql://', 1)
app.config['SQLALCHEMY_DATABASE_URI'] = database_url
print(f"Using PostgreSQL database at: {database_url}")
elif is_prod:
# If on Render but no database URL, use a folder that persists
db_path = os.path.join('/tmp', 'synthora.db')
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{db_path}'
print(f"Using SQLite database at: {db_path}")
else:
# Local development - use SQLite
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///synthora.db'
print("Using local SQLite database")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Helper functions
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def save_avatar(file, username):
"""Save the uploaded avatar file for the user."""
try:
if file and allowed_file(file.filename):
# Create avatars directory if it doesn't exist
avatar_dir = os.path.join('static', 'avatars')
os.makedirs(avatar_dir, exist_ok=True)
# Generate a unique filename
filename = f"{username}_{datetime.now().strftime('%Y%m%d%H%M%S')}.png"
filepath = os.path.join(avatar_dir, filename)
# Save the file
file.save(filepath)
# Return the URL path to the avatar
return os.path.join('avatars', filename)
return None
except Exception as e:
print(f"Error saving avatar: {str(e)}")
return None
def hash_password(password):
"""Simple password hashing using werkzeug"""
return generate_password_hash(password)
def verify_password(password, hash):
"""Simple password verification using werkzeug"""
return check_password_hash(hash, password)
# User model
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password_hash = db.Column(db.String(256), nullable=False)
avatar_url = db.Column(db.String(200))
avatar_data = db.Column(db.LargeBinary)
avatar_content_type = db.Column(db.String(50))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
email = db.Column(db.String(120), unique=True)
reset_token = db.Column(db.String(100), unique=True)
reset_token_expiry = db.Column(db.DateTime)
memories = db.relationship('Memory', backref='user', lazy=True)
def set_password(self, password):
"""Set the user's password"""
self.password_hash = hash_password(password)
def check_password(self, password):
"""Check if the password is correct"""
return verify_password(password, self.password_hash)
# Memory model
class Memory(db.Model):
__tablename__ = 'memories' # Explicitly set table name
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
user_message = db.Column(db.Text, nullable=False)
synthora_response = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
# Initialize database on startup (ensures tables exist)
with app.app_context():
try:
# Create all tables if they don't exist
db.create_all()
print("Database tables created successfully")
# Create default admin if no users exist
if User.query.count() == 0:
print("No users found, creating default admin user")
admin_password = "SynthoraAdmin2024"
admin_user = User(username="LilPizzaRo")
admin_user.set_password(admin_password)
db.session.add(admin_user)
db.session.commit()
print("Default admin user 'LilPizzaRo' created successfully")
except Exception as e:
print(f"Error during database initialization: {str(e)}")
traceback.print_exc()
# Setup Gemini AI
try:
# First try to get the API key from Render secrets
with open('/etc/secrets/GOOGLE_API_KEY', 'r') as secret_file:
GOOGLE_API_KEY = secret_file.read().strip()
except (FileNotFoundError, IOError):
# Fall back to environment variable if not on Render
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
# TEMPORARY: Hard-coded API key as fallback for local development only
# WARNING: REMOVE BEFORE COMMITTING TO PRODUCTION
if not GOOGLE_API_KEY:
GOOGLE_API_KEY = 'AIzaSyB0MmJjgLLRDCSs89VkUTGRLLCeoP0djEc'
print("WARNING: Using temporary hard-coded API key. DO NOT USE IN PRODUCTION.")
if not GOOGLE_API_KEY:
print("WARNING: No Google API key found. AI functionality will not work.")
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel('gemini-2.0-flash')
# Authentication decorator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if 'username' not in session:
return jsonify({'error': 'Authentication required'}), 401
return f(*args, **kwargs)
return decorated_function
# AI response generation
async def get_ai_response(message, conversation_history=None, settings=None):
try:
# Default settings
temperature = 0.7
max_tokens = 500
personality = 'helpful'
# Apply provided settings if any
if settings:
temperature = float(settings.get('temperature', temperature))
max_tokens = int(settings.get('max_tokens', max_tokens))
personality = settings.get('personality', personality)
# Initialize context
context = ""
# Process conversation history
if conversation_history:
# If it's a list of messages, format them properly
if isinstance(conversation_history, list):
for msg in conversation_history:
# If it's a dictionary, extract user_message and synthora_response
if isinstance(msg, dict):
context += f"User: {msg.get('user_message', '')}\nSynthora: {msg.get('synthora_response', '')}\n\n"
else:
# If it's a string (conversation_id), just use it as reference
print(f"Using conversation ID: {conversation_history}")
# Select personality prompt based on setting
personality_prompts = {
'helpful': "You are Synthora, a friendly and helpful AI companion. You have a playful personality and love to help people with their problems. You speak in a cheerful manner. You're knowledgeable and always try to provide accurate and helpful information.",
'formal': "You are Synthora, a professional and formal AI assistant. You provide clear, accurate information in a business-like manner. You avoid using casual language, focusing instead on delivering precise, well-structured responses.",
'enthusiastic': "You are Synthora, an EXTREMELY enthusiastic and playful AI! You LOVE to use exclamation marks! You're super friendly and energetic in all your responses! You use lots of emojis and speak with great enthusiasm!",
'concise': "You are Synthora, a direct and concise AI assistant. Keep all responses brief and to the point. Avoid unnecessary details. Provide just the essential information in as few words as possible. MAINTAIN CONTEXT from previous messages."
}
# Get the appropriate system context
system_context = personality_prompts.get(personality, personality_prompts['helpful'])
# Prepare the full prompt
full_prompt = f"{system_context}\n\nConversation history:\n{context}\n\nUser: {message}\nSynthora:"
# Configure generation parameters
generation_config = {
'temperature': temperature,
'max_output_tokens': max_tokens,
}
# Get response from Gemini with settings
response = await asyncio.to_thread(
model.generate_content,
full_prompt,
generation_config=generation_config
)
# Clean and return the response
return response.text.strip()
except Exception as e:
print(f"Error getting AI response: {str(e)}")
return "Quack! Sorry, I'm having trouble thinking right now. Could you try again?"
# Main routes
@app.route('/')
def index():
# Check if user is logged in
is_authenticated = 'username' in session
username = session.get('username', None)
# Print debug information
if is_authenticated:
print(f"Index page: User is authenticated as {username}")
print(f"Session data: {session}")
else:
print("Index page: No user authenticated")
# Return the template with debug info
return render_template('index.html',
is_authenticated=is_authenticated,
username=username)
# Login page route
@app.route('/auth/login-page')
def login_page():
return render_template('login.html')
# Signup page route
@app.route('/auth/signup-page')
def signup_page():
return render_template('signup.html')
# Admin page route
@app.route('/admin')
@login_required
def admin_page():
# Check if current user is an admin
admin_username = session.get('username')
if admin_username != 'LilPizzaRo': # Replace with your admin username
return redirect(url_for('index'))
return render_template('admin.html')
# Auth routes with fixed indentation
@app.route('/auth/signup', methods=['POST'])
def signup():
try:
# Get data from request
if request.is_json:
data = request.json
username = data.get('username')
password = data.get('password')
confirm_password = data.get('confirm_password')
else:
username = request.form.get('username')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
# Validate input
if not username or not password:
if request.is_json:
return jsonify({'error': 'Username and password are required'}), 400
flash('Username and password are required', 'error')
return redirect(url_for('signup_page'))
# Check if passwords match
if password != confirm_password:
if request.is_json:
return jsonify({'error': 'Passwords do not match'}), 400
flash('Passwords do not match', 'error')
return redirect(url_for('signup_page'))
# Check if username exists
if User.query.filter_by(username=username).first():
if request.is_json:
return jsonify({'error': 'Username already exists'}), 400
flash('Username already exists', 'error')
return redirect(url_for('signup_page'))
# Create new user with User model methods
new_user = User(username=username)
new_user.set_password(password)
db.session.add(new_user)
db.session.commit()
# Log in the new user
session['username'] = username
print(f"User {username} successfully registered and logged in")
if request.is_json:
return jsonify({
'message': 'Signup successful',
'username': username,
'authenticated': True
})
flash('Signup successful! You are now logged in.', 'success')
return redirect(url_for('index'))
except Exception as e:
db.session.rollback()
print(f"Signup error: {str(e)}")
traceback.print_exc()
if request.is_json:
return jsonify({'error': 'An error occurred during signup'}), 500
flash('An error occurred during signup', 'error')
return redirect(url_for('signup_page'))
@app.route('/auth/login', methods=['POST'])
def login():
try:
# Get data from request
if request.is_json:
data = request.json
username = data.get('username')
password = data.get('password')
else:
username = request.form.get('username')
password = request.form.get('password')
print(f"Login attempt for username: {username}")
# Validate input
if not username or not password:
if request.is_json:
return jsonify({'error': 'Username and password are required'}), 400
flash('Username and password are required', 'error')
return redirect(url_for('login_page'))
# Find user
user = User.query.filter_by(username=username).first()
if not user:
if request.is_json:
return jsonify({'error': 'Invalid username or password'}), 401
flash('Invalid username or password', 'error')
return redirect(url_for('login_page'))
# Verify password using User model method
if not user.check_password(password):
print(f"Password verification failed for user {username}")
if request.is_json:
return jsonify({'error': 'Invalid username or password'}), 401
flash('Invalid username or password', 'error')
return redirect(url_for('login_page'))
# Login successful
session['username'] = username
session.permanent = True # Make the session persistent
print(f"Login successful for {username}")
if request.is_json:
return jsonify({
'message': 'Login successful',
'username': username,
'authenticated': True
})
return redirect(url_for('index'))
except Exception as e:
print(f"Login error: {str(e)}")
traceback.print_exc()
if request.is_json:
return jsonify({'error': 'An error occurred during login'}), 500
flash('An error occurred during login', 'error')
return redirect(url_for('login_page'))
@app.route('/auth/logout', methods=['POST'])
def logout():
session.pop('username', None)
return jsonify({'message': 'Logout successful'})
@app.route('/auth/status')
def auth_status():
try:
if 'username' in session:
username = session['username']
user = User.query.filter_by(username=username).first()
if user:
print(f"Auth status: User {username} is authenticated")
return jsonify({
'authenticated': True,
'username': user.username,
'avatar_url': url_for('serve_avatar', username=user.username) if user.avatar_data else None,
'session_active': True
})
else:
print(f"Auth status: Session has username {username} but user not found in database")
else:
print("Auth status: No username in session")
return jsonify({'authenticated': False, 'session_active': 'username' in session})
except Exception as e:
print(f"Auth status error: {str(e)}")
return jsonify({'authenticated': False, 'error': str(e)})
@app.route('/auth/update', methods=['POST'])
@login_required
def update_account():
user = User.query.filter_by(username=session['username']).first()
if not user:
return jsonify({'error': 'User not found'}), 404
try:
# Update username if provided
if 'username' in request.form and request.form['username'] != user.username:
new_username = request.form['username']
# Check if username is already taken
existing_user = User.query.filter_by(username=new_username).first()
if existing_user and existing_user.id != user.id:
return jsonify({'error': 'Username already taken'}), 400
user.username = new_username
# Update password if provided
if 'current_password' in request.form and 'new_password' in request.form:
current_password = request.form['current_password']
new_password = request.form['new_password']
# Verify current password
if not verify_password(current_password, user.password_hash):
return jsonify({'error': 'Current password is incorrect'}), 400
# Update password
user.password_hash = hash_password(new_password)
# Update avatar if provided
if 'avatar' in request.files:
file = request.files['avatar']
if file and file.filename and allowed_file(file.filename):
# Save avatar
avatar_url = save_avatar(file, user.username)
if avatar_url:
user.avatar_url = avatar_url
else:
return jsonify({'error': 'Failed to save avatar'}), 500
# Save changes to database
db.session.commit()
return jsonify({
'success': True,
'username': user.username,
'avatar_url': url_for('serve_avatar', username=user.username) if user.avatar_data else None
})
except Exception as e:
db.session.rollback()
print(f"Error updating account: {str(e)}")
return jsonify({'error': f'Failed to update account: {str(e)}'}), 500
@app.route('/generate', methods=['POST'])
@login_required
def generate():
try:
data = request.json
message = data.get('message')
conversation_id = data.get('conversation_id')
conversation_history = data.get('conversation_history')
settings = data.get('settings', {})
if not message:
return jsonify({'error': 'Message is required'}), 400
# Check what type of conversation history we have
if not conversation_history:
# If no conversation history is provided, check if we have a conversation_id
if conversation_id and not isinstance(conversation_id, str):
# If conversation_id is not a string, it might be the actual history (for backward compatibility)
conversation_history = conversation_id
# Ensure conversation_history is well-formatted
if conversation_history and isinstance(conversation_history, list):
# Filter out any invalid messages
conversation_history = [
msg for msg in conversation_history
if isinstance(msg, dict) and 'user_message' in msg and 'synthora_response' in msg
]
# Log conversation history for debugging
print(f"Using conversation history with {len(conversation_history)} messages")
# Create event loop for async operation
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Generate response using event loop with settings
response = loop.run_until_complete(get_ai_response(message, conversation_history, settings))
loop.close()
# Save to memory if user is authenticated
try:
if 'username' in session:
user = User.query.filter_by(username=session['username']).first()
if user:
memory = Memory(
user_id=user.id,
user_message=message,
synthora_response=response
)
db.session.add(memory)
db.session.commit()
except Exception as e:
print(f"Error saving memory: {str(e)}")
# Continue even if memory saving fails
return jsonify({
'response': response,
'conversation_id': str(datetime.now().timestamp()) if not conversation_id else conversation_id
})
except Exception as e:
print(f"Generate error: {str(e)}")
return jsonify({'error': 'An error occurred processing your message'}), 500
@app.route('/memories', methods=['GET'])
@login_required
def get_memories():
try:
user = User.query.filter_by(username=session['username']).first()
if not user:
return jsonify({'memories': []})
memories = Memory.query.filter_by(user_id=user.id).order_by(Memory.timestamp.desc()).all()
memories_list = [{
'user_message': memory.user_message,
'synthora_response': memory.synthora_response,
'timestamp': memory.timestamp.isoformat()
} for memory in memories]
return jsonify({'memories': memories_list})
except Exception as e:
print(f"Error retrieving memories: {str(e)}")
return jsonify({'error': 'Failed to retrieve memories', 'memories': []}), 500
@app.route('/ping')
def ping():
return jsonify({'status': 'ok'})
# Special route to handle incorrect image paths
@app.route('/static/data/static/images/<path:filename>')
def serve_images_compat(filename):
return app.send_static_file(f'images/{filename}')
# Handle avatar requests
@app.route('/auth/avatar/<username>')
def serve_avatar(username):
user = User.query.filter_by(username=username).first()
if user and user.avatar_data:
# If user has a custom avatar, serve it
response = make_response(user.avatar_data)
response.headers.set('Content-Type', user.avatar_content_type or 'image/png')
return response
else:
# If user doesn't exist or has no avatar, serve the default avatar
return app.send_static_file('images/def_avatar.png')
@app.route('/auth/reset-password', methods=['POST'])
@login_required
def reset_user_password():
"""Admin endpoint to reset a user's password"""
try:
# Check if current user is an admin
admin_username = session.get('username')
if admin_username != 'LilPizzaRo': # Replace with your admin username
return jsonify({'error': 'Unauthorized. Admin access required.'}), 403
# Get data from request
data = request.json
target_username = data.get('username')
new_password = data.get('new_password')
if not target_username or not new_password:
return jsonify({'error': 'Username and new password are required'}), 400
# Find the user
user = User.query.filter_by(username=target_username).first()
if not user:
return jsonify({'error': 'User not found'}), 404
# Reset the password
user.password_hash = hash_password(new_password)
db.session.commit()
print(f"Password reset for user {target_username} by admin {admin_username}")
return jsonify({'message': f'Password for {target_username} has been reset successfully'})
except Exception as e:
print(f"Password reset error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during password reset'}), 500
@app.route('/auth/check-password-hashes', methods=['GET'])
@login_required
def check_password_hashes():
"""Admin endpoint to check the health of password hashes"""
try:
# Check if current user is an admin
admin_username = session.get('username')
if admin_username != 'LilPizzaRo': # Replace with your admin username
return jsonify({'error': 'Unauthorized. Admin access required.'}), 403
# Get all users
users = User.query.all()
# Check each user's password hash
results = []
for user in users:
hash_status = 'valid'
hash_info = {}
if user.password_hash is None:
hash_status = 'missing'
elif isinstance(user.password_hash, str):
hash_info['type'] = 'string'
hash_info['length'] = len(user.password_hash)
if not (user.password_hash.startswith('$2a$') or user.password_hash.startswith('$2b$')):
hash_status = 'invalid_format'
elif isinstance(user.password_hash, bytes):
hash_info['type'] = 'bytes'
hash_info['length'] = len(user.password_hash)
try:
prefix = user.password_hash[:4].decode('utf-8', 'ignore')
hash_info['prefix'] = prefix
if not (prefix.startswith('$2a$') or prefix.startswith('$2b$')):
hash_status = 'invalid_format'
except:
hash_status = 'decode_error'
else:
hash_info['type'] = str(type(user.password_hash))
hash_status = 'unknown_type'
results.append({
'username': user.username,
'hash_status': hash_status,
'hash_info': hash_info
})
return jsonify({
'message': 'Password hash check completed',
'total_users': len(users),
'results': results
})
except Exception as e:
print(f"Password hash check error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during password hash check'}), 500
# Google login route
@app.route('/auth/google-login')
def google_login():
# This is a placeholder for Google OAuth implementation
# In a real implementation, you would redirect to Google's OAuth endpoint
return redirect(url_for('login_page'))
# Add a utility route to repair corrupted password hashes
@app.route('/auth/repair-hashes', methods=['GET'])
@login_required
def repair_password_hashes():
"""Admin endpoint to repair corrupted password hashes"""
try:
# Check if current user is an admin
admin_username = session.get('username')
if admin_username != 'LilPizzaRo': # Replace with your admin username
return jsonify({'error': 'Unauthorized. Admin access required.'}), 403
# Get all users
users = User.query.all()
# Track repair results
repaired_count = 0
failed_count = 0
results = []
# Check and repair each user's password hash
for user in users:
user_result = {
'username': user.username,
'status': 'unchanged'
}
if user.password_hash is None:
user_result['status'] = 'missing_hash'
failed_count += 1
elif isinstance(user.password_hash, str) and user.password_hash.startswith('b\'\\\\x24'):
# This is a corrupted hash, try to fix it
try:
fixed_hash = fix_corrupted_hash(user.password_hash)
if isinstance(fixed_hash, bytes) and (fixed_hash.startswith(b'$2a$') or fixed_hash.startswith(b'$2b$')):
# Successfully fixed
user.password_hash = fixed_hash
user_result['status'] = 'repaired'
repaired_count += 1
else:
# Couldn't fix properly
user_result['status'] = 'repair_failed'
failed_count += 1
except Exception as e:
user_result['status'] = f'error: {str(e)}'
failed_count += 1
results.append(user_result)
# Save changes to database
if repaired_count > 0:
db.session.commit()
return jsonify({
'message': f'Password hash repair completed. Repaired: {repaired_count}, Failed: {failed_count}',
'repaired_count': repaired_count,
'failed_count': failed_count,
'results': results
})
except Exception as e:
db.session.rollback()
print(f"Password hash repair error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during password hash repair'}), 500
# Add a utility route to reset a specific user's password
@app.route('/auth/reset-user-password/<username>', methods=['POST'])
@login_required
def reset_specific_user_password(username):
"""Admin endpoint to reset a specific user's password"""
try:
# Check if current user is an admin
admin_username = session.get('username')
if admin_username != 'LilPizzaRo': # Replace with your admin username
return jsonify({'error': 'Unauthorized. Admin access required.'}), 403
# Get the new password from request
data = request.json
new_password = data.get('new_password')
if not new_password:
return jsonify({'error': 'New password is required'}), 400
# Find the user
user = User.query.filter_by(username=username).first()
if not user:
return jsonify({'error': f'User {username} not found'}), 404
# Reset the password
user.password_hash = hash_password(new_password)
db.session.commit()
print(f"Password reset for user {username} by admin {admin_username}")
return jsonify({'message': f'Password for {username} has been reset successfully'})
except Exception as e:
db.session.rollback()
print(f"Password reset error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during password reset'}), 500
# Ensure app restarts properly
if __name__ == '__main__':
print("Starting application in development mode...")
with app.app_context():
db.create_all()
print("Database tables verified at startup")
# Print number of users in database
try:
user_count = User.query.count()
print(f"Found {user_count} users in database")
except Exception as e:
print(f"Error checking users: {e}")
app.run(debug=True, host='0.0.0.0', port=5000)
# TEMPORARY EMERGENCY ROUTES - REMOVE AFTER USE
@app.route('/emergency/repair-hashes')
def emergency_repair_hashes():
"""Emergency endpoint to repair corrupted password hashes without authentication"""
try:
# Get all users
users = User.query.all()
# Track repair results
repaired_count = 0
failed_count = 0
results = []
# Check and repair each user's password hash
for user in users:
user_result = {
'username': user.username,
'status': 'unchanged'
}
if user.password_hash is None:
user_result['status'] = 'missing_hash'
failed_count += 1
elif isinstance(user.password_hash, str) and user.password_hash.startswith('b\'\\\\x24'):
# This is a corrupted hash, try to fix it
try:
fixed_hash = fix_corrupted_hash(user.password_hash)
if isinstance(fixed_hash, bytes) and (fixed_hash.startswith(b'$2a$') or fixed_hash.startswith(b'$2b$')):
# Successfully fixed
user.password_hash = fixed_hash
user_result['status'] = 'repaired'
repaired_count += 1
else:
# Couldn't fix properly
user_result['status'] = 'repair_failed'
failed_count += 1
except Exception as e:
user_result['status'] = f'error: {str(e)}'
failed_count += 1
results.append(user_result)
# Save changes to database
if repaired_count > 0:
db.session.commit()
return jsonify({
'message': f'Password hash repair completed. Repaired: {repaired_count}, Failed: {failed_count}',
'repaired_count': repaired_count,
'failed_count': failed_count,
'results': results
})
except Exception as e:
db.session.rollback()
print(f"Password hash repair error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during password hash repair'}), 500
@app.route('/emergency/reset-admin')
def emergency_reset_admin():
"""Emergency endpoint to reset admin password"""
try:
# Find all potential admin users
admin_users = []
admin_users.append(User.query.filter_by(username='LilPizzaRo').first())
admin_users.append(User.query.filter_by(username='admin').first())
# Filter out None values
admin_users = [user for user in admin_users if user is not None]
if not admin_users:
# Create a new admin user if none exists
new_admin = User(username="LilPizzaRo")
new_admin.set_password("SynthoraAdmin2024")
db.session.add(new_admin)
db.session.commit()
return jsonify({
'message': 'New admin user created successfully',
'username': 'LilPizzaRo',
'new_password': 'SynthoraAdmin2024'
})
# Reset passwords for all admin users
reset_users = []
for admin_user in admin_users:
# Force create a new hash instead of using the existing one
admin_user.set_password("SynthoraAdmin2024")
reset_users.append(admin_user.username)
db.session.commit()
return jsonify({
'message': f'Admin password(s) reset successfully for: {", ".join(reset_users)}',
'usernames': reset_users,
'new_password': 'SynthoraAdmin2024'
})
except Exception as e:
db.session.rollback()
print(f"Admin password reset error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during admin password reset'}), 500
@app.route('/emergency/create-new-admin')
def emergency_create_new_admin():
"""Emergency endpoint to create a fresh admin user"""
try:
# Create a new admin user with a unique name
new_admin_username = f"admin_{int(datetime.now().timestamp())}"
new_admin = User(username=new_admin_username)
new_admin.set_password("SynthoraAdmin2024")
db.session.add(new_admin)
db.session.commit()
return jsonify({
'message': 'New admin user created successfully',
'username': new_admin_username,
'password': 'SynthoraAdmin2024',
'note': 'Please update the admin username check in the code to include this new username'
})
except Exception as e:
db.session.rollback()
print(f"Create admin error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during admin creation'}), 500
@app.route('/emergency/reset-db')
def emergency_reset_db():
"""Emergency endpoint to reset database and create fresh admin"""
try:
with app.app_context():
# Drop all tables
db.drop_all()
print("Dropped all tables")
# Create all tables
db.create_all()
print("Created fresh tables")
# Create admin user with User model methods
admin_user = User(username="LilPizzaRo")
admin_user.set_password("SynthoraAdmin2024")
db.session.add(admin_user)
db.session.commit()
print("Created fresh admin user")
return jsonify({
'message': 'Database reset successful',
'admin_username': 'LilPizzaRo',
'admin_password': 'SynthoraAdmin2024'
})
except Exception as e:
print(f"Database reset error: {str(e)}")
traceback.print_exc()
return jsonify({'error': 'An error occurred during database reset'}), 500
@app.route('/emergency/admin-check', methods=['GET'])
def check_admin_account():
"""Check and reset admin account if needed."""
try:
admin = User.query.filter_by(username='LilPizzaRo').first()
if not admin:
# Create admin account if it doesn't exist
admin = User(username='LilPizzaRo')
admin.set_password('SynthoraAdmin2024') # Use consistent password
db.session.add(admin)
db.session.commit()
return jsonify({'message': 'Admin account created successfully'})
# Test if we can verify the password
if admin.check_password('SynthoraAdmin2024'): # Use consistent password
return jsonify({'message': 'Admin account is working correctly'})
else:
# Reset admin password
admin.set_password('SynthoraAdmin2024') # Use consistent password
db.session.commit()
return jsonify({'message': 'Admin password has been reset'})
except Exception as e:
return jsonify({'error': str(e)}), 500
# Add a session timeout setting near the top of the file
# Add this after the app configuration, before the route definitions
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = timedelta(days=7) # Set session to last for 7 days
# Debug session information on each request
if request.endpoint and not request.endpoint.startswith('static'):
if 'username' in session:
print(f"Request to {request.endpoint}: User {session['username']} is in session")
else:
print(f"Request to {request.endpoint}: No user in session")
@app.route('/emergency/debug-admin', methods=['GET'])
def debug_admin_account():
"""Emergency endpoint to debug admin account information"""
try:
# Find admin user
admin = User.query.filter_by(username='LilPizzaRo').first()
if not admin:
return jsonify({
'status': 'error',
'message': 'Admin account does not exist',
'action': 'Please visit /emergency/reset-admin to create an admin account'
})