add support for Google Drive with restricted scope, closes #622

This commit is contained in:
Philipp Crocoll
2022-02-02 03:50:51 +01:00
parent 26f0ab6661
commit 35f74f5ea4
11 changed files with 85 additions and 18 deletions

View File

@@ -17,7 +17,7 @@ namespace keepass2android.Io
public class GoogleDriveFileStorage : JavaFileStorage
{
public GoogleDriveFileStorage(Context ctx, IKp2aApp app) :
base(new Keepass2android.Javafilestorage.GoogleDriveFileStorage(), app)
base(new Keepass2android.Javafilestorage.GoogleDriveFullFileStorage(), app)
{
}
@@ -27,5 +27,19 @@ namespace keepass2android.Io
get { return false; }
}
}
public class GoogleDriveAppDataFileStorage : JavaFileStorage
{
public GoogleDriveAppDataFileStorage(Context ctx, IKp2aApp app) :
base(new Keepass2android.Javafilestorage.GoogleDriveAppDataFileStorage(), app)
{
}
public override bool UserShouldBackup
{
get { return false; }
}
}
}
#endif

View File

@@ -2998,7 +2998,10 @@ namespace keepass2android
public static int config_tooltipAnimTime = 2131230727;
// aapt resource value: 0x7F080008
public static int status_bar_notification_info_maxnum = 2131230728;
public static int google_play_services_version = 2131230728;
// aapt resource value: 0x7F080009
public static int status_bar_notification_info_maxnum = 2131230729;
static Integer()
{
@@ -3560,13 +3563,16 @@ namespace keepass2android
public static int app_name = 2131492964;
// aapt resource value: 0x7F0C0065
public static int library_name = 2131492965;
public static int common_google_play_services_unknown_issue = 2131492965;
// aapt resource value: 0x7F0C0066
public static int search_menu_title = 2131492966;
public static int library_name = 2131492966;
// aapt resource value: 0x7F0C0067
public static int status_bar_notification_info_overflow = 2131492967;
public static int search_menu_title = 2131492967;
// aapt resource value: 0x7F0C0068
public static int status_bar_notification_info_overflow = 2131492968;
static String()
{

View File

@@ -0,0 +1,18 @@
package keepass2android.javafilestorage;
import com.google.android.gms.common.Scopes;
public class GoogleDriveAppDataFileStorage extends GoogleDriveBaseFileStorage
{
private static final String GDRIVE_PROTOCOL_ID = "gdriveKP2A";
@Override
protected String getScopeString() {
return Scopes.DRIVE_FILE;
}
@Override
public String getProtocolId() {
return GDRIVE_PROTOCOL_ID;
}
}

View File

@@ -55,9 +55,9 @@ import com.google.android.gms.tasks.Task;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
public class GoogleDriveFileStorage extends JavaFileStorageBase {
public abstract class GoogleDriveBaseFileStorage extends JavaFileStorageBase {
private static final String GDRIVE_PROTOCOL_ID = "gdrive";
private static final String FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
static final int MAGIC_GDRIVE=2082334;
static final int REQUEST_ACCOUNT_PICKER = MAGIC_GDRIVE+1;
@@ -89,7 +89,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
public String getRootPathForAccount(String accountName) throws UnsupportedEncodingException {
return GDRIVE_PROTOCOL_ID+"://"+encode(accountName)+"/";
return getProtocolId()+"://"+encode(accountName)+"/";
}
class GDrivePath
@@ -276,7 +276,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
};
public GoogleDriveFileStorage()
public GoogleDriveBaseFileStorage()
{
logDebug("Creating GDrive FileStorage.");
}
@@ -612,7 +612,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
try {
// Signed in successfully
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
if (GoogleSignIn.hasPermissions(account, new Scope(Scopes.DRIVE_FULL))) {
if (GoogleSignIn.hasPermissions(account, getScope())) {
initializeAccountOrPath(setupAct, account.getAccount().name);
return;
@@ -851,10 +851,6 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
}
@Override
public String getProtocolId() {
return GDRIVE_PROTOCOL_ID;
}
@Override
@@ -882,7 +878,7 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.DRIVE_FULL))
.requestScopes(getScope())
.requestEmail()
.build();
// [END configure_signin]
@@ -922,10 +918,15 @@ public class GoogleDriveFileStorage extends JavaFileStorageBase {
}
protected Scope getScope() {
return new Scope(getScopeString());
}
protected abstract String getScopeString();
private GoogleAccountCredential createCredential(Context appContext) {
List<String> scopes = new ArrayList<String>();
scopes.add(DriveScopes.DRIVE);
scopes.add(getScopeString());
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(appContext, scopes);
return credential;
}

View File

@@ -0,0 +1,20 @@
package keepass2android.javafilestorage;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.Scope;
public class GoogleDriveFullFileStorage extends GoogleDriveBaseFileStorage
{
private static final String GDRIVE_PROTOCOL_ID = "gdrive";
@Override
protected String getScopeString() {
return Scopes.DRIVE_FULL;
}
@Override
public String getProtocolId() {
return GDRIVE_PROTOCOL_ID;
}
}

View File

@@ -145,7 +145,7 @@ import java.util.ArrayList;
import java.util.List;
//import keepass2android.javafilestorage.DropboxCloudRailStorage;
import keepass2android.javafilestorage.GoogleDriveFileStorage;
import keepass2android.javafilestorage.GoogleDriveAppDataFileStorage;
import keepass2android.javafilestorage.JavaFileStorage;
import keepass2android.javafilestorage.JavaFileStorage.FileEntry;
import keepass2android.javafilestorage.SftpStorage;
@@ -543,7 +543,7 @@ public class MainActivity extends Activity implements JavaFileStorage.FileStorag
//storageToTest = new SkyDriveFileStorage("000000004010C234", appContext);
storageToTest = new GoogleDriveFileStorage();
storageToTest = new GoogleDriveAppDataFileStorage();
/*storageToTest = new WebDavStorage(new ICertificateErrorHandler() {
@Override
public boolean onValidationError(String error) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -608,6 +608,8 @@
<string name="filestoragename_dropboxKP2A">Dropbox (KP2A folder)</string>
<string name="filestoragehelp_dropboxKP2A">If you do not want to give KP2A access to your full Dropbox, you may select this option. It will request only access to the folder Apps/Keepass2Android. This is especially suited when creating a new database. If you already have a database, select this option to create the folder, then place your file inside the folder (from your PC) and then select this option again for opening the file.</string>
<string name="filestoragename_gdrive">Google Drive</string>
<string name="filestoragename_gdriveKP2A">Google Drive (KP2A files)</string>
<string name="filestoragehelp_gdriveKP2A">If you do not want to give KP2A access to your full Google Drive, you may select this option. Note that you need to create a database file first, existing files are not visible to the app. Either choose this option from the Create database screen or, if you already opened a database, by exporting the database choosing this option.</string>
<string name="filestoragename_pcloud">PCloud</string>
<string name="filestoragehelp_pcloud">This storage type will only request access to the pCloud folder "Applications/Keepass2Android". If you want to use an existing database from your pCloud account, please make sure the file is placed in this pCloud folder.</string>
<string name="filestoragename_onedrive">OneDrive</string>

View File

@@ -44,6 +44,7 @@ using keepass2android.addons.OtpKeyProv;
using keepass2android.database.edit;
using KeePassLib.Interfaces;
using KeePassLib.Utility;
using GoogleDriveAppDataFileStorage = keepass2android.Io.GoogleDriveAppDataFileStorage;
#if !NoNet
#if !EXCLUDE_JAVAFILESTORAGE
using Android.Gms.Common;
@@ -725,6 +726,7 @@ namespace keepass2android
new DropboxFileStorage(LocaleManager.LocalizedAppContext, this),
new DropboxAppFolderFileStorage(LocaleManager.LocalizedAppContext, this),
GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(LocaleManager.LocalizedAppContext)==ConnectionResult.Success ? new GoogleDriveFileStorage(LocaleManager.LocalizedAppContext, this) : null,
GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(LocaleManager.LocalizedAppContext)==ConnectionResult.Success ? new GoogleDriveAppDataFileStorage(LocaleManager.LocalizedAppContext, this) : null,
new OneDriveFileStorage(LocaleManager.LocalizedAppContext, this),
new OneDrive2FullFileStorage(),
new OneDrive2MyFilesFileStorage(),

View File

@@ -474,6 +474,7 @@
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_ftp.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_ftps.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_gdrive.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_gdriveKP2A.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_http.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_https.png" />
<AndroidResource Include="Resources\drawable-mdpi\ic_storage_owncloud.png" />
@@ -1100,6 +1101,9 @@
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\ic_storage_gdrive.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\ic_storage_gdriveKP2A.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\ic_storage_http.png" />
</ItemGroup>