-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.go
More file actions
391 lines (354 loc) · 11.1 KB
/
auth.go
File metadata and controls
391 lines (354 loc) · 11.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
type AuthState struct {
Configured bool `json:"configured"`
Connected bool `json:"connected"`
ApiURL string `json:"api_url"`
WhitelabelName string `json:"whitelabel_name"`
WhitelabelID int `json:"whitelabel_id"`
LogoURL string `json:"logo_url,omitempty"`
Error string `json:"error,omitempty"`
}
func IsAuthConfigured() bool {
cfg := getConfig()
if cfg.ApiURL == "" {
return false
}
return (cfg.ApiKey != "" && cfg.ApiSecret != "") || cfg.ApiToken != ""
}
func GetAuthState() AuthState {
cfg := getConfig()
return AuthState{
Configured: IsAuthConfigured(),
ApiURL: cfg.ApiURL,
WhitelabelName: cfg.Whitelabel.Name,
WhitelabelID: cfg.Whitelabel.ID,
LogoURL: cfg.Whitelabel.LogoURL,
}
}
// handleAuthState — GET /auth/state
func handleAuthState(w http.ResponseWriter, r *http.Request) {
state := GetAuthState()
if state.Configured {
client := NewApiClient(getConfig())
if err := client.TestConnection(); err != nil {
state.Connected = false
state.Error = err.Error()
} else {
state.Connected = true
}
}
jsonResponse(w, http.StatusOK, state)
}
// handleAuthLogin — POST /auth/login { api_url, api_key, api_secret, wl_id? }
func handleAuthLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonResponse(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
var req struct {
ApiURL string `json:"api_url"`
ApiKey string `json:"api_key"`
ApiSecret string `json:"api_secret"`
WlID int `json:"wl_id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
return
}
if req.ApiURL == "" || req.ApiKey == "" || req.ApiSecret == "" {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "api_url, api_key, api_secret required"})
return
}
// Test connection with temporary config
testCfg := AppConfig{
ApiURL: req.ApiURL,
ApiKey: req.ApiKey,
ApiSecret: req.ApiSecret,
ApiWhiteLabel: req.WlID,
}
client := NewApiClient(testCfg)
if err := client.TestConnection(); err != nil {
log.Printf("[auth] login failed for %s: %v", req.ApiURL, err)
jsonResponse(w, http.StatusUnauthorized, map[string]string{"error": "connection failed: " + err.Error()})
return
}
// Save credentials
configMu.Lock()
appConfig.ApiURL = req.ApiURL
appConfig.ApiKey = req.ApiKey
appConfig.ApiSecret = req.ApiSecret
if req.WlID > 0 {
appConfig.ApiWhiteLabel = req.WlID
}
configMu.Unlock()
// Fetch brand info (name, logo, colors) from backend
brand, err := client.FetchBrandInfo()
if err != nil {
log.Printf("[auth] brand fetch failed (non-fatal): %v", err)
} else if brand != nil {
configMu.Lock()
name := brand.BrandName
if name == "" {
name = brand.WLName
}
logo := brand.BrandLogo
if logo == "" {
logo = brand.WLLogo
}
if name != "" {
appConfig.Whitelabel.Name = name
}
if logo != "" {
appConfig.Whitelabel.LogoURL = logo
}
if brand.PrimaryColor != "" {
appConfig.Whitelabel.PrimaryColor = brand.PrimaryColor
}
if brand.SecondaryColor != "" {
appConfig.Whitelabel.AccentColor = brand.SecondaryColor
}
configMu.Unlock()
log.Printf("[auth] brand detected: %s (logo=%s)", name, logo)
}
if err := saveConfig(); err != nil {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "save failed"})
return
}
log.Printf("[auth] login successful for %s (wl=%d)", req.ApiURL, req.WlID)
jsonResponse(w, http.StatusOK, map[string]string{"status": "connected"})
}
// handleAuthLoginPassword — POST /auth/login-password { api_url, wl_id, email, password }
// Authenticates via backend using email+password, generates API key, and stores credentials.
// Only store owners are allowed (staff users are rejected).
func handleAuthLoginPassword(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonResponse(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
var req struct {
ApiURL string `json:"api_url"`
WlID int `json:"wl_id"`
Email string `json:"email"`
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "invalid JSON"})
return
}
if req.ApiURL == "" || req.Email == "" || req.Password == "" {
jsonResponse(w, http.StatusBadRequest, map[string]string{"error": "api_url, email, password required"})
return
}
// Step 1: Authenticate via backend POST /users/authorize
authResult, err := backendAuthorize(req.ApiURL, req.WlID, req.Email, req.Password)
if err != nil {
log.Printf("[auth] password login failed for %s: %v", req.Email, err)
jsonResponse(w, http.StatusUnauthorized, map[string]string{"error": err.Error()})
return
}
// Step 2: Reject staff users — only store owners allowed
if authResult.IsStaff {
log.Printf("[auth] rejected staff login for %s", req.Email)
jsonResponse(w, http.StatusForbidden, map[string]string{"error": "Solo el dueño de la tienda puede conectar el bridge. Los usuarios staff no tienen permiso."})
return
}
// Step 3: Generate API key using the JWT token
apiKey, apiSecret, err := backendGenerateApiKey(req.ApiURL, req.WlID, authResult.Token)
if err != nil {
log.Printf("[auth] API key generation failed for %s: %v", req.Email, err)
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "Login exitoso pero no se pudo generar API key: " + err.Error()})
return
}
// Step 4: Test connection with the generated credentials
testCfg := AppConfig{
ApiURL: req.ApiURL,
ApiKey: apiKey,
ApiSecret: apiSecret,
ApiWhiteLabel: req.WlID,
}
client := NewApiClient(testCfg)
if err := client.TestConnection(); err != nil {
log.Printf("[auth] connection test failed after key generation: %v", err)
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "API key generada pero falló la conexión: " + err.Error()})
return
}
// Step 5: Save credentials
configMu.Lock()
appConfig.ApiURL = req.ApiURL
appConfig.ApiKey = apiKey
appConfig.ApiSecret = apiSecret
appConfig.ApiToken = ""
if req.WlID > 0 {
appConfig.ApiWhiteLabel = req.WlID
}
configMu.Unlock()
// Fetch brand info
brand, err := client.FetchBrandInfo()
if err != nil {
log.Printf("[auth] brand fetch failed (non-fatal): %v", err)
} else if brand != nil {
configMu.Lock()
name := brand.BrandName
if name == "" {
name = brand.WLName
}
logo := brand.BrandLogo
if logo == "" {
logo = brand.WLLogo
}
if name != "" {
appConfig.Whitelabel.Name = name
}
if logo != "" {
appConfig.Whitelabel.LogoURL = logo
}
if brand.PrimaryColor != "" {
appConfig.Whitelabel.PrimaryColor = brand.PrimaryColor
}
if brand.SecondaryColor != "" {
appConfig.Whitelabel.AccentColor = brand.SecondaryColor
}
configMu.Unlock()
log.Printf("[auth] brand detected: %s", name)
}
if err := saveConfig(); err != nil {
jsonResponse(w, http.StatusInternalServerError, map[string]string{"error": "save failed"})
return
}
log.Printf("[auth] password login successful for %s (wl=%d)", req.Email, req.WlID)
jsonResponse(w, http.StatusOK, map[string]string{"status": "connected"})
}
// authResult holds the result of a backend /users/authorize call.
type authResult struct {
Token string
IsStaff bool
}
// backendAuthorize calls POST /users/authorize on the backend.
func backendAuthorize(apiURL string, wlID int, email, password string) (*authResult, error) {
payload := map[string]any{
"email": email,
"password": password,
"staff": 0,
}
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
client := &http.Client{Timeout: 15 * time.Second}
req, err := http.NewRequest("POST", apiURL+"/users/authorize", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if wlID > 0 {
req.Header.Set("X-Any-Wl", fmt.Sprintf("%d", wlID))
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("no se pudo contactar el servidor: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error leyendo respuesta: %w", err)
}
var result struct {
Status int `json:"status"`
Message string `json:"message"`
Data struct {
Token string `json:"token"`
User struct {
StaffID any `json:"staff_id"` // 0 or null for owners, >0 for staff
} `json:"user"`
} `json:"data"`
}
if err := json.Unmarshal(respBody, &result); err != nil {
return nil, fmt.Errorf("respuesta inválida del servidor")
}
if result.Status != 1 {
msg := result.Message
if msg == "" {
msg = "credenciales inválidas"
}
return nil, fmt.Errorf("%s", msg)
}
if result.Data.Token == "" {
return nil, fmt.Errorf("el servidor no devolvió un token")
}
// Check if staff: staff_id can be float64 (from JSON number) or string
isStaff := false
switch v := result.Data.User.StaffID.(type) {
case float64:
isStaff = v > 0
case string:
isStaff = v != "" && v != "0"
}
return &authResult{
Token: result.Data.Token,
IsStaff: isStaff,
}, nil
}
// backendGenerateApiKey calls POST /users/api/key with a JWT token.
func backendGenerateApiKey(apiURL string, wlID int, jwtToken string) (string, string, error) {
client := &http.Client{Timeout: 15 * time.Second}
req, err := http.NewRequest("POST", apiURL+"/users/api/key", nil)
if err != nil {
return "", "", err
}
req.Header.Set("Authorization", "Bearer "+jwtToken)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if wlID > 0 {
req.Header.Set("X-Any-Wl", fmt.Sprintf("%d", wlID))
}
resp, err := client.Do(req)
if err != nil {
return "", "", fmt.Errorf("no se pudo generar API key: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", fmt.Errorf("error leyendo respuesta: %w", err)
}
var result struct {
Status int `json:"status"`
Data struct {
Key string `json:"key"`
Secret string `json:"secret"`
} `json:"data"`
}
if err := json.Unmarshal(respBody, &result); err != nil {
return "", "", fmt.Errorf("respuesta inválida")
}
if result.Status != 1 || result.Data.Key == "" {
return "", "", fmt.Errorf("el servidor no generó API key")
}
return result.Data.Key, result.Data.Secret, nil
}
// handleAuthLogout — POST /auth/logout
func handleAuthLogout(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
jsonResponse(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
return
}
configMu.Lock()
appConfig.ApiURL = ""
appConfig.ApiKey = ""
appConfig.ApiSecret = ""
appConfig.ApiToken = ""
appConfig.Whitelabel = WhitelabelConfig{}
configMu.Unlock()
saveConfig()
log.Printf("[auth] logged out")
jsonResponse(w, http.StatusOK, map[string]string{"status": "logged_out"})
}