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 refresh token support #196

Open
wants to merge 2 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
40 changes: 40 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Continuous Integration

on:
push:
branches: [ 'master' ]
pull_request:

permissions:
contents: read
pull-requests: read

jobs:

build:
runs-on: ubuntu-latest
steps:

- uses: actions/setup-go@v3
with:
go-version: '1.19'

- uses: actions/checkout@v3

- name: Cache build dependencies
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/_go.sum') }}

- name: Install dependencies
run: go get .

- name: Build
run: go build -o dbxcli -v

- uses: actions/upload-artifact@v3
with:
name: dbxcli
path: dbxcli

2 changes: 1 addition & 1 deletion cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func logout(cmd *cobra.Command, args []string) error {
for domain, tokens := range tokMap {
for _, token := range tokens {
config := dropbox.Config{
Token: token,
Token: token.AccessToken,
LogLevel: dropbox.LogOff,
Logger: nil,
AsMemberID: "",
Expand Down
24 changes: 18 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
// TokenMap maps domains to a map of commands to tokens.
// For each 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]oauth2.Token

var config dropbox.Config

Expand Down Expand Up @@ -169,12 +169,12 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
tokenMap = make(TokenMap)
}
if tokenMap[domain] == nil {
tokenMap[domain] = make(map[string]string)
tokenMap[domain] = make(map[string]oauth2.Token)
}
tokens := tokenMap[domain]

if err != nil || tokens[tokType] == "" {
fmt.Printf("1. Go to %v\n", conf.AuthCodeURL("state"))
if err != nil || tokens[tokType].AccessToken == "" {
fmt.Printf("1. Go to %v\n", conf.AuthCodeURL("state", oauth2.SetAuthURLParam("token_access_type", "offline")))
fmt.Printf("2. Click \"Allow\" (you might have to log in first).\n")
fmt.Printf("3. Copy the authorization code.\n")
fmt.Printf("Enter the authorization code here: ")
Expand All @@ -189,7 +189,19 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
if err != nil {
return
}
tokens[tokType] = token.AccessToken
tokens[tokType] = *token
writeTokens(filePath, tokenMap)
}
ctx := context.Background()

tmpToken := tokens[tokType]
refreshedToken, err := conf.TokenSource(ctx, &tmpToken).Token()
if err != nil {
return err
}

if refreshedToken.AccessToken != tmpToken.AccessToken || refreshedToken.Expiry != tmpToken.Expiry {
tokens[tokType] = *refreshedToken
writeTokens(filePath, tokenMap)
}

Expand All @@ -198,7 +210,7 @@ func initDbx(cmd *cobra.Command, args []string) (err error) {
logLevel = dropbox.LogInfo
}
config = dropbox.Config{
Token: tokens[tokType],
Token: tokens[tokType].AccessToken,
LogLevel: logLevel,
Logger: nil,
AsMemberID: asMember,
Expand Down