RunLoginAsMachineSecret facilitates the authentication process using client credentials (client ID, client secret).
(ctx context.Context, inputs LoginInputs, cli *cli, cmd *cobra.Command)
| 361 | |
| 362 | // RunLoginAsMachineSecret facilitates the authentication process using client credentials (client ID, client secret). |
| 363 | func RunLoginAsMachineSecret(ctx context.Context, inputs LoginInputs, cli *cli, cmd *cobra.Command) error { |
| 364 | if err := loginClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { |
| 365 | return err |
| 366 | } |
| 367 | |
| 368 | if err := loginClientSecret.AskPassword(cmd, &inputs.ClientSecret); err != nil { |
| 369 | return err |
| 370 | } |
| 371 | |
| 372 | token, err := auth.GetAccessTokenFromClientCreds( |
| 373 | ctx, |
| 374 | auth.ClientCredentials{ |
| 375 | ClientID: inputs.ClientID, |
| 376 | ClientSecret: inputs.ClientSecret, |
| 377 | Domain: inputs.Domain, |
| 378 | }, |
| 379 | ) |
| 380 | if err != nil { |
| 381 | return fmt.Errorf("failed to fetch access token using client credentials. \n\nEnsure that the provided client-id, client-secret and domain are correct. \n\nerror: %w", err) |
| 382 | } |
| 383 | |
| 384 | if err = keyring.StoreClientSecret(inputs.Domain, inputs.ClientSecret); err != nil { |
| 385 | cli.renderer.Warnf("Could not store the client secret and the access token to the keyring: %s", err) |
| 386 | cli.renderer.Warnf("Expect to login again when your access token expires.") |
| 387 | } |
| 388 | |
| 389 | tenant := config.Tenant{ |
| 390 | Name: strings.Split(inputs.Domain, ".")[0], |
| 391 | Domain: inputs.Domain, |
| 392 | ExpiresAt: token.ExpiresAt, |
| 393 | ClientID: inputs.ClientID, |
| 394 | } |
| 395 | |
| 396 | if err := keyring.StoreAccessToken(inputs.Domain, token.AccessToken); err != nil { |
| 397 | // In case we don't have a keyring, we want the |
| 398 | // access token to be saved in the config file. |
| 399 | tenant.AccessToken = token.AccessToken |
| 400 | } |
| 401 | |
| 402 | if err = cli.Config.AddTenant(tenant); err != nil { |
| 403 | return fmt.Errorf("failed to save tenant data: %w", err) |
| 404 | } |
| 405 | |
| 406 | cli.renderer.Newline() |
| 407 | cli.renderer.Infof("Successfully logged in.") |
| 408 | cli.renderer.Infof("Tenant: %s", inputs.Domain) |
| 409 | |
| 410 | cli.tracker.TrackFirstLogin(cli.Config.InstallID, "As-Machine") |
| 411 | |
| 412 | return nil |
| 413 | } |
| 414 | |
| 415 | // RunLoginAsMachineJWT facilitates the authentication process using the client credentials |
| 416 | // with Private Key JWT authentication flow. (client ID, client Assertion Private key, client Assertion Signing Algorithm). |
no test coverage detected