Skip to content

Commit 19250c7

Browse files
committed
8.3.5 beta - fixes to google drive upload regarding authentication flow
1 parent 4760b28 commit 19250c7

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ AUTO-GENERATED FILE, CHANGES SHOULD BE DONE IN ./JPM.java or ./src/main/java/JPM
1111
<modelVersion>4.0.0</modelVersion>
1212
<groupId>com.osiris.autoplug.client</groupId>
1313
<artifactId>AutoPlug-Client</artifactId>
14-
<version>8.3.3</version>
14+
<version>8.3.5</version>
1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<java.version>9</java.version>
18-
<version>8.3.3</version>
18+
<version>8.3.5</version>
1919
<main-class>com.osiris.autoplug.client.Main</main-class>
2020
<slf4j.version>2.0.13</slf4j.version>
2121
<name>AutoPlug-Client</name>
@@ -96,18 +96,18 @@ AUTO-GENERATED FILE, CHANGES SHOULD BE DONE IN ./JPM.java or ./src/main/java/JPM
9696
<version>3.26.1</version>
9797
<scope>compile</scope>
9898
</dependency>
99-
<dependency>
100-
<groupId>org.jetbrains</groupId>
101-
<artifactId>annotations</artifactId>
102-
<version>23.0.0</version>
103-
<scope>compile</scope>
104-
</dependency>
10599
<dependency>
106100
<groupId>org.jline</groupId>
107101
<artifactId>jline-reader</artifactId>
108102
<version>3.20.0</version>
109103
<scope>compile</scope>
110104
</dependency>
105+
<dependency>
106+
<groupId>org.jetbrains</groupId>
107+
<artifactId>annotations</artifactId>
108+
<version>23.0.0</version>
109+
<scope>compile</scope>
110+
</dependency>
111111
<dependency>
112112
<groupId>com.google.http-client</groupId>
113113
<artifactId>google-http-client</artifactId>

src/main/java/JPM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ThisProject(List<String> args) {
2121
// Override default configurations
2222
this.groupId = "com.osiris.autoplug.client";
2323
this.artifactId = "AutoPlug-Client";
24-
this.version = "8.3.4";
24+
this.version = "8.3.5";
2525
this.mainClass = "com.osiris.autoplug.client.Main";
2626
this.jarName = "AutoPlug-Client-original.jar";
2727
this.fatJarName = "AutoPlug-Client.jar";

src/main/java/com/osiris/autoplug/client/configs/BackupConfig.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class BackupConfig extends MyYaml {
4343
public YamlSection backup_upload_rsa;
4444
public YamlSection backup_upload_alternatives_google_drive;
4545
public YamlSection backup_upload_alternatives_google_drive_enable;
46+
public YamlSection backup_upload_alternatives_google_drive_project_id;
4647
public YamlSection backup_upload_alternatives_google_drive_client_id;
4748
public YamlSection backup_upload_alternatives_google_drive_client_secret;
4849
public YamlSection backup_upload_alternatives_google_drive_refresh_token;
@@ -140,20 +141,24 @@ public BackupConfig() throws IOException, DuplicateKeyException, YamlReaderExcep
140141
"How This Works:\n" +
141142
"1. You create OAuth credentials in your own Google Cloud Console\n" +
142143
"2. You enter your Client ID and Client Secret in the sections below\n" +
143-
"3. You get an URL to open with your browser and log in\n" +
144-
"4. After authentication, a refresh token is saved for future use and to avoid repeating the steps shown below\n" +
144+
"3. When enabled, your browser will automatically open for authentication\n" +
145+
"4. After authentication, a refresh token is saved for future use\n" +
146+
"\n" +
145147
"Google-Drive Setup:\n" +
146-
"1. Create a project in Google Cloud Console (or use an existing one) and make sure the project_id is autoplug-client: https://developers.google.com/workspace/guides/create-project\n" +
148+
"1. Create a project in Google Cloud Console (or use an existing one) and enter its project_id below: https://developers.google.com/workspace/guides/create-project\n" +
147149
"2. Enable the Google Drive API: https://console.cloud.google.com/flows/enableapi?apiid=drive.googleapis.com\n" +
148150
"3. Create OAuth 2.0 login panel: https://console.cloud.google.com/auth/branding\n" +
149151
"4. Create a Client (type \"Desktop app\"): https://console.cloud.google.com/auth/clients\n" +
150-
"5. Add urn:ietf:wg:oauth:2.0:oob as authorized redirect URI\n" +
152+
"5. Add http://localhost:8888/Callback as authorized redirect URI\n" +
151153
"6. Enter your Client ID and Client Secret into the sections below\n" +
152-
"7. Set 'enable' to 'true' and save this file" +
153-
"8. An URL should be shown in your console, open it via your browser and authenticate, then paste the code back into the console\n" +
154-
"9. After authentication, the backups will be uploaded to your personal Google Drive\n" +
155-
"!!!IMPORTANT!!! This file now contains data that must be kept secret, do not share with anyone!\n");
154+
"7. Set 'enable' to 'true' and save this file\n" +
155+
"8. Your browser will automatically open for authentication, or you can find the URL in the console and open it manually\n" +
156+
"9. After authentication, backups will be uploaded to your Google Drive\n" +
157+
"\n" +
158+
"!!!IMPORTANT!!! This file contains sensitive data - do not share it with anyone!\n");
156159
backup_upload_alternatives_google_drive_enable = put(name, "upload", "alternatives", "google-drive", "enable").setDefValues("false");
160+
backup_upload_alternatives_google_drive_project_id = put(name, "upload", "alternatives", "google-drive", "project-id").setComments(
161+
"Get this from Google Cloud Console after creating your project. You also use an existing project and enter its name/project-id here.").setDefValues("autoplug-client");
157162
backup_upload_alternatives_google_drive_client_id = put(name, "upload", "alternatives", "google-drive", "client-id").setComments(
158163
"Get this from Google Cloud Console after creating OAuth credentials.");
159164
backup_upload_alternatives_google_drive_client_secret = put(name, "upload", "alternatives", "google-drive", "client-secret").setComments(

src/main/java/com/osiris/autoplug/client/tasks/backup/BackupGoogleDrive.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ public BackupGoogleDrive() throws GeneralSecurityException, IOException {
3333
}
3434

3535
public Credential getCredentials(BackupConfig config) throws Exception {
36+
AL.info("Initializing Google Drive backup credentials...");
37+
3638
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
3739
GsonFactory.getDefaultInstance(),
3840
new InputStreamReader(new java.io.ByteArrayInputStream(
3941
("{\"installed\":{\"client_id\":\"" + config.backup_upload_alternatives_google_drive_client_id.asString() + "\"," +
40-
"\"project_id\":\"autoplug-client\",\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\"," +
42+
"\"project_id\":\""+config.backup_upload_alternatives_google_drive_project_id.asString()+"\",\"auth_uri\":\"https://accounts.google.com/o/oauth2/auth\"," +
4143
"\"token_uri\":\"https://oauth2.googleapis.com/token\"," +
4244
"\"auth_provider_x509_cert_url\":\"https://www.googleapis.com/oauth2/v1/certs\"," +
4345
"\"client_secret\":\"" + config.backup_upload_alternatives_google_drive_client_secret.asString() + "\"," +
44-
"\"redirect_uris\":[\"urn:ietf:wg:oauth:2.0:oob\"]}").getBytes())));
46+
"\"redirect_uris\":[\"http://localhost:8888/Callback\"]}").getBytes())));
4547

4648
// Try to use refresh token if available
4749
if (config.backup_upload_alternatives_google_drive_refresh_token.asString() != null &&
4850
!config.backup_upload_alternatives_google_drive_refresh_token.asString().isEmpty()) {
51+
AL.info("Using existing refresh token for authentication...");
4952
TokenResponse response = new TokenResponse();
5053
response.setRefreshToken(config.backup_upload_alternatives_google_drive_refresh_token.asString());
5154
return new GoogleCredential.Builder()
@@ -56,38 +59,41 @@ public Credential getCredentials(BackupConfig config) throws Exception {
5659
.setFromTokenResponse(response);
5760
}
5861

59-
// Manual authorization flow
62+
AL.info("No existing credentials found, starting new authentication flow...");
63+
64+
// Automatic authorization flow with local server
6065
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
6166
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
6267
Collections.singleton(DriveScopes.DRIVE_FILE))
6368
.setAccessType("offline")
6469
.build();
6570

66-
String url = flow.newAuthorizationUrl()
67-
.setRedirectUri("urn:ietf:wg:oauth:2.0:oob")
71+
LocalServerReceiver receiver = new LocalServerReceiver.Builder()
72+
.setPort(8888)
6873
.build();
6974

70-
AL.info("Please open this URL in your browser: " + url);
71-
AL.info("After authorization, paste the code you received here and press enter:");
72-
73-
// Read code from console input
74-
String code = new java.util.Scanner(System.in).nextLine();
75+
AL.info("Opening browser for Google authentication...");
76+
AL.info("If browser doesn't open automatically, check your console for the URL");
7577

76-
TokenResponse response = flow.newTokenRequest(code)
77-
.setRedirectUri("urn:ietf:wg:oauth:2.0:oob")
78-
.execute();
78+
try {
79+
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
7980

80-
Credential credential = flow.createAndStoreCredential(response, "user");
81+
if (credential.getRefreshToken() != null) {
82+
AL.info("Authentication successful! Saving refresh token for future use...");
83+
config.backup_upload_alternatives_google_drive_refresh_token.setValues(credential.getRefreshToken());
84+
config.save();
85+
} else {
86+
AL.warn("Authentication succeeded but no refresh token was received. You may need to re-authenticate later.");
87+
}
8188

82-
// Save refresh token for future use
83-
if (credential.getRefreshToken() != null) {
84-
config.backup_upload_alternatives_google_drive_refresh_token.setValues(credential.getRefreshToken());
85-
config.save();
89+
return credential;
90+
} catch (Exception e) {
91+
AL.warn("Google Drive authentication failed!", e);
92+
throw e;
8693
}
87-
88-
return credential;
8994
}
9095

96+
9197
// Modify the uploadToGoogleDrive method
9298
public void uploadToGoogleDrive(java.io.File fileToUpload, BackupConfig config)
9399
throws Exception {

0 commit comments

Comments
 (0)