|
15 | 15 | */ |
16 | 16 | package com.okta.cli.commands; |
17 | 17 |
|
18 | | -import com.okta.cli.common.service.DefaultSdkConfigurationService; |
19 | | -import com.okta.cli.common.service.SdkConfigurationService; |
| 18 | +import com.okta.cli.common.model.OktaProfile; |
| 19 | +import com.okta.cli.common.service.DefaultProfileConfigurationService; |
| 20 | +import com.okta.cli.common.service.ProfileConfigurationService; |
20 | 21 | import com.okta.cli.console.ConsoleOutput; |
21 | 22 | import com.okta.commons.configcheck.ConfigurationValidator; |
22 | | -import com.okta.commons.lang.Strings; |
23 | | -import com.okta.sdk.impl.config.ClientConfiguration; |
24 | 23 | import picocli.CommandLine; |
25 | 24 |
|
| 25 | +import java.io.File; |
| 26 | +import java.util.Optional; |
| 27 | + |
26 | 28 | @CommandLine.Command(name = "login", |
27 | 29 | description = "Authorizes the Okta CLI tool") |
28 | 30 | public class Login extends BaseCommand { |
29 | 31 |
|
| 32 | + @CommandLine.Option(names = {"--profile-name"}, description = "Name for this profile (e.g., 'acme-corp', 'dev-tenant')") |
| 33 | + private String profileName; |
| 34 | + |
30 | 35 | @Override |
31 | 36 | public int runCommand() throws Exception { |
32 | 37 |
|
33 | | - // check if okta client config exists? |
34 | | - SdkConfigurationService sdkConfigurationService = new DefaultSdkConfigurationService(); |
35 | | - ClientConfiguration clientConfiguration = sdkConfigurationService.loadUnvalidatedConfiguration(); |
36 | | - String orgUrl = clientConfiguration.getBaseUrl(); |
| 38 | + ProfileConfigurationService profileService = new DefaultProfileConfigurationService(); |
| 39 | + File configFile = getEnvironment().getOktaPropsFile(); |
| 40 | + |
| 41 | + // Migrate legacy format if needed |
| 42 | + if (((DefaultProfileConfigurationService) profileService).isLegacyFormat(configFile)) { |
| 43 | + ((DefaultProfileConfigurationService) profileService).migrateFromLegacyFormat(configFile); |
| 44 | + } |
| 45 | + |
| 46 | + // Determine profile name: --profile-name flag > --profile flag > prompt |
| 47 | + String targetProfile = profileName; |
| 48 | + if (targetProfile == null) { |
| 49 | + String envProfile = getEnvironment().getProfile(); |
| 50 | + if (!ProfileConfigurationService.DEFAULT_PROFILE_NAME.equals(envProfile)) { |
| 51 | + targetProfile = envProfile; |
| 52 | + } |
| 53 | + } |
37 | 54 |
|
38 | 55 | try (ConsoleOutput out = getConsoleOutput()) { |
39 | | - // prompt user to overwrite config file |
40 | | - if (Strings.hasText(orgUrl) |
41 | | - && !configQuestions().isOverwriteExistingConfig(orgUrl, getEnvironment().getOktaPropsFile().getAbsolutePath())) { |
42 | | - return 0; |
| 56 | + // Prompt for profile name if not provided |
| 57 | + if (targetProfile == null) { |
| 58 | + targetProfile = getPrompter().promptUntilIfEmpty(null, "Profile name", ProfileConfigurationService.DEFAULT_PROFILE_NAME); |
43 | 59 | } |
44 | 60 |
|
45 | | - // prompt for Base URL |
46 | | - orgUrl = getPrompter().promptUntilValue("Okta Org URL"); |
| 61 | + // Check if profile already exists |
| 62 | + Optional<OktaProfile> existingProfile = profileService.getProfile(configFile, targetProfile); |
| 63 | + if (existingProfile.isPresent()) { |
| 64 | + out.writeLine("Profile '" + targetProfile + "' already exists with org: " + existingProfile.get().getOrgUrl()); |
| 65 | + if (!getPrompter().promptYesNo("Overwrite this profile?")) { |
| 66 | + return 0; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Prompt for Okta Org URL |
| 71 | + String orgUrl = getPrompter().promptUntilValue("Okta Org URL"); |
47 | 72 | ConfigurationValidator.assertOrgUrl(orgUrl); |
48 | 73 |
|
| 74 | + // Prompt for API token |
49 | 75 | out.writeLine("Enter your Okta API token, for more information see: https://bit.ly/get-okta-api-token"); |
50 | 76 | String apiToken = getPrompter().promptUntilValue(null, "Okta API token"); |
51 | 77 | ConfigurationValidator.assertApiToken(apiToken); |
52 | 78 |
|
53 | | - sdkConfigurationService.writeOktaYaml(orgUrl, apiToken, getEnvironment().getOktaPropsFile()); |
| 79 | + // Determine if this should be the active profile |
| 80 | + boolean setAsActive = true; |
| 81 | + String currentActive = profileService.getActiveProfileName(configFile); |
| 82 | + if (!currentActive.equals(targetProfile) && profileService.profileExists(configFile, currentActive)) { |
| 83 | + setAsActive = getPrompter().promptYesNo("Set '" + targetProfile + "' as the active profile?", true); |
| 84 | + } |
| 85 | + |
| 86 | + // Save the profile |
| 87 | + OktaProfile newProfile = new OktaProfile(targetProfile, orgUrl, apiToken); |
| 88 | + profileService.saveProfile(configFile, newProfile, setAsActive); |
| 89 | + |
| 90 | + out.writeLine(""); |
| 91 | + out.bold("Profile '" + targetProfile + "' saved successfully!"); |
| 92 | + out.writeLine(""); |
| 93 | + out.writeLine("Org URL: " + orgUrl); |
| 94 | + if (setAsActive) { |
| 95 | + out.writeLine("This profile is now active."); |
| 96 | + } else { |
| 97 | + out.writeLine("Use 'okta --profile " + targetProfile + " <command>' or 'okta profiles use " + targetProfile + "' to switch."); |
| 98 | + } |
54 | 99 | } |
55 | 100 |
|
56 | 101 | return 0; |
|
0 commit comments