mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
feat: add support for comma list in label domain check
This commit is contained in:
@@ -292,17 +292,17 @@ func ParseSecretFile(contents string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check if a string matches a regex or a whitelist
|
||||
func CheckWhitelist(whitelist string, str string) bool {
|
||||
// Check if the whitelist is empty
|
||||
if len(strings.TrimSpace(whitelist)) == 0 {
|
||||
// Check if a string matches a regex or if it is included in a comma separated list
|
||||
func CheckFilter(filter string, str string, regex bool) bool {
|
||||
// Check if the filter is empty
|
||||
if len(strings.TrimSpace(filter)) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
// Check if the whitelist is a regex
|
||||
if strings.HasPrefix(whitelist, "/") && strings.HasSuffix(whitelist, "/") {
|
||||
// Check if the filter is a regex
|
||||
if strings.HasPrefix(filter, "/") && strings.HasSuffix(filter, "/") && regex {
|
||||
// Create regex
|
||||
re, err := regexp.Compile(whitelist[1 : len(whitelist)-1])
|
||||
re, err := regexp.Compile(filter[1 : len(filter)-1])
|
||||
|
||||
// Check if there was an error
|
||||
if err != nil {
|
||||
@@ -316,11 +316,11 @@ func CheckWhitelist(whitelist string, str string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Split the whitelist by comma
|
||||
whitelistSplit := strings.Split(whitelist, ",")
|
||||
// Split the filter by comma
|
||||
filterSplit := strings.Split(filter, ",")
|
||||
|
||||
// Loop through the whitelist
|
||||
for _, item := range whitelistSplit {
|
||||
// Loop through the filter items
|
||||
for _, item := range filterSplit {
|
||||
// Check if the item matches with the string
|
||||
if strings.TrimSpace(item) == str {
|
||||
return true
|
||||
|
||||
@@ -377,77 +377,77 @@ func TestParseUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Test the whitelist function
|
||||
func TestCheckWhitelist(t *testing.T) {
|
||||
t.Log("Testing check whitelist with a comma whitelist")
|
||||
// Test the check filter function
|
||||
func TestCheckFilter(t *testing.T) {
|
||||
t.Log("Testing check filter with a comma separated list")
|
||||
|
||||
// Create variables
|
||||
whitelist := "user1,user2,user3"
|
||||
filter := "user1,user2,user3"
|
||||
str := "user1"
|
||||
expected := true
|
||||
|
||||
// Test the check whitelist function
|
||||
result := utils.CheckWhitelist(whitelist, str)
|
||||
// Test the check filter function
|
||||
result := utils.CheckFilter(filter, str, false)
|
||||
|
||||
// Check if the result is equal to the expected
|
||||
if result != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
t.Log("Testing check whitelist with a regex whitelist")
|
||||
t.Log("Testing check filter with a regex filter")
|
||||
|
||||
// Create variables
|
||||
whitelist = "/^user[0-9]+$/"
|
||||
filter = "/^user[0-9]+$/"
|
||||
str = "user1"
|
||||
expected = true
|
||||
|
||||
// Test the check whitelist function
|
||||
result = utils.CheckWhitelist(whitelist, str)
|
||||
// Test the check filter function
|
||||
result = utils.CheckFilter(filter, str, true)
|
||||
|
||||
// Check if the result is equal to the expected
|
||||
if result != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
t.Log("Testing check whitelist with an empty whitelist")
|
||||
t.Log("Testing check filter with an empty filter")
|
||||
|
||||
// Create variables
|
||||
whitelist = ""
|
||||
filter = ""
|
||||
str = "user1"
|
||||
expected = true
|
||||
|
||||
// Test the check whitelist function
|
||||
result = utils.CheckWhitelist(whitelist, str)
|
||||
// Test the check filter function
|
||||
result = utils.CheckFilter(filter, str, false)
|
||||
|
||||
// Check if the result is equal to the expected
|
||||
if result != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
t.Log("Testing check whitelist with an invalid regex whitelist")
|
||||
t.Log("Testing check filter with an invalid regex filter")
|
||||
|
||||
// Create variables
|
||||
whitelist = "/^user[0-9+$/"
|
||||
filter = "/^user[0-9+$/"
|
||||
str = "user1"
|
||||
expected = false
|
||||
|
||||
// Test the check whitelist function
|
||||
result = utils.CheckWhitelist(whitelist, str)
|
||||
// Test the check filter function
|
||||
result = utils.CheckFilter(filter, str, true)
|
||||
|
||||
// Check if the result is equal to the expected
|
||||
if result != expected {
|
||||
t.Fatalf("Expected %v, got %v", expected, result)
|
||||
}
|
||||
|
||||
t.Log("Testing check whitelist with a non matching whitelist")
|
||||
t.Log("Testing check filter with a non matching list")
|
||||
|
||||
// Create variables
|
||||
whitelist = "user1,user2,user3"
|
||||
filter = "user1,user2,user3"
|
||||
str = "user4"
|
||||
expected = false
|
||||
|
||||
// Test the check whitelist function
|
||||
result = utils.CheckWhitelist(whitelist, str)
|
||||
// Test the check filter function
|
||||
result = utils.CheckFilter(filter, str, false)
|
||||
|
||||
// Check if the result is equal to the expected
|
||||
if result != expected {
|
||||
|
||||
Reference in New Issue
Block a user