loadLoginSourceFiles loads login sources from file system.
(authdPath string, clock func() time.Time)
| 109 | |
| 110 | // loadLoginSourceFiles loads login sources from file system. |
| 111 | func loadLoginSourceFiles(authdPath string, clock func() time.Time) (loginSourceFilesStore, error) { |
| 112 | if !osutil.IsDir(authdPath) { |
| 113 | return &loginSourceFiles{clock: clock}, nil |
| 114 | } |
| 115 | |
| 116 | store := &loginSourceFiles{clock: clock} |
| 117 | return store, filepath.Walk(authdPath, func(path string, info os.FileInfo, err error) error { |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | if path == authdPath || !strings.HasSuffix(path, ".conf") { |
| 123 | return nil |
| 124 | } else if info.IsDir() { |
| 125 | return filepath.SkipDir |
| 126 | } |
| 127 | |
| 128 | authSource, err := ini.Load(path) |
| 129 | if err != nil { |
| 130 | return errors.Wrap(err, "load file") |
| 131 | } |
| 132 | authSource.NameMapper = ini.TitleUnderscore |
| 133 | |
| 134 | // Set general attributes |
| 135 | s := authSource.Section("") |
| 136 | loginSource := &LoginSource{ |
| 137 | ID: s.Key("id").MustInt64(), |
| 138 | Name: s.Key("name").String(), |
| 139 | IsActived: s.Key("is_activated").MustBool(), |
| 140 | IsDefault: s.Key("is_default").MustBool(), |
| 141 | File: &loginSourceFile{ |
| 142 | path: path, |
| 143 | file: authSource, |
| 144 | }, |
| 145 | } |
| 146 | |
| 147 | fi, err := os.Stat(path) |
| 148 | if err != nil { |
| 149 | return errors.Wrap(err, "stat file") |
| 150 | } |
| 151 | loginSource.Updated = fi.ModTime() |
| 152 | |
| 153 | // Parse authentication source file |
| 154 | authType := s.Key("type").String() |
| 155 | cfgSection := authSource.Section("config") |
| 156 | switch authType { |
| 157 | case "ldap_bind_dn": |
| 158 | var cfg ldap.Config |
| 159 | err = cfgSection.MapTo(&cfg) |
| 160 | if err != nil { |
| 161 | return errors.Wrap(err, `map "config" section`) |
| 162 | } |
| 163 | loginSource.Type = auth.LDAP |
| 164 | loginSource.Provider = ldap.NewProvider(false, &cfg) |
| 165 | |
| 166 | case "ldap_simple_auth": |
| 167 | var cfg ldap.Config |
| 168 | err = cfgSection.MapTo(&cfg) |
no test coverage detected