mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 16:15:42 -06:00
95ea4bad6f
* API: Add reqSignedIn to router groups * AuthN: Add fall through in context handler * AuthN: Add IsAnonymous field * AuthN: add priority to context aware clients * ContextHandler: Add comment * AuthN: Add a simple priority queue * AuthN: Add Name to client interface * AuthN: register clients with function * AuthN: update mock and fake to implement interface * AuthN: rewrite test without reflection * AuthN: add comment * AuthN: fix queue insert * AuthN: rewrite tests * AuthN: make the queue generic so we can reuse it for hooks * ContextHandler: Add fixme for auth headers * AuthN: remove unused variable * AuthN: use multierror * AuthN: write proper tests for queue * AuthN: Add queue item that can store the value and priority Co-authored-by: Jo <joao.guerreiro@grafana.com>
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package clients
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
|
)
|
|
|
|
var (
|
|
errDecodingBasicAuthHeader = errutil.NewBase(errutil.StatusBadRequest, "basic-auth.invalid-header", errutil.WithPublicMessage("Invalid Basic Auth Header"))
|
|
)
|
|
|
|
var _ authn.ContextAwareClient = new(Basic)
|
|
|
|
func ProvideBasic(client authn.PasswordClient) *Basic {
|
|
return &Basic{client}
|
|
}
|
|
|
|
type Basic struct {
|
|
client authn.PasswordClient
|
|
}
|
|
|
|
func (c *Basic) Name() string {
|
|
return authn.ClientBasic
|
|
}
|
|
|
|
func (c *Basic) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
|
|
username, password, err := util.DecodeBasicAuthHeader(getBasicAuthHeaderFromRequest(r))
|
|
if err != nil {
|
|
return nil, errDecodingBasicAuthHeader.Errorf("failed to decode basic auth header: %w", err)
|
|
}
|
|
|
|
return c.client.AuthenticatePassword(ctx, r, username, password)
|
|
}
|
|
|
|
func (c *Basic) Test(ctx context.Context, r *authn.Request) bool {
|
|
return looksLikeBasicAuthRequest(r)
|
|
}
|
|
|
|
func (c *Basic) Priority() uint {
|
|
return 40
|
|
}
|
|
|
|
func looksLikeBasicAuthRequest(r *authn.Request) bool {
|
|
return getBasicAuthHeaderFromRequest(r) != ""
|
|
}
|
|
|
|
func getBasicAuthHeaderFromRequest(r *authn.Request) string {
|
|
if r.HTTPRequest == nil {
|
|
return ""
|
|
}
|
|
|
|
header := r.HTTPRequest.Header.Get(authorizationHeaderName)
|
|
if header == "" {
|
|
return ""
|
|
}
|
|
|
|
if !strings.HasPrefix(header, basicPrefix) {
|
|
return ""
|
|
}
|
|
|
|
return header
|
|
}
|