-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathpolicies.go
More file actions
138 lines (121 loc) · 4.81 KB
/
Copy pathpolicies.go
File metadata and controls
138 lines (121 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"context"
"log/slog"
"github.com/barrydeen/haven/pkg/wot"
"github.com/fiatjaf/khatru"
"github.com/nbd-wtf/go-nostr"
)
func MustBeWhitelistedToQuery(ctx context.Context, _ nostr.Filter) (bool, string) {
authenticatedUser := khatru.GetAuthed(ctx)
if _, ok := config.WhitelistedPubKeys[authenticatedUser]; !ok {
slog.Debug("🚫 query rejected: user is not whitelisted", "user", authenticatedUser)
return true, "restricted: you must be whitelisted to query this relay"
}
return false, ""
}
func MustBeInWotToQuery(ctx context.Context, _ nostr.Filter) (bool, string) {
authenticatedUser := khatru.GetAuthed(ctx)
if !wot.GetInstance().Has(ctx, authenticatedUser) {
slog.Debug("🚫 query rejected: user is not in the web of trust", "user", authenticatedUser)
return true, "restricted: you must be in the web of trust to query this relay"
}
return false, ""
}
func MustBeWhitelistedToPost(ctx context.Context, event *nostr.Event) (bool, string) {
// Event from a whitelisted pubkey can always be posted, even if the user is not authenticated
if _, ok := config.WhitelistedPubKeys[event.PubKey]; ok {
return false, ""
}
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
return true, "auth-required: you must be authenticated to post to this relay"
}
if _, ok := config.WhitelistedPubKeys[authenticatedUser]; !ok {
slog.Debug("🚫 event rejected: user is not whitelisted", "event", event.ID, "pubkey", authenticatedUser)
return true, "restricted: you must be whitelisted to post to this relay"
}
return false, ""
}
func MustBeInWotToPost(ctx context.Context, event *nostr.Event) (bool, string) {
// Event from a pubkey in the WoT can always be posted, even if the user is not authenticated
if wot.GetInstance().Has(ctx, event.PubKey) {
return false, ""
}
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
return true, "auth-required: you must be authenticated to post to this relay"
}
if !wot.GetInstance().Has(ctx, authenticatedUser) {
slog.Debug("🚫 event rejected: user is not in web of trust", "event", event.ID, "pubkey", authenticatedUser)
return true, "you must be in the web of trust to post to this relay"
}
return false, ""
}
func MustNotBeBlacklistedToPost(ctx context.Context, event *nostr.Event) (bool, string) {
// Events from a blacklisted pubkey ARE always rejected
if _, ok := config.BlacklistedPubKeys[event.PubKey]; ok {
slog.Debug("🚫 event rejected: event author is blacklisted", "event", event.ID, "pubkey", event.PubKey)
return true, "you are blacklisted from this relay"
}
// Still need auth due to GiftWrap and other events with random pubkeys
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
return true, "auth-required: you must be authenticated to post to this relay"
}
if _, ok := config.BlacklistedPubKeys[authenticatedUser]; ok {
slog.Debug("🚫 event rejected: authenticated user is blacklisted", "event", event.ID, "pubkey", authenticatedUser)
return true, "you are blacklisted from this relay"
}
return false, ""
}
var allowedChatKinds = map[int]struct{}{
// Regular kinds
nostr.KindSimpleGroupChatMessage: {},
nostr.KindSimpleGroupThreadedReply: {},
nostr.KindSimpleGroupThread: {},
nostr.KindSimpleGroupReply: {},
nostr.KindChannelMessage: {},
nostr.KindChannelHideMessage: {},
nostr.KindGiftWrap: {},
nostr.KindSimpleGroupPutUser: {},
nostr.KindSimpleGroupRemoveUser: {},
nostr.KindSimpleGroupEditMetadata: {},
nostr.KindSimpleGroupDeleteEvent: {},
nostr.KindSimpleGroupCreateGroup: {},
nostr.KindSimpleGroupDeleteGroup: {},
nostr.KindSimpleGroupCreateInvite: {},
nostr.KindSimpleGroupJoinRequest: {},
nostr.KindSimpleGroupLeaveRequest: {},
// Addressable kinds
nostr.KindSimpleGroupMetadata: {},
nostr.KindSimpleGroupAdmins: {},
nostr.KindSimpleGroupMembers: {},
nostr.KindSimpleGroupRoles: {},
}
func EventMustBeChatRelated(_ context.Context, event *nostr.Event) (bool, string) {
if _, ok := allowedChatKinds[event.Kind]; ok {
return false, ""
}
return true, "only chat related events are allowed"
}
func OnlyGiftWrappedDMs(_ context.Context, event *nostr.Event) (bool, string) {
if event.Kind == nostr.KindEncryptedDirectMessage {
return true, "only gift wrapped DMs are supported"
}
return false, ""
}
func MustTagWhitelistedPubKey(_ context.Context, event *nostr.Event) (bool, string) {
// User must tag at least one whitelisted pubkey in this relay
tags := event.Tags.FindAll("p")
for tag := range tags {
if len(tag) < 2 {
continue
}
if _, ok := config.WhitelistedPubKeys[tag[1]]; ok {
return false, ""
}
}
slog.Debug("🚫 event rejected: event does not tag any whitelisted pubkey", "eventID", event.ID)
return true, "you can only post notes if you've tagged a whitelisted pubkey in this relay"
}