git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #83 from 18F/case-insensitive-comparisons
78e080eJehiah Czebotar
validator.go+6−1
@@ -23,11 +23,16 @@ func NewValidator(domains []string, usersFile string) func(string) bool {
23 csv_reader.TrimLeadingSpace = true
23 csv_reader.TrimLeadingSpace = true
24 records, err := csv_reader.ReadAll()
24 records, err := csv_reader.ReadAll()
25 for _, r := range records {
25 for _, r := range records {
26− validUsers[r[0]] = true
26+ validUsers[strings.ToLower(r[0])] = true
27 }
27 }
28 }
28 }
29
29
30+ for i, domain := range domains {
31+ domains[i] = strings.ToLower(domain)
32+ }
33+
30 validator := func(email string) bool {
34 validator := func(email string) bool {
35+ email = strings.ToLower(email)
31 valid := false
36 valid := false
32 for _, domain := range domains {
37 for _, domain := range domains {
33 emailSuffix := fmt.Sprintf("@%s", domain)
38 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+}