fix(pikpak): refresh_token contention (#6501 close #6511)

pull/6552/head
foxxorcat 2024-05-27 21:31:59 +08:00 committed by GitHub
parent 8e2b9c681a
commit 163af0515f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 22 deletions

View File

@ -41,10 +41,6 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg" d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
} }
withClient := func(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, base.HttpClient)
}
oauth2Config := &oauth2.Config{ oauth2Config := &oauth2.Config{
ClientID: d.ClientID, ClientID: d.ClientID,
ClientSecret: d.ClientSecret, ClientSecret: d.ClientSecret,
@ -55,11 +51,13 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
}, },
} }
oauth2Token, err := oauth2Config.PasswordCredentialsToken(withClient(ctx), d.Username, d.Password) d.oauth2Token = oauth2.ReuseTokenSource(nil, utils.TokenSource(func() (*oauth2.Token, error) {
if err != nil { return oauth2Config.PasswordCredentialsToken(
return err context.WithValue(context.Background(), oauth2.HTTPClient, base.HttpClient),
} d.Username,
d.oauth2Token = oauth2Config.TokenSource(withClient(context.Background()), oauth2Token) d.Password,
)
}))
return nil return nil
} }

View File

@ -33,10 +33,6 @@ func (d *PikPakShare) Init(ctx context.Context) error {
d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg" d.ClientSecret = "dbw2OtmVEeuUvIptb1Coyg"
} }
withClient := func(ctx context.Context) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, base.HttpClient)
}
oauth2Config := &oauth2.Config{ oauth2Config := &oauth2.Config{
ClientID: d.ClientID, ClientID: d.ClientID,
ClientSecret: d.ClientSecret, ClientSecret: d.ClientSecret,
@ -47,17 +43,16 @@ func (d *PikPakShare) Init(ctx context.Context) error {
}, },
} }
oauth2Token, err := oauth2Config.PasswordCredentialsToken(withClient(ctx), d.Username, d.Password) d.oauth2Token = oauth2.ReuseTokenSource(nil, utils.TokenSource(func() (*oauth2.Token, error) {
if err != nil { return oauth2Config.PasswordCredentialsToken(
return err context.WithValue(context.Background(), oauth2.HTTPClient, base.HttpClient),
} d.Username,
d.oauth2Token = oauth2Config.TokenSource(withClient(context.Background()), oauth2Token) d.Password,
)
}))
if d.SharePwd != "" { if d.SharePwd != "" {
err = d.getSharePassToken() return d.getSharePassToken()
if err != nil {
return err
}
} }
return nil return nil
} }

15
pkg/utils/oauth2.go Normal file
View File

@ -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}
}