AuthN: Use BasicAuth from http request (#62792)

AuthN: use BasicAuth from http request
This commit is contained in:
Karl Persson 2023-02-03 09:11:53 +01:00 committed by GitHub
parent 48e0ab2142
commit ad068ed533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,8 @@ 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"
)
@ -28,9 +26,9 @@ func (c *Basic) Name() string {
}
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)
username, password, ok := getBasicAuthFromRequest(r)
if !ok {
return nil, errDecodingBasicAuthHeader.Errorf("failed to decode basic auth header")
}
return c.client.AuthenticatePassword(ctx, r, username, password)
@ -45,22 +43,14 @@ func (c *Basic) Priority() uint {
}
func looksLikeBasicAuthRequest(r *authn.Request) bool {
return getBasicAuthHeaderFromRequest(r) != ""
_, _, ok := getBasicAuthFromRequest(r)
return ok
}
func getBasicAuthHeaderFromRequest(r *authn.Request) string {
func getBasicAuthFromRequest(r *authn.Request) (string, string, bool) {
if r.HTTPRequest == nil {
return ""
return "", "", false
}
header := r.HTTPRequest.Header.Get(authorizationHeaderName)
if header == "" {
return ""
}
if !strings.HasPrefix(header, basicPrefix) {
return ""
}
return header
return r.HTTPRequest.BasicAuth()
}