File size: 4,250 Bytes
b338c26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
FleetMind API Key Generator

Generate API keys for users to authenticate with FleetMind MCP Server.

Usage:
    python generate_api_key.py
    python generate_api_key.py --email user@example.com --name "John Doe"
    python generate_api_key.py --list
    python generate_api_key.py --revoke user@example.com
"""

import argparse
import sys
from database.api_keys import generate_api_key, list_api_keys, revoke_api_key

def main():
    parser = argparse.ArgumentParser(
        description='FleetMind API Key Management',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Interactive mode (prompts for email and name)
  python generate_api_key.py

  # Generate key with arguments
  python generate_api_key.py --email alice@company.com --name "Alice Smith"

  # List all API keys
  python generate_api_key.py --list

  # Revoke a key
  python generate_api_key.py --revoke alice@company.com
        """
    )

    parser.add_argument('--email', help='User email address')
    parser.add_argument('--name', help='User display name')
    parser.add_argument('--list', action='store_true', help='List all API keys')
    parser.add_argument('--revoke', metavar='EMAIL', help='Revoke API key for email')

    args = parser.parse_args()

    # List API keys
    if args.list:
        print("\n" + "="*80)
        print("FLEETMIND API KEYS")
        print("="*80 + "\n")

        keys = list_api_keys()
        if not keys:
            print("No API keys found.\n")
            return

        for key in keys:
            status = "βœ… Active" if key['is_active'] else "❌ Revoked"
            print(f"Email:      {key['email']}")
            print(f"Name:       {key['name']}")
            print(f"User ID:    {key['user_id']}")
            print(f"Key:        {key['key_preview']}")
            print(f"Created:    {key['created_at']}")
            print(f"Last Used:  {key['last_used_at'] or 'Never'}")
            print(f"Status:     {status}")
            print("-" * 80)

        print()
        return

    # Revoke API key
    if args.revoke:
        print(f"\n⚠️  Revoking API key for {args.revoke}...")
        result = revoke_api_key(args.revoke)

        if result['success']:
            print(f"βœ… {result['message']}\n")
        else:
            print(f"❌ Error: {result['error']}\n")
            sys.exit(1)
        return

    # Generate new API key
    if not args.email:
        # Interactive mode
        print("\n" + "="*80)
        print("GENERATE NEW FLEETMIND API KEY")
        print("="*80 + "\n")

        email = input("Enter user email: ").strip()
        if not email:
            print("❌ Email is required")
            sys.exit(1)

        name = input("Enter user name (optional): ").strip() or None
    else:
        email = args.email
        name = args.name

    print(f"\nGenerating API key for {email}...")
    result = generate_api_key(email, name)

    if not result['success']:
        print(f"\n❌ Error: {result['error']}\n")
        sys.exit(1)

    # Success! Display the API key
    print("\n" + "="*80)
    print("βœ… API KEY GENERATED SUCCESSFULLY")
    print("="*80 + "\n")

    print(f"Email:      {result['email']}")
    print(f"Name:       {result['name']}")
    print(f"User ID:    {result['user_id']}")
    print(f"Created:    {result['created_at']}")
    print()
    print("πŸ”‘ API KEY (copy this now - it won't be shown again!):")
    print("-" * 80)
    print(result['api_key'])
    print("-" * 80)
    print()
    print("πŸ“‹ Add this to your Claude Desktop config:")
    print()
    print('  {')
    print('    "mcpServers": {')
    print('      "fleetmind": {')
    print('        "command": "npx",')
    print('        "args": [')
    print('          "mcp-remote",')
    print('          "https://mcp-1st-birthday-fleetmind-dispatch-ai.hf.space/sse"')
    print('        ],')
    print('        "env": {')
    print(f'          "FLEETMIND_API_KEY": "{result["api_key"]}"')
    print('        }')
    print('      }')
    print('    }')
    print('  }')
    print()
    print("⚠️  IMPORTANT: Save this API key securely. You won't be able to see it again!")
    print()

if __name__ == "__main__":
    main()