setupWithAuthentication will fetch the tenant from the config.json and regenerate its access token if needed. The access token will then be used to configure an instance of the Auth0 Management SDK.
(ctx context.Context)
| 57 | // and regenerate its access token if needed. The access token will |
| 58 | // then be used to configure an instance of the Auth0 Management SDK. |
| 59 | func (c *cli) setupWithAuthentication(ctx context.Context) error { |
| 60 | // Validate that we have at least one tenant that we can use. |
| 61 | if err := c.Config.Validate(); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | // If we didn't pass any tenant through the |
| 66 | // flags we're going to use the default one. |
| 67 | if c.tenant == "" { |
| 68 | c.tenant = c.Config.DefaultTenant |
| 69 | } |
| 70 | |
| 71 | // Get the tenant from the config. |
| 72 | tenant, err := c.Config.GetTenant(c.tenant) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | // Check authentication status. |
| 78 | err = tenant.CheckAuthenticationStatus() |
| 79 | var scopesErr config.ErrTokenMissingRequiredScopes |
| 80 | if errors.As(err, &scopesErr) { |
| 81 | c.renderer.Warnf("Required scopes have changed (missing: %s). Please log in to re-authorize the CLI.\n", strings.Join(scopesErr.MissingScopes, ", ")) |
| 82 | tenant, err = RunLoginAsUser(ctx, c, scopesErr.MissingScopes, "") |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if errors.Is(err, config.ErrInvalidToken) { |
| 89 | if tenant.IsAuthenticatedWithDeviceCodeFlow() { |
| 90 | c.renderer.Warnf("Your user login session has expired.") |
| 91 | c.renderer.Warnf("Please log in to re-authorize the CLI.\n") |
| 92 | |
| 93 | // In --no-input mode, fail immediately instead of hanging on an interactive prompt. |
| 94 | if c.noInput { |
| 95 | return fmt.Errorf( |
| 96 | "auth token expired and --no-input is set; run 'auth0 login' to re-authenticate", |
| 97 | ) |
| 98 | } |
| 99 | |
| 100 | // Determine tenant domain for login. |
| 101 | tenantDomain := "" |
| 102 | if c.Config.DefaultTenant != "" { |
| 103 | if prompt.Confirm(fmt.Sprintf("Continue login with default tenant '%s'?", c.Config.DefaultTenant)) { |
| 104 | tenantDomain = c.Config.DefaultTenant |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | tenant, err = RunLoginAsUser(ctx, c, tenant.GetExtraRequestedScopes(), tenantDomain) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | } else if err := tenant.RegenerateAccessToken(ctx); err != nil { |
| 113 | errorMessage := fmt.Errorf( |
| 114 | "failed to fetch access token using client credentials: %w\n\n"+ |
| 115 | "This may occur if the designated Auth0 application has been deleted, "+ |
| 116 | "the client secret has been rotated or previous failure to store client "+ |
no test coverage detected