Skip to content

Commit

Permalink
Merge pull request #2368 from stakwork/feat/fix_non_public_keys_bounties
Browse files Browse the repository at this point in the history
Feat: added public key check for bounty creation
  • Loading branch information
elraphty authored Jan 8, 2025
2 parents 51a6a21 + 258b00e commit 1e615d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 13 additions & 0 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ func (h *bountyHandler) CreateOrEditBounty(w http.ResponseWriter, r *http.Reques
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)

// return 401 if pubKeyFromAuth is empty
if pubKeyFromAuth == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}

// check if use exists
user := h.db.GetPersonByPubkey(pubKeyFromAuth)
if user.OwnerPubKey == "" {
w.WriteHeader(http.StatusUnauthorized)
return
}

bounty := db.NewBounty{}
body, err := io.ReadAll(r.Body)
r.Body.Close()
Expand Down
11 changes: 7 additions & 4 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ func TestCreateOrEditBounty(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

// create user
db.TestDB.CreateOrEditPerson(bountyOwner)

existingBounty := db.NewBounty{
Type: "coding",
Title: "existing bounty",
Description: "existing bounty description",
WorkspaceUuid: "work-1",
OwnerID: "first-user",
OwnerID: bountyOwner.OwnerPubKey,
Price: 2000,
}

Expand All @@ -143,19 +146,19 @@ func TestCreateOrEditBounty(t *testing.T) {
Title: "new bounty",
Description: "new bounty description",
WorkspaceUuid: "work-1",
OwnerID: "test-key",
OwnerID: bountyOwner.OwnerPubKey,
Price: 1500,
}

failedBounty := db.NewBounty{
Type: "coding",
Title: "new bounty",
Description: "failed bounty description",
WorkspaceUuid: "work-1",
OwnerID: bountyOwner.OwnerPubKey,
Price: 1500,
}

ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
ctx := context.WithValue(context.Background(), auth.ContextKey, bountyOwner.OwnerPubKey)
mockClient := mocks.NewHttpClient(t)
mockUserHasManageBountyRolesTrue := func(pubKeyFromAuth string, uuid string) bool {
return true
Expand Down

0 comments on commit 1e615d5

Please sign in to comment.