File size: 2,095 Bytes
da8ce94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""
Initialize JSON database with admin user
"""

import json
import os
import sys
import bcrypt
from datetime import datetime

# Add project to path
sys.path.insert(0, os.path.dirname(__file__))

# Database paths
DB_DIR = os.path.join(os.path.dirname(__file__), 'data')
USERS_FILE = os.path.join(DB_DIR, 'users.json')
BLACKLISTED_TOKENS_FILE = os.path.join(DB_DIR, 'blacklisted_tokens.json')
REFRESH_TOKENS_FILE = os.path.join(DB_DIR, 'refresh_tokens.json')

def create_initial_data():
    """Create initial data with admin user"""
    
    # Ensure directory exists
    os.makedirs(DB_DIR, exist_ok=True)
    print(f"? Directory created/verified: {DB_DIR}")
    
    # Create users.json with admin user
    admin_password = "admin@123"
    password_hash = bcrypt.hashpw(admin_password.encode('utf-8'), bcrypt.gensalt())
    
    users_data = {
        "admin": {
            "id": 1,
            "username": "admin",
            "password_hash": password_hash.decode('utf-8'),
            "role": "admin",
            "created_at": datetime.now().isoformat()
        }
    }
    
    with open(USERS_FILE, 'w', encoding='utf-8') as f:
        json.dump(users_data, f, indent=2, ensure_ascii=False)
    print(f"? Created {USERS_FILE}")
    print(f"  - Username: admin")
    print(f"  - Password: admin@123 (hashed)")
    print(f"  - Role: admin")
    
    # Create empty blacklisted_tokens.json
    with open(BLACKLISTED_TOKENS_FILE, 'w', encoding='utf-8') as f:
        json.dump({}, f, indent=2)
    print(f"? Created {BLACKLISTED_TOKENS_FILE}")
    
    # Create empty refresh_tokens.json
    with open(REFRESH_TOKENS_FILE, 'w', encoding='utf-8') as f:
        json.dump({}, f, indent=2)
    print(f"? Created {REFRESH_TOKENS_FILE}")
    
    print("\n? Database initialization complete!")
    print("\nYou can now use:")
    print("  POST /auth/login")
    print("  {\"username\": \"admin\", \"password\": \"admin@123\"}")

if __name__ == '__main__':
    try:
        create_initial_data()
    except Exception as e:
        print(f"? Error: {e}")
        sys.exit(1)