chekwhitelist refactored

This commit is contained in:
0ceanSlim 2024-11-08 16:57:35 -05:00
parent c51199737b
commit 4fef7088bb

View File

@ -4,28 +4,17 @@ import (
"fmt" "fmt"
nostr "grain/server/types" nostr "grain/server/types"
"grain/server/utils" "grain/server/utils"
"log"
"strconv" "strconv"
) )
// CheckWhitelist checks if an event meets the whitelist criteria. // CheckWhitelist checks if an event meets the whitelist criteria.
func CheckWhitelist(evt nostr.Event) (bool, string) { func CheckWhitelist(evt nostr.Event) (bool, string) {
// Get the current whitelist configuration
whitelistCfg := GetWhitelistConfig() whitelistCfg := GetWhitelistConfig()
if whitelistCfg == nil { if whitelistCfg == nil {
return false, "Internal server error: whitelist configuration is missing" return false, "Internal server error: whitelist configuration is missing"
} }
// If domain whitelisting is enabled, fetch pubkeys from domains
if whitelistCfg.DomainWhitelist.Enabled {
domains := whitelistCfg.DomainWhitelist.Domains
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
if err != nil {
return false, "Error fetching pubkeys from domains"
}
// Update the whitelisted pubkeys dynamically
whitelistCfg.PubkeyWhitelist.Pubkeys = append(whitelistCfg.PubkeyWhitelist.Pubkeys, pubkeys...)
}
// Check if the event's kind is whitelisted // Check if the event's kind is whitelisted
if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) { if whitelistCfg.KindWhitelist.Enabled && !IsKindWhitelisted(evt.Kind) {
return false, "not allowed: event kind is not whitelisted" return false, "not allowed: event kind is not whitelisted"
@ -39,23 +28,25 @@ func CheckWhitelist(evt nostr.Event) (bool, string) {
return true, "" return true, ""
} }
// Check if a pubkey or npub is whitelisted // IsPubKeyWhitelisted checks if a pubkey or npub is whitelisted, considering pubkeys from domains.
func IsPubKeyWhitelisted(pubKey string) bool { func IsPubKeyWhitelisted(pubKey string) bool {
cfg := GetWhitelistConfig() cfg := GetWhitelistConfig()
if !cfg.PubkeyWhitelist.Enabled { if cfg == nil || !cfg.PubkeyWhitelist.Enabled {
return true return true // Whitelisting is not enforced if the configuration is missing or disabled
} }
// Check statically defined pubkeys
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys { for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
if pubKey == whitelistedKey { if pubKey == whitelistedKey {
return true return true
} }
} }
// Check statically defined npubs after decoding them to pubkeys
for _, npub := range cfg.PubkeyWhitelist.Npubs { for _, npub := range cfg.PubkeyWhitelist.Npubs {
decodedPubKey, err := utils.DecodeNpub(npub) decodedPubKey, err := utils.DecodeNpub(npub)
if err != nil { if err != nil {
fmt.Println("Error decoding npub:", err) log.Printf("Error decoding npub: %v", err)
continue continue
} }
if pubKey == decodedPubKey { if pubKey == decodedPubKey {
@ -63,6 +54,22 @@ func IsPubKeyWhitelisted(pubKey string) bool {
} }
} }
// 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
}
}
}
return false return false
} }