mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-09-30 03:31:27 +02:00
initial code import
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"log"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user