Skip to content
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

[Fix] PulsarCtl 1266 Oauth2 Client credentials flow use scopes from the keyfile as well #1244

21 changes: 18 additions & 3 deletions oauth2/client_credentials_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package oauth2

import (
"net/http"

"github.com/apache/pulsar-client-go/oauth2/clock"
"net/http"
"strings"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -68,7 +68,6 @@ func newClientCredentialsFlow(
// NewDefaultClientCredentialsFlow provides an easy way to build up a default
// client credentials flow with all the correct configuration.
func NewDefaultClientCredentialsFlow(options ClientCredentialsFlowOptions) (*ClientCredentialsFlow, error) {

credsProvider := NewClientCredentialsProviderFromKeyFile(options.KeyFile)
keyFile, err := credsProvider.GetClientCredentials()
if err != nil {
Expand All @@ -82,6 +81,22 @@ func NewDefaultClientCredentialsFlow(options ClientCredentialsFlowOptions) (*Cli

tokenRetriever := NewTokenRetriever(&http.Client{})

// Merge the scopes of the options AdditionalScopes with the scopes read from the keyFile config
var scopesToAdd []string
if len(options.AdditionalScopes) > 0 {
for _, scope := range options.AdditionalScopes {
scopesToAdd = append(scopesToAdd, scope)
}
}

if keyFile.Scope != "" {
scopesSplit := strings.Split(keyFile.Scope, " ")
for _, scope := range scopesSplit {
scopesToAdd = append(scopesToAdd, scope)
}
}
options.AdditionalScopes = scopesToAdd

return newClientCredentialsFlow(
options,
keyFile,
Expand Down
1 change: 1 addition & 0 deletions oauth2/client_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type KeyFile struct {
ClientSecret string `json:"client_secret"`
ClientEmail string `json:"client_email"`
IssuerURL string `json:"issuer_url"`
Scope string `json:"scope"`
}

func NewClientCredentialsProviderFromKeyFile(keyFile string) *KeyFileProvider {
Expand Down
2 changes: 2 additions & 0 deletions oauth2/client_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func TestNewClientCredentialsProviderFromKeyFile(t *testing.T) {
ClientSecret := "CLIENT_SECRET"
ClientEmail := "CLIENT_EMAIL"
IssuerURL := "ISSUER_URL"
Scope := "SCOPE"
keyFile := &KeyFile{
Type: oauthType,
ClientID: clientID,
ClientSecret: ClientSecret,
ClientEmail: ClientEmail,
IssuerURL: IssuerURL,
Scope: Scope,
RobertIndie marked this conversation as resolved.
Show resolved Hide resolved
}

b, err := json.Marshal(keyFile)
Expand Down
Loading