mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
Merge pull request #94803 from azush26/modify-bearertoken
Limit the max number of splitting.
This commit is contained in:
commit
0c3286e135
@ -39,7 +39,7 @@ func (a *Authenticator) AuthenticateRequest(req *http.Request) (*authenticator.R
|
|||||||
if auth == "" {
|
if auth == "" {
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
parts := strings.Split(auth, " ")
|
parts := strings.SplitN(auth, " ", 3)
|
||||||
if len(parts) < 2 || strings.ToLower(parts[0]) != "bearer" {
|
if len(parts) < 2 || strings.ToLower(parts[0]) != "bearer" {
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,28 @@ func TestAuthenticateRequest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAuthenticateRequestIncludingValueAfterToken(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
Req *http.Request
|
||||||
|
}{
|
||||||
|
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a"}}}},
|
||||||
|
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a b c"}}}},
|
||||||
|
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a"}}}},
|
||||||
|
}
|
||||||
|
for i, testCase := range testCases {
|
||||||
|
auth := New(authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) {
|
||||||
|
if token != "token" {
|
||||||
|
t.Errorf("unexpected token: %s", token)
|
||||||
|
}
|
||||||
|
return &authenticator.Response{User: &user.DefaultInfo{Name: "user"}}, true, nil
|
||||||
|
}))
|
||||||
|
resp, ok, err := auth.AuthenticateRequest(testCase.Req)
|
||||||
|
if !ok || resp == nil || err != nil {
|
||||||
|
t.Errorf("%d: expected valid user", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAuthenticateRequestTokenInvalid(t *testing.T) {
|
func TestAuthenticateRequestTokenInvalid(t *testing.T) {
|
||||||
auth := New(authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) {
|
auth := New(authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) {
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user