From 2a7c24f917bcbeb41f23756c4d7e1ca996e01632 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 15 Nov 2023 20:50:06 -0600 Subject: [PATCH] Moving validation to BlacklistValidator type --- accounts/blacklists.go | 58 +++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/accounts/blacklists.go b/accounts/blacklists.go index c655fd6..d151875 100644 --- a/accounts/blacklists.go +++ b/accounts/blacklists.go @@ -6,19 +6,59 @@ import ( "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, "" } -// UsernameSafe will verify a username is not in the blacklist -func UsernameSafe(username string) bool { - return true != slices.Contains(usernameBlacklist, username) +// 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) +} + +// 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 -- 2.45.2