-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapi_keys.go
More file actions
288 lines (232 loc) · 6.58 KB
/
api_keys.go
File metadata and controls
288 lines (232 loc) · 6.58 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
/*
* Copyright (C) 2025 Micr0Byte <micr0@micr0.dev>
* Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 (AGPLv3)
*/
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"sync"
"time"
)
// APIKey represents a single API key and its metadata
type APIKey struct {
Key string `json:"key"`
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
UsageMonth int `json:"usage_month"`
LastReset time.Time `json:"last_reset"`
Active bool `json:"active"`
Note string `json:"note,omitempty"`
}
// APIKeyStore manages all API keys
type APIKeyStore struct {
Keys map[string]*APIKey `json:"keys"`
mu sync.RWMutex
filePath string
}
// Global API key store
var apiKeyStore *APIKeyStore
// InitAPIKeyStore initializes the API key store
func InitAPIKeyStore(filePath string) error {
apiKeyStore = &APIKeyStore{
Keys: make(map[string]*APIKey),
filePath: filePath,
}
if err := apiKeyStore.LoadFromFile(); err != nil {
if os.IsNotExist(err) {
fmt.Println("No API keys file found. Starting fresh.")
return apiKeyStore.SaveToFile()
}
return err
}
fmt.Printf("Loaded %d API keys\n", len(apiKeyStore.Keys))
return nil
}
// LoadFromFile loads API keys from the JSON file
func (store *APIKeyStore) LoadFromFile() error {
store.mu.Lock()
defer store.mu.Unlock()
data, err := os.ReadFile(store.filePath)
if err != nil {
return err
}
return json.Unmarshal(data, &store.Keys)
}
// SaveToFile saves API keys to the JSON file (acquires lock)
func (store *APIKeyStore) SaveToFile() error {
store.mu.Lock()
defer store.mu.Unlock()
return store.saveToFileUnlocked()
}
// saveToFileUnlocked saves without acquiring lock (caller must hold lock)
func (store *APIKeyStore) saveToFileUnlocked() error {
data, err := json.MarshalIndent(store.Keys, "", " ")
if err != nil {
return err
}
return os.WriteFile(store.filePath, data, 0644)
}
// GenerateAPIKey creates a new API key for a user
func GenerateAPIKey(email string, durationDays int, note string) (*APIKey, error) {
keyBytes := make([]byte, 32)
if _, err := rand.Read(keyBytes); err != nil {
return nil, fmt.Errorf("failed to generate random key: %v", err)
}
keyString := "altbot_" + hex.EncodeToString(keyBytes)
now := time.Now()
apiKey := &APIKey{
Key: keyString,
Email: email,
CreatedAt: now,
ExpiresAt: now.AddDate(0, 0, durationDays),
UsageMonth: 0,
LastReset: now,
Active: true,
Note: note,
}
apiKeyStore.mu.Lock()
apiKeyStore.Keys[keyString] = apiKey
err := apiKeyStore.saveToFileUnlocked()
apiKeyStore.mu.Unlock()
if err != nil {
return nil, fmt.Errorf("failed to save API key: %v", err)
}
return apiKey, nil
}
// ValidateAPIKey checks if an API key is valid and not expired
func ValidateAPIKey(key string) (*APIKey, error) {
apiKeyStore.mu.RLock()
apiKey, exists := apiKeyStore.Keys[key]
apiKeyStore.mu.RUnlock()
// If key not found in memory, try reloading from file
if !exists {
if err := apiKeyStore.LoadFromFile(); err == nil {
apiKeyStore.mu.RLock()
apiKey, exists = apiKeyStore.Keys[key]
apiKeyStore.mu.RUnlock()
}
}
if !exists {
return nil, fmt.Errorf("invalid API key")
}
if !apiKey.Active {
return nil, fmt.Errorf("API key is deactivated")
}
if time.Now().After(apiKey.ExpiresAt) {
return nil, fmt.Errorf("API key has expired")
}
return apiKey, nil
}
// CheckAndIncrementUsage checks if user is within limits and increments usage
func CheckAndIncrementUsage(key string, monthlyLimit int) error {
apiKeyStore.mu.Lock()
defer apiKeyStore.mu.Unlock()
apiKey, exists := apiKeyStore.Keys[key]
if !exists {
return fmt.Errorf("invalid API key")
}
// Reset monthly counter if we're in a new month
now := time.Now()
if now.Month() != apiKey.LastReset.Month() || now.Year() != apiKey.LastReset.Year() {
apiKey.UsageMonth = 0
apiKey.LastReset = now
}
if apiKey.UsageMonth >= monthlyLimit {
return fmt.Errorf("monthly usage limit exceeded (%d/%d)", apiKey.UsageMonth, monthlyLimit)
}
apiKey.UsageMonth++
// Save periodically (every 10 requests)
if apiKey.UsageMonth%10 == 0 {
go func() {
apiKeyStore.mu.Lock()
apiKeyStore.saveToFileUnlocked()
apiKeyStore.mu.Unlock()
}()
}
return nil
}
// GetAPIKeyUsage returns usage info for an API key
func GetAPIKeyUsage(key string) (int, int, time.Time, error) {
apiKeyStore.mu.RLock()
defer apiKeyStore.mu.RUnlock()
apiKey, exists := apiKeyStore.Keys[key]
if !exists {
return 0, 0, time.Time{}, fmt.Errorf("invalid API key")
}
daysRemaining := int(time.Until(apiKey.ExpiresAt).Hours() / 24)
if daysRemaining < 0 {
daysRemaining = 0
}
return apiKey.UsageMonth, daysRemaining, apiKey.ExpiresAt, nil
}
// RevokeAPIKey deactivates an API key
func RevokeAPIKey(key string) error {
apiKeyStore.mu.Lock()
defer apiKeyStore.mu.Unlock()
apiKey, exists := apiKeyStore.Keys[key]
if !exists {
return fmt.Errorf("API key not found")
}
apiKey.Active = false
return apiKeyStore.saveToFileUnlocked()
}
// ExtendAPIKey extends the expiration of an existing key
func ExtendAPIKey(key string, additionalDays int) error {
apiKeyStore.mu.Lock()
defer apiKeyStore.mu.Unlock()
apiKey, exists := apiKeyStore.Keys[key]
if !exists {
return fmt.Errorf("API key not found")
}
// If expired, extend from now; otherwise extend from current expiry
if time.Now().After(apiKey.ExpiresAt) {
apiKey.ExpiresAt = time.Now().AddDate(0, 0, additionalDays)
} else {
apiKey.ExpiresAt = apiKey.ExpiresAt.AddDate(0, 0, additionalDays)
}
apiKey.Active = true
return apiKeyStore.saveToFileUnlocked() // Use unlocked version!
}
// ListAPIKeys returns all API keys (for admin purposes)
func ListAPIKeys() []*APIKey {
apiKeyStore.mu.RLock()
defer apiKeyStore.mu.RUnlock()
keys := make([]*APIKey, 0, len(apiKeyStore.Keys))
for _, key := range apiKeyStore.Keys {
keys = append(keys, key)
}
return keys
}
// FindAPIKeyByEmail finds an API key by email
func FindAPIKeyByEmail(email string) *APIKey {
apiKeyStore.mu.RLock()
defer apiKeyStore.mu.RUnlock()
for _, key := range apiKeyStore.Keys {
if key.Email == email {
return key
}
}
return nil
}
// CleanupExpiredKeys removes keys that have been expired for more than 30 days
func CleanupExpiredKeys() int {
apiKeyStore.mu.Lock()
defer apiKeyStore.mu.Unlock()
cutoff := time.Now().AddDate(0, 0, -30)
removed := 0
for key, apiKey := range apiKeyStore.Keys {
if apiKey.ExpiresAt.Before(cutoff) {
delete(apiKeyStore.Keys, key)
removed++
}
}
if removed > 0 {
apiKeyStore.saveToFileUnlocked()
}
return removed
}