Skip to content

Commit

Permalink
Renaming, minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
loosla committed Jun 29, 2024
1 parent c959949 commit 10c27bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestGetFileHandler(t *testing.T) {
os.WriteFile(testFile, []byte(encryptedContent), 0644)

// Create a request to pass to the handler
reqBody := Password{Password: password}
reqBody := Password{Password: password} // TODO: Should be protected.
reqBodyBytes, _ := json.Marshal(reqBody)
req, err := http.NewRequest("GET", "/file", bytes.NewBuffer(reqBodyBytes))
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ func getFileHandler(w http.ResponseWriter, r *http.Request) {
var req Password
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized) // TODO: check errors.
http.Error(w, "unauthorized", http.StatusUnauthorized) // TODO: check errors.
return
}

fileContent, err := readFromFile(defaultFile)
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
http.Error(w, "file not found", http.StatusNotFound)
return
}

key := createAESKey(req.Password)

decrypted, err := decrypt(fileContent, []byte(key))
if err != nil {
log.Fatalf("Failed to decrypt message: %v", err)
log.Fatalf("failed to decrypt message: %v", err)
}

resp := File{Content: string(decrypted)}
Expand All @@ -42,17 +42,17 @@ func getFileHandler(w http.ResponseWriter, r *http.Request) {
}

func updateFileHandler(w http.ResponseWriter, r *http.Request) {
var response File
if err := json.NewDecoder(r.Body).Decode(&response); err != nil {
var file File
if err := json.NewDecoder(r.Body).Decode(&file); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

key := createAESKey(response.Password)
key := createAESKey(file.Password)

encrypted, err := encrypt(response.Content, []byte(key))
encrypted, err := encrypt(file.Content, []byte(key))
if err != nil {
log.Fatalf("Failed to encrypt message: %v", err)
log.Fatalf("failed to encrypt message: %v", err)
}

mu.Lock()
Expand Down

0 comments on commit 10c27bf

Please sign in to comment.