Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Token Profiles #165

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.11.x"
- "1.16.x"

before_script:
- go get -u github.com/mitchellh/gox
Expand Down
23 changes: 12 additions & 11 deletions cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,27 @@
package cmd

import (
"os"
"path"
"fmt"

"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/auth"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// Command logout revokes all saved API tokens and deletes auth.json.
func logout(cmd *cobra.Command, args []string) error {
dir, err := homedir.Dir()
if err != nil {
return err
}
filePath := path.Join(dir, ".config", "dbxcli", configFileName)

tokMap, err := readTokens(filePath)
tokMap, err := readTokens()
if err != nil {
return err
}
profileTokMap := tokMap[viper.GetString("profile")]
if profileTokMap == nil {
return fmt.Errorf("cannot find profile")
}

for domain, tokens := range tokMap {
for domain, tokens := range profileTokMap {
for _, token := range tokens {
config := dropbox.Config{
Token: token,
Expand All @@ -57,7 +55,10 @@ func logout(cmd *cobra.Command, args []string) error {
}
}

return os.Remove(filePath)
delete(tokMap, viper.GetString("profile"))
writeTokens(tokMap)

return nil
}

// logoutCmd represents the logout command
Expand Down
126 changes: 65 additions & 61 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"golang.org/x/oauth2"
Expand All @@ -29,24 +26,17 @@ import (
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/files"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
)

const (
configFileName = "auth.json"
configFileName = "dbxcli"
tokenPersonal = "personal"
tokenTeamAccess = "teamAccess"
tokenTeamManage = "teamManage"
)

func getEnv(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = fallback
}
return value
}

var (
personalAppKey = "mvhz183vwqibe7q"
personalAppSecret = "q0kquhzgetjwcz1"
Expand All @@ -56,22 +46,22 @@ var (
teamManageAppSecret = "t8ms714yun7nu5s"
)

// TokenMap maps domains to a map of commands to tokens.
// For each domain, we want to save different tokens depending on the
// TokenMap maps profiles to domain map to a map of commands to tokens.
// For each profile and domain, we want to save different tokens depending on the
// command type: personal, team access and team manage
type TokenMap map[string]map[string]string
type TokenMap map[string]map[string]map[string]string

var config dropbox.Config

func oauthConfig(tokenType string, domain string) *oauth2.Config {
var appKey, appSecret string
switch tokenType {
case "personal":
appKey, appSecret = personalAppKey, personalAppSecret
appKey, appSecret = viper.GetString("app_key_personal"), viper.GetString("app_secret_personal")
case "teamAccess":
appKey, appSecret = teamAccessAppKey, teamAccessAppSecret
appKey, appSecret = viper.GetString("app_key_team_access"), viper.GetString("app_secret_team_access")
case "teamManage":
appKey, appSecret = teamManageAppKey, teamManageAppSecret
appKey, appSecret = viper.GetString("app_key_team_manage"), viper.GetString("app_secret_team_manage")
}
return &oauth2.Config{
ClientID: appKey,
Expand Down Expand Up @@ -107,36 +97,17 @@ func makeRelocationArg(s string, d string) (arg *files.RelocationArg, err error)
return
}

func readTokens(filePath string) (TokenMap, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}

func readTokens() (TokenMap, error) {
var tokens TokenMap
if json.Unmarshal(b, &tokens) != nil {
if err := viper.UnmarshalKey("tokens", &tokens); err != nil {
return nil, err
}

return tokens, nil
}

func writeTokens(filePath string, tokens TokenMap) {
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// Doesn't exist; lets create it
err = os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
return
}
}

// At this point, file must exist. Lets (over)write it.
b, err := json.Marshal(tokens)
if err != nil {
return
}
if err = ioutil.WriteFile(filePath, b, 0600); err != nil {
func writeTokens(tokens TokenMap) {
viper.Set("tokens", tokens)
if err := viper.WriteConfig(); err != nil {
return
}
}
Expand All @@ -154,24 +125,22 @@ func tokenType(cmd *cobra.Command) string {
func initDbx(cmd *cobra.Command, args []string) (err error) {
verbose, _ := cmd.Flags().GetBool("verbose")
asMember, _ := cmd.Flags().GetString("as-member")
domain, _ := cmd.Flags().GetString("domain")

dir, err := homedir.Dir()
if err != nil {
return
}
filePath := path.Join(dir, ".config", "dbxcli", configFileName)
tokType := tokenType(cmd)
conf := oauthConfig(tokType, domain)
conf := oauthConfig(tokType, viper.GetString("domain"))

tokenMap, err := readTokens(filePath)
tokenMap, err := readTokens()
if tokenMap == nil {
tokenMap = make(TokenMap)
}
if tokenMap[domain] == nil {
tokenMap[domain] = make(map[string]string)
if tokenMap[viper.GetString("profile")] == nil {
tokenMap[viper.GetString("profile")] = make(map[string]map[string]string)
}
tokens := tokenMap[domain]
profileTokens := tokenMap[viper.GetString("profile")]
if profileTokens[viper.GetString("domain")] == nil {
profileTokens[viper.GetString("domain")] = make(map[string]string)
}
tokens := profileTokens[viper.GetString("domain")]

if err != nil || tokens[tokType] == "" {
fmt.Printf("1. Go to %v\n", conf.AuthCodeURL("state"))
Expand All @@ -190,7 +159,7 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
return
}
tokens[tokType] = token.AccessToken
writeTokens(filePath, tokenMap)
writeTokens(tokenMap)
}

logLevel := dropbox.LogOff
Expand All @@ -202,7 +171,7 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
LogLevel: logLevel,
Logger: nil,
AsMemberID: asMember,
Domain: domain,
Domain: viper.GetString("domain"),
Client: nil,
HeaderGenerator: nil,
URLGenerator: nil,
Expand Down Expand Up @@ -230,16 +199,51 @@ func Execute() {
}

func init() {

dir, err := homedir.Dir()
if err != nil {
return
}
viper.SetConfigName(configFileName)
viper.SetConfigType("json")

// default configuration path
viper.AddConfigPath(path.Join(dir, ".config"))
// short configuration path (useful for docker and testing)
viper.AddConfigPath("/config/")
// super useful for testing
viper.AddConfigPath(".")

viper.SetDefault("app_key_personal", personalAppKey)
viper.SetDefault("app_secret_personal", personalAppSecret)
viper.SetDefault("app_key_team_access", teamAccessAppKey)
viper.SetDefault("app_secret_team_access", teamAccessAppSecret)
viper.SetDefault("app_key_team_manage", teamManageAppKey)
viper.SetDefault("app_secret_team_manage", teamManageAppSecret)
viper.SetDefault("domain", "")

viper.BindEnv("app_key_personal", "DROPBOX_PERSONAL_APP_KEY")
viper.BindEnv("app_secret_personal", "DROPBOX_PERSONAL_APP_SECRET")
viper.BindEnv("app_key_team_access", "DROPBOX_TEAM_APP_KEY")
viper.BindEnv("app_secret_team_access", "DROPBOX_TEAM_APP_SECRET")
viper.BindEnv("app_key_team_manage", "DROPBOX_MANAGE_APP_KEY")
viper.BindEnv("app_secret_team_manage", "DROPBOX_MANAGE_APP_SECRET")
viper.BindEnv("domain", "DROPBOX_DOMAIN")

RootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose logging")
RootCmd.PersistentFlags().String("as-member", "", "Member ID to perform action as")
RootCmd.PersistentFlags().String("profile", "default", "Set the configuration profile [for tokens]")
// This flag should only be used for testing. Marked hidden so it doesn't clutter usage etc.
RootCmd.PersistentFlags().String("domain", "", "Override default Dropbox domain, useful for testing")
RootCmd.PersistentFlags().MarkHidden("domain")

personalAppKey = getEnv("DROPBOX_PERSONAL_APP_KEY", personalAppKey)
personalAppSecret = getEnv("DROPBOX_PERSONAL_APP_SECRET", personalAppSecret)
teamAccessAppKey = getEnv("DROPBOX_TEAM_APP_KEY", teamAccessAppKey)
teamAccessAppSecret = getEnv("DROPBOX_TEAM_APP_SECRET", teamAccessAppSecret)
teamManageAppKey = getEnv("DROPBOX_MANAGE_APP_KEY", teamManageAppKey)
teamManageAppSecret = getEnv("DROPBOX_MANAGE_APP_SECRET", teamAccessAppSecret)
viper.BindPFlag("domain", RootCmd.PersistentFlags().Lookup("domain"))
viper.BindPFlag("profile", RootCmd.PersistentFlags().Lookup("profile"))

viper.ReadInConfig()

if err := viper.SafeWriteConfig(); err != nil {
return
}

}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dropbox/dbxcli

go 1.11
go 1.16

require (
github.com/dropbox/dropbox-sdk-go-unofficial v1.0.1-0.20210112084502-47d7ce03959a
Expand All @@ -11,6 +11,7 @@ require (
github.com/mitchellh/ioprogress v0.0.0-20180201004757-6a23b12fa88e
github.com/spf13/cobra v0.0.4-0.20190109003409-7547e83b2d85
github.com/spf13/pflag v1.0.3 // indirect
github.com/spf13/viper v1.7.1 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5
Expand Down
Loading