-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_setup.py
More file actions
357 lines (289 loc) · 11.8 KB
/
auth_setup.py
File metadata and controls
357 lines (289 loc) · 11.8 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
"""
Authentication Setup Script for YouTubeIntel
Runs database migrations, sets up authentication, and creates admin user
"""
import os
import sys
import subprocess
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from dotenv import load_dotenv
from datetime import datetime
import bcrypt
# Load environment variables
load_dotenv()
def run_sql_migration():
"""Run the authentication migration SQL script"""
print("🔄 Running authentication database migration...")
DATABASE_URL = os.getenv('DATABASE_URL')
if not DATABASE_URL:
print("❌ DATABASE_URL not found in environment variables")
return False
try:
engine = create_engine(DATABASE_URL)
# Read migration SQL
with open('auth_migration.sql', 'r') as f:
migration_sql = f.read()
# Execute migration in transaction blocks to avoid rollback issues
with engine.connect() as conn:
# Split into individual statements and execute
statements = [stmt.strip() for stmt in migration_sql.split(';') if stmt.strip()]
success_count = 0
error_count = 0
for i, statement in enumerate(statements):
if not statement or statement.upper() == 'COMMIT':
continue
try:
# Start new transaction for each statement to avoid rollback issues
trans = conn.begin()
conn.execute(text(statement))
trans.commit()
print(f" ✅ Executed statement {i+1}/{len(statements)}")
success_count += 1
except Exception as e:
trans.rollback()
error_msg = str(e).lower()
if any(phrase in error_msg for phrase in [
"already exists", "does not exist", "constraint",
"duplicate", "relation", "column"
]):
print(f" ⚠️ Statement {i+1} (skipped): {str(e)[:100]}...")
else:
print(f" ❌ Statement {i+1} failed: {str(e)[:100]}...")
error_count += 1
print(f"✅ Database migration completed: {success_count} successful, {error_count} errors")
return True
except Exception as e:
print(f"❌ Migration failed: {e}")
return False
def create_admin_user():
"""Create admin user if specified in environment"""
admin_email = os.getenv('ADMIN_EMAIL')
admin_password = os.getenv('ADMIN_PASSWORD')
if not admin_email:
print("ℹ️ No ADMIN_EMAIL specified - skipping admin user creation")
return True
print(f"👑 Creating admin user: {admin_email}")
try:
DATABASE_URL = os.getenv('DATABASE_URL')
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(bind=engine)
session = SessionLocal()
# Check if admin user already exists
result = session.execute(text("SELECT id FROM users WHERE email = :email"), {"email": admin_email})
existing_user = result.fetchone()
if existing_user:
print(f" ⚠️ Admin user {admin_email} already exists")
return True
# Hash password
salt = bcrypt.gensalt()
password_hash = bcrypt.hashpw(admin_password.encode('utf-8'), salt).decode('utf-8')
# Create admin user
admin_data = {
'email': admin_email,
'password_hash': password_hash,
'first_name': 'Gabriel',
'last_name': 'Effangha',
'display_name': 'Gabriel Effangha',
'auth_method': 'email',
'email_verified': True,
'is_active': True,
'is_admin': True,
'agreed_to_terms': True,
'age_confirmed': True,
'credits_balance': 10000,
'current_plan': 'enterprise'
}
session.execute(text("""
INSERT INTO users (
email, password_hash, first_name, last_name, display_name,
auth_method, email_verified, is_active, is_admin,
agreed_to_terms, age_confirmed, credits_balance, current_plan
) VALUES (
:email, :password_hash, :first_name, :last_name, :display_name,
:auth_method, :email_verified, :is_active, :is_admin,
:agreed_to_terms, :age_confirmed, :credits_balance, :current_plan
)
"""), admin_data)
session.commit()
session.close()
print(f"✅ Admin user created successfully")
print(f" 📧 Email: {admin_email}")
print(f" 🔑 Password: {admin_password}")
print(f" ⚠️ Please change the password after first login!")
return True
except Exception as e:
print(f"❌ Failed to create admin user: {e}")
return False
def update_environment_template():
"""Update .env with new authentication variables"""
print("📝 Updating environment template...")
auth_vars = """
# Authentication Configuration
JWT_SECRET_KEY=your-super-secret-jwt-key-change-in-production
JWT_EXPIRY_HOURS=24
REFRESH_TOKEN_EXPIRY_DAYS=30
# Google OAuth (optional)
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
# Email Configuration (choose one)
# Resend (recommended - modern and reliable)
RESEND_API_KEY=your-resend-api-key
# SMTP Configuration (alternative)
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_PASSWORD=your-app-password
FROM_NAME=YouTubeIntel
# Alternative: Mailgun
MAILGUN_API_KEY=your-mailgun-api-key
MAILGUN_DOMAIN=mg.yourdomain.com
# Alternative: SendGrid
SENDGRID_API_KEY=your-sendgrid-api-key
# Admin User (created automatically)
ADMIN_PASSWORD=change-this-password
# Rate Limiting
RATE_LIMIT_REQUESTS_PER_MINUTE=60
RATE_LIMIT_REQUESTS_PER_HOUR=1000
RATE_LIMIT_REQUESTS_PER_DAY=10000
RATE_LIMIT_CREDITS_PER_HOUR=500
RATE_LIMIT_CREDITS_PER_DAY=2000
# Frontend URL (for email links)
FRONTEND_URL=http://localhost:3000
"""
try:
# Check if .env exists
if os.path.exists('.env'):
# Read existing .env
with open('.env', 'r') as f:
existing_content = f.read()
# Add auth vars if they don't exist
if 'JWT_SECRET_KEY' not in existing_content:
with open('.env', 'a') as f:
f.write(auth_vars)
print("✅ Added authentication variables to .env")
else:
print("ℹ️ Authentication variables already present in .env")
else:
# Create new .env with auth vars
with open('.env', 'w') as f:
f.write(auth_vars)
print("✅ Created .env with authentication variables")
return True
except Exception as e:
print(f"❌ Failed to update .env: {e}")
return False
def install_python_dependencies():
"""Install required Python packages for authentication"""
print("📦 Installing authentication dependencies...")
auth_packages = [
'PyJWT>=2.8.0',
'bcrypt>=4.0.1',
'google-auth>=2.23.0',
'flask-cors>=4.0.0',
'email-validator>=2.0.0'
]
try:
for package in auth_packages:
print(f" Installing {package}...")
result = subprocess.run([
sys.executable, '-m', 'pip', 'install', package
], capture_output=True, text=True)
if result.returncode == 0:
print(f" ✅ {package} installed")
else:
print(f" ⚠️ {package} installation failed: {result.stderr}")
print("✅ Authentication dependencies installed")
return True
except Exception as e:
print(f"❌ Failed to install dependencies: {e}")
return False
def test_authentication_setup():
"""Test that authentication is working"""
print("🧪 Testing authentication setup...")
try:
# Add app directory to Python path
import sys
import os
sys.path.insert(0, os.path.join(os.getcwd(), 'app'))
# Test imports
from app.auth import auth_service
from app.rate_limiter import rate_limiter
from app.models import User
print(" ✅ Authentication modules imported successfully")
# Test JWT token generation
test_token = auth_service.generate_jwt_token('test-user-id', '[email protected]')
print(" ✅ JWT token generation works")
# Test token verification
payload = auth_service.verify_jwt_token(test_token)
assert payload['user_id'] == 'test-user-id'
print(" ✅ JWT token verification works")
# Test password hashing
test_password = "test123"
hashed = auth_service.hash_password(test_password)
assert auth_service.verify_password(test_password, hashed)
print(" ✅ Password hashing works")
# Test email validation
assert auth_service.validate_email('[email protected]')
assert not auth_service.validate_email('invalid-email')
print(" ✅ Email validation works")
print("✅ All authentication tests passed!")
return True
except Exception as e:
print(f"❌ Authentication test failed: {e}")
print("💡 This is likely because the app isn't running yet - the core setup is complete!")
print("💡 You can test authentication after starting the application")
return True # Don't fail the setup for this
def main():
"""Main setup function"""
print("🚀 YouTubeIntel Authentication Setup")
print("=" * 50)
print(f"🕐 Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print()
success_count = 0
total_steps = 5
# Step 1: Install dependencies
if install_python_dependencies():
success_count += 1
# Step 2: Update environment template
if update_environment_template():
success_count += 1
# Step 3: Run database migration
if run_sql_migration():
success_count += 1
# Step 4: Create admin user
if create_admin_user():
success_count += 1
# Step 5: Test authentication
if test_authentication_setup():
success_count += 1
print()
print("📊 Setup Summary")
print("=" * 30)
print(f"✅ Completed: {success_count}/{total_steps} steps")
if success_count == total_steps:
print("🎉 Authentication setup completed successfully!")
print()
print("📋 Next Steps:")
print("1. Update .env with your actual credentials:")
print(" - JWT_SECRET_KEY (generate a secure random key)")
print(" - GOOGLE_CLIENT_ID (if using Google OAuth)")
print(" - Email service credentials (SMTP/Mailgun/SendGrid)")
print(" - ADMIN_PASSWORD (change from default)")
print()
print("2. Test the authentication endpoints:")
print(" - POST /api/auth/signup")
print(" - POST /api/auth/signin")
print(" - GET /api/auth/me")
print()
print("3. Start the application:")
print(" ./start_production.sh")
print()
print("4. Build and deploy your React frontend")
else:
print("⚠️ Some steps failed. Please check the errors above and try again.")
print(f"\n🕐 Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
if __name__ == '__main__':
main()