update to Dropbox Core SDK v4.0.0, add support for short-lived access tokens + refresh token (as the previous method will be removed)

This commit is contained in:
Philipp Crocoll
2021-06-28 10:56:04 +02:00
parent 712f476da8
commit d3db23d1b7
6 changed files with 74 additions and 17 deletions

View File

@@ -139,9 +139,6 @@
<ItemGroup>
<EmbeddedReferenceJar Include="Jars\jackson-core-2.7.4.jar" />
</ItemGroup>
<ItemGroup>
<EmbeddedJar Include="Jars\dropbox-core-sdk-3.1.1.jar" />
</ItemGroup>
<ItemGroup>
<EmbeddedReferenceJar Include="Jars\gson-2.8.1.jar" />
</ItemGroup>
@@ -154,4 +151,7 @@
<ItemGroup>
<EmbeddedReferenceJar Include="Jars\okhttp-4.10.0-RC1.jar" />
</ItemGroup>
<ItemGroup>
<EmbeddedJar Include="Jars\dropbox-core-sdk-4.0.0.jar" />
</ItemGroup>
</Project>

View File

@@ -26,7 +26,7 @@ NOTE: If you change dependencies here, don't forget to update the jar files in J
*/
dependencies {
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.squareup.okhttp3:okhttp:4.10.0-RC1'
compile 'com.burgstaller:okhttp-digest:2.5'
compile 'com.google.android.gms:play-services:4.0.30'
@@ -35,7 +35,7 @@ dependencies {
exclude group: 'com.google.android.google-play-services'
}
compile 'com.google.apis:google-api-services-drive:v2-rev102-1.16.0-rc'
compile 'com.dropbox.core:dropbox-core-sdk:3.1.1'
compile 'com.dropbox.core:dropbox-core-sdk:4.0.0'
//onedrive:
compile('com.onedrive.sdk:onedrive-sdk-android:1.2.0') {
transitive = false

View File

@@ -7,6 +7,8 @@ import com.dropbox.core.DbxOAuth1Upgrader;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.InvalidAccessTokenException;
import com.dropbox.core.android.Auth;
import com.dropbox.core.json.JsonReadException;
import com.dropbox.core.oauth.DbxCredential;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.http.OkHttp3Requestor;
import com.dropbox.core.v2.files.DeleteErrorException;
@@ -25,6 +27,7 @@ import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -39,17 +42,27 @@ import android.util.Log;
import android.widget.Toast;
class DbxRequestConfigFactory {
private static DbxRequestConfig sDbxRequestConfig;
public static DbxRequestConfig getRequestConfig() {
if (sDbxRequestConfig == null) {
sDbxRequestConfig = DbxRequestConfig.newBuilder("Keepass2Android")
.withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
.build();
}
return sDbxRequestConfig;
}
}
/**
* Created by Philipp on 18.11.2016.
*/
public class DropboxV2Storage extends JavaFileStorageBase
{
private List<String> scope = new ArrayList<String>(Arrays.asList("account_info.read", "files.metadata.write","files.content.write","files.content.read"));
private DbxAppInfo appInfo;
public void bla()
{
}
DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("kp2a")
.withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
.build();
@@ -60,6 +73,8 @@ public class DropboxV2Storage extends JavaFileStorageBase
final static private String ACCESS_KEY_V1_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_V1_NAME = "ACCESS_SECRET";
final static private String ACCESS_TOKEN_NAME = "ACCESS_TOKEN_V2";
//key for serialized dropbox credentials (used to store access + refresh tokens as long-living access tokens (v2) have been deprecated
final static private String SERIALIZED_CREDENTIALS = "SERIALIZED_CREDENTIALS";
private boolean mLoggedIn = false;
private Context mContext;
@@ -101,7 +116,9 @@ public class DropboxV2Storage extends JavaFileStorageBase
}
private void initialize(Context ctx, String _appKey, String _appSecret,
boolean clearKeysOnStart, AccessType accessType) {
boolean clearKeysOnStart, AccessType accessType)
{
Log.d("KP2A","Initializing Dropbox storage. Update for use with short-lived access tokens.");
appInfo = new DbxAppInfo(_appKey,_appSecret);
mContext = ctx;
@@ -117,7 +134,7 @@ public class DropboxV2Storage extends JavaFileStorageBase
public boolean tryConnect(Activity activity)
{
if (!mLoggedIn)
Auth.startOAuth2Authentication(activity, appInfo.getKey());
Auth.startOAuth2PKCE(activity, appInfo.getKey(), DbxRequestConfigFactory.getRequestConfig(), scope);
return mLoggedIn;
}
@@ -252,6 +269,22 @@ public class DropboxV2Storage extends JavaFileStorageBase
}
private DbxCredential getStoredCredential(){
SharedPreferences prefs = mContext.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String serialized = prefs.getString(SERIALIZED_CREDENTIALS, null);
if (serialized == null)
return null;
try {
return DbxCredential.Reader.readFully(serialized);
} catch (JsonReadException e) {
return null;
}
}
//stores a long-living access token from API v2
//New tokens of this kind will no longer be issued, but we have a v1-updater which converts v1 to v2 tokens so we should still be able to store them.
private void storeKey(String v2token) {
Log.d(TAG, "Storing Dropbox accessToken");
// Save the access key for later
@@ -270,6 +303,16 @@ public class DropboxV2Storage extends JavaFileStorageBase
private void buildSession() {
DbxCredential credential = getStoredCredential();
if (credential != null)
{
credential = new DbxCredential(credential.getAccessToken(), -1L, credential.getRefreshToken(), credential.getAppKey());
dbxClient = new DbxClientV2(DbxRequestConfigFactory.getRequestConfig(), credential);
return;
}
String v2Token = getKeyV2();
if (v2Token != null)
@@ -548,13 +591,13 @@ public class DropboxV2Storage extends JavaFileStorageBase
if (storageSetupAct.getState().containsKey("hasStartedAuth")) {
Log.d("KP2AJ", "auth started");
String v2Token = Auth.getOAuth2Token();
DbxCredential dbxCredential = Auth.getDbxCredential();
if (v2Token != null) {
if (dbxCredential != null) {
Log.d("KP2AJ", "auth successful");
try {
storeKey(v2Token);
storeCredentials(dbxCredential);
buildSession();
finishActivityWithSuccess(activity);
return;
@@ -574,12 +617,25 @@ public class DropboxV2Storage extends JavaFileStorageBase
((Activity) activity).finish();
} else {
Log.d("KP2AJ", "Starting auth");
Auth.startOAuth2Authentication((Activity) activity, appInfo.getKey());
Auth.startOAuth2PKCE((Activity) activity, appInfo.getKey(), DbxRequestConfigFactory.getRequestConfig(), scope);
Log.d("KP2AJ", "Started auth");
storageSetupAct.getState().putBoolean("hasStartedAuth", true);
Log.d("KP2AJ", "add state flag");
}
}
private void storeCredentials(DbxCredential dbxCredential)
{
Log.d(TAG, "Storing Dropbox credentials");
// Save the access key for later
SharedPreferences prefs = mContext.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(SERIALIZED_CREDENTIALS, dbxCredential.toString());
edit.commit();
}
@Override
public void onStart(FileStorageSetupActivity activity) {

View File

@@ -145,6 +145,7 @@ import java.util.ArrayList;
import java.util.List;
//import keepass2android.javafilestorage.DropboxCloudRailStorage;
import keepass2android.javafilestorage.DropboxV2Storage;
import keepass2android.javafilestorage.JavaFileStorage;
import keepass2android.javafilestorage.JavaFileStorage.FileEntry;
import keepass2android.javafilestorage.PCloudFileStorage;
@@ -539,7 +540,7 @@ public class MainActivity extends Activity implements JavaFileStorage.FileStorag
static JavaFileStorage createStorageToTest(Context ctx, Context appContext, boolean simulateRestart) {
//storageToTest = new SftpStorage(ctx.getApplicationContext());
storageToTest = new PCloudFileStorage(ctx, "yCeH59Ffgtm");
//storageToTest = new PCloudFileStorage(ctx, "yCeH59Ffgtm");
//storageToTest = new SkyDriveFileStorage("000000004010C234", appContext);
@@ -556,7 +557,7 @@ public class MainActivity extends Activity implements JavaFileStorage.FileStorag
}
});*/
//storageToTest = new DropboxV2Storage(ctx,"4ybka4p4a1027n6", "1z5lv528un9nre8", !simulateRestart);
storageToTest = new DropboxV2Storage(ctx,"4ybka4p4a1027n6", "3s86datjhkihwyc", !simulateRestart);
//storageToTest = new DropboxFileStorage(ctx,"4ybka4p4a1027n6", "1z5lv528un9nre8", !simulateRestart);
//storageToTest = new DropboxAppFolderFileStorage(ctx,"ax0268uydp1ya57", "3s86datjhkihwyc", true);