Skip to content

Commit

Permalink
fix(pikpak): remove oauth2 method (#7567 close #7545)
Browse files Browse the repository at this point in the history
  • Loading branch information
Three-taile-dragon authored Dec 7, 2024
1 parent 2d3605c commit fa15c57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 83 deletions.
33 changes: 3 additions & 30 deletions drivers/pikpak/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
hash_extend "github.com/alist-org/alist/v3/pkg/utils/hash"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"net/http"
"strconv"
"strings"
Expand All @@ -24,7 +23,6 @@ type PikPak struct {
*Common
RefreshToken string
AccessToken string
oauth2Token oauth2.TokenSource
}

func (d *PikPak) Config() driver.Config {
Expand Down Expand Up @@ -84,41 +82,16 @@ func (d *PikPak) Init(ctx context.Context) (err error) {
d.Addition.DeviceID = d.Common.DeviceID
op.MustSaveDriverStorage(d)
}
// 初始化 oauth2Config
oauth2Config := &oauth2.Config{
ClientID: d.ClientID,
ClientSecret: d.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: "https://user.mypikpak.net/v1/auth/signin",
TokenURL: "https://user.mypikpak.net/v1/auth/token",
AuthStyle: oauth2.AuthStyleInParams,
},
}

// 如果已经有RefreshToken,直接获取AccessToken
if d.Addition.RefreshToken != "" {
if d.RefreshTokenMethod == "oauth2" {
// 使用 oauth2 刷新令牌
// 初始化 oauth2Token
d.initializeOAuth2Token(ctx, oauth2Config, d.Addition.RefreshToken)
if err := d.refreshTokenByOAuth2(); err != nil {
return err
}
} else {
if err := d.refreshToken(d.Addition.RefreshToken); err != nil {
return err
}
if err = d.refreshToken(d.Addition.RefreshToken); err != nil {
return err
}

} else {
// 如果没有填写RefreshToken,尝试登录 获取 refreshToken
if err := d.login(); err != nil {
if err = d.login(); err != nil {
return err
}
if d.RefreshTokenMethod == "oauth2" {
d.initializeOAuth2Token(ctx, oauth2Config, d.RefreshToken)
}

}

// 获取CaptchaToken
Expand Down
15 changes: 7 additions & 8 deletions drivers/pikpak/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (

type Addition struct {
driver.RootID
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
Platform string `json:"platform" required:"true" default:"web" type:"select" options:"android,web,pc"`
RefreshToken string `json:"refresh_token" required:"true" default:""`
RefreshTokenMethod string `json:"refresh_token_method" required:"true" type:"select" options:"oauth2,http"`
CaptchaToken string `json:"captcha_token" default:""`
DeviceID string `json:"device_id" required:"false" default:""`
DisableMediaLink bool `json:"disable_media_link" default:"true"`
Username string `json:"username" required:"true"`
Password string `json:"password" required:"true"`
Platform string `json:"platform" required:"true" default:"web" type:"select" options:"android,web,pc"`
RefreshToken string `json:"refresh_token" required:"true" default:""`
CaptchaToken string `json:"captcha_token" default:""`
DeviceID string `json:"device_id" required:"false" default:""`
DisableMediaLink bool `json:"disable_media_link" default:"true"`
}

var config = driver.Config{
Expand Down
48 changes: 3 additions & 45 deletions drivers/pikpak/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pikpak

import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
Expand All @@ -14,7 +13,6 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"io"
"net/http"
"path/filepath"
Expand All @@ -27,8 +25,6 @@ import (
"github.com/go-resty/resty/v2"
)

// do others that not defined in Driver interface

var AndroidAlgorithms = []string{
"7xOq4Z8s",
"QE9/9+IQco",
Expand Down Expand Up @@ -171,30 +167,6 @@ func (d *PikPak) refreshToken(refreshToken string) error {
return nil
}

func (d *PikPak) initializeOAuth2Token(ctx context.Context, oauth2Config *oauth2.Config, refreshToken string) {
d.oauth2Token = oauth2.ReuseTokenSource(nil, utils.TokenSource(func() (*oauth2.Token, error) {
return oauth2Config.TokenSource(ctx, &oauth2.Token{
RefreshToken: refreshToken,
}).Token()
}))
}

func (d *PikPak) refreshTokenByOAuth2() error {
token, err := d.oauth2Token.Token()
if err != nil {
return err
}
d.Status = "work"
d.RefreshToken = token.RefreshToken
d.AccessToken = token.AccessToken
// 获取用户ID
userID := token.Extra("sub").(string)
d.Common.SetUserID(userID)
d.Addition.RefreshToken = d.RefreshToken
op.MustSaveDriverStorage(d)
return nil
}

func (d *PikPak) request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
req := base.RestyClient.R()
req.SetHeaders(map[string]string{
Expand All @@ -203,14 +175,7 @@ func (d *PikPak) request(url string, method string, callback base.ReqCallback, r
"X-Device-ID": d.GetDeviceID(),
"X-Captcha-Token": d.GetCaptchaToken(),
})
if d.RefreshTokenMethod == "oauth2" && d.oauth2Token != nil {
// 使用oauth2 获取 access_token
token, err := d.oauth2Token.Token()
if err != nil {
return nil, err
}
req.SetAuthScheme(token.TokenType).SetAuthToken(token.AccessToken)
} else if d.AccessToken != "" {
if d.AccessToken != "" {
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
}

Expand All @@ -232,16 +197,9 @@ func (d *PikPak) request(url string, method string, callback base.ReqCallback, r
return res.Body(), nil
case 4122, 4121, 16:
// access_token 过期
if d.RefreshTokenMethod == "oauth2" {
if err1 := d.refreshTokenByOAuth2(); err1 != nil {
return nil, err1
}
} else {
if err1 := d.refreshToken(d.RefreshToken); err1 != nil {
return nil, err1
}
if err1 := d.refreshToken(d.RefreshToken); err1 != nil {
return nil, err1
}

return d.request(url, method, callback, resp)
case 9: // 验证码token过期
if err = d.RefreshCaptchaTokenAtLogin(GetAction(method, url), d.GetUserID()); err != nil {
Expand Down

0 comments on commit fa15c57

Please sign in to comment.