| 97 | } |
| 98 | |
| 99 | func checkClientIsAuthorizedForAPI(ctx context.Context, cli *cli, client *management.Client, audience, organization string) error { |
| 100 | var list *management.ClientGrantList |
| 101 | if err := ansi.Waiting(func() (err error) { |
| 102 | list, err = cli.api.ClientGrant.List( |
| 103 | ctx, |
| 104 | management.Parameter("audience", audience), |
| 105 | management.Parameter("client_id", client.GetClientID()), |
| 106 | ) |
| 107 | return err |
| 108 | }); err != nil { |
| 109 | return fmt.Errorf( |
| 110 | "failed to find client grants for API identifier %q and client ID %q: %w", |
| 111 | audience, |
| 112 | client.GetClientID(), |
| 113 | err, |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | if len(list.ClientGrants) < 1 { |
| 118 | return fmt.Errorf( |
| 119 | "the %s application is not authorized to request access tokens for this API %s.\n\n"+ |
| 120 | "Run: 'auth0 apps open %s' to open the dashboard and authorize the application", |
| 121 | ansi.Bold(client.GetName()), |
| 122 | ansi.Bold(audience), |
| 123 | client.GetClientID(), |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | grant := list.ClientGrants[0] |
| 128 | if grant.GetOrganizationUsage() == "require" && organization == "" { |
| 129 | return fmt.Errorf( |
| 130 | "the client grant for %s requires an organization.\n\n"+ |
| 131 | "Use the --organization flag to specify one", |
| 132 | ansi.Bold(audience), |
| 133 | ) |
| 134 | } |
| 135 | |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | // runLoginFlowPreflightChecks checks if we need to make any updates |
| 140 | // to the client being tested in order to log in successfully. |