diff --git a/cmd/other/login.go b/cmd/other/login.go index 2b8132d..0d9e2ac 100644 --- a/cmd/other/login.go +++ b/cmd/other/login.go @@ -451,10 +451,7 @@ func executeUserLogin(currentEnv string) { } accessToken, refreshToken, newAccessToken, err := getValidTokens(currentEnv) - if err == nil && refreshToken != "" && !isTokenExpired(refreshToken) { - // Use existing valid refresh token - accessToken = newAccessToken - } else { + if err != nil || refreshToken == "" || isTokenExpired(refreshToken) { // Get new tokens with password password := promptPassword() accessToken, refreshToken, err = issueToken(baseUrl, tempUserID, password, domainID) @@ -1598,7 +1595,7 @@ func validateAndDecodeToken(token string) (map[string]interface{}, error) { } // Check required fields - requiredFields := []string{"exp", "rol", "did"} + requiredFields := []string{"exp", "did"} for _, field := range requiredFields { if _, ok := claims[field]; !ok { return nil, fmt.Errorf("invalid token format: missing required field '%s'", field) @@ -1668,34 +1665,18 @@ func getValidTokens(currentEnv string) (accessToken, refreshToken, newAccessToke envCacheDir := filepath.Join(homeDir, ".cfctl", "cache", currentEnv) - // Try to read and validate grant token first - if newAccessToken, err = readTokenFromFile(envCacheDir, "grant_token"); err == nil { - claims, err := validateAndDecodeToken(newAccessToken) + if refreshToken, err = readTokenFromFile(envCacheDir, "refresh_token"); err == nil { + claims, err := validateAndDecodeToken(refreshToken) if err == nil { - // Check if token has expired if exp, ok := claims["exp"].(float64); ok { if time.Now().Unix() < int64(exp) { accessToken, _ = readTokenFromFile(envCacheDir, "access_token") - refreshToken, _ = readTokenFromFile(envCacheDir, "refresh_token") + newAccessToken, _ = readTokenFromFile(envCacheDir, "grant_token") return accessToken, refreshToken, newAccessToken, nil } } } } - // If grant token is invalid or expired, check refresh token - if accessToken, err = readTokenFromFile(envCacheDir, "access_token"); err == nil { - if refreshToken, err = readTokenFromFile(envCacheDir, "refresh_token"); err == nil { - claims, err := validateAndDecodeToken(refreshToken) - if err == nil { - if exp, ok := claims["exp"].(float64); ok { - if time.Now().Unix() < int64(exp) { - return accessToken, refreshToken, "", nil - } - } - } - } - } - return "", "", "", fmt.Errorf("no valid tokens found") } diff --git a/cmd/other/setting.go b/cmd/other/setting.go index c381dd7..d17aeee 100644 --- a/cmd/other/setting.go +++ b/cmd/other/setting.go @@ -46,9 +46,9 @@ var settingInitURLCmd = &cobra.Command{ Short: "Initialize configuration with a URL", Long: `Specify a URL to initialize the environment configuration.`, Args: cobra.NoArgs, - Example: ` cfctl setting init url -u https://spaceone.spaceone.megazone.io --app + Example: ` cfctl setting init url -u https://example.com --app or - cfctl setting init url -u https://spaceone.spaceone.megazone.io --user`, + cfctl setting init url -u https://example.com --user`, Run: func(cmd *cobra.Command, args []string) { urlStr, _ := cmd.Flags().GetString("url") appFlag, _ := cmd.Flags().GetBool("app") @@ -182,7 +182,7 @@ var settingInitLocalCmd = &cobra.Command{ if appFlag { updateLocalSetting(envName, "app", mainSettingPath) } else { - updateLocalSetting(envName, "user", filepath.Join(settingDir, "cache", "setting.toml")) + updateLocalSetting(envName, "user", mainSettingPath) } // Update the current environment in the main setting @@ -226,7 +226,12 @@ func updateLocalSetting(envName, settingType, settingPath string) { // Set environment configuration v.Set(fmt.Sprintf("environments.%s.endpoint", envName), "grpc://localhost:50051") - v.Set(fmt.Sprintf("environments.%s.token", envName), "") + if settingType == "app" { + v.Set(fmt.Sprintf("environments.%s.token", envName), "") + v.Set(fmt.Sprintf("environments.%s.proxy", envName), true) + } else if settingType == "user" { + v.Set(fmt.Sprintf("environments.%s.proxy", envName), false) + } // Write configuration if err := v.WriteConfig(); err != nil { @@ -404,12 +409,6 @@ var showCmd = &cobra.Command{ return } - // Load user configuration - if err := loadSetting(userV, userSettingPath); err != nil { - pterm.Error.Println(err) - return - } - currentEnv := getCurrentEnvironment(appV) if currentEnv == "" { pterm.Sprintf("No environment set in %s\n", appSettingPath) diff --git a/cmd/root.go b/cmd/root.go index 6ac3f90..be74c50 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -94,11 +94,18 @@ func init() { pterm.DisableColor() } - // Skip configuration check for settings init commands - if len(os.Args) >= 3 && os.Args[1] == "settings" && os.Args[2] == "init" { - // Skip configuration check for initialization - } else { - // Try to add dynamic service commands + // Determine if the current command is 'setting environment -l' + skipDynamicCommands := false + if len(os.Args) >= 3 && os.Args[1] == "setting" && os.Args[2] == "environment" { + for _, arg := range os.Args[3:] { + if arg == "-l" || arg == "--list" { + skipDynamicCommands = true + break + } + } + } + + if !skipDynamicCommands { if err := addDynamicServiceCommands(); err != nil { showInitializationGuide(err) }