mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-16 19:49:56 +00:00
logoutFromProfile failed hard when the management server returned NotFound (peer already deleted from the dashboard), blocking both profile logout and profile removal. Treat NotFound as success — the peer is already gone, so deregistering it is already satisfied. Also drop the user-side per-profile state file on logout. The account email is sourced from <profile>.state.json (written by the CLI after SSO login), which the root daemon can't reach, so logout left a stale email showing in the UI. Connection.Logout now removes it from the UI process after a successful logout; the next SSO login recreates it.
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package profilemanager
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
)
|
|
|
|
type ProfileState struct {
|
|
Email string `json:"email"`
|
|
}
|
|
|
|
func (pm *ProfileManager) GetProfileState(profileName string) (*ProfileState, error) {
|
|
configDir, err := getConfigDir()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get config directory: %w", err)
|
|
}
|
|
|
|
stateFile := filepath.Join(configDir, profileName+".state.json")
|
|
stateFileExists, err := fileExists(stateFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to check if profile state file exists: %w", err)
|
|
}
|
|
if !stateFileExists {
|
|
return nil, errors.New("profile state file does not exist")
|
|
}
|
|
|
|
var state ProfileState
|
|
_, err = util.ReadJson(stateFile, &state)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read profile state: %w", err)
|
|
}
|
|
|
|
return &state, nil
|
|
}
|
|
|
|
func (pm *ProfileManager) SetActiveProfileState(state *ProfileState) error {
|
|
configDir, err := getConfigDir()
|
|
if err != nil {
|
|
return fmt.Errorf("get config directory: %w", err)
|
|
}
|
|
|
|
activeProf, err := pm.GetActiveProfile()
|
|
if err != nil {
|
|
if errors.Is(err, ErrNoActiveProfile) {
|
|
return fmt.Errorf("no active profile set: %w", err)
|
|
}
|
|
return fmt.Errorf("get active profile: %w", err)
|
|
}
|
|
|
|
stateFile := filepath.Join(configDir, activeProf.Name+".state.json")
|
|
err = util.WriteJsonWithRestrictedPermission(context.Background(), stateFile, state)
|
|
if err != nil {
|
|
return fmt.Errorf("write profile state: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// RemoveProfileState deletes the per-profile state file (which holds the
|
|
// account email used for the SSO login hint and the UI display). Called after
|
|
// a successful logout so a logged-out profile no longer shows a stale account
|
|
// email. The state file only stores the email, so deleting it is equivalent to
|
|
// clearing it; the next SSO login recreates it. A missing file is not an error.
|
|
func (pm *ProfileManager) RemoveProfileState(profileName string) error {
|
|
configDir, err := getConfigDir()
|
|
if err != nil {
|
|
return fmt.Errorf("get config directory: %w", err)
|
|
}
|
|
|
|
stateFile := filepath.Join(configDir, profileName+".state.json")
|
|
if err := os.Remove(stateFile); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("remove profile state: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|