Skip to content

Commit

Permalink
refactor: Remove all OAuth related code
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Feb 7, 2025
1 parent f4e6593 commit 162cef4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 54 deletions.
5 changes: 1 addition & 4 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hasura/go-graphql-client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/oauth2"

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

var (
tokenString string
token *oauth2.Token
err error
)

Expand Down
5 changes: 0 additions & 5 deletions internal/cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func runLogout(f *cmdutil.Factory, opts *logoutOptions) error {
f.Config.SetUser("")
f.Config.SetUsername("")

// reset token detail if exists
if f.Config.GetToken() != nil {
f.Config.SetToken(nil)
}

// reset context
f.Config.GetContext().ClearAll()

Expand Down
5 changes: 1 addition & 4 deletions internal/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
package root

import (
"context"
"fmt"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"golang.org/x/oauth2"

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

// Child commands
Expand Down
44 changes: 3 additions & 41 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config

import (
"github.com/spf13/viper"
"golang.org/x/oauth2"

"github.com/zeabur/cli/pkg/zcontext"
)
Expand All @@ -13,32 +12,18 @@ const (
KeyTokenString = "token"
KeyUser = "user"
KeyUsername = "username"

// if we use "token.access_token" as key, the env ZEABUR_TOKEN will override all token details,
// because "token" is the prefix of "token.access_token".
// Therefore, we use "token_detail" as the key to store all token details.

KeyTokenDetail = "token_detail"
KeyTokenAccess = KeyTokenDetail + ".access_token"
KeyTokenExpiry = KeyTokenDetail + ".expiry"
KeyTokenType = KeyTokenDetail + ".token_type"
KeyTokenRefresh = KeyTokenDetail + ".refresh_token"
)

// Keys about CLI behavior
const (
KeyInteractive = "interactive"
KeyAutoRefreshToken = "auto_refresh_token"
KeyAutoCheckUpdate = "auto_check_update"
KeyInteractive = "interactive"
KeyAutoCheckUpdate = "auto_check_update"
)

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

GetToken() *oauth2.Token // token is the detail of token, if the token is from OAuth2, it will be set
SetToken(token *oauth2.Token)

GetUser() string // nickname of user
SetUser(user string)
GetUsername() string // it is kind like id of user
Expand Down Expand Up @@ -72,29 +57,6 @@ func (c *config) SetTokenString(token string) {
viper.Set(KeyTokenString, token)
}

func (c *config) GetToken() *oauth2.Token {
token := &oauth2.Token{}
token.AccessToken = viper.GetString(KeyTokenAccess)
token.RefreshToken = viper.GetString(KeyTokenRefresh)
token.TokenType = viper.GetString(KeyTokenType)
token.Expiry = viper.GetTime(KeyTokenExpiry)
if token.AccessToken == "" || token.RefreshToken == "" || token.TokenType == "" || token.Expiry.IsZero() {
return nil
}
return token
}

func (c *config) SetToken(token *oauth2.Token) {
if token == nil {
viper.Set(KeyTokenDetail, "")
return
}
viper.Set(KeyTokenAccess, token.AccessToken)
viper.Set(KeyTokenRefresh, token.RefreshToken)
viper.Set(KeyTokenType, token.TokenType)
viper.Set(KeyTokenExpiry, token.Expiry)
}

func (c *config) GetUser() string {
return viper.GetString(KeyUser)
}
Expand Down

0 comments on commit 162cef4

Please sign in to comment.