Skip to content

Commit

Permalink
feat(139): refresh token periodically (#6146)
Browse files Browse the repository at this point in the history
* 139定时刷新token

* fix build fail
  • Loading branch information
zzc10086 committed Mar 11, 2024
1 parent bdfc159 commit 195c869
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions drivers/139/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/pkg/cron"
log "github.com/sirupsen/logrus"
)

type Yun139 struct {
model.Storage
Addition
cron *cron.Cron
Account string
}

Expand All @@ -35,6 +38,13 @@ func (d *Yun139) Init(ctx context.Context) error {
if d.Authorization == "" {
return fmt.Errorf("authorization is empty")
}
d.cron = cron.NewCron(time.Hour * 24 * 7)
d.cron.Do(func() {
err := d.refreshToken()
if err != nil {
log.Errorf("%+v", err)
}
})
switch d.Addition.Type {
case MetaPersonalNew:
if len(d.Addition.RootFolderID) == 0 {
Expand Down Expand Up @@ -72,6 +82,9 @@ func (d *Yun139) Init(ctx context.Context) error {
}

func (d *Yun139) Drop(ctx context.Context) error {
if d.cron != nil {
d.cron.Stop()
}
return nil
}

Expand Down
13 changes: 13 additions & 0 deletions drivers/139/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package _139

import (
"encoding/xml"
)

const (
MetaPersonal string = "personal"
MetaFamily string = "family"
Expand Down Expand Up @@ -230,3 +234,12 @@ type PersonalUploadResp struct {
UploadId string `json:"uploadId"`
}
}

type RefreshTokenResp struct {
XMLName xml.Name `xml:"root"`
Return string `xml:"return"`
Token string `xml:"token"`
Expiretime int32 `xml:"expiretime"`
AccessToken string `xml:"accessToken"`
Desc string `xml:"desc"`
}
27 changes: 27 additions & 0 deletions drivers/139/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/pkg/utils/random"
"github.com/alist-org/alist/v3/internal/op"
"github.com/go-resty/resty/v2"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,6 +53,32 @@ func getTime(t string) time.Time {
return stamp
}

func (d *Yun139) refreshToken() error {
url := "https://aas.caiyun.feixin.10086.cn:443/tellin/authTokenRefresh.do"
var resp RefreshTokenResp
decode, err := base64.StdEncoding.DecodeString(d.Authorization)
if err != nil {
return err
}
decodeStr := string(decode)
splits := strings.Split(decodeStr, ":")
reqBody := "<root><token>" + splits[2] + "</token><account>" + splits[1] + "</account><clienttype>656</clienttype></root>"
_, err = base.RestyClient.R().
//ForceContentType("application/json").
SetBody(reqBody).
SetResult(&resp).
Post(url)
if err != nil {
return err
}
if resp.Return != "0" {
return fmt.Errorf("failed to refresh token: %s", resp.Desc)
}
d.Authorization = base64.StdEncoding.EncodeToString([]byte(splits[0] + splits[1] + ":" + resp.Token))
op.MustSaveDriverStorage(d)
return nil
}

func (d *Yun139) request(pathname string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
url := "https://yun.139.com" + pathname
req := base.RestyClient.R()
Expand Down

0 comments on commit 195c869

Please sign in to comment.