Skip to content

Commit

Permalink
fix(pikpak): refresh_token contention (#6501 close #6511)
Browse files Browse the repository at this point in the history
  • Loading branch information
foxxorcat authored May 27, 2024
1 parent 8e2b9c6 commit 163af05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
16 changes: 7 additions & 9 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
}

withClient := func(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, base.HttpClient)
}

oauth2Config := &oauth2.Config{
ClientID: d.ClientID,
ClientSecret: d.ClientSecret,
Expand All @@ -55,11 +51,13 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
},
}

oauth2Token, err := oauth2Config.PasswordCredentialsToken(withClient(ctx), d.Username, d.Password)
if err != nil {
return err
}
d.oauth2Token = oauth2Config.TokenSource(withClient(context.Background()), oauth2Token)
d.oauth2Token = oauth2.ReuseTokenSource(nil, utils.TokenSource(func() (*oauth2.Token, error) {
return oauth2Config.PasswordCredentialsToken(
context.WithValue(context.Background(), oauth2.HTTPClient, base.HttpClient),
d.Username,
d.Password,
)
}))
return nil
}

Expand Down
21 changes: 8 additions & 13 deletions drivers/pikpak_share/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func (d *PikPakShare) Init(ctx context.Context) error {
d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
}

withClient := func(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, base.HttpClient)
}

oauth2Config := &oauth2.Config{
ClientID: d.ClientID,
ClientSecret: d.ClientSecret,
Expand All @@ -47,17 +43,16 @@ func (d *PikPakShare) Init(ctx context.Context) error {
},
}

oauth2Token, err := oauth2Config.PasswordCredentialsToken(withClient(ctx), d.Username, d.Password)
if err != nil {
return err
}
d.oauth2Token = oauth2Config.TokenSource(withClient(context.Background()), oauth2Token)
d.oauth2Token = oauth2.ReuseTokenSource(nil, utils.TokenSource(func() (*oauth2.Token, error) {
return oauth2Config.PasswordCredentialsToken(
context.WithValue(context.Background(), oauth2.HTTPClient, base.HttpClient),
d.Username,
d.Password,
)
}))

if d.SharePwd != "" {
err = d.getSharePassToken()
if err != nil {
return err
}
return d.getSharePassToken()
}
return nil
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/oauth2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import "golang.org/x/oauth2"

type tokenSource struct {
fn func() (*oauth2.Token, error)
}

func (t *tokenSource) Token() (*oauth2.Token, error) {
return t.fn()
}

func TokenSource(fn func() (*oauth2.Token, error)) oauth2.TokenSource {
return &tokenSource{fn}
}

0 comments on commit 163af05

Please sign in to comment.