github.com/shoshinnikita/budget-manager@v0.7.1-0.20220131195411-8c46ff1c6778/internal/web/config.go (about) 1 package web 2 3 import ( 4 "encoding" 5 "errors" 6 "strings" 7 ) 8 9 type Config struct { //nolint:maligned 10 Port int 11 12 // UseEmbed defines whether server should use embedded templates and static files 13 UseEmbed bool 14 15 // EnableProfiling can be used to enable pprof handlers 16 EnableProfiling bool 17 18 // Auth contains auth configuration 19 Auth AuthConfig 20 } 21 22 type AuthConfig struct { 23 // Disable disables auth 24 Disable bool 25 26 // BasicAuthCreds is a list of pairs 'login:password' separated by comma. 27 // Passwords must be hashed using BCrypt 28 BasicAuthCreds Credentials 29 } 30 31 type Credentials map[string]string 32 33 var _ encoding.TextUnmarshaler = (*Credentials)(nil) 34 35 func (c *Credentials) UnmarshalText(text []byte) error { 36 m := make(Credentials) 37 38 pairs := strings.Split(string(text), ",") 39 for _, pair := range pairs { 40 split := strings.Split(pair, ":") 41 if len(split) != 2 { 42 return errors.New("invalid credential pair") 43 } 44 45 login := split[0] 46 password := split[1] 47 if login == "" || password == "" { 48 return errors.New("credentials can't be empty") 49 } 50 51 m[login] = password 52 } 53 54 *c = m 55 56 return nil 57 } 58 59 func (c Credentials) Get(username string) (secret string, ok bool) { 60 secret, ok = c[username] 61 return secret, ok 62 }