11 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
14// UserMap holds information from the authenticated emails file
20// NewUserMap parses the authenticated emails file into a new UserMap
21func NewUserMap(usersFile string, done <-chan bool, onUpdate func()) *UserMap {
22 um := &UserMap{usersFile: usersFile}
23 m := make(map[string]bool)
24 atomic.StorePointer(&um.m, unsafe.Pointer(&m))
26 logger.Printf("using authenticated emails file %s", usersFile)
27 WatchForUpdates(usersFile, done, func() {
28 um.LoadAuthenticatedEmailsFile()
31 um.LoadAuthenticatedEmailsFile()
36// IsValid checks if an email is allowed
37func (um *UserMap) IsValid(email string) (result bool) {
38 m := *(*map[string]bool)(atomic.LoadPointer(&um.m))
43// LoadAuthenticatedEmailsFile loads the authenticated emails file from disk
44// and parses the contents as CSV
45func (um *UserMap) LoadAuthenticatedEmailsFile() {
46 r, err := os.Open(um.usersFile)
48 logger.Fatalf("failed opening authenticated-emails-file=%q, %s", um.usersFile, err)
51 csvReader := csv.NewReader(r)
53 csvReader.Comment = '#'
54 csvReader.TrimLeadingSpace = true
55 records, err := csvReader.ReadAll()
57 logger.Printf("error reading authenticated-emails-file=%q, %s", um.usersFile, err)
60 updated := make(map[string]bool)
61 for _, r := range records {
62 address := strings.ToLower(strings.TrimSpace(r[0]))
63 updated[address] = true
65 atomic.StorePointer(&um.m, unsafe.Pointer(&updated))
68func newValidatorImpl(domains []string, usersFile string,
69 done <-chan bool, onUpdate func()) func(string) bool {
70 validUsers := NewUserMap(usersFile, done, onUpdate)
73 for i, domain := range domains {
78 domains[i] = fmt.Sprintf("@%s", strings.ToLower(domain))
81 validator := func(email string) (valid bool) {
85 email = strings.ToLower(email)
86 for _, domain := range domains {
87 valid = valid || strings.HasSuffix(email, domain)
90 valid = validUsers.IsValid(email)
100// NewValidator constructs a function to validate email addresses
101func NewValidator(domains []string, usersFile string) func(string) bool {
102 return newValidatorImpl(domains, usersFile, nil, func() {})
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list