File size: 1,731 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package basic
import (
"context"
"net/http"
"github.com/pkg/errors"
"github.com/target/goalert/auth"
"github.com/target/goalert/config"
"github.com/target/goalert/util/errutil"
"github.com/target/goalert/util/log"
"github.com/target/goalert/validation/validate"
)
// Info implements the auth.Provider interface.
func (Provider) Info(ctx context.Context) auth.ProviderInfo {
cfg := config.FromContext(ctx)
return auth.ProviderInfo{
Title: "Basic",
Fields: []auth.Field{
{ID: "username", Label: "Username", Required: true},
{ID: "password", Label: "Password", Password: true, Required: true},
},
Enabled: !cfg.Auth.DisableBasic,
}
}
func userPass(req *http.Request) (string, string) {
if req.URL.User == nil {
return req.FormValue("username"), req.FormValue("password")
}
p, _ := req.URL.User.Password()
return req.URL.User.Username(), p
}
// ExtractIdentity implements the auth.IdentityProvider interface, providing identity based
// on the given username and password fields.
func (p *Provider) ExtractIdentity(route *auth.RouteInfo, w http.ResponseWriter, req *http.Request) (*auth.Identity, error) {
ctx := req.Context()
username, password := userPass(req)
err := validate.Username("Username", username)
if err != nil {
return nil, auth.Error("invalid username")
}
ctx = log.WithField(ctx, "username", username)
err = p.lim.Lock(ctx, username)
if errutil.HTTPError(ctx, w, err) {
return nil, err
}
defer p.lim.Unlock(username)
_, err = p.b.Validate(ctx, username, password)
if err != nil {
log.Debug(ctx, errors.Wrap(err, "basic login"))
auth.Delay(ctx)
return nil, auth.Error("unknown username/password")
}
return &auth.Identity{
SubjectID: username,
}, nil
}
|