Skip to content

Commit e9bdfb2

Browse files
update onedrive tokens to allow longer sessions (#185)
* retrieving a new accesstoken for each upload * update refreshtoken when retrieving a new accesstoken
1 parent 756d28a commit e9bdfb2

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

DriveBackup/src/main/java/ratismal/drivebackup/uploaders/Authenticator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import org.bukkit.command.CommandSender;
88
import org.jetbrains.annotations.NotNull;
99
import org.json.JSONObject;
10-
import ratismal.drivebackup.UploadThread.UploadLogger;
1110
import ratismal.drivebackup.handler.commandHandler.BasicCommands;
1211
import ratismal.drivebackup.plugin.DriveBackup;
13-
import ratismal.drivebackup.uploaders.googledrive.GoogleDriveUploader;
1412
import ratismal.drivebackup.util.Logger;
1513
import ratismal.drivebackup.util.MessageUtil;
1614
import ratismal.drivebackup.util.NetUtil;
@@ -198,12 +196,12 @@ public static void linkSuccess(CommandSender initiator, @NotNull AuthenticationP
198196
BasicCommands.sendBriefBackupList(initiator);
199197
}
200198

201-
private static void saveRefreshToken(@NotNull AuthenticationProvider provider, String token) throws Exception {
199+
public static void saveRefreshToken(@NotNull AuthenticationProvider provider, String token) throws IOException {
202200
JSONObject jsonObject = new JSONObject();
203201
jsonObject.put("refresh_token", token);
204-
FileWriter file = new FileWriter(provider.getCredStoreLocation());
205-
file.write(jsonObject.toString());
206-
file.close();
202+
try (FileWriter file = new FileWriter(provider.getCredStoreLocation())) {
203+
file.write(jsonObject.toString());
204+
}
207205
}
208206

209207
private static void enableBackupMethod(@NotNull AuthenticationProvider provider, Logger logger) {

DriveBackup/src/main/java/ratismal/drivebackup/uploaders/onedrive/OneDriveUploader.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public OneDriveUploader(UploadLogger logger) {
6060
this.logger = logger;
6161
setAuthProvider(AuthenticationProvider.ONEDRIVE);
6262
try {
63-
refreshToken = Authenticator.getRefreshToken(AuthenticationProvider.ONEDRIVE);
63+
refreshToken = Authenticator.getRefreshToken(getAuthProvider());
6464
retrieveNewAccessToken();
6565
} catch (Exception e) {
6666
MessageUtil.sendConsoleException(e);
@@ -76,7 +76,7 @@ public OneDriveUploader(UploadLogger logger) {
7676
*/
7777
private void retrieveNewAccessToken() throws Exception {
7878
RequestBody requestBody = new FormBody.Builder()
79-
.add("client_id", Obfusticate.decrypt(AuthenticationProvider.ONEDRIVE.getClientId()))
79+
.add("client_id", Obfusticate.decrypt(getAuthProvider().getClientId()))
8080
.add("scope", "offline_access Files.ReadWrite")
8181
.add("refresh_token", refreshToken)
8282
.add("grant_type", "refresh_token")
@@ -94,6 +94,7 @@ private void retrieveNewAccessToken() throws Exception {
9494
throw new IOException(String.format("%s : %s", error, description));
9595
}
9696
accessToken = parsedResponse.getString("access_token");
97+
refreshToken = parsedResponse.getString("refresh_token");
9798
}
9899
}
99100

@@ -129,31 +130,40 @@ public void test(File testFile) {
129130
@Override
130131
public void uploadFile(File file, String location) {
131132
try {
133+
retrieveNewAccessToken();
132134
String destinationRoot = normalizePath(ConfigParser.getConfig().backupStorage.remoteDirectory);
133135
String destinationPath = concatPath(destinationRoot, normalizePath(location));
134136
FQID destinationId = createPath(destinationPath);
135137
String uploadURL = createUploadSession(file.getName(), destinationId);
136138
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
137139
uploadToSession(uploadURL, raf);
138-
try {
139-
pruneBackups(destinationId);
140-
} catch (Exception e) {
141-
logger.log(intl("backup-method-prune-failed"));
142-
throw e;
143-
}
144140
}
145-
} catch (Exception exception) {
141+
try {
142+
pruneBackups(destinationId);
143+
} catch (Exception e) {
144+
logger.log(intl("backup-method-prune-failed"));
145+
throw e;
146+
}
147+
}
148+
catch (Exception exception) {
146149
NetUtil.catchException(exception, "graph.microsoft.com", logger);
147150
MessageUtil.sendConsoleException(exception);
148151
setErrorOccurred(true);
152+
if (exception instanceof InterruptedException) {
153+
Thread.currentThread().interrupt();
154+
}
149155
}
150156
}
151157

152158
/**
153159
* Closes any remaining connections retrieveNewAccessToken
154160
*/
155161
public void close() {
156-
// nothing needs to be done
162+
try {
163+
Authenticator.saveRefreshToken(getAuthProvider(), refreshToken);
164+
} catch (Exception e) {
165+
MessageUtil.sendConsoleException(e);
166+
}
157167
}
158168

159169
/**

0 commit comments

Comments
 (0)