| 14 | ) |
| 15 | |
| 16 | func NewStaticAuth(param_url *url.URL, logger *clog.CondLogger) (*BasicAuth, error) { |
| 17 | values, err := url.ParseQuery(param_url.RawQuery) |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | username := values.Get("username") |
| 22 | if username == "" { |
| 23 | return nil, errors.New("\"username\" parameter is missing from auth config URI") |
| 24 | } |
| 25 | password := values.Get("password") |
| 26 | if password == "" { |
| 27 | return nil, errors.New("\"password\" parameter is missing from auth config URI") |
| 28 | } |
| 29 | hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost) |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | buf := bytes.NewBufferString(username) |
| 34 | buf.WriteByte(':') |
| 35 | buf.Write(hashedPassword) |
| 36 | |
| 37 | f, err := htpasswd.NewFromReader(buf, htpasswd.DefaultSystems, func(parseError error) { |
| 38 | logger.Error("static auth: password entry parse error: %v", err) |
| 39 | }) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("can't instantiate pwFile: %w", err) |
| 42 | } |
| 43 | |
| 44 | ba := &BasicAuth{ |
| 45 | hiddenDomain: strings.ToLower(values.Get("hidden_domain")), |
| 46 | logger: logger, |
| 47 | stopChan: make(chan struct{}), |
| 48 | } |
| 49 | ba.pw.Store(&pwFile{file: f}) |
| 50 | if nextAuth := values.Get("else"); nextAuth != "" { |
| 51 | nap, err := NewAuth(nextAuth, logger) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("chained auth provider construction failed: %w", err) |
| 54 | } |
| 55 | ba.next = nap |
| 56 | } |
| 57 | return ba, nil |
| 58 | } |