Skip to content

Add bearer support for authorization headers #19775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/auth/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const (

tokenTypeSimple = "simple"
tokenTypeJWT = "jwt"

bearerPrefix = "Bearer "
)

type AuthInfo struct {
Expand Down Expand Up @@ -1072,6 +1074,12 @@ func (as *authStore) AuthInfoFromCtx(ctx context.Context) (*AuthInfo, error) {
}

token := ts[0]

// support authorization headers with bearer prefix.
if strings.HasPrefix(token, bearerPrefix) {
token = strings.Split(token, bearerPrefix)[1]
}

authInfo, uok := as.authInfoFromToken(ctx, token)
if !uok {
as.lg.Warn("invalid auth token", zap.String("token", token))
Expand Down
27 changes: 27 additions & 0 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,30 @@ func TestAuthInfoFromCtx(t *testing.T) {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Bearer"}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: "Invalid.Token"}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: bearerPrefix + "Invalid.Token"}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: bearerPrefix}))
_, err = as.AuthInfoFromCtx(ctx)
if !errors.Is(err, ErrInvalidAuthToken) {
t.Errorf("expected %v, got %v", ErrInvalidAuthToken, err)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: resp.Token}))
ai, err = as.AuthInfoFromCtx(ctx)
if err != nil {
Expand All @@ -850,6 +868,15 @@ func TestAuthInfoFromCtx(t *testing.T) {
if ai.Username != "foo" {
t.Errorf("expected %v, got %v", "foo", ai.Username)
}

ctx = metadata.NewIncomingContext(t.Context(), metadata.New(map[string]string{rpctypes.TokenFieldNameGRPC: bearerPrefix + resp.Token}))
ai, err = as.AuthInfoFromCtx(ctx)
if err != nil {
t.Error(err)
}
if ai.Username != "foo" {
t.Errorf("expected %v, got %v", "foo", ai.Username)
}
}

func TestAuthDisable(t *testing.T) {
Expand Down