@@ -11,12 +11,10 @@ import (
11
11
12
12
"github.com/gin-gonic/gin"
13
13
"github.com/lib/pq"
14
- "github.com/rs/zerolog/log"
15
14
"github.com/threefoldtech/tfgrid4-sdk-go/node-registrar/pkg/db"
16
15
)
17
16
18
17
const (
19
- PubKeySize = 32
20
18
MaxTimestampDelta = 2 * time .Second
21
19
)
22
20
@@ -78,13 +76,14 @@ func (s Server) getFarmHandler(c *gin.Context) {
78
76
79
77
farm , err := s .db .GetFarm (id )
80
78
if err != nil {
81
- status := http .StatusBadRequest
79
+ status := http .StatusInternalServerError
82
80
83
81
if errors .Is (err , db .ErrRecordNotFound ) {
84
82
status = http .StatusNotFound
85
83
}
86
84
87
85
c .JSON (status , gin.H {"error" : err .Error ()})
86
+ return
88
87
}
89
88
90
89
c .JSON (http .StatusOK , farm )
@@ -117,7 +116,7 @@ func (s Server) createFarmHandler(c *gin.Context) {
117
116
118
117
farmID , err := s .db .CreateFarm (farm )
119
118
if err != nil {
120
- status := http .StatusBadRequest
119
+ status := http .StatusInternalServerError
121
120
122
121
if errors .Is (err , db .ErrRecordAlreadyExists ) {
123
122
status = http .StatusConflict
@@ -148,7 +147,7 @@ type UpdateFarmRequest struct {
148
147
// @Failure 401 {object} map[string]any "Unauthorized"
149
148
// @Failure 404 {object} map[string]any "Farm not found"
150
149
// @Router /farms/{farm_id} [patch]
151
- func (s Server ) updateFarmsHandler (c * gin.Context ) {
150
+ func (s Server ) updateFarmHandler (c * gin.Context ) {
152
151
var req UpdateFarmRequest
153
152
farmID := c .Param ("farm_id" )
154
153
@@ -186,7 +185,7 @@ func (s Server) updateFarmsHandler(c *gin.Context) {
186
185
(len (req .StellarAddress ) != 0 && existingFarm .StellarAddress != req .StellarAddress ) {
187
186
err = s .db .UpdateFarm (id , req .FarmName , req .StellarAddress )
188
187
if err != nil {
189
- status := http .StatusBadRequest
188
+ status := http .StatusInternalServerError
190
189
191
190
if errors .Is (err , db .ErrRecordNotFound ) {
192
191
status = http .StatusNotFound
@@ -229,7 +228,7 @@ func (s Server) listNodesHandler(c *gin.Context) {
229
228
230
229
nodes , err := s .db .ListNodes (filter , limit )
231
230
if err != nil {
232
- c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
231
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
233
232
return
234
233
}
235
234
@@ -262,7 +261,7 @@ func (s Server) getNodeHandler(c *gin.Context) {
262
261
return
263
262
}
264
263
265
- c .JSON (http .StatusBadRequest , gin.H {"error" : err .Error ()})
264
+ c .JSON (http .StatusInternalServerError , gin.H {"error" : err .Error ()})
266
265
return
267
266
}
268
267
@@ -319,7 +318,7 @@ func (s Server) registerNodeHandler(c *gin.Context) {
319
318
320
319
nodeID , err := s .db .RegisterNode (node )
321
320
if err != nil {
322
- status := http .StatusBadRequest
321
+ status := http .StatusInternalServerError
323
322
324
323
if errors .Is (err , db .ErrRecordAlreadyExists ) {
325
324
status = http .StatusConflict
@@ -382,7 +381,6 @@ func (s *Server) updateNodeHandler(c *gin.Context) {
382
381
c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "invalid request body" })
383
382
return
384
383
}
385
- log .Debug ().Any ("req" , req ).Send ()
386
384
387
385
updatedNode := db.Node {
388
386
FarmID : req .FarmID ,
@@ -540,11 +538,13 @@ func (s *Server) createAccountHandler(c *gin.Context) {
540
538
publicKeyBytes , err := base64 .StdEncoding .DecodeString (req .PublicKey )
541
539
if err != nil {
542
540
c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid public key format" })
541
+ return
543
542
}
544
543
// Decode signature from base64
545
544
signatureBytes , err := base64 .StdEncoding .DecodeString (req .Signature )
546
545
if err != nil {
547
546
c .JSON (http .StatusBadRequest , gin.H {"error" : fmt .Sprintf ("invalid signature format: %v" , err )})
547
+ return
548
548
}
549
549
// Verify signature of the challenge
550
550
err = verifySignature (publicKeyBytes , challenge , signatureBytes )
@@ -572,13 +572,6 @@ func (s *Server) createAccountHandler(c *gin.Context) {
572
572
c .JSON (http .StatusCreated , account )
573
573
}
574
574
575
- /* // verifySignature verifies an ED25519 signature
576
- func verifySignature(publicKey, chalange, signature []byte) (bool, error) {
577
-
578
- // Verify the signature
579
- return ed25519.Verify(publicKey, chalange, signature), nil
580
- } */
581
-
582
575
type UpdateAccountRequest struct {
583
576
Relays pq.StringArray `json:"relays"`
584
577
RMBEncKey string `json:"rmb_enc_key"`
@@ -669,7 +662,7 @@ func (s *Server) getAccountHandler(c *gin.Context) {
669
662
670
663
account , err := s .db .GetAccount (twinID )
671
664
if err != nil {
672
- if err == db .ErrRecordNotFound {
665
+ if errors . Is ( err , db .ErrRecordNotFound ) {
673
666
c .JSON (http .StatusNotFound , gin.H {"error" : "account not found" })
674
667
return
675
668
}
@@ -684,7 +677,7 @@ func (s *Server) getAccountHandler(c *gin.Context) {
684
677
if publicKeyParam != "" {
685
678
account , err := s .db .GetAccountByPublicKey (publicKeyParam )
686
679
if err != nil {
687
- if err == db .ErrRecordNotFound {
680
+ if errors . Is ( err , db .ErrRecordNotFound ) {
688
681
c .JSON (http .StatusNotFound , gin.H {"error" : "account not found" })
689
682
return
690
683
}
@@ -759,7 +752,7 @@ func (s *Server) getZOSVersionHandler(c *gin.Context) {
759
752
c .JSON (http .StatusOK , version )
760
753
}
761
754
762
- // Helper function to validate public key format
755
+ // Helper function to validate public key length
763
756
func isValidPublicKey (publicKeyBase64 string ) bool {
764
757
publicKeyBytes , err := base64 .StdEncoding .DecodeString (publicKeyBase64 )
765
758
if err != nil {
@@ -771,8 +764,8 @@ func isValidPublicKey(publicKeyBase64 string) bool {
771
764
// Helper function to ensure the request is from the owner
772
765
func ensureOwner (c * gin.Context , twinID uint64 ) {
773
766
// Retrieve twinID set by the authMiddleware
774
- authTwinID , exists := c .Get ( "twinID" )
775
- if ! exists {
767
+ authTwinID := c .Request . Context (). Value ( twinIDKey {} )
768
+ if authTwinID == nil {
776
769
c .AbortWithStatusJSON (http .StatusUnauthorized , gin.H {"error" : "not authorized" })
777
770
return
778
771
}
0 commit comments