~netlandish/gobwebs

2a7c24f917bcbeb41f23756c4d7e1ca996e01632 — Peter Sanchez 1 year, 10 days ago c5b01fe
Moving validation to BlacklistValidator type
1 files changed, 49 insertions(+), 9 deletions(-)

M accounts/blacklists.go
M accounts/blacklists.go => accounts/blacklists.go +49 -9
@@ 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