Skip to content

Commit bd602a1

Browse files
committed
Fix bug to make DAuth work.
1 parent 2e485be commit bd602a1

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

examples/android/src/main/java/com/dropbox/core/examples/android/DropboxActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dropbox.core.examples.android;
22

3+
import android.content.Context;
34
import android.content.SharedPreferences;
45
import android.support.v7.app.AppCompatActivity;
56

@@ -41,6 +42,7 @@ protected void onResume() {
4142
throw new IllegalStateException("Credential data corrupted: " + e.getMessage());
4243
}
4344
}
45+
4446
} else {
4547
String accessToken = prefs.getString("access-token", null);
4648
if (accessToken == null) {
@@ -85,11 +87,11 @@ protected boolean hasToken() {
8587
}
8688
}
8789

88-
public static void startOAuth2Authentication(Context context) {
90+
public static void startOAuth2Authentication(Context context, String app_key, String scope) {
8991
if (USE_SLT) {
90-
Auth.startOAuth2PKCE(context, getString(R.string.app_key), DbxRequestConfigFactory.getRequestConfig());
92+
Auth.startOAuth2PKCE(context, app_key, DbxRequestConfigFactory.getRequestConfig(), scope);
9193
} else {
92-
Auth.startOAuth2Authentication(context, getString(R.string.app_key));
94+
Auth.startOAuth2Authentication(context, app_key);
9395
}
9496
}
9597
}

examples/android/src/main/java/com/dropbox/core/examples/android/UserActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import android.widget.Button;
99
import android.widget.TextView;
1010

11-
import com.dropbox.core.android.Auth;
1211
import com.dropbox.core.examples.android.internal.OpenWithActivity;
1312
import com.dropbox.core.v2.users.FullAccount;
1413

@@ -32,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) {
3231
loginButton.setOnClickListener(new View.OnClickListener() {
3332
@Override
3433
public void onClick(View v) {
35-
DropboxActivity.startOAuth2Authentication(UserActivity.this);
34+
DropboxActivity.startOAuth2Authentication(UserActivity.this, getString(R.string.app_key), null);
3635
}
3736
});
3837

src/main/java/com/dropbox/core/android/Auth.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ public static void startOAuth2Authentication(Context context, String appKey, Str
4141
*/
4242
public static void startOAuth2PKCE(Context context, String appKey,
4343
DbxRequestConfig requestConfig) {
44-
startOAuth2PKCE(context, appKey, requestConfig, null);
44+
startOAuth2PKCE(context, appKey, requestConfig, null, null);
45+
}
46+
47+
/**
48+
* <b>Beta</b>: This feature is not available to all developers. Please do NOT use it unless you are
49+
* early access partner of this feature. The function signature is subject to change
50+
* in next minor version release.
51+
*
52+
* @param scope A list of scope returned by Dropbox server. Each scope correspond to a group of
53+
* API endpoints. To call one API endpoint you have to obtains the scope first otherwise you
54+
* will get HTTP 401.
55+
* @see Auth#startOAuth2PKCE(Context, String, DbxRequestConfig, DbxHost)
56+
*/
57+
public static void startOAuth2PKCE(Context context, String appKey, DbxRequestConfig
58+
requestConfig, String scope) {
59+
startOAuth2PKCE(context, appKey, requestConfig, null, scope);
4560
}
4661

4762
/**
@@ -69,6 +84,15 @@ public static void startOAuth2PKCE(Context context, String appKey, DbxRequestCon
6984
startOAuth2Authentication(context, appKey, null, null, null, null, TokenAccessType
7085
.OFFLINE, requestConfig, host);
7186
}
87+
88+
public static void startOAuth2PKCE(Context context, String appKey, DbxRequestConfig
89+
requestConfig, DbxHost host, String scope) {
90+
if (requestConfig == null) {
91+
throw new IllegalArgumentException("Invalid Dbx requestConfig for PKCE flow.");
92+
}
93+
startOAuth2Authentication(context, appKey, null, null, null, null, TokenAccessType
94+
.OFFLINE, requestConfig, host, scope);
95+
}
7296

7397
/**
7498
* Starts the Dropbox authentication process by launching an external app
@@ -120,6 +144,20 @@ private static void startOAuth2Authentication(Context context,
120144
TokenAccessType tokenAccessType,
121145
DbxRequestConfig requestConfig,
122146
DbxHost host) {
147+
startOAuth2Authentication(context, appKey, desiredUid, alreadyAuthedUids, sessionId,
148+
webHost, tokenAccessType, requestConfig, host, null);
149+
}
150+
151+
private static void startOAuth2Authentication(Context context,
152+
String appKey,
153+
String desiredUid,
154+
String[] alreadyAuthedUids,
155+
String sessionId,
156+
String webHost,
157+
TokenAccessType tokenAccessType,
158+
DbxRequestConfig requestConfig,
159+
DbxHost host,
160+
String scope) {
123161
if (!AuthActivity.checkAppBeforeAuth(context, appKey, true /*alertUser*/)) {
124162
return;
125163
}
@@ -132,7 +170,7 @@ private static void startOAuth2Authentication(Context context,
132170
String apiType = "1";
133171
Intent intent = AuthActivity.makeIntent(
134172
context, appKey, desiredUid, alreadyAuthedUids, sessionId, webHost, apiType,
135-
tokenAccessType, requestConfig, host
173+
tokenAccessType, requestConfig, host, scope
136174
);
137175
if (!(context instanceof Activity)) {
138176
// If starting the intent outside of an Activity, must include
@@ -193,4 +231,25 @@ public static DbxCredential getDbxCredential() {
193231

194232
return new DbxCredential(secret, nullableExpiresAt, refreshToken, appKey);
195233
}
234+
235+
/**
236+
* <b>Beta</b>: This feature is not available to all developers. Please do NOT use it unless you are
237+
* early access partner of this feature. The function signature is subject to change
238+
* in next minor version release.
239+
*
240+
* Get the scope authorized in this OAuth flow.
241+
*
242+
* @return A list of scope returned by Dropbox server. Each scope correspond to a group of
243+
* API endpoints. To call one API endpoint you have to obtains the scope first otherwise you
244+
* will get HTTP 401.
245+
*/
246+
public static String getScope() {
247+
Intent data = AuthActivity.result;
248+
249+
if (data == null) {
250+
return null;
251+
}
252+
253+
return data.getStringExtra(AuthActivity.EXTRA_SCOPE);
254+
}
196255
}

src/main/java/com/dropbox/core/android/AuthActivity.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public class AuthActivity extends Activity {
7272

7373
public static final String EXTRA_EXPIRES_AT = "EXPIRES_AT";
7474

75+
public static final String EXTRA_SCOPE = "SCOPE";
76+
7577
/**
7678
* Used for internal authentication. You won't ever have to use this.
7779
*/
@@ -181,6 +183,7 @@ public SecureRandom getSecureRandom() {
181183
private static TokenAccessType sTokenAccessType;
182184
private static DbxRequestConfig sRequestConfig;
183185
private static DbxHost sHost;
186+
private static String sScope;
184187

185188
// These instance variables need not be stored in savedInstanceState as onNewIntent()
186189
// does not read them.
@@ -192,7 +195,8 @@ public SecureRandom getSecureRandom() {
192195
private TokenAccessType mTokenAccessType;
193196
private DbxPKCEManager mPKCEManager;
194197
private DbxRequestConfig mRequestConfig;
195-
private static DbxHost mHost;
198+
private DbxHost mHost;
199+
private String mScope;
196200

197201
// Stored in savedInstanceState to track an ongoing auth attempt, which
198202
// must include a locally-generated nonce in the response.
@@ -215,7 +219,8 @@ static void setAuthParams(String appKey, String desiredUid,
215219
*/
216220
static void setAuthParams(String appKey, String desiredUid,
217221
String[] alreadyAuthedUids, String webHost, String apiType) {
218-
setAuthParams(appKey, desiredUid, alreadyAuthedUids, null, null, null, null, null, null);
222+
setAuthParams(appKey, desiredUid, alreadyAuthedUids, null, null, null, null, null, null,
223+
null);
219224
}
220225

221226
/**
@@ -224,7 +229,7 @@ static void setAuthParams(String appKey, String desiredUid,
224229
static void setAuthParams(String appKey, String desiredUid,
225230
String[] alreadyAuthedUids, String sessionId) {
226231
setAuthParams(appKey, desiredUid, alreadyAuthedUids, sessionId, null, null, null, null,
227-
null);
232+
null, null);
228233
}
229234

230235
/**
@@ -234,7 +239,7 @@ static void setAuthParams(String appKey, String desiredUid,
234239
static void setAuthParams(String appKey, String desiredUid,
235240
String[] alreadyAuthedUids, String sessionId, String webHost,
236241
String apiType, TokenAccessType tokenAccessType,
237-
DbxRequestConfig requestConfig, DbxHost host) {
242+
DbxRequestConfig requestConfig, DbxHost host, String scope) {
238243
sAppKey = appKey;
239244
sDesiredUid = desiredUid;
240245
sAlreadyAuthedUids = (alreadyAuthedUids != null) ? alreadyAuthedUids : new String[0];
@@ -252,6 +257,7 @@ static void setAuthParams(String appKey, String desiredUid,
252257
} else {
253258
sHost = DbxHost.DEFAULT;
254259
}
260+
sScope = scope;
255261
}
256262

257263
/**
@@ -267,7 +273,8 @@ static void setAuthParams(String appKey, String desiredUid,
267273
*/
268274
public static Intent makeIntent(Context context, String appKey, String webHost,
269275
String apiType) {
270-
return makeIntent(context, appKey, null, null, null, webHost, apiType, null, null, null);
276+
return makeIntent(context, appKey, null, null, null, webHost, apiType, null, null, null,
277+
null);
271278
}
272279

273280
/**
@@ -296,7 +303,7 @@ public static Intent makeIntent(Context context, String appKey, String desiredUi
296303
throw new IllegalArgumentException("'appKey' can't be null");
297304
}
298305
setAuthParams(appKey, desiredUid, alreadyAuthedUids, sessionId, webHost, apiType, null,
299-
null, null);
306+
null, null, null);
300307
return new Intent(context, AuthActivity.class);
301308
}
302309

@@ -306,12 +313,12 @@ public static Intent makeIntent(Context context, String appKey, String desiredUi
306313
static Intent makeIntent(
307314
Context context, String appKey, String desiredUid, String[] alreadyAuthedUids,
308315
String sessionId, String webHost, String apiType, TokenAccessType tokenAccessType,
309-
DbxRequestConfig requestConfig, DbxHost host
316+
DbxRequestConfig requestConfig, DbxHost host, String scope
310317
) {
311318
if (appKey == null) throw new IllegalArgumentException("'appKey' can't be null");
312319
setAuthParams(
313320
appKey, desiredUid, alreadyAuthedUids, sessionId, webHost, apiType, tokenAccessType,
314-
requestConfig, host
321+
requestConfig, host, scope
315322
);
316323
return new Intent(context, AuthActivity.class);
317324
}
@@ -429,6 +436,7 @@ protected void onCreate(Bundle savedInstanceState) {
429436
mTokenAccessType = sTokenAccessType;
430437
mRequestConfig = sRequestConfig;
431438
mHost = sHost;
439+
mScope = sScope;
432440

433441
if (savedInstanceState == null) {
434442
result = null;
@@ -626,6 +634,7 @@ protected void onNewIntent(Intent intent) {
626634
newResult.putExtra(EXTRA_EXPIRES_AT, dbxAuthFinish.getExpiresAt());
627635
newResult.putExtra(EXTRA_UID, dbxAuthFinish.getUserId());
628636
newResult.putExtra(EXTRA_CONSUMER_KEY, mAppKey);
637+
newResult.putExtra(EXTRA_SCOPE, dbxAuthFinish.getScope());
629638
}
630639
} catch (Exception e) {
631640
newResult = null;
@@ -637,7 +646,6 @@ protected void onNewIntent(Intent intent) {
637646
// Unsuccessful auth, or missing required parameters.
638647
newResult = null;
639648
}
640-
641649
authFinished(newResult);
642650
}
643651

@@ -692,13 +700,19 @@ private String createPKCEStateNonce() {
692700
}
693701

694702
private String createExtraQueryParams() {
695-
return String.format(
703+
String param = String.format(Locale.US,
696704
"%s=%s&%s=%s&%s=%s&%s=%s",
697705
"code_challenge", mPKCEManager.getCodeChallenge(),
698706
"code_challenge_method", DbxPKCEManager.CODE_CHALLENGE_METHODS,
699707
"token_access_type", mTokenAccessType.toString(),
700708
"response_type", "code"
701709
);
710+
711+
if (mScope != null) {
712+
param += String.format(Locale.US, "%s=%s", "scope", mScope);
713+
}
714+
715+
return param;
702716
}
703717

704718
private enum TokenType {

0 commit comments

Comments
 (0)