runClientCredentialsFlow runs an M2M client credentials flow without opening a browser.
( ctx context.Context, cli *cli, client *management.Client, audience string, tenantDomain string, organization string, )
| 62 | // runClientCredentialsFlow runs an M2M client |
| 63 | // credentials flow without opening a browser. |
| 64 | func runClientCredentialsFlow( |
| 65 | ctx context.Context, |
| 66 | cli *cli, |
| 67 | client *management.Client, |
| 68 | audience string, |
| 69 | tenantDomain string, |
| 70 | organization string, |
| 71 | ) (*authutil.TokenResponse, error) { |
| 72 | if err := checkClientIsAuthorizedForAPI(ctx, cli, client, audience, organization); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | |
| 76 | tokenURL := BuildOauthTokenURL(tenantDomain) |
| 77 | payload := BuildOauthTokenParams(client.GetClientID(), client.GetClientSecret(), audience, organization) |
| 78 | |
| 79 | var tokenResponse *authutil.TokenResponse |
| 80 | err := ansi.Spinner("Waiting for token", func() error { |
| 81 | response, err := http.PostForm(tokenURL, payload) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | defer func() { |
| 86 | _ = response.Body.Close() |
| 87 | }() |
| 88 | |
| 89 | if err = json.NewDecoder(response.Body).Decode(&tokenResponse); err != nil { |
| 90 | return fmt.Errorf("failed to decode the response: %w", err) |
| 91 | } |
| 92 | |
| 93 | return nil |
| 94 | }) |
| 95 | |
| 96 | return tokenResponse, err |
| 97 | } |
| 98 | |
| 99 | func checkClientIsAuthorizedForAPI(ctx context.Context, cli *cli, client *management.Client, audience, organization string) error { |
| 100 | var list *management.ClientGrantList |
no test coverage detected