Skip to content

Commit

Permalink
finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
golangisfun123 committed Apr 30, 2024
1 parent b5a29e1 commit 496d67a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
41 changes: 28 additions & 13 deletions contrib/screener-api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (c clientImpl) ScreenAddress(ctx context.Context, ruleset, address string)
}

type BlackListBody struct {
TypeReq string `json:"type" binding:"required"`
Id string `json:"id" binding:"required"`
TypeReq string `json:"typereq"`
Id string `json:"id"`
Data string `json:"data"`
Address string `json:"address"`
Network string `json:"network"`
Expand All @@ -77,29 +77,41 @@ type BlackListBody struct {

type blacklistResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}

func (c clientImpl) BlacklistAddress(ctx context.Context, body BlackListBody) (string, error) {
var blacklistRes blacklistResponse

// change/move it later
// TODO: remove, just for testing purposes
// future, take it from some .env or something
appsecret := "appsecret"
appid := "appid"

nonce := strings.Replace(uuid.New().String(), "-", "", -1)[:32]
timestamp := fmt.Sprintf("%d", time.Now().Unix())
queryString := "" // there is no query string in this post request, ask about this

signature := GenerateSignature(appsecret, body)
signature := GenerateSignature(appsecret, appid, timestamp, nonce, queryString, body)

resp, err := c.rClient.R().
SetContext(ctx).
SetAuthToken(signature).
SetHeader("Content-Type", "application/json").
SetHeader("appid", appid).
SetHeader("timestamp", timestamp).
SetHeader("nonce", nonce).
SetHeader("queryString", queryString).
SetHeader("signature", signature).
SetResult(&blacklistRes).
SetBody(body).
Post(BlacklistEndpoint)

if err != nil {
return "", fmt.Errorf("error from server: %s: %w", resp.Status(), err)
return resp.Status(), fmt.Errorf("error from server: %s: %w", resp.String(), err)
}

if resp.IsError() {
return "", fmt.Errorf("error from server: %s", resp.Status())
return resp.Status(), fmt.Errorf("error from server: %s", resp.String())
}

return blacklistRes.Status, nil
Expand All @@ -121,14 +133,16 @@ func (n noOpClient) BlacklistAddress(_ context.Context, _ BlackListBody) (string
return "", nil
}

func GenerateSignature(secret string, body BlackListBody) string {
func GenerateSignature(secret string,
appid string,
timestamp string,
nonce string,
queryString string,
body BlackListBody,
) string {
key := []byte(secret)

appid := "appid"
nonce := strings.Replace(uuid.New().String(), "-", "", -1)[:32]
timestamp := fmt.Sprintf("%d", time.Now().Unix())
queryString := "" // there is no query string in this post request

// concatenate the body
message := fmt.Sprintf(
"%s%s%s%s%s%s%s",
appid,
Expand All @@ -140,6 +154,7 @@ func GenerateSignature(secret string, body BlackListBody) string {
body,
)
h := hmac.New(sha256.New, key)
// hash it
h.Write([]byte(message))

return strings.ToLower(hex.EncodeToString(h.Sum(nil)))
Expand Down
2 changes: 1 addition & 1 deletion contrib/screener-api/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (d *DBSuite) TestBlacklist() {

// delete nonexistent
err = testDB.DeleteBlacklistedAddress(d.GetTestContext(), "NonexistentId")
d.Require().Error(err)
d.Require().NoError(err)

})
}
2 changes: 1 addition & 1 deletion contrib/screener-api/db/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type BlacklistedAddress struct {
CreatedAt time.Time
UpdatedAt time.Time

Id string `gorm:"column:id;primary_key"`
TypeReq string `gorm:"column:typereq"`
Id string `gorm:"column:id;primary_key"`
Data string `gorm:"column:data"`
Address string `gorm:"column:address"`
Network string `gorm:"column:network"`
Expand Down
4 changes: 2 additions & 2 deletions contrib/screener-api/db/sql/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (s *Store) UpdateBlacklistedAddress(ctx context.Context, id string, body db

func (s *Store) DeleteBlacklistedAddress(ctx context.Context, id string) error {
if dbTx := s.db.WithContext(ctx).Where(
"id = ?", id).Delete(&db.BlacklistedAddress{}); dbTx.Error != nil || dbTx.RowsAffected == 0 {
return fmt.Errorf("failed to delete blacklisted address")
"id = ?", id).Delete(&db.BlacklistedAddress{}); dbTx.Error != nil {
return fmt.Errorf("failed to delete blacklisted address: %w", dbTx.Error)
}
return nil
}
Expand Down
22 changes: 18 additions & 4 deletions contrib/screener-api/screener/screener.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func NewScreener(ctx context.Context, cfg config.Config, metricHandler metrics.H
screener.router = ginhelper.New(logger)
screener.router.Handle(http.MethodGet, "/:ruleset/address/:address", screener.screenAddress)

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

return &screener, nil
}
Expand Down Expand Up @@ -123,8 +124,8 @@ 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 {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -193,11 +194,24 @@ func (s *screenerImpl) authMiddleware(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": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"error": "Auth middleware fucked up"})
return
}

if c.GetHeader("Authorization") != client.GenerateSignature("appsecret", blacklistBody) {
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()
}

// 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()
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/screener-api/screener/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (s *ScreenerSuite) TestScreener() {
blacklistBody := client.BlackListBody{
TypeReq: "create",
Id: "1",
Data: "",
Data: "{\"test\":\"data\"}",
Address: "0x123",
Network: "eth",
Tag: "tag",
Expand Down

0 comments on commit 496d67a

Please sign in to comment.