git.iko.soy/typeable/oauth2-proxy
on master help sign in
Make address/domain comparisons case-insensitive
commit781f34e8174952fb468f679fb7ff6c105e8f6a8d authorMike Bland <michael.bland@gsa.gov> dateTue 7 Apr 2015, 17:25 (12 years ago) parent9534808
2 files changed+46−1
validator.go+6−1 validator_test.go+40
validator.go+6−1
@@ -23,11 +23,16 @@ func NewValidator(domains []string, usersFile string) func(string) bool {
2323 csv_reader.TrimLeadingSpace = true
2424 records, err := csv_reader.ReadAll()
2525 for _, r := range records {
26− validUsers[r[0]] = true
26+ validUsers[strings.ToLower(r[0])] = true
2727 }
2828 }
2929
30+ for i, domain := range domains {
31+ domains[i] = strings.ToLower(domain)
32+ }
33+
3034 validator := func(email string) bool {
35+ email = strings.ToLower(email)
3136 valid := false
3237 for _, domain := range domains {
3338 emailSuffix := fmt.Sprintf("@%s", domain)
validator_test.gonew file+40
@@ -0,0 +1,40 @@
1+package main
2+
3+import (
4+ "io/ioutil"
5+ "os"
6+ "strings"
7+ "testing"
8+)
9+
10+func TestValidatorComparisonsAreCaseInsensitive(t *testing.T) {
11+ auth_email_file, err := ioutil.TempFile("", "test_auth_emails_")
12+ if err != nil {
13+ t.Fatal("failed to create temp file: " + err.Error())
14+ }
15+ defer os.Remove(auth_email_file.Name())
16+
17+ auth_email_file.WriteString(
18+ strings.Join([]string{"Foo.Bar@Example.Com"}, "\n"))
19+ err = auth_email_file.Close()
20+ if err != nil {
21+ t.Fatal("failed to close temp file " + auth_email_file.Name() +
22+ ": " + err.Error())
23+ }
24+
25+ domains := []string{"Frobozz.Com"}
26+ validator := NewValidator(domains, auth_email_file.Name())
27+
28+ if !validator("foo.bar@example.com") {
29+ t.Error("loaded email addresses are not lower-cased")
30+ }
31+ if !validator("Foo.Bar@Example.Com") {
32+ t.Error("validated email addresses are not lower-cased")
33+ }
34+ if !validator("foo.bar@frobozz.com") {
35+ t.Error("loaded domains are not lower-cased")
36+ }
37+ if !validator("foo.bar@Frobozz.Com") {
38+ t.Error("validated domains are not lower-cased")
39+ }
40+}