1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2024-11-24 08:52:25 +02:00
oauth2-proxy/validator.go
Jehiah Czebotar 42f539109e testing
2012-12-17 13:38:33 -05:00

47 lines
939 B
Go

package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strings"
)
func NewValidator(domain string, usersFile string) func(string) bool {
validUsers := make(map[string]bool)
emailSuffix := ""
if domain != "" {
emailSuffix = fmt.Sprintf("@%s", domain)
}
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 {
var valid bool
if emailSuffix != "" {
valid = strings.HasSuffix(email, emailSuffix)
}
if !valid {
_, valid = validUsers[email]
}
log.Printf("validating: is %s valid? %v", email, valid)
return valid
}
return validator
}