Repository navigation
Expand file tree
/
Copy pathapp.go
More file actions
476 lines (401 loc) · 14.7 KB
/
Copy pathapp.go
File metadata and controls
476 lines (401 loc) · 14.7 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"strings"
"Modsec/clientside/auth"
"Modsec/clientside/service"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/crypto/pbkdf2"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
log.Println("ModSec application starting...")
}
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) SimplePOC(title string) {
runtime.WindowSetTitle(a.ctx, title)
}
func (a *App) EmailToSHA256(email string) string {
// This function turn email SInto sha256
// which will be used as salt in the future. or maybe store in database
// Input as string
// Output as string
// Trimming spaces and converting to lowercase
// This maybe removed in the future if raise concern!
normalizedEmail := strings.TrimSpace(strings.ToLower(email))
// Hash the email using SHA-256
emailHash := sha256.Sum256([]byte(normalizedEmail))
// Convert the hash to a hexadecimal string
// If you need to use it as salt you may convert into byte
return hex.EncodeToString(emailHash[:])
}
func (a *App) PBKDF2Function(password, salt string, iterations, keyLength int) string {
// This function is for PBKDF2
// It use SHA256 to do the hashing and the number of iteration can be control
// Input: Password(Str) Salt(Str) Iteration(int) Keylength(int) <== Depend on algorithm we use
// Output as string
// Convert password and salt to byte slices
passwordBytes := []byte(password)
saltBytes := []byte(salt)
// Doing funky hashing by using sha256(For now)
key := pbkdf2.Key(passwordBytes, saltBytes, iterations, keyLength, sha256.New)
// Return the key as a hexadecimal string
return hex.EncodeToString(key)
}
func GenerateRandomBytes(size int) ([]byte, error) {
// Create a byte slice to hold the random bytes
// This function will be used with generateIV and generateSessionkey
// Input: size(int)
// Output: byte and error
randomBytes := make([]byte, size)
// Fill in the randombyte with secure random byte
_, err := rand.Read(randomBytes) //secure Crypto lib btw
if err != nil {
return nil, fmt.Errorf("failed to generate random bytes: %w", err)
}
return randomBytes, nil
}
func (a *App) GenerateIV() ([]byte, error) {
// Request: GenerateRandomBytes function
//
return GenerateRandomBytes(12) // Returns raw 12 byte (96)
}
func (a *App) GenerateSessionKey() ([]byte, error) {
// Request: GenerateRandomBytes function
//
return GenerateRandomBytes(32) // Returns raw 32 byte (256)
}
func (a *App) EncryptAES256GCM(plaintext []byte, key []byte, IV []byte) ([]byte, error) {
// AES256 using GCM mode
// Input: plaintext(byte) Key 32(byte) IV 12(byte)
// Output: byte, Error
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("failed to create AES cipher: %w", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to create GCM mode: %w", err)
}
// Encrypt the plaintext using the provided IV
ciphertext := gcm.Seal(nil, IV, plaintext, nil)
// Return only the ciphertext (nonce should be stored separately)
return ciphertext, nil
}
// decryptAES256GCM decrypts ciphertext using AES-256 GCM with a provided nonce.
func (a *App) DecryptAES256GCM(ciphertext []byte, key []byte, IV []byte) ([]byte, error) {
// AES256 using GCM mode
// Input: ciphertext(byte) Key 32(byte) IV 12(byte)
// Output: byte, Error
block, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("failed to create AES cipher: %w", err)
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to create GCM mode: %w", err)
}
// Decrypt the ciphertext using the provided nonce
plaintext, err := aesGCM.Open(nil, IV, ciphertext, nil)
if err != nil {
return nil, fmt.Errorf("failed to decrypt ciphertext: %w", err)
}
return plaintext, nil
}
// RegisterUser handles the user registration process
func (a *App) RegisterUser(email string, password string) (string, error) {
// Validate input
if !auth.ValidateEmailFormat(email) {
return "", fmt.Errorf("invalid email format")
}
valid, msg := auth.ValidatePasswordStrength(password)
if !valid {
return "", fmt.Errorf(msg)
}
// Call the modularized registration function that now returns seedPhrase
return auth.RegisterUser(email, password)
}
// LoginUser is a wails-exported function to handle login
func (a *App) LoginUser(email, password string) map[string]interface{} {
// Add more detailed logging at entry point
log.Printf("LoginUser called for email: %s", email)
// Call the auth.LoginUser function
response, err := auth.LoginUser(email, password)
result := map[string]interface{}{
"success": false,
"message": "",
}
if err != nil {
errMsg := err.Error()
log.Printf("LoginUser error: %v", errMsg)
// Check for "login communication failed" first since it may contain other error messages
if strings.Contains(errMsg, "login communication failed") {
// Look for "Password not match" within the communication failed message
if strings.Contains(errMsg, "Password not match") {
result["message"] = "Incorrect password"
return result
}
// Check for this specific error message from your backend
if strings.Contains(errMsg, "An error occurred during login") {
// When this error happens during login attempt with valid email,
// it's typically a password issue
result["message"] = "Incorrect password"
return result
}
// Extract more specific error message if available
parts := strings.Split(errMsg, "login communication failed: ")
if len(parts) > 1 && parts[1] != "" {
// Look for common error patterns within the extracted message
extractedMsg := parts[1]
if strings.Contains(extractedMsg, "Password not match") ||
strings.Contains(extractedMsg, "invalid password") {
result["message"] = "Incorrect password"
} else if strings.Contains(extractedMsg, "invalid email") ||
strings.Contains(extractedMsg, "email not found") {
result["message"] = "Email not found"
} else {
// Use the extracted message directly if it doesn't match known patterns
result["message"] = extractedMsg
}
return result
} else {
result["message"] = "Connection error"
return result
}
}
// Check for other specific error patterns
if strings.Contains(errMsg, "Password not match") {
result["message"] = "Incorrect password"
} else if strings.Contains(errMsg, "failed to decrypt vault key") {
result["message"] = "Incorrect password"
} else if strings.Contains(errMsg, "invalid password") {
result["message"] = "Incorrect password"
} else if strings.Contains(errMsg, "An error occurred during login") {
// Added this specific check for your error message
result["message"] = "Incorrect password"
} else if strings.Contains(errMsg, "invalid email") ||
strings.Contains(errMsg, "failed to find user") {
result["message"] = "Email not found"
} else if strings.Contains(errMsg, "login failed with status: 401") {
result["message"] = "Invalid email or password"
} else if strings.Contains(errMsg, "login failed with status: 500") {
// Status 500 often means password errors in your backend
result["message"] = "Incorrect password"
} else {
// Log the actual error for debugging but return a user-friendly message
log.Printf("Unrecognized login error: %v", errMsg)
result["message"] = "Server error. Please try again later"
}
return result
}
// If no error but the response indicates failure
if response != nil && !response.Success {
// Handle backend response failure cases
log.Printf("Backend returned success=false with message: %s", response.Message)
// Check for known error patterns in the response message
if strings.Contains(strings.ToLower(response.Message), "password") {
result["message"] = "Incorrect password"
} else {
result["message"] = response.Message
}
return result
}
// Success case
log.Println("Login successful")
result["success"] = true
result["message"] = "Login successful"
return result
}
// CheckSession verifies if the user has a valid session
func (a *App) CheckSession() map[string]interface{} {
response, err := auth.SessionCheckUser()
if err != nil {
return map[string]interface{}{
"Success": false,
"Message": "Session invalid",
}
}
return map[string]interface{}{
"Success": response.Success,
"Message": response.Message,
"Email": response.Email,
}
}
// Add this function to your App struct to expose the LogoutUser functionality
func (a *App) LogoutUser() map[string]interface{} {
response, err := auth.LogoutUser()
if err != nil {
return map[string]interface{}{
"Success": false,
"Message": err.Error(),
}
}
return map[string]interface{}{
"Success": response.Success,
"Message": response.Message,
}
}
// Add this function to expose RecoveryProcess to the frontend
func (a *App) RecoveryProcess(email, password, seedPhrase string) (string, error) {
// Call the auth package's RecoveryProcess function
return auth.RecoveryProcess(email, password, seedPhrase)
}
// Add this function to expose RecoverySetup to the frontend if needed
func (a *App) RecoverySetup(email string) (string, error) {
return auth.RecoverySetup(email)
}
// CreateItemClient exposes the client-side service function to the frontend
func (a *App) CreateItemClient(title, typename string, ItemData map[string]interface{}) (*service.CreateItemResponse, error) {
log.Printf("CreateItemClient called with title: %s, type: %s", title, typename)
return service.CreateItemClient(title, typename, ItemData)
}
func (a *App) ToggleBookmark(itemId uint, bookmark bool) (*service.BookmarkResponse, error) {
return service.BookmarkClient(itemId, bookmark)
}
// Function to get the list of categories with their counts
func (a *App) GetCategoryList() ([]map[string]interface{}, error) {
// Get actual category data from the service
items, categories, err := service.GetListItemClient()
if err != nil {
log.Printf("Error getting category list: %v", err)
return nil, err
}
// If no categories were found, return an empty array
if categories == nil {
return []map[string]interface{}{}, nil
}
// Create a map to store item counts for each category
categoryCounts := make(map[uint]int)
// Count items per category
if items != nil {
for _, item := range *items {
// Check the item's Data map for CategoryID
if categoryID, ok := item.Data["CategoryID"]; ok {
if floatID, isFloat := categoryID.(float64); isFloat {
// Convert float64 to uint
categoryCounts[uint(floatID)]++
} else if intID, isInt := categoryID.(int); isInt {
// Handle int type
categoryCounts[uint(intID)]++
} else if uintID, isUint := categoryID.(uint); isUint {
// Handle uint type directly
categoryCounts[uintID]++
}
}
}
}
// Build the response in the format expected by the frontend
result := make([]map[string]interface{}, 0, len(*categories))
for _, cat := range *categories {
result = append(result, map[string]interface{}{
"CategoryID": cat.CategoryID,
"CategoryName": cat.CategoryName,
"ItemCount": categoryCounts[cat.CategoryID], // Get count from our map
})
}
return result, nil
}
// Function to create a new category
func (a *App) CreateCategoryClient(categoryName string) (*service.CreateCategoryResponse, error) {
return service.CreateCategoryClient(categoryName)
}
// Function to delete a category
func (a *App) DeleteCategoryClient(categoryId uint) (*service.DeleteCategoryResponse, error) {
return service.DeleteCategoryClient(categoryId)
}
// Function to update an existing category
func (a *App) UpdateItemClient(itemId uint, categoryId *uint, title string, ItemData map[string]interface{}) (*service.UpdateItemResponse, error) {
return service.UpdateItemClient(itemId, categoryId, title, ItemData)
}
// GetPasswordList returns password items with their complete information including categories
func (a *App) GetPasswordList() ([]map[string]interface{}, error) {
log.Println("GetPasswordList called from frontend")
// Get both items and categories from the client service
items, categories, err := service.GetListItemClient()
if err != nil {
log.Printf("GetPasswordList error: %v", err)
return nil, err
}
// Make sure we don't return nil
if items == nil {
log.Println("GetPasswordList: items is nil, returning empty array")
return []map[string]interface{}{}, nil
}
// Create a map of category ID to name for easy lookup
categoryMap := make(map[uint]string)
if categories != nil {
for _, cat := range *categories {
categoryMap[cat.CategoryID] = cat.CategoryName
}
}
// Build response with items including their category names
result := make([]map[string]interface{}, 0, len(*items))
for _, item := range *items {
// Create a map with all item fields
itemMap := map[string]interface{}{
"ItemID": item.ItemID,
"Title": item.Title,
"TypeName": item.TypeName,
"DateCreate": item.DateCreate,
"DateModify": item.DateModify,
"Data": item.Data,
"IsBookmark": item.IsBookmark,
}
// Add category information if available
if item.CategoryID != nil {
itemMap["CategoryID"] = *item.CategoryID
if catName, ok := categoryMap[*item.CategoryID]; ok {
itemMap["CategoryName"] = catName
} else {
itemMap["CategoryName"] = ""
}
} else {
itemMap["CategoryID"] = nil
itemMap["CategoryName"] = ""
}
result = append(result, itemMap)
}
log.Printf("GetPasswordList returning %d items", len(result))
return result, nil
}
// DeleteItemClient exposes the delete item functionality to the frontend
func (a *App) DeleteItemClient(itemId uint) (*service.DeleteItemResponse, error) {
log.Printf("DeleteItemClient called with itemId: %d", itemId)
response, err := service.DeleteItemClient(itemId)
if err != nil {
log.Printf("Error deleting item %d: %v", itemId, err)
return nil, err
}
log.Printf("Successfully deleted item %d, response: %+v", itemId, response)
return response, nil
}
// UpdateCategoryClient exposes the category update functionality to the frontend
func (a *App) UpdateCategoryClient(categoryId uint, categoryName string) (*service.UpdateCategoryResponse, error) {
log.Printf("UpdateCategoryClient called with categoryId: %d, name: %s", categoryId, categoryName)
response, err := service.UpdateCategoryClient(categoryId, categoryName)
if err != nil {
log.Printf("Error updating category %d: %v", categoryId, err)
return nil, err
}
log.Printf("Successfully updated category %d, response: %+v", categoryId, response)
return response, nil
}