Skip to content

Commit 19af01d

Browse files
committed
fix: after switch account first time download report error
1 parent 1331cc3 commit 19af01d

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

cmd/common.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,30 @@ func newLogger(format OutputFormat, verbose bool) log.Logger {
5252
)
5353
}
5454

55+
type persistentCookieJar struct {
56+
*cookiejar.Jar
57+
path string
58+
}
59+
60+
func (j *persistentCookieJar) Reset() error {
61+
if err := os.Remove(j.path); err != nil && !os.IsNotExist(err) {
62+
return fmt.Errorf("failed to remove cookie jar file: %w", err)
63+
}
64+
65+
jar, err := cookiejar.New(&cookiejar.Options{Filename: j.path})
66+
if err != nil {
67+
return fmt.Errorf("failed to create cookie jar: %w", err)
68+
}
69+
70+
j.Jar = jar
71+
return nil
72+
}
73+
5574
// newCookieJar returns a new cookie jar instance.
5675
func newCookieJar(machine machine.Machine) http.CookieJar {
57-
return util.Must(cookiejar.New(&cookiejar.Options{
58-
Filename: filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName),
59-
}))
76+
path := filepath.Join(machine.HomeDirectory(), ConfigDirectoryName, CookieJarFileName)
77+
jar := util.Must(cookiejar.New(&cookiejar.Options{Filename: path}))
78+
return &persistentCookieJar{Jar: jar, path: path}
6079
}
6180

6281
// newKeychain returns a new keychain instance.

pkg/appstore/appstore.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type appstore struct {
4646
platformClient http.Client[platformVersionLookupResult]
4747
bagClient http.Client[bagResult]
4848
httpClient http.Client[interface{}]
49+
cookieJar http.CookieJar
4950
machine machine.Machine
5051
os operatingsystem.OperatingSystem
5152
}
@@ -71,6 +72,7 @@ func NewAppStore(args Args) AppStore {
7172
platformClient: http.NewClient[platformVersionLookupResult](clientArgs),
7273
bagClient: http.NewClient[bagResult](clientArgs),
7374
httpClient: http.NewClient[interface{}](clientArgs),
75+
cookieJar: args.CookieJar,
7476
machine: args.Machine,
7577
os: args.OperatingSystem,
7678
}

pkg/appstore/appstore_account_info.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ func (t *appstore) SwitchAccount(email string) (Account, error) {
134134

135135
accountStorage.Current = email
136136

137+
if t.cookieJar != nil {
138+
if err := t.cookieJar.Reset(); err != nil {
139+
return Account{}, fmt.Errorf("failed to reset account cookies: %w", err)
140+
}
141+
}
142+
137143
rootData, err := json.Marshal(accountStorage)
138144
if err != nil {
139145
return Account{}, fmt.Errorf("failed to marshal json: %w", err)

pkg/appstore/appstore_account_info_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66

7+
"github.com/majd/ipatool/v2/pkg/http"
78
"github.com/majd/ipatool/v2/pkg/keychain"
89
. "github.com/onsi/ginkgo/v2"
910
. "github.com/onsi/gomega"
@@ -74,6 +75,44 @@ var _ = Describe("AppStore (AccountInfo)", func() {
7475
})
7576
})
7677

78+
When("switching accounts", func() {
79+
var mockCookieJar *http.MockCookieJar
80+
81+
BeforeEach(func() {
82+
mockCookieJar = http.NewMockCookieJar(ctrl)
83+
appstore = NewAppStore(Args{
84+
Keychain: mockKeychain,
85+
CookieJar: mockCookieJar,
86+
})
87+
88+
accountStorage := AccountStorage{
89+
Current: testEmail,
90+
Accounts: []Account{
91+
{Email: testEmail, Name: testName},
92+
{Email: "other@example.com", Name: "Other"},
93+
},
94+
}
95+
storageData, err := json.Marshal(accountStorage)
96+
Expect(err).ToNot(HaveOccurred())
97+
98+
mockKeychain.EXPECT().
99+
Get(AccountKey).
100+
Return(storageData, nil).
101+
AnyTimes()
102+
mockKeychain.EXPECT().
103+
Set(AccountKey, gomock.Any()).
104+
Return(nil)
105+
mockCookieJar.EXPECT().
106+
Reset().
107+
Return(nil)
108+
})
109+
110+
It("resets session cookies before switching", func() {
111+
_, err := appstore.SwitchAccount("other@example.com")
112+
Expect(err).ToNot(HaveOccurred())
113+
})
114+
})
115+
77116
When("keychain returns error", func() {
78117
BeforeEach(func() {
79118
mockKeychain.EXPECT().

pkg/http/cookiejar.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type CookieJar interface {
77
http.CookieJar
88

99
Save() error
10+
Reset() error
1011
}

0 commit comments

Comments
 (0)