From 2f9401cb2af19ae1f5166aa587c6cae8f75c958c Mon Sep 17 00:00:00 2001 From: Mark Pashmfouroush Date: Thu, 30 May 2024 11:57:04 +0100 Subject: [PATCH] warp: fix duplicate device bug Signed-off-by: Mark Pashmfouroush --- warp/account.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/warp/account.go b/warp/account.go index 78132d7b2..5c668cad4 100644 --- a/warp/account.go +++ b/warp/account.go @@ -35,25 +35,35 @@ func LoadOrCreateIdentity(l *slog.Logger, path, license string) (*Identity, erro if err := os.RemoveAll(path); err != nil { return nil, err } + if err := os.MkdirAll(path, os.ModePerm); err != nil { return nil, err } - i, err = CreateIdentity(l, path, license) + + i, err = CreateIdentity(l, license) if err != nil { return nil, err } + + if err = saveIdentity(i, path); err != nil { + return nil, err + } } if license != "" && i.Account.License != license { - l.Info("license recreating identity with new license") - if err := os.RemoveAll(path); err != nil { + l.Info("updating account license key") + _, err := UpdateAccount(i.Token, i.ID, license) + if err != nil { return nil, err } - if err := os.MkdirAll(path, os.ModePerm); err != nil { + + iAcc, err := GetAccount(i.Token, i.ID) + if err != nil { return nil, err } - i, err = CreateIdentity(l, path, license) - if err != nil { + i.Account = iAcc + + if err = saveIdentity(i, path); err != nil { return nil, err } } @@ -87,7 +97,7 @@ func LoadIdentity(path string) (Identity, error) { return *i, nil } -func CreateIdentity(l *slog.Logger, path, license string) (Identity, error) { +func CreateIdentity(l *slog.Logger, license string) (Identity, error) { priv, err := GeneratePrivateKey() if err != nil { return Identity{}, err @@ -103,7 +113,12 @@ func CreateIdentity(l *slog.Logger, path, license string) (Identity, error) { if license != "" { l.Info("updating account license key") - ac, err := UpdateAccount(i.Token, i.ID, license) + _, err := UpdateAccount(i.Token, i.ID, license) + if err != nil { + return Identity{}, err + } + + ac, err := GetAccount(i.Token, i.ID) if err != nil { return Identity{}, err } @@ -112,10 +127,5 @@ func CreateIdentity(l *slog.Logger, path, license string) (Identity, error) { i.PrivateKey = privateKey - err = saveIdentity(i, path) - if err != nil { - return Identity{}, err - } - return i, nil }