Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
golangisfun123 committed May 28, 2024
1 parent 3ef75a6 commit f11a684
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
12 changes: 3 additions & 9 deletions contrib/screener-api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import (
"github.com/synapsecns/sanguine/core/metrics"
)

var (
// BlacklistEndpoint is the endpoint for blacklisting an address.
BlacklistEndpoint = "/api/data/sync/"
)

// ScreenerClient is an interface for the Screener API.
type ScreenerClient interface {
ScreenAddress(ctx context.Context, ruleset, address string) (blocked bool, err error)
Expand Down Expand Up @@ -95,24 +90,23 @@ func (c clientImpl) BlacklistAddress(ctx context.Context, appsecret string, appi
if err != nil {
return "", fmt.Errorf("error marshalling body: %w", err)
}
bodyStr := string(bodyBz)

message := fmt.Sprintf("%s%s%s%s%s%s%s",
appid, timestamp, nonce, "POST", BlacklistEndpoint, queryString, bodyStr)
appid, timestamp, nonce, "POST", "/api/data/sync/", queryString, string(bodyBz))

signature := GenerateSignature(appsecret, message)

resp, err := c.rClient.R().
SetContext(ctx).
SetHeader("Content-Type", "application/json").
SetHeader("Appid", appid).
SetHeader("AppID", appid).
SetHeader("Timestamp", timestamp).
SetHeader("Nonce", nonce).
SetHeader("QueryString", queryString).
SetHeader("Signature", signature).
SetBody(body).
SetResult(&blacklistRes).
Post(BlacklistEndpoint)
Post("/api/data/sync/")

if err != nil {
return resp.Status(), fmt.Errorf("error from server: %s: %w", resp.String(), err)
Expand Down
20 changes: 14 additions & 6 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,26 @@ func (s *screenerImpl) blacklistAddress(c *gin.Context) {
// compare it with the signature provided. If they match, the request is allowed to pass through.
func (s *screenerImpl) authMiddleware(cfg config.Config) gin.HandlerFunc {
return func(c *gin.Context) {
appID := c.Request.Header.Get("Appid")
_, span := s.metrics.Tracer().Start(c.Request.Context(), "authMiddleware")

appID := c.Request.Header.Get("AppID")
timestamp := c.Request.Header.Get("Timestamp")
nonce := c.Request.Header.Get("Nonce")
signature := c.Request.Header.Get("Signature")

queryString := c.Request.Header.Get("QueryString")
bodyBytes, _ := io.ReadAll(c.Request.Body)
bodyString := string(bodyBytes)
bodyStr := string(bodyBytes)

c.Request.Body = io.NopCloser(strings.NewReader(bodyStr))

c.Request.Body = io.NopCloser(strings.NewReader(bodyString))
span.SetAttributes(attribute.String("appId", appID))
span.SetAttributes(attribute.String("timestamp", timestamp))
span.SetAttributes(attribute.String("nonce", nonce))
span.SetAttributes(attribute.String("signature", signature))
span.SetAttributes(attribute.String("bodyString", bodyStr))

message := fmt.Sprintf("%s%s%s%s%s%s",
appID, timestamp, nonce, "POST", client.BlacklistEndpoint, bodyString)
message := fmt.Sprintf("%s%s%s%s%s%s%s",
appID, timestamp, nonce, "POST", "/api/data/sync/", queryString, bodyStr)

expectedSignature := client.GenerateSignature(cfg.AppSecret, message)

Expand Down
4 changes: 4 additions & 0 deletions contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (s *ScreenerSuite) TestScreener() {

// post to the blacklist
status, err := apiClient.BlacklistAddress(s.GetTestContext(), cfg.AppSecret, cfg.AppID, blacklistBody)
fmt.Println(status)
Equal(s.T(), "success", status)
Nil(s.T(), err)

Expand All @@ -174,6 +175,7 @@ func (s *ScreenerSuite) TestScreener() {
blacklistBody.Remark = "new remark"

status, err = apiClient.BlacklistAddress(s.GetTestContext(), cfg.AppSecret, cfg.AppID, blacklistBody)
fmt.Println(status)
Equal(s.T(), "success", status)
Nil(s.T(), err)

Expand All @@ -182,11 +184,13 @@ func (s *ScreenerSuite) TestScreener() {
blacklistBody.ID = "1"

status, err = apiClient.BlacklistAddress(s.GetTestContext(), cfg.AppSecret, cfg.AppID, blacklistBody)
fmt.Println(status)
Equal(s.T(), "success", status)
Nil(s.T(), err)

// unauthorized
status, err = apiClient.BlacklistAddress(s.GetTestContext(), "bad", cfg.AppID, blacklistBody)
fmt.Println(status)
NotEqual(s.T(), "success", status)
NotNil(s.T(), err)
}
Expand Down

0 comments on commit f11a684

Please sign in to comment.