go-auth0 icon indicating copy to clipboard operation
go-auth0 copied to clipboard

Add WithClientCredentialsAndTokenURL option

Open medev21 opened this issue 1 year ago • 2 comments

Checklist

Describe the problem you'd like to have solved

We are trying to use the SDK for Auth0 API operations, but running into issues with the tokens.

We realized that in your code, you're using the uri to generate the token url by concatenating the domain with oauth/token. Unfortunately for us, the token url is different than our auth0 domain; not sure why this is the case.

So, the option WithClientCredentials will not work. We have tried to use WithClient option by generating our own http.Client and pass it to that option.

func generateCilent() *http.Client {
    ctx := context.Background()
   
    conf := clientCredentials.Config{
        ClientId: "client id",
        ClientSecret: "secret",
        TokenURL:  "token url",
        EndpointParams: "params goes here",
        AuthStyle: "oauth style"
    }

    return conf.Client(ctx)
}

We were expecting it to work but unfortunately it did not, we came across with this error.

oauth2: Transport's Source is nil

We took a look under hood and saw that WithClient option only updates the m.http, so the m.tokenSource remains empty; we think.

We used WithStaticToken, which it works but that's assuming the token never expires. We want to refresh the tokens.

Describe the ideal solution

Ideally, we would like another option to the tokenURL, so something like this

WithClientCredentialsAndTokenURL(clientID string, clientSecret string, tokenURL string) management.Option

so this tokenURL field will be used to update the TokenURL under this function, https://github.com/auth0/go-auth0/blob/main/internal/client/client.go#L223

func OAuth2ClientCredentialsAndAudience(
	ctx context.Context,
	uri,
	clientID,
	clientSecret,
	audience string,
) oauth2.TokenSource {
	cfg := &clientcredentials.Config{
		ClientID:     clientID,
		ClientSecret: clientSecret,
		TokenURL:     uri + "/oauth/token",
		EndpointParams: url.Values{
			"audience": []string{audience},
		},
	}

	return cfg.TokenSource(ctx)
}

This is an idea from what we have seen from your code, but the main point is to provide an alternative to pass a token URL that's different the Auth0 domain.

Alternatives and current workarounds

No response

Additional context

No response

medev21 avatar Mar 21 '23 16:03 medev21