runLoginFlow initiates a full user-facing login flow, waits for a response and returns the retrieved tokens to the caller when done.
(ctx context.Context, cli *cli, c *management.Client, connName, audience, prompt string, scopes []string, customDomain string, customParams map[string]string)
| 170 | // runLoginFlow initiates a full user-facing login flow, waits for a response |
| 171 | // and returns the retrieved tokens to the caller when done. |
| 172 | func runLoginFlow(ctx context.Context, cli *cli, c *management.Client, connName, audience, prompt string, scopes []string, customDomain string, customParams map[string]string) (*authutil.TokenResponse, error) { |
| 173 | var tokenResponse *authutil.TokenResponse |
| 174 | |
| 175 | err := ansi.Spinner("Waiting for login flow to complete", func() error { |
| 176 | callbackAdded, err := addLocalCallbackURLToClient(ctx, cli.api.Client, c) |
| 177 | if err != nil { |
| 178 | return err |
| 179 | } |
| 180 | |
| 181 | state, err := generateState(cliLoginTestingStateSize) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | |
| 186 | domain := cli.tenant |
| 187 | if customDomain != "" { |
| 188 | domain = customDomain |
| 189 | } |
| 190 | |
| 191 | // Build a login URL and initiate login in a browser window. |
| 192 | loginURL, err := authutil.BuildLoginURL(domain, c.GetClientID(), cliLoginTestingCallbackURL, state, connName, audience, prompt, scopes, customParams) |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | |
| 197 | if cli.noInput { |
| 198 | cli.renderer.Infof("Open the following URL in a browser: %s\n", loginURL) |
| 199 | } else { |
| 200 | if err := browser.OpenURL(loginURL); err != nil { |
| 201 | return err |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Launch a HTTP server to wait for the callback to capture the auth |
| 206 | // code. |
| 207 | authCode, authState, err := authutil.WaitForBrowserCallback(cliLoginTestingCallbackAddr) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | |
| 212 | if state != authState { |
| 213 | return fmt.Errorf("unexpected auth state") |
| 214 | } |
| 215 | |
| 216 | // Once the callback is received, exchange the code for an access |
| 217 | // token. |
| 218 | tokenResponse, err = authutil.ExchangeCodeForToken( |
| 219 | http.DefaultClient, |
| 220 | cli.tenant, |
| 221 | c.GetClientID(), |
| 222 | c.GetClientSecret(), |
| 223 | authCode, |
| 224 | cliLoginTestingCallbackURL, |
| 225 | ) |
| 226 | if err != nil { |
| 227 | return fmt.Errorf("%w", err) |
| 228 | } |
| 229 |
no test coverage detected