oauth2_proxy/validator.go

43 lines
921 B
Go
Raw Normal View History

2012-12-11 01:59:23 +00:00
package main
import (
"encoding/csv"
"fmt"
2012-12-17 18:38:33 +00:00
"log"
"os"
2012-12-11 01:59:23 +00:00
"strings"
)
func NewValidator(domains []string, usersFile string) func(string) bool {
2012-12-11 01:59:23 +00:00
validUsers := make(map[string]bool)
if usersFile != "" {
r, err := os.Open(usersFile)
if err != nil {
log.Fatalf("failed opening -authenticated-emails-file=%v, %s", usersFile, err.Error())
}
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)
2012-12-11 01:59:23 +00:00
}
if !valid {
_, valid = validUsers[email]
}
log.Printf("validating: is %s valid? %v", email, valid)
return valid
}
return validator
}