2024-09-02 00:51:02 +00:00
|
|
|
package config
|
2024-08-07 21:24:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-08 21:35:54 +00:00
|
|
|
nostr "grain/server/types"
|
2024-09-02 00:51:02 +00:00
|
|
|
"grain/server/utils"
|
2024-11-08 21:57:35 +00:00
|
|
|
"log"
|
2024-08-07 21:24:38 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2024-11-08 21:35:54 +00:00
|
|
|
// CheckWhitelist checks if an event meets the whitelist criteria.
|
|
|
|
func CheckWhitelist(evt nostr.Event) (bool, string) {
|
|
|
|
whitelistCfg := GetWhitelistConfig()
|
|
|
|
if whitelistCfg == nil {
|
|
|
|
return false, "Internal server error: whitelist configuration is missing"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event's kind is whitelisted
|
|
|
|
if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) {
|
|
|
|
return false, "not allowed: event kind is not whitelisted"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the event's pubkey is whitelisted
|
|
|
|
if whitelistCfg.PubkeyWhitelist.Enabled && !IsPubKeyWhitelisted(evt.PubKey) {
|
|
|
|
return false, "not allowed: pubkey or npub is not whitelisted"
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, ""
|
|
|
|
}
|
|
|
|
|
2024-11-08 21:57:35 +00:00
|
|
|
// IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
|
2024-08-07 21:24:38 +00:00
|
|
|
func IsPubKeyWhitelisted(pubKey string) bool {
|
2024-10-16 13:57:58 +00:00
|
|
|
cfg := GetWhitelistConfig()
|
2024-11-08 21:57:35 +00:00
|
|
|
if cfg == nil || !cfg.PubkeyWhitelist.Enabled {
|
|
|
|
return true // Whitelisting is not enforced if the configuration is missing or disabled
|
2024-10-16 13:57:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-08 21:57:35 +00:00
|
|
|
// Check statically defined pubkeys
|
2024-10-16 13:57:58 +00:00
|
|
|
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
|
|
|
if pubKey == whitelistedKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 21:57:35 +00:00
|
|
|
// Check statically defined npubs after decoding them to pubkeys
|
2024-10-16 13:57:58 +00:00
|
|
|
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
|
|
|
decodedPubKey, err := utils.DecodeNpub(npub)
|
|
|
|
if err != nil {
|
2024-11-08 21:57:35 +00:00
|
|
|
log.Printf("Error decoding npub: %v", err)
|
2024-10-16 13:57:58 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if pubKey == decodedPubKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-08 21:57:35 +00:00
|
|
|
// Fetch and check pubkeys from domains if domain whitelisting is enabled
|
|
|
|
if cfg.DomainWhitelist.Enabled {
|
|
|
|
domains := cfg.DomainWhitelist.Domains
|
|
|
|
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error fetching pubkeys from domains: %v", err)
|
|
|
|
return false // Consider returning true or handling based on your application's needs
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domainPubKey := range pubkeys {
|
|
|
|
if pubKey == domainPubKey {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
return false
|
2024-08-07 21:24:38 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 13:57:58 +00:00
|
|
|
// Check if a kind is whitelisted
|
2024-08-07 21:24:38 +00:00
|
|
|
func IsKindWhitelisted(kind int) bool {
|
2024-10-16 13:57:58 +00:00
|
|
|
cfg := GetWhitelistConfig()
|
|
|
|
if !cfg.KindWhitelist.Enabled {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
|
|
|
|
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error converting whitelisted kind to int:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if kind == whitelistedKind {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|