add relogin action when token expired

This commit is contained in:
tickstep 2022-01-31 12:09:28 +08:00
parent ea82ec8320
commit b6b71b45e3
4 changed files with 34 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/tickstep/aliyunpan-api/aliyunpan/apierror" "github.com/tickstep/aliyunpan-api/aliyunpan/apierror"
"github.com/tickstep/aliyunpan/cmder/cmdliner" "github.com/tickstep/aliyunpan/cmder/cmdliner"
"github.com/tickstep/aliyunpan/internal/config" "github.com/tickstep/aliyunpan/internal/config"
"github.com/tickstep/aliyunpan/internal/functions/panlogin"
"github.com/tickstep/library-go/logger" "github.com/tickstep/library-go/logger"
"github.com/urfave/cli" "github.com/urfave/cli"
"sync" "sync"
@ -75,7 +76,29 @@ func TryLogin() *config.PanUser {
// login // login
_, webToken, err := DoLoginHelper(u.RefreshToken) _, webToken, err := DoLoginHelper(u.RefreshToken)
if err != nil { if err != nil {
logger.Verboseln("automatically login error") logger.Verboseln("automatically login use saved refresh token error ", err)
if u.TokenId != "" {
logger.Verboseln("try to login use tokenId")
h := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost)
r, e := h.GetRefreshToken(u.TokenId)
if e != nil {
logger.Verboseln("try to login use tokenId error", e)
break
}
refreshToken, e := h.ParseSecureRefreshToken("", r.SecureRefreshToken)
if e != nil {
logger.Verboseln("try to parse refresh token error", e)
break
}
_, webToken, err = DoLoginHelper(refreshToken)
if err != nil {
logger.Verboseln("try to use refresh token from tokenId error", e)
break
}
fmt.Println("Token重新自动登录成功")
// save new refresh token
u.RefreshToken = refreshToken
}
break break
} }
// success // success

View File

@ -133,7 +133,7 @@ func CmdLogout() cli.Command {
func RunLogin(useQrCodeLogin bool, refreshToken string) (tokenId, refreshTokenStr string, webToken aliyunpan.WebLoginToken, error error) { func RunLogin(useQrCodeLogin bool, refreshToken string) (tokenId, refreshTokenStr string, webToken aliyunpan.WebLoginToken, error error) {
if useQrCodeLogin { if useQrCodeLogin {
h := panlogin.NewLoginHelper("http://localhost:8977") h := panlogin.NewLoginHelper(config.DefaultTokenServiceWebHost)
qrCodeUrlResult, err := h.GetQRCodeLoginUrl("") qrCodeUrlResult, err := h.GetQRCodeLoginUrl("")
if err != nil { if err != nil {
fmt.Println("二维码登录错误:", err) fmt.Println("二维码登录错误:", err)

View File

@ -39,11 +39,15 @@ const (
// ConfigVersion 配置文件版本 // ConfigVersion 配置文件版本
ConfigVersion string = "1.0" ConfigVersion string = "1.0"
// DefaultUploadParallelNum 默认的文件上传并发数量 // DefaultFileUploadParallelNum 默认的文件上传并发数量
DefaultFileUploadParallelNum = 10 DefaultFileUploadParallelNum = 10
// DefaultFileDownloadParallelNum 默认的文件下载并发数量 // DefaultFileDownloadParallelNum 默认的文件下载并发数量
DefaultFileDownloadParallelNum = 5 DefaultFileDownloadParallelNum = 5
// DefaultTokenServiceWebHost 默认的token服务
DefaultTokenServiceWebHost = "http://api.tickstep.com"
//DefaultTokenServiceWebHost = "http://localhost:8977"
) )
var ( var (
@ -75,7 +79,7 @@ type PanConfig struct {
MaxDownloadRate int64 `json:"maxDownloadRate"` // 限制最大下载速度,单位 B/s, 即字节/每秒 MaxDownloadRate int64 `json:"maxDownloadRate"` // 限制最大下载速度,单位 B/s, 即字节/每秒
MaxUploadRate int64 `json:"maxUploadRate"` // 限制最大上传速度,单位 B/s, 即字节/每秒 MaxUploadRate int64 `json:"maxUploadRate"` // 限制最大上传速度,单位 B/s, 即字节/每秒
TransferUrlType int `json:"transferUrlType"` // 上传/下载URL类别1-默认2-阿里云ECS TransferUrlType int `json:"transferUrlType"` // 上传/下载URL类别1-默认2-阿里云ECS
SaveDir string `json:"saveDir"` // 下载储存路径 SaveDir string `json:"saveDir"` // 下载储存路径
@ -282,7 +286,7 @@ func GetConfigDir() string {
// 3. ~/.aliyunpan/ // 3. ~/.aliyunpan/
if runtime.GOOS == "linux" || runtime.GOOS == "windows" { if runtime.GOOS == "linux" || runtime.GOOS == "windows" {
cd,er := homedir.Expand("~/.aliyunpan") cd, er := homedir.Expand("~/.aliyunpan")
if er == nil { if er == nil {
if IsFolderExist(cd) { if IsFolderExist(cd) {
logger.Verboseln("use config dir: ", cd) logger.Verboseln("use config dir: ", cd)

View File

@ -41,9 +41,6 @@ type QRCodeLoginResult struct {
} }
func NewLoginHelper(webHost string) *LoginHelper { func NewLoginHelper(webHost string) *LoginHelper {
if webHost == "" {
webHost = "http://api.tickstep.com"
}
return &LoginHelper{ return &LoginHelper{
webHost: webHost, webHost: webHost,
} }