# HG changeset patch # User Peter Sanchez # Date 1700103006 21600 # Wed Nov 15 20:50:06 2023 -0600 # Node ID 60eda120778e9f9fde1f8fde8f9e7d2f583477ab # Parent 7954bb5dbb2a4f42d3452b9199bf3567c9d42a84 Moving validation to BlacklistValidator type diff --git a/accounts/blacklists.go b/accounts/blacklists.go --- a/accounts/blacklists.go +++ b/accounts/blacklists.go @@ -6,19 +6,59 @@ "golang.org/x/exp/slices" ) -// EmailSafe will verify an email domain is not in the blacklist -func EmailSafe(email string) bool { +// EmailSplit will return the username and domain parts of an email address +func EmailSplit(email string) (string, string) { at := strings.LastIndex(email, "@") - if at <= 0 { - return false + if at > 0 { + return email[:at], email[at+1:] } - domain := email[at+1:] - return true != slices.Contains(emailBlacklist, domain) + return email, "" +} + +// BlacklistValidator is a checker to validate input against blacklists +type BlacklistValidator struct{} + +func (b *BlacklistValidator) checkSafe(listing []string, value string) bool { + return true != slices.Contains(listing, value) +} + +func (b *BlacklistValidator) emailSafe(listing []string, value string) bool { + _, domain := EmailSplit(value) + return b.checkSafe(listing, domain) +} + +// EmailSafe will verify an email domain is not in the default blacklist +func (b *BlacklistValidator) EmailSafe(email string) bool { + return b.emailSafe(emailBlacklist, email) } -// UsernameSafe will verify a username is not in the blacklist -func UsernameSafe(username string) bool { - return true != slices.Contains(usernameBlacklist, username) +// EmailSafePlus will concatenate given list to default `emailBlacklist` and check +// the given email to see if the domain is in either list. +func (b *BlacklistValidator) EmailSafePlus(listing []string, email string) bool { + return b.emailSafe(append(emailBlacklist, listing...), email) +} + +// EmailSafeOnly will only check the given email domain against the provided list. +// This will exclude the default `emailBlacklist` +func (b *BlacklistValidator) EmailSafeOnly(listing []string, email string) bool { + return b.emailSafe(listing, email) +} + +// UsernameSafe will verify a username is not in the default blacklist +func (b *BlacklistValidator) UsernameSafe(username string) bool { + return b.checkSafe(usernameBlacklist, username) +} + +// UsernameSafePlus will concatenate given list to default `usernameBlacklist` and check +// the given email to see if the domain is in either list. +func (b *BlacklistValidator) UsernameSafePlus(listing []string, email string) bool { + return b.checkSafe(append(usernameBlacklist, listing...), email) +} + +// UsernameSafeOnly will only check the given email domain against the provided list. +// This will exclude the default `emailBlacklist` +func (b *BlacklistValidator) UsernameSafeOnly(listing []string, email string) bool { + return b.checkSafe(listing, email) } // https://git.sr.ht/~sircmpwn/meta.sr.ht/tree/master/item/metasrht/blacklist.py