-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from tahoward/email-addresses
sso-auth: add provider for individual e-mail address authentication
- Loading branch information
Showing
9 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package options | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// NewEmailAddressValidator returns a function that checks whether a given email is valid based on a list | ||
// of email addresses. The address "*" is a wild card that matches any non-empty email. | ||
func NewEmailAddressValidator(emails []string) func(string) bool { | ||
allowAll := false | ||
for i, email := range emails { | ||
if email == "*" { | ||
allowAll = true | ||
} | ||
emails[i] = fmt.Sprintf("%s", strings.ToLower(email)) | ||
} | ||
|
||
if allowAll { | ||
return func(email string) bool { return email != "" } | ||
} | ||
|
||
return func(email string) bool { | ||
if email == "" { | ||
return false | ||
} | ||
email = strings.ToLower(email) | ||
for _, emailItem := range emails { | ||
if email == emailItem { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package options | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestEmailAddressValidatorValidator(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
domains []string | ||
email string | ||
expectValid bool | ||
}{ | ||
{ | ||
name: "nothing should validate when address list is empty", | ||
domains: []string(nil), | ||
email: "foo@example.com", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "single address validation", | ||
domains: []string{"foo@example.com"}, | ||
email: "foo@example.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "substring matches are rejected", | ||
domains: []string{"foo@example.com"}, | ||
email: "foo@hackerexample.com", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "no subdomain rollup happens", | ||
domains: []string{"foo@example.com"}, | ||
email: "foo@bar.example.com", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "multiple address validation still rejects other addresses", | ||
domains: []string{"foo@abc.com", "foo@xyz.com"}, | ||
email: "foo@example.com", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "multiple address validation still accepts emails from either address", | ||
domains: []string{"foo@abc.com", "foo@xyz.com"}, | ||
email: "foo@abc.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "multiple address validation still rejects other addresses", | ||
domains: []string{"foo@abc.com", "bar@xyz.com"}, | ||
email: "bar@xyz.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "comparisons are case insensitive", | ||
domains: []string{"Foo@Example.Com"}, | ||
email: "foo@example.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "comparisons are case insensitive", | ||
domains: []string{"Foo@Example.Com"}, | ||
email: "foo@EXAMPLE.COM", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "comparisons are case insensitive", | ||
domains: []string{"foo@example.com"}, | ||
email: "foo@ExAmPlE.CoM", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "single wildcard allows all", | ||
domains: []string{"*"}, | ||
email: "foo@example.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "single wildcard allows all", | ||
domains: []string{"*"}, | ||
email: "bar@gmail.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "wildcard in list allows all", | ||
domains: []string{"foo@example.com", "*"}, | ||
email: "foo@example.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "wildcard in list allows all", | ||
domains: []string{"foo@example.com", "*"}, | ||
email: "foo@gmail.com", | ||
expectValid: true, | ||
}, | ||
{ | ||
name: "empty email rejected", | ||
domains: []string{"foo@example.com"}, | ||
email: "", | ||
expectValid: false, | ||
}, | ||
{ | ||
name: "wildcard still rejects empty emails", | ||
domains: []string{"*"}, | ||
email: "", | ||
expectValid: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
emailValidator := NewEmailAddressValidator(tc.domains) | ||
valid := emailValidator(tc.email) | ||
if valid != tc.expectValid { | ||
t.Fatalf("expected %v, got %v", tc.expectValid, valid) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters