Skip to content

Commit 2525d8b

Browse files
committed
refactor: Remove all OAuth related code
1 parent 36cabfd commit 2525d8b

File tree

4 files changed

+5
-54
lines changed

4 files changed

+5
-54
lines changed

internal/cmd/auth/login/login.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/hasura/go-graphql-client"
1111
"github.com/spf13/cobra"
1212
"github.com/spf13/viper"
13-
"golang.org/x/oauth2"
1413

1514
"github.com/zeabur/cli/internal/cmdutil"
1615
"github.com/zeabur/cli/pkg/api"
@@ -70,8 +69,7 @@ func RunLogin(f *cmdutil.Factory, opts *Options) error {
7069
return fmt.Errorf("failed to get user info: %w", err)
7170
}
7271
} else {
73-
f.Log.Debugw("Already logged in", "token string", f.Config.GetTokenString(),
74-
"token detail", f.Config.GetToken(), "user", user)
72+
f.Log.Debugw("Already logged in", "token", f.Config.GetTokenString(), "user", user)
7573
f.Log.Infof("Already logged in as %s, "+
7674
"if you want to use a different account, please logout first", user.Name)
7775
return nil
@@ -80,7 +78,6 @@ func RunLogin(f *cmdutil.Factory, opts *Options) error {
8078

8179
var (
8280
tokenString string
83-
token *oauth2.Token
8481
err error
8582
)
8683

internal/cmd/auth/logout/logout.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func runLogout(f *cmdutil.Factory, opts *logoutOptions) error {
3737
f.Config.SetUser("")
3838
f.Config.SetUsername("")
3939

40-
// reset token detail if exists
41-
if f.Config.GetToken() != nil {
42-
f.Config.SetToken(nil)
43-
}
44-
4540
// reset context
4641
f.Config.GetContext().ClearAll()
4742

internal/cmd/root/root.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
package root
33

44
import (
5+
"context"
56
"fmt"
6-
"time"
77

88
"github.com/MakeNowJust/heredoc"
99
"github.com/spf13/cobra"
10-
"golang.org/x/oauth2"
1110

1211
authCmd "github.com/zeabur/cli/internal/cmd/auth"
1312
completionCmd "github.com/zeabur/cli/internal/cmd/completion"
@@ -112,8 +111,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman
112111
// Persistent flags
113112
cmd.PersistentFlags().BoolVar(&f.Debug, "debug", false, "Enable debug logging")
114113
cmd.PersistentFlags().BoolVarP(&f.Interactive, config.KeyInteractive, "i", true, "use interactive mode")
115-
cmd.PersistentFlags().BoolVar(&f.AutoRefreshToken, config.KeyAutoRefreshToken, true,
116-
"automatically refresh token when it's expired, only works when the token is from browser(OAuth2)")
117114
cmd.PersistentFlags().BoolVar(&f.AutoCheckUpdate, config.KeyAutoCheckUpdate, true, "automatically check update")
118115

119116
// Child commands

pkg/config/config.go

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33

44
import (
55
"github.com/spf13/viper"
6-
"golang.org/x/oauth2"
76

87
"github.com/zeabur/cli/pkg/zcontext"
98
)
@@ -13,32 +12,18 @@ const (
1312
KeyTokenString = "token"
1413
KeyUser = "user"
1514
KeyUsername = "username"
16-
17-
// if we use "token.access_token" as key, the env ZEABUR_TOKEN will override all token details,
18-
// because "token" is the prefix of "token.access_token".
19-
// Therefore, we use "token_detail" as the key to store all token details.
20-
21-
KeyTokenDetail = "token_detail"
22-
KeyTokenAccess = KeyTokenDetail + ".access_token"
23-
KeyTokenExpiry = KeyTokenDetail + ".expiry"
24-
KeyTokenType = KeyTokenDetail + ".token_type"
25-
KeyTokenRefresh = KeyTokenDetail + ".refresh_token"
2615
)
2716

2817
// Keys about CLI behavior
2918
const (
30-
KeyInteractive = "interactive"
31-
KeyAutoRefreshToken = "auto_refresh_token"
32-
KeyAutoCheckUpdate = "auto_check_update"
19+
KeyInteractive = "interactive"
20+
KeyAutoCheckUpdate = "auto_check_update"
3321
)
3422

3523
type Config interface {
36-
GetTokenString() string // token string is the single token string, it may be set by user or generated from OAuth2
24+
GetTokenString() string // token string is the single token string, it may be set by user or our login function
3725
SetTokenString(token string)
3826

39-
GetToken() *oauth2.Token // token is the detail of token, if the token is from OAuth2, it will be set
40-
SetToken(token *oauth2.Token)
41-
4227
GetUser() string // nickname of user
4328
SetUser(user string)
4429
GetUsername() string // it is kind like id of user
@@ -72,29 +57,6 @@ func (c *config) SetTokenString(token string) {
7257
viper.Set(KeyTokenString, token)
7358
}
7459

75-
func (c *config) GetToken() *oauth2.Token {
76-
token := &oauth2.Token{}
77-
token.AccessToken = viper.GetString(KeyTokenAccess)
78-
token.RefreshToken = viper.GetString(KeyTokenRefresh)
79-
token.TokenType = viper.GetString(KeyTokenType)
80-
token.Expiry = viper.GetTime(KeyTokenExpiry)
81-
if token.AccessToken == "" || token.RefreshToken == "" || token.TokenType == "" || token.Expiry.IsZero() {
82-
return nil
83-
}
84-
return token
85-
}
86-
87-
func (c *config) SetToken(token *oauth2.Token) {
88-
if token == nil {
89-
viper.Set(KeyTokenDetail, "")
90-
return
91-
}
92-
viper.Set(KeyTokenAccess, token.AccessToken)
93-
viper.Set(KeyTokenRefresh, token.RefreshToken)
94-
viper.Set(KeyTokenType, token.TokenType)
95-
viper.Set(KeyTokenExpiry, token.Expiry)
96-
}
97-
9860
func (c *config) GetUser() string {
9961
return viper.GetString(KeyUser)
10062
}

0 commit comments

Comments
 (0)