fix(common.oauth): Initialize 'EndpointParams' to avoid panic with 'audience' settings (#14331)

This commit is contained in:
irving 2023-11-27 16:14:55 +02:00 committed by GitHub
parent 062ccb3086
commit 0e591ea8cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package oauth
import ( import (
"context" "context"
"net/http" "net/http"
"net/url"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials" "golang.org/x/oauth2/clientcredentials"
@ -23,10 +24,11 @@ func (o *OAuth2Config) CreateOauth2Client(ctx context.Context, client *http.Clie
} }
oauthConfig := clientcredentials.Config{ oauthConfig := clientcredentials.Config{
ClientID: o.ClientID, ClientID: o.ClientID,
ClientSecret: o.ClientSecret, ClientSecret: o.ClientSecret,
TokenURL: o.TokenURL, TokenURL: o.TokenURL,
Scopes: o.Scopes, Scopes: o.Scopes,
EndpointParams: make(url.Values),
} }
if o.Audience != "" { if o.Audience != "" {

View File

@ -499,6 +499,34 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}, },
}, },
{
name: "audience",
plugin: &HTTP{
URL: u.String() + "/write",
HTTPClientConfig: httpconfig.HTTPClientConfig{
OAuth2Config: oauth.OAuth2Config{
ClientID: "howdy",
ClientSecret: "secret",
TokenURL: u.String() + "/token",
Scopes: []string{"urn:opc:idm:__myscopes__"},
Audience: "audience",
},
},
},
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
values := url.Values{}
values.Add("access_token", token)
values.Add("token_type", "bearer")
values.Add("expires_in", "3600")
_, err = w.Write([]byte(values.Encode()))
require.NoError(t, err)
},
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
require.Equal(t, []string{"Bearer " + token}, r.Header["Authorization"])
w.WriteHeader(http.StatusOK)
},
},
} }
for _, tt := range tests { for _, tt := range tests {