Skip to content

Commit

Permalink
add auth
Browse files Browse the repository at this point in the history
  • Loading branch information
golangisfun123 committed Apr 30, 2024
1 parent 496d67a commit df7ffc6
Showing 1 changed file with 45 additions and 51 deletions.
96 changes: 45 additions & 51 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/ipfs/go-log"
"github.com/synapsecns/sanguine/contrib/screener-api/client"
"github.com/synapsecns/sanguine/contrib/screener-api/config"
Expand Down Expand Up @@ -85,7 +86,7 @@ func NewScreener(ctx context.Context, cfg config.Config, metricHandler metrics.H
screener.router.Handle(http.MethodGet, "/:ruleset/address/:address", screener.screenAddress)

// idk the middleware is faking up
screener.router.Handle(http.MethodPost, "/api/data/sync", screener.blacklistAddress)
screener.router.Handle(http.MethodPost, "/api/data/sync", screener.authMiddleware(), screener.blacklistAddress)

return &screener, nil
}
Expand Down Expand Up @@ -124,33 +125,26 @@ func (s *screenerImpl) fetchBlacklist(ctx context.Context) {

func (s *screenerImpl) blacklistAddress(c *gin.Context) {
var blacklistBody client.BlackListBody
// grab the body

if err := c.ShouldBindJSON(&blacklistBody); err != nil {
// Grab the body of the JSON request and unmarshal it into the blacklistBody struct.
if err := c.ShouldBindBodyWith(&blacklistBody, binding.JSON); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

Check warning on line 133 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L131-L133

Added lines #L131 - L133 were not covered by tests

type_req := blacklistBody.TypeReq
id := blacklistBody.Id
data := blacklistBody.Data
address := blacklistBody.Address
network := blacklistBody.Network
tag := blacklistBody.Tag
remark := blacklistBody.Remark
address = strings.ToLower(address)
blacklistedAddress := db.BlacklistedAddress{
TypeReq: blacklistBody.TypeReq,
Id: blacklistBody.Id,
Data: blacklistBody.Data,
Network: blacklistBody.Network,
Tag: blacklistBody.Tag,
Remark: blacklistBody.Remark,
Address: strings.ToLower(blacklistBody.Address),
}

switch type_req {
switch blacklistBody.TypeReq {
case "create":
if err := s.db.PutBlacklistedAddress(c, db.BlacklistedAddress{
Id: id,
TypeReq: type_req,
Data: data,
Address: address,
Network: network,
Tag: tag,
Remark: remark,
}); err != nil {
if err := s.db.PutBlacklistedAddress(c, blacklistedAddress); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

Check warning on line 150 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L148-L150

Added lines #L148 - L150 were not covered by tests
Expand All @@ -159,15 +153,7 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
return

case "update":
if err := s.db.UpdateBlacklistedAddress(c, id, db.BlacklistedAddress{
Id: id,
TypeReq: type_req,
Data: data,
Address: address,
Network: network,
Tag: tag,
Remark: remark,
}); err != nil {
if err := s.db.UpdateBlacklistedAddress(c, blacklistedAddress.Id, blacklistedAddress); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

Check warning on line 159 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L157-L159

Added lines #L157 - L159 were not covered by tests
Expand All @@ -176,10 +162,11 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
return

case "delete":
if err := s.db.DeleteBlacklistedAddress(c, address); err != nil {
if err := s.db.DeleteBlacklistedAddress(c, blacklistedAddress.Address); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

Check warning on line 168 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L166-L168

Added lines #L166 - L168 were not covered by tests

c.JSON(http.StatusOK, gin.H{"status": "success"})
return

Expand All @@ -190,30 +177,37 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {

}

func (s *screenerImpl) authMiddleware(c *gin.Context) {
var blacklistBody client.BlackListBody
// This function takes the HTTP headers and the body of the request and reconstructs the signature to
// compare it with the signature provided. If they match, the request is allowed to pass through.
func (s *screenerImpl) authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
var blacklistBody client.BlackListBody

if err := c.ShouldBindJSON(&blacklistBody); err != nil {
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"error": "Auth middleware fucked up"})
return
}
if err := c.ShouldBindBodyWith(&blacklistBody, binding.JSON); err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
c.Abort()
return
}

Check warning on line 190 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L187-L190

Added lines #L187 - L190 were not covered by tests

nonce := c.GetHeader("nonce")
timestamp := c.GetHeader("timestamp")
appid := c.GetHeader("appid")
queryString := c.GetHeader("queryString")
if nonce == "" || timestamp == "" || appid == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing headers"})
c.Abort()
}
nonce := c.GetHeader("nonce")
timestamp := c.GetHeader("timestamp")
appid := c.GetHeader("appid")
queryString := c.GetHeader("queryString")
if nonce == "" || timestamp == "" || appid == "" {
c.JSON(http.StatusConflict, gin.H{"error": "missing headers"})
c.Abort()
return
}

Check warning on line 200 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L197-L200

Added lines #L197 - L200 were not covered by tests

// reconstruct signature
expected := client.GenerateSignature("appsecret", appid, timestamp, nonce, queryString, blacklistBody)
// reconstruct signature
expected := client.GenerateSignature("appsecret", appid, timestamp, nonce, queryString, blacklistBody)

if c.GetHeader("Signature") != expected {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.Abort()
if c.GetHeader("Signature") != expected {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized your mom"})
c.Abort()
return
}

Check warning on line 209 in contrib/screener-api/screener/screener.go

View check run for this annotation

Codecov / codecov/patch

contrib/screener-api/screener/screener.go#L206-L209

Added lines #L206 - L209 were not covered by tests
c.Next()
}
}

Expand Down

0 comments on commit df7ffc6

Please sign in to comment.