Skip to content

Commit 89d9b1b

Browse files
Merge pull request #237 from dropbox/use-bundled-personal-app-key
Use bundled personal app key for login without prompting
2 parents 8098a28 + 0da3fd2 commit 89d9b1b

4 files changed

Lines changed: 58 additions & 26 deletions

File tree

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ For newcomers the go build process can be a bit arcane, these steps can be follo
5454
3. `go get github.com/dropbox/dbxcli`. That's right, you don't manually clone it, this does it for you.
5555
4. `cd ~/go/src/github.com/dropbox/dbxcli` (adapt accordingly based on step 2).
5656

57-
Now we need to pause for a second to get development keys.
58-
59-
5. Head to `https://www.dropbox.com/developers/apps` (sign in if necessary) and choose "Create app". Use the Dropbox API and give it Full Dropbox access. Name and create the app.
60-
6. You'll be presented with a dashboard with an "App key".
61-
7. Provide the app key when running `dbxcli`:
57+
To use your own Dropbox app while developing, provide its app key when running
58+
`dbxcli`:
6259
```sh
6360
$ export DROPBOX_PERSONAL_APP_KEY=your-app-key
6461
```
@@ -116,15 +113,14 @@ $ dbxcli login
116113
Commands require saved credentials. If no saved credentials are available, run
117114
`dbxcli login` first or provide a token with `DBXCLI_ACCESS_TOKEN`.
118115

119-
If the bundled app credentials are still in use, `dbxcli` asks for your Dropbox
120-
app key before printing the authorization URL. You can pass the app key as an
121-
option:
116+
Personal login uses the bundled Dropbox app key by default. You can pass a
117+
custom app key as an option:
122118

123119
```sh
124120
$ dbxcli login --app-key=your-app-key
125121
```
126122

127-
You can also set it with an environment variable to skip the prompt:
123+
You can also set it with an environment variable:
128124

129125
```sh
130126
$ DROPBOX_PERSONAL_APP_KEY=your-app-key dbxcli login

cmd/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func loginCommand(tokType string) string {
257257
case tokenTeamManage:
258258
return "dbxcli login team-manage --app-key=<your-app-key>"
259259
default:
260-
return "dbxcli login --app-key=<your-app-key>"
260+
return "dbxcli login"
261261
}
262262
}
263263

cmd/auth_test.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func TestGetAccessTokenRefreshFailureLeavesAuthFileUnchanged(t *testing.T) {
371371
if err == nil {
372372
t.Fatal("expected refresh failure")
373373
}
374-
if !strings.Contains(err.Error(), "dbxcli login --app-key=<your-app-key>") {
374+
if !strings.Contains(err.Error(), "dbxcli login") {
375375
t.Fatalf("expected login hint, got %q", err)
376376
}
377377

@@ -384,7 +384,7 @@ func TestGetAccessTokenRefreshFailureLeavesAuthFileUnchanged(t *testing.T) {
384384
}
385385
}
386386

387-
func TestGetAccessTokenMissingTokenWithBundledCredentialsReturnsLoginError(t *testing.T) {
387+
func TestGetAccessTokenMissingTokenWithDefaultPersonalCredentialsReturnsLoginError(t *testing.T) {
388388
restoreOAuthCredentials(t)
389389
setOAuthCredentials(tokenPersonal, defaultPersonalAppKey)
390390

@@ -415,7 +415,7 @@ func TestGetAccessTokenMissingTokenWithBundledCredentialsReturnsLoginError(t *te
415415
if err == nil {
416416
t.Fatal("expected missing credentials error")
417417
}
418-
if !strings.Contains(err.Error(), "dbxcli login --app-key=<your-app-key>") {
418+
if !strings.Contains(err.Error(), "dbxcli login") {
419419
t.Fatalf("expected login hint, got %q", err)
420420
}
421421
if !strings.Contains(err.Error(), envAccessToken) {
@@ -454,7 +454,7 @@ func TestGetAccessTokenMissingTokenWithConfiguredAppKeyReturnsLoginError(t *test
454454
if err == nil {
455455
t.Fatal("expected missing credentials error")
456456
}
457-
if !strings.Contains(err.Error(), "dbxcli login --app-key=<your-app-key>") {
457+
if !strings.Contains(err.Error(), "dbxcli login") {
458458
t.Fatalf("expected login hint, got %q", err)
459459
}
460460
}
@@ -464,7 +464,7 @@ func TestLoginCommandForTokenType(t *testing.T) {
464464
tokType string
465465
want string
466466
}{
467-
{tokenPersonal, "dbxcli login --app-key=<your-app-key>"},
467+
{tokenPersonal, "dbxcli login"},
468468
{tokenTeamAccess, "dbxcli login team-access --app-key=<your-app-key>"},
469469
{tokenTeamManage, "dbxcli login team-manage --app-key=<your-app-key>"},
470470
}
@@ -543,9 +543,9 @@ func TestRequestAccessTokenReturnsReadError(t *testing.T) {
543543
}
544544
}
545545

546-
func TestRequestAccessTokenPromptsForAppKeyWhenUsingBundledDefaults(t *testing.T) {
546+
func TestRequestAccessTokenPromptsForAppKeyWhenUsingBundledTeamDefaults(t *testing.T) {
547547
restoreOAuthCredentials(t)
548-
setOAuthCredentials(tokenPersonal, defaultPersonalAppKey)
548+
setOAuthCredentials(tokenTeamManage, defaultTeamManageAppKey)
549549

550550
origReadAuthorizationCode := readAuthorizationCode
551551
origExchangeAuthorizationCode := exchangeAuthorizationCode
@@ -555,8 +555,8 @@ func TestRequestAccessTokenPromptsForAppKeyWhenUsingBundledDefaults(t *testing.T
555555
})
556556

557557
readAppCredentials = func(tokType string) (appCredentials, error) {
558-
if tokType != tokenPersonal {
559-
t.Fatalf("expected personal app credentials prompt, got %q", tokType)
558+
if tokType != tokenTeamManage {
559+
t.Fatalf("expected team manage app credentials prompt, got %q", tokType)
560560
}
561561
return appCredentials{Key: "prompt-key"}, nil
562562
}
@@ -573,15 +573,48 @@ func TestRequestAccessTokenPromptsForAppKeyWhenUsingBundledDefaults(t *testing.T
573573
return &oauth2.Token{AccessToken: "access-token", RefreshToken: "refresh-token", TokenType: "Bearer", Expiry: time.Now().Add(time.Hour)}, nil
574574
}
575575

576-
token, err := requestAccessToken(tokenPersonal, "")
576+
token, err := requestAccessToken(tokenTeamManage, "")
577577
if err != nil {
578578
t.Fatal(err)
579579
}
580580
if token != "access-token" {
581581
t.Fatalf("expected access token, got %q", token)
582582
}
583-
if personalAppKey != "prompt-key" {
584-
t.Fatalf("expected prompted app key to be saved for this process, got %q", personalAppKey)
583+
if teamManageAppKey != "prompt-key" {
584+
t.Fatalf("expected prompted app key to be saved for this process, got %q", teamManageAppKey)
585+
}
586+
}
587+
588+
func TestRequestAccessTokenUsesDefaultPersonalAppKey(t *testing.T) {
589+
restoreOAuthCredentials(t)
590+
setOAuthCredentials(tokenPersonal, defaultPersonalAppKey)
591+
592+
origReadAuthorizationCode := readAuthorizationCode
593+
origExchangeAuthorizationCode := exchangeAuthorizationCode
594+
t.Cleanup(func() {
595+
readAuthorizationCode = origReadAuthorizationCode
596+
exchangeAuthorizationCode = origExchangeAuthorizationCode
597+
})
598+
599+
readAppCredentials = func(tokType string) (appCredentials, error) {
600+
t.Fatal("app credential prompt should not be used for the default personal app key")
601+
return appCredentials{}, nil
602+
}
603+
readAuthorizationCode = func() (string, error) {
604+
return "auth-code", nil
605+
}
606+
exchangeAuthorizationCode = func(ctx context.Context, conf *oauth2.Config, code string, verifier string) (*oauth2.Token, error) {
607+
if conf.ClientID != defaultPersonalAppKey {
608+
t.Fatalf("expected default personal app key, got %q", conf.ClientID)
609+
}
610+
if conf.ClientSecret != "" {
611+
t.Fatalf("expected no client secret for PKCE, got %q", conf.ClientSecret)
612+
}
613+
return &oauth2.Token{AccessToken: "access-token", RefreshToken: "refresh-token", TokenType: "Bearer", Expiry: time.Now().Add(time.Hour)}, nil
614+
}
615+
616+
if _, err := requestAccessToken(tokenPersonal, ""); err != nil {
617+
t.Fatal(err)
585618
}
586619
}
587620

@@ -691,7 +724,7 @@ func TestRequestAccessTokenUsesConfiguredAppCredentials(t *testing.T) {
691724

692725
func TestRequestAccessTokenRejectsEmptyAppCredentials(t *testing.T) {
693726
restoreOAuthCredentials(t)
694-
setOAuthCredentials(tokenPersonal, defaultPersonalAppKey)
727+
setOAuthCredentials(tokenTeamManage, defaultTeamManageAppKey)
695728

696729
origReadAuthorizationCode := readAuthorizationCode
697730
origExchangeAuthorizationCode := exchangeAuthorizationCode
@@ -712,7 +745,7 @@ func TestRequestAccessTokenRejectsEmptyAppCredentials(t *testing.T) {
712745
return nil, nil
713746
}
714747

715-
if _, err := requestAccessToken(tokenPersonal, ""); err == nil {
748+
if _, err := requestAccessToken(tokenTeamManage, ""); err == nil {
716749
t.Fatal("expected empty app credentials to fail")
717750
}
718751
}

cmd/root.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
tokenTeamAccess = "teamAccess"
3030
tokenTeamManage = "teamManage"
3131

32-
defaultPersonalAppKey = "mvhz183vwqibe7q"
32+
defaultPersonalAppKey = "07o23gulcj8qi69"
3333
defaultTeamAccessAppKey = "zud1va492pnehkc"
3434
defaultTeamManageAppKey = "xxe04eai4wmlitv"
3535
)
@@ -89,10 +89,13 @@ func setOAuthCredentials(tokenType string, appKey string) {
8989

9090
func needsOAuthCredentialsOverride(tokenType string) bool {
9191
appKey := oauthCredentials(tokenType)
92-
defaultAppKey := defaultOAuthCredentials(tokenType)
9392
if appKey == "" {
9493
return true
9594
}
95+
if tokenType == tokenPersonal {
96+
return false
97+
}
98+
defaultAppKey := defaultOAuthCredentials(tokenType)
9699
return defaultAppKey != "" && appKey == defaultAppKey
97100
}
98101

0 commit comments

Comments
 (0)