Skip to content

Commit

Permalink
chore: remove support for apps without permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 30, 2024
1 parent 78eec5c commit 0ef85e2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
26 changes: 3 additions & 23 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -365,13 +364,9 @@ func (svc *Service) createResponse(initialEvent *nostr.Event, content interface{

func (svc *Service) GetMethods(app *App) []string {
appPermissions := []AppPermission{}
findPermissionsResult := svc.db.Find(&appPermissions, &AppPermission{
svc.db.Find(&appPermissions, &AppPermission{
AppId: app.ID,
})
if findPermissionsResult.RowsAffected == 0 {
// No permissions created for this app. It can do anything
return strings.Split(NIP_47_CAPABILITIES, ",")
}
requestMethods := make([]string, 0, len(appPermissions))
for _, appPermission := range appPermissions {
requestMethods = append(requestMethods, appPermission.RequestMethod)
Expand All @@ -380,24 +375,9 @@ func (svc *Service) GetMethods(app *App) []string {
}

func (svc *Service) hasPermission(app *App, event *nostr.Event, requestMethod string, amount int64) (result bool, code string, message string) {
// find all permissions for the app
appPermissions := []AppPermission{}
findPermissionsResult := svc.db.Find(&appPermissions, &AppPermission{
AppId: app.ID,
})
if findPermissionsResult.RowsAffected == 0 {
// No permissions created for this app. It can do anything
svc.Logger.WithFields(logrus.Fields{
"eventId": event.ID,
"requestMethod": requestMethod,
"appId": app.ID,
"pubkey": app.NostrPubkey,
}).Info("No permissions found for app")
return true, "", ""
}

appPermission := AppPermission{}
findPermissionResult := findPermissionsResult.Limit(1).Find(&appPermission, &AppPermission{
findPermissionResult := svc.db.Find(&appPermission, &AppPermission{
AppId: app.ID,
RequestMethod: requestMethod,
})
if findPermissionResult.RowsAffected == 0 {
Expand Down
13 changes: 3 additions & 10 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,6 @@ func TestHandleEvent(t *testing.T) {
app := App{Name: "test", NostrPubkey: senderPubkey}
err = svc.db.Save(&app).Error
assert.NoError(t, err)
//test old payload
res, err = svc.HandleEvent(ctx, &nostr.Event{
ID: "test_event_2",
Kind: NIP_47_REQUEST_KIND,
PubKey: senderPubkey,
Content: payload,
})
assert.NoError(t, err)
assert.NotNil(t, res)
//test new payload
newPayload, err := nip04.Encrypt(nip47PayJson, ss)
assert.NoError(t, err)
Expand All @@ -204,7 +195,9 @@ func TestHandleEvent(t *testing.T) {
}
err = json.Unmarshal([]byte(decrypted), received)
assert.NoError(t, err)
assert.Equal(t, received.Result.(*Nip47PayResponse).Preimage, "123preimage")
// this app has no permission
assert.Equal(t, received.Error.Code, NIP_47_ERROR_RESTRICTED)

malformedPayload, err := nip04.Encrypt(nip47PayJsonNoInvoice, ss)
assert.NoError(t, err)
res, err = svc.HandleEvent(ctx, &nostr.Event{
Expand Down

0 comments on commit 0ef85e2

Please sign in to comment.