diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 2a61fdc1b..9955fe8e3 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -288,7 +288,7 @@ type ClientCredentials struct { } // GetAccessTokenFromClientCreds generates an access token from client credentials -func GetAccessTokenFromClientCreds(args ClientCredentials) (Result, error) { +func GetAccessTokenFromClientCreds(ctx context.Context, args ClientCredentials) (Result, error) { u, err := url.Parse("https://" + args.Domain) if err != nil { return Result{}, err @@ -305,7 +305,7 @@ func GetAccessTokenFromClientCreds(args ClientCredentials) (Result, error) { }, } - resp, err := credsConfig.Token(context.Background()) + resp, err := credsConfig.Token(ctx) if err != nil { return Result{}, err } diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 927d7223c..c98224b15 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/pflag" "github.com/auth0/auth0-cli/internal/analytics" + "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/auth" "github.com/auth0/auth0-cli/internal/auth0" "github.com/auth0/auth0-cli/internal/buildinfo" @@ -109,11 +110,14 @@ func (t *Tenant) hasExpiredToken() bool { func (t *Tenant) regenerateAccessToken(ctx context.Context, c *cli) error { if t.authenticatedWithClientCredentials() { - token, err := auth.GetAccessTokenFromClientCreds(auth.ClientCredentials{ - ClientID: t.ClientID, - ClientSecret: t.ClientSecret, - Domain: t.Domain, - }) + token, err := auth.GetAccessTokenFromClientCreds( + ctx, + auth.ClientCredentials{ + ClientID: t.ClientID, + ClientSecret: t.ClientSecret, + Domain: t.Domain, + }, + ) if err != nil { return err } @@ -207,18 +211,28 @@ func (c *cli) prepareTenant(ctx context.Context) (Tenant, error) { return Tenant{}, err } - if t.AccessToken == "" || (scopesChanged(t) && t.authenticatedWithDeviceCodeFlow()) { - return RunLoginAsUser(ctx, c, true) + if scopesChanged(t) && t.authenticatedWithDeviceCodeFlow() { + c.renderer.Warnf("Required scopes have changed. Please log in to re-authorize the CLI.\n") + return RunLoginAsUser(ctx, c) } - if !t.hasExpiredToken() { + if t.AccessToken != "" && !t.hasExpiredToken() { return t, nil } if err := t.regenerateAccessToken(ctx, c); err != nil { - // Ask and guide the user through the login process. - c.renderer.Errorf("failed to renew access token, %s", err) - return RunLoginAsUser(ctx, c, true) + if t.authenticatedWithClientCredentials() { + return t, fmt.Errorf( + "failed to fetch access token using client credentials.\n\n"+ + "This may occur if the designated application has been deleted or the client secret has been rotated.\n\n"+ + "Please re-authenticate by running: %s", + ansi.Bold("auth0 login --domain --client-secret "), + ) + } + + c.renderer.Warnf("Failed to renew access token. Please log in to re-authorize the CLI.\n") + + return RunLoginAsUser(ctx, c) } if err := c.addTenant(t); err != nil { diff --git a/internal/cli/login.go b/internal/cli/login.go index 23c5693bf..60a013a4b 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -66,7 +66,13 @@ auth0 login --domain --client-id --client-secret --client-id --client-secret --client-id --client-secret