grafana/pkg/services/authn/clients/form.go
Karl Persson 2324597d8d
AuthN: Perform login with authn.Service (#61466)
* AuthN: Create password client wrapper and use that on in basic auth
client

* AuthN: fix basic auth client test

* AuthN: Add tests for form authentication

* API: Inject authn service

* Login: If authnService feature flag is enabled use authn login

* Login: Handle token creation errors
2023-01-17 09:11:45 +01:00

43 lines
1.1 KiB
Go

package clients
import (
"context"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/grafana/grafana/pkg/web"
)
var (
errBadForm = errutil.NewBase(errutil.StatusBadRequest, "form-auth.invalid", errutil.WithPublicMessage("bad login data"))
)
var _ authn.Client = new(Form)
func ProvideForm(client authn.PasswordClient) *Form {
return &Form{client}
}
type Form struct {
client authn.PasswordClient
}
type loginForm struct {
Username string `json:"user" binding:"Required"`
Password string `json:"password" binding:"Required"`
}
func (f *Form) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
form := loginForm{}
if err := web.Bind(r.HTTPRequest, &form); err != nil {
return nil, errBadForm.Errorf("failed to parse request: %w", err)
}
return f.client.AuthenticatePassword(ctx, r, form.Username, form.Password)
}
func (f *Form) Test(ctx context.Context, r *authn.Request) bool {
// FIXME: How should we detect this??
// Maybe create client test interface and not all clients has to implement this??
return true
}