Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions api/handlers/ownerinfo.go

This file was deleted.

70 changes: 31 additions & 39 deletions api/handlers/rvinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@ import (
"io"
"log/slog"
"net/http"
"sync"

"github.com/fido-device-onboard/go-fdo-server/internal/db"
"gorm.io/gorm"
)

func RvInfoHandler() http.HandlerFunc {
var mu sync.Mutex
return func(w http.ResponseWriter, r *http.Request) {
slog.Debug("Received RV request", "method", r.Method, "path", r.URL.Path)
switch r.Method {
case http.MethodGet:
getRvInfo(w, r)
case http.MethodPost:
createRvInfo(w, r, &mu)
createRvInfo(w, r)
case http.MethodPut:
updateRvInfo(w, r, &mu)
updateRvInfo(w, r)
default:
slog.Debug("Method not allowed", "method", r.Method, "path", r.URL.Path)
slog.Error("Method not allowed", "method", r.Method, "path", r.URL.Path)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}
Expand All @@ -37,10 +35,10 @@ func getRvInfo(w http.ResponseWriter, _ *http.Request) {
rvInfoJSON, err := db.FetchRvInfoJSON()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Debug("No rvInfo found")
slog.Error("No rvInfo found")
http.Error(w, "No rvInfo found", http.StatusNotFound)
} else {
slog.Debug("Error fetching rvInfo", "error", err)
slog.Error("Error fetching rvInfo", "error", err)
http.Error(w, "Error fetching rvInfo", http.StatusInternalServerError)
}
return
Expand All @@ -50,29 +48,26 @@ func getRvInfo(w http.ResponseWriter, _ *http.Request) {
w.Write(rvInfoJSON)
}

func createRvInfo(w http.ResponseWriter, r *http.Request, mu *sync.Mutex) {
mu.Lock()
defer mu.Unlock()

func createRvInfo(w http.ResponseWriter, r *http.Request) {
rvInfo, err := io.ReadAll(r.Body)
if err != nil {
slog.Debug("Error reading body", "error", err)
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}

if _, err := db.FetchRvInfoJSON(); err == nil {
slog.Debug("rvInfo already exists, cannot create new entry")
http.Error(w, "rvInfo already exists", http.StatusConflict)
return
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
slog.Debug("Error checking rvInfo existence", "error", err)
http.Error(w, "Error processing rvInfo", http.StatusInternalServerError)
return
}

if err := db.InsertRvInfo(rvInfo); err != nil {
slog.Debug("Error inserting rvInfo", "error", err)
if errors.Is(err, gorm.ErrDuplicatedKey) {
slog.Error("rvInfo already exists (constraint)", "error", err)
http.Error(w, "rvInfo already exists", http.StatusConflict)
return
}
if errors.Is(err, db.ErrInvalidRvInfo) {
slog.Error("Invalid rvInfo payload", "error", err)
http.Error(w, "Invalid rvInfo", http.StatusBadRequest)
return
}
slog.Error("Error inserting rvInfo", "error", err)
http.Error(w, "Error inserting rvInfo", http.StatusInternalServerError)
return
}
Expand All @@ -84,29 +79,26 @@ func createRvInfo(w http.ResponseWriter, r *http.Request, mu *sync.Mutex) {
w.Write(rvInfo)
}

func updateRvInfo(w http.ResponseWriter, r *http.Request, mu *sync.Mutex) {
mu.Lock()
defer mu.Unlock()

func updateRvInfo(w http.ResponseWriter, r *http.Request) {
rvInfo, err := io.ReadAll(r.Body)
if err != nil {
slog.Debug("Error reading body", "error", err)
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}

if _, err := db.FetchRvInfoJSON(); errors.Is(err, gorm.ErrRecordNotFound) {
slog.Debug("rvInfo does not exist, cannot update")
http.Error(w, "rvInfo does not exist", http.StatusNotFound)
return
} else if err != nil {
slog.Debug("Error checking rvInfo existence", "error", err)
http.Error(w, "Error processing rvInfo", http.StatusInternalServerError)
return
}

if err := db.UpdateRvInfo(rvInfo); err != nil {
slog.Debug("Error updating rvInfo", "error", err)
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Error("rvInfo does not exist, cannot update")
http.Error(w, "rvInfo does not exist", http.StatusNotFound)
return
}
if errors.Is(err, db.ErrInvalidRvInfo) {
slog.Error("Invalid rvInfo payload", "error", err)
http.Error(w, "Invalid rvInfo", http.StatusBadRequest)
return
}
slog.Error("Error updating rvInfo", "error", err)
http.Error(w, "Error updating rvInfo", http.StatusInternalServerError)
return
}
Expand Down
108 changes: 108 additions & 0 deletions api/handlers/rvto2addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// SPDX-FileCopyrightText: (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache 2.0

package handlers

import (
"errors"
"io"
"log/slog"
"net/http"

"github.com/fido-device-onboard/go-fdo-server/internal/db"
"gorm.io/gorm"
)

func RVTO2AddrHandler(w http.ResponseWriter, r *http.Request) {
slog.Debug("Received RVTO2Addr request", "method", r.Method, "path", r.URL.Path)
switch r.Method {
case http.MethodGet:
getRVTO2Addr(w, r)
case http.MethodPost:
createRVTO2Addr(w, r)
case http.MethodPut:
updateRVTO2Addr(w, r)
default:
slog.Error("Method not allowed", "method", r.Method, "path", r.URL.Path)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}

func getRVTO2Addr(w http.ResponseWriter, _ *http.Request) {
slog.Debug("Fetching rvto2Addr")
rvto2AddrJSON, err := db.FetchRVTO2AddrJSON()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Error("No rvto2Addr found")
http.Error(w, "No rvto2Addr found", http.StatusNotFound)
} else {
slog.Error("Error fetching rvto2Addr", "error", err)
http.Error(w, "Error fetching rvto2Addr", http.StatusInternalServerError)
}
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(rvto2AddrJSON)
}

func createRVTO2Addr(w http.ResponseWriter, r *http.Request) {
rvto2Addr, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}

if err := db.InsertRVTO2Addr(rvto2Addr); err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
slog.Error("rvto2Addr already exists (constraint)", "error", err)
http.Error(w, "rvto2Addr already exists", http.StatusConflict)
return
}
if errors.Is(err, db.ErrInvalidRVTO2Addr) {
slog.Error("Invalid rvto2Addr payload", "error", err)
http.Error(w, "Invalid rvto2Addr", http.StatusBadRequest)
return
}
slog.Error("Error inserting rvto2Addr", "error", err)
http.Error(w, "Error inserting rvto2Addr", http.StatusInternalServerError)
return
}

slog.Debug("rvto2Addr created")

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write(rvto2Addr)
}

func updateRVTO2Addr(w http.ResponseWriter, r *http.Request) {
rvto2Addr, err := io.ReadAll(r.Body)
if err != nil {
slog.Error("Error reading body", "error", err)
http.Error(w, "Error reading body", http.StatusInternalServerError)
return
}

if err := db.UpdateRVTO2Addr(rvto2Addr); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
slog.Error("rvto2Addr does not exist, cannot update")
http.Error(w, "rvto2Addr does not exist", http.StatusNotFound)
return
}
if errors.Is(err, db.ErrInvalidRVTO2Addr) {
slog.Error("Invalid rvto2Addr payload", "error", err)
http.Error(w, "Invalid rvto2Addr", http.StatusBadRequest)
return
}
slog.Error("Error updating rvto2Addr", "error", err)
http.Error(w, "Error updating rvto2Addr", http.StatusInternalServerError)
return
}

slog.Debug("rvto2Addr updated")

w.Header().Set("Content-Type", "application/json")
w.Write(rvto2Addr)
}
Loading