-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapi_admin.go
More file actions
281 lines (236 loc) · 6.66 KB
/
api_admin.go
File metadata and controls
281 lines (236 loc) · 6.66 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
/*
* Copyright (C) 2025 Micr0Byte <micr0@micr0.dev>
* Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (AGPLv3)
*/
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
)
// RunAdminCommand handles admin CLI commands
func RunAdminCommand(args []string) {
if len(args) < 1 {
printAdminHelp()
return
}
// Initialize API key store (needed for all commands)
if err := InitAPIKeyStore("api_keys.json"); err != nil {
fmt.Printf("Error initializing API key store: %v\n", err)
os.Exit(1)
}
command := args[0]
switch command {
case "create-key":
handleCreateKey(args[1:])
case "list-keys":
handleListKeys()
case "revoke-key":
handleRevokeKey(args[1:])
case "extend-key":
handleExtendKey(args[1:])
case "lookup":
handleLookup(args[1:])
case "cleanup":
handleCleanup()
default:
fmt.Printf("Unknown command: %s\n", command)
printAdminHelp()
}
}
func printAdminHelp() {
fmt.Println(`Altbot Admin Commands:
create-key --email <email> [--days <days>] [--note <note>]
Create a new API key for a user
Default: 30 days
list-keys
List all API keys
revoke-key <key>
Revoke/deactivate an API key
extend-key <key> --days <days>
Extend an API key's expiration
lookup --email <email>
Find API key by email
cleanup
Remove keys expired more than 30 days ago
Examples:
./altbot admin create-key --email lily@example.com --days 30 --note "Ko-fi purchase"
./altbot admin list-keys
./altbot admin revoke-key altbot_abc123...
./altbot admin extend-key altbot_abc123... --days 30
./altbot admin lookup --email lily@example.com`)
}
func handleCreateKey(args []string) {
var email string
days := 30
note := "Manual creation"
for i := 0; i < len(args); i++ {
switch args[i] {
case "--email", "-e":
if i+1 < len(args) {
email = args[i+1]
i++
}
case "--days", "-d":
if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &days)
i++
}
case "--note", "-n":
if i+1 < len(args) {
note = args[i+1]
i++
}
}
}
if email == "" {
fmt.Println("Error: --email is required")
return
}
// Check if email already has a key
existing := FindAPIKeyByEmail(email)
if existing != nil && existing.Active && time.Now().Before(existing.ExpiresAt) {
fmt.Printf("Warning: User %s already has an active key (expires %s)\n",
email, existing.ExpiresAt.Format("2006-01-02"))
fmt.Println("Use 'extend-key' to extend it, or 'revoke-key' first to create a new one.")
return
}
apiKey, err := GenerateAPIKey(email, days, note)
if err != nil {
fmt.Printf("Error creating key: %v\n", err)
return
}
fmt.Printf("\n%s=== API Key Created ===%s\n", Green, Reset)
fmt.Printf("Email: %s\n", apiKey.Email)
fmt.Printf("Key: %s\n", apiKey.Key)
fmt.Printf("Expires: %s (%d days)\n", apiKey.ExpiresAt.Format("2006-01-02"), days)
fmt.Printf("Note: %s\n", note)
fmt.Printf("%s========================%s\n\n", Green, Reset)
fmt.Println("Send this key to the user!")
}
func handleListKeys() {
keys := ListAPIKeys()
if len(keys) == 0 {
fmt.Println("No API keys found.")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "EMAIL\tSTATUS\tUSAGE\tEXPIRES\tKEY (prefix)")
fmt.Fprintln(w, "-----\t------\t-----\t-------\t-----------")
for _, key := range keys {
status := "active"
if !key.Active {
status = "revoked"
} else if time.Now().After(key.ExpiresAt) {
status = "expired"
}
keyPrefix := key.Key
if len(keyPrefix) > 20 {
keyPrefix = keyPrefix[:20] + "..."
}
fmt.Fprintf(w, "%s\t%s\t%d\t%s\t%s\n",
key.Email,
status,
key.UsageMonth,
key.ExpiresAt.Format("2006-01-02"),
keyPrefix,
)
}
w.Flush()
fmt.Printf("\nTotal: %d keys\n", len(keys))
}
func handleRevokeKey(args []string) {
if len(args) < 1 {
fmt.Println("Error: API key required")
fmt.Println("Usage: revoke-key <key>")
return
}
key := args[0]
if err := RevokeAPIKey(key); err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("API key revoked successfully.\n")
}
func handleExtendKey(args []string) {
if len(args) < 1 {
fmt.Println("Error: API key required")
fmt.Println("Usage: extend-key <key> --days <days>")
return
}
key := args[0]
days := 30
for i := 1; i < len(args); i++ {
if args[i] == "--days" || args[i] == "-d" {
if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &days)
}
}
}
if err := ExtendAPIKey(key, days); err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Get updated info
_, daysRemaining, expiresAt, _ := GetAPIKeyUsage(key)
fmt.Printf("API key extended by %d days.\n", days)
fmt.Printf("New expiration: %s (%d days remaining)\n", expiresAt.Format("2006-01-02"), daysRemaining)
}
func handleLookup(args []string) {
var email string
for i := 0; i < len(args); i++ {
if args[i] == "--email" || args[i] == "-e" {
if i+1 < len(args) {
email = args[i+1]
}
}
}
if email == "" {
fmt.Println("Error: --email is required")
return
}
key := FindAPIKeyByEmail(email)
if key == nil {
fmt.Printf("No API key found for %s\n", email)
return
}
status := "active"
if !key.Active {
status = "revoked"
} else if time.Now().After(key.ExpiresAt) {
status = "expired"
}
fmt.Printf("\n%s=== API Key Details ===%s\n", Cyan, Reset)
fmt.Printf("Email: %s\n", key.Email)
fmt.Printf("Key: %s\n", key.Key)
fmt.Printf("Status: %s\n", status)
fmt.Printf("Created: %s\n", key.CreatedAt.Format("2006-01-02 15:04"))
fmt.Printf("Expires: %s\n", key.ExpiresAt.Format("2006-01-02 15:04"))
fmt.Printf("Usage: %d this month\n", key.UsageMonth)
if key.Note != "" {
fmt.Printf("Note: %s\n", key.Note)
}
fmt.Printf("%s========================%s\n", Cyan, Reset)
}
func handleCleanup() {
removed := CleanupExpiredKeys()
fmt.Printf("Cleaned up %d expired keys.\n", removed)
}
// FormatKeyForEmail formats an API key message for copy-pasting into an email
func FormatKeyForEmail(apiKey *APIKey) string {
var sb strings.Builder
sb.WriteString("Hi!\n\n")
sb.WriteString("Thank you for supporting Altbot! Here's your API key:\n\n")
sb.WriteString(fmt.Sprintf("API Key: %s\n\n", apiKey.Key))
sb.WriteString(fmt.Sprintf("This key is valid until: %s\n\n", apiKey.ExpiresAt.Format("January 2, 2006")))
sb.WriteString("Quick start:\n")
sb.WriteString("curl -X POST https://your-server/api/v1/alt-text \\\n")
sb.WriteString(fmt.Sprintf(" -H \"Authorization: Bearer %s\" \\\n", apiKey.Key))
sb.WriteString(" -F \"image=@your-image.jpg\"\n\n")
sb.WriteString("Documentation: https://github.com/micr0-dev/Altbot/blob/main/API.md\n\n")
sb.WriteString("If you have any questions, feel free to reach out!\n\n")
sb.WriteString("- Altbot\n")
return sb.String()
}