forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
43 lines (39 loc) · 974 Bytes
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strings"
)
func NewValidator(domains []string, usersFile string) func(string) bool {
validUsers := make(map[string]bool)
if usersFile != "" {
log.Printf("using authenticated emails file %s", usersFile)
r, err := os.Open(usersFile)
if err != nil {
log.Fatalf("failed opening authenticated-emails-file=%q, %s", usersFile, err)
}
csv_reader := csv.NewReader(r)
csv_reader.Comma = ','
csv_reader.Comment = '#'
csv_reader.TrimLeadingSpace = true
records, err := csv_reader.ReadAll()
for _, r := range records {
validUsers[r[0]] = true
}
}
validator := func(email string) bool {
valid := false
for _, domain := range domains {
emailSuffix := fmt.Sprintf("@%s", domain)
valid = valid || strings.HasSuffix(email, emailSuffix)
}
if !valid {
_, valid = validUsers[email]
}
log.Printf("validating: is %s valid? %v", email, valid)
return valid
}
return validator
}