Skip to content

Commit

Permalink
refactor: change grant_token to access_token
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <[email protected]>
  • Loading branch information
yjinjo committed Dec 18, 2024
1 parent 68368da commit af23616
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions cmd/common/fetchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ func loadConfig() (*Config, error) {

// Handle token based on environment type
if strings.HasSuffix(currentEnv, "-user") {
// For user environments, read from grant_token file
grantTokenPath := filepath.Join(home, ".cfctl", "cache", currentEnv, "grant_token")
// For user environments, read from access_token file (Actual token is grant_token)
grantTokenPath := filepath.Join(home, ".cfctl", "cache", currentEnv, "access_token")
tokenBytes, err := os.ReadFile(grantTokenPath)
if err == nil {
envConfig.Token = strings.TrimSpace(string(tokenBytes))
Expand Down
26 changes: 11 additions & 15 deletions cmd/other/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func executeUserLogin(currentEnv string) {
pterm.Info.Printf("Logging in as: %s\n", userID)
}

accessToken, refreshToken, newAccessToken, err := getValidTokens(currentEnv)
accessToken, refreshToken, err := getValidTokens(currentEnv)
if err != nil || refreshToken == "" || isTokenExpired(refreshToken) {
// Get new tokens with password
password := promptPassword()
Expand Down Expand Up @@ -509,25 +509,20 @@ func executeUserLogin(currentEnv string) {
}

// Grant new token using the refresh token
newAccessToken, err = grantToken(baseUrl, refreshToken, scope, domainID, workspaceID)
newAccessToken, err := grantToken(baseUrl, refreshToken, scope, domainID, workspaceID)
if err != nil {
pterm.Error.Println("Failed to retrieve new access token:", err)
exitWithError()
}

// Save all tokens
if err := os.WriteFile(filepath.Join(envCacheDir, "access_token"), []byte(accessToken), 0600); err != nil {
pterm.Error.Printf("Failed to save access token: %v\n", err)
exitWithError()
}

if err := os.WriteFile(filepath.Join(envCacheDir, "refresh_token"), []byte(refreshToken), 0600); err != nil {
pterm.Error.Printf("Failed to save refresh token: %v\n", err)
exitWithError()
}

if err := os.WriteFile(filepath.Join(envCacheDir, "grant_token"), []byte(newAccessToken), 0600); err != nil {
pterm.Error.Printf("Failed to save grant token: %v\n", err)
if err := os.WriteFile(filepath.Join(envCacheDir, "access_token"), []byte(newAccessToken), 0600); err != nil {
pterm.Error.Printf("Failed to save access token: %v\n", err)
exitWithError()
}

Expand Down Expand Up @@ -1657,10 +1652,10 @@ func readTokenFromFile(envDir, tokenType string) (string, error) {
}

// getValidTokens checks for existing valid tokens in the environment cache directory
func getValidTokens(currentEnv string) (accessToken, refreshToken, newAccessToken string, err error) {
func getValidTokens(currentEnv string) (accessToken, refreshToken string, err error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", "", "", err
return "", "", err
}

envCacheDir := filepath.Join(homeDir, ".cfctl", "cache", currentEnv)
Expand All @@ -1670,13 +1665,14 @@ func getValidTokens(currentEnv string) (accessToken, refreshToken, newAccessToke
if err == nil {
if exp, ok := claims["exp"].(float64); ok {
if time.Now().Unix() < int64(exp) {
accessToken, _ = readTokenFromFile(envCacheDir, "access_token")
newAccessToken, _ = readTokenFromFile(envCacheDir, "grant_token")
return accessToken, refreshToken, newAccessToken, nil
if accessToken, err = readTokenFromFile(envCacheDir, "access_token"); err == nil {
return accessToken, refreshToken, nil
}
return accessToken, refreshToken, nil
}
}
}
}

return "", "", "", fmt.Errorf("no valid tokens found")
return "", "", fmt.Errorf("no valid tokens found")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func loadConfig() (*Config, error) {
if strings.HasSuffix(currentEnv, "-user") {
// For user environments, read from cache directory
envCacheDir := filepath.Join(home, ".cfctl", "cache", currentEnv)
grantTokenPath := filepath.Join(envCacheDir, "grant_token")
grantTokenPath := filepath.Join(envCacheDir, "access_token")
data, err := os.ReadFile(grantTokenPath)
if err != nil {
return nil, fmt.Errorf("no valid token found in cache")
Expand Down

0 comments on commit af23616

Please sign in to comment.