Skip to content

Commit 2f9e70c

Browse files
committed
Migrate password to OAuth token
1 parent e75a07e commit 2f9e70c

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/actions/MigrateSettings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package fi.helsinki.cs.tmc.actions;
22

3+
import com.google.common.base.Optional;
4+
import fi.helsinki.cs.tmc.core.TmcCore;
35
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
6+
import fi.helsinki.cs.tmc.core.domain.ProgressObserver;
7+
import fi.helsinki.cs.tmc.core.holders.TmcSettingsHolder;
8+
import fi.helsinki.cs.tmc.core.utilities.TmcServerAddressNormalizer;
49
import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl;
510
import fi.helsinki.cs.tmc.model.PersistableSettings;
11+
import java.util.logging.Level;
612

713
import java.util.logging.Logger;
814
import java.util.prefs.BackingStoreException;
@@ -30,4 +36,18 @@ public static void run() {
3036
logger.fine("Could not migrate wrong settings.");
3137
}
3238
}
39+
40+
public static void tryToMigratePasswordToOAuthToken() {
41+
final TmcSettings settings = TmcSettingsHolder.get();
42+
try {
43+
TmcServerAddressNormalizer normalizer = new TmcServerAddressNormalizer();
44+
normalizer.normalize();
45+
TmcCore.get().authenticate(ProgressObserver.NULL_OBSERVER, settings.getPassword().get()).call();
46+
normalizer.selectOrganizationAndCourse();
47+
} catch (Exception ex) {
48+
logger.log(Level.WARNING, "Couldn't migrate password to OAuth token. The user will be asked to log in.");
49+
} finally {
50+
settings.setPassword(Optional.<String>absent());
51+
}
52+
}
3353
}

tmc-plugin/src/fi/helsinki/cs/tmc/actions/TmcModuleInstall.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public void run() {
7575

7676
new EnsureMavenBinaryIsExecutable().run();
7777

78+
if (settings.getPassword().isPresent()) {
79+
MigrateSettings.tryToMigratePasswordToOAuthToken();
80+
}
81+
7882
boolean isFirstRun = prefs.getBoolean(PREF_FIRST_RUN, true);
7983
if (isFirstRun) {
8084
doFirstRun();

0 commit comments

Comments
 (0)