@@ -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
@@ -377,13 +376,11 @@ func (s *Server) updateNodeHandler(c *gin.Context) {
377
376
return
378
377
}
379
378
380
- log .Info ().Any ("req is" , c .Request .Body )
381
379
var req UpdateNodeRequest
382
380
if err := c .ShouldBindJSON (& req ); err != nil {
383
381
c .AbortWithStatusJSON (http .StatusBadRequest , gin.H {"error" : "invalid request body" })
384
382
return
385
383
}
386
- log .Debug ().Any ("req" , req ).Send ()
387
384
388
385
updatedNode := db.Node {
389
386
FarmID : req .FarmID ,
@@ -541,11 +538,13 @@ func (s *Server) createAccountHandler(c *gin.Context) {
541
538
publicKeyBytes , err := base64 .StdEncoding .DecodeString (req .PublicKey )
542
539
if err != nil {
543
540
c .JSON (http .StatusBadRequest , gin.H {"error" : "invalid public key format" })
541
+ return
544
542
}
545
543
// Decode signature from base64
546
544
signatureBytes , err := base64 .StdEncoding .DecodeString (req .Signature )
547
545
if err != nil {
548
546
c .JSON (http .StatusBadRequest , gin.H {"error" : fmt .Sprintf ("invalid signature format: %v" , err )})
547
+ return
549
548
}
550
549
// Verify signature of the challenge
551
550
err = verifySignature (publicKeyBytes , challenge , signatureBytes )
@@ -571,13 +570,6 @@ func (s *Server) createAccountHandler(c *gin.Context) {
571
570
c .JSON (http .StatusCreated , account )
572
571
}
573
572
574
- /* // verifySignature verifies an ED25519 signature
575
- func verifySignature(publicKey, chalange, signature []byte) (bool, error) {
576
-
577
- // Verify the signature
578
- return ed25519.Verify(publicKey, chalange, signature), nil
579
- } */
580
-
581
573
type UpdateAccountRequest struct {
582
574
Relays pq.StringArray `json:"relays"`
583
575
RMBEncKey string `json:"rmb_enc_key"`
@@ -668,7 +660,7 @@ func (s *Server) getAccountHandler(c *gin.Context) {
668
660
669
661
account , err := s .db .GetAccount (twinID )
670
662
if err != nil {
671
- if err == db .ErrRecordNotFound {
663
+ if errors . Is ( err , db .ErrRecordNotFound ) {
672
664
c .JSON (http .StatusNotFound , gin.H {"error" : "account not found" })
673
665
return
674
666
}
@@ -683,14 +675,13 @@ func (s *Server) getAccountHandler(c *gin.Context) {
683
675
if publicKeyParam != "" {
684
676
account , err := s .db .GetAccountByPublicKey (publicKeyParam )
685
677
if err != nil {
686
- if err == db .ErrRecordNotFound {
678
+ if errors . Is ( err , db .ErrRecordNotFound ) {
687
679
c .JSON (http .StatusNotFound , gin.H {"error" : "account not found" })
688
680
return
689
681
}
690
682
c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to get account" })
691
683
return
692
684
}
693
- log .Info ().Any ("account" , account ).Send ()
694
685
c .JSON (http .StatusOK , account )
695
686
return
696
687
}
@@ -759,7 +750,7 @@ func (s *Server) getZOSVersionHandler(c *gin.Context) {
759
750
c .JSON (http .StatusOK , version )
760
751
}
761
752
762
- // Helper function to validate public key format
753
+ // Helper function to validate public key length
763
754
func isValidPublicKey (publicKeyBase64 string ) bool {
764
755
publicKeyBytes , err := base64 .StdEncoding .DecodeString (publicKeyBase64 )
765
756
if err != nil {
@@ -771,8 +762,8 @@ func isValidPublicKey(publicKeyBase64 string) bool {
771
762
// Helper function to ensure the request is from the owner
772
763
func ensureOwner (c * gin.Context , twinID uint64 ) {
773
764
// Retrieve twinID set by the authMiddleware
774
- authTwinID , exists := c .Get ( "twinID" )
775
- if ! exists {
765
+ authTwinID := c .Request . Context (). Value ( twinIDKey {} )
766
+ if authTwinID == nil {
776
767
c .AbortWithStatusJSON (http .StatusUnauthorized , gin.H {"error" : "not authorized" })
777
768
return
778
769
}
0 commit comments