Skip to content

Commit 790bebf

Browse files
feat: raise ls protocol version to support oauth2 [HEAD-117] (#136)
* feat: WIP initial refresh token impl * task: WIP implementing oauth * feat: handle token message [HEAD-117] * feat: add token refresh * chore: remove redundant preference * chore: remove redundant expiry pref * chore: cleanup redundant code * feat: testing oauth implementation * feat: testing * feat: adds more tests * fix: borked tests * fix: borking tests * chore: pr feedback * chore: increase LS protocol version to 8 * chore: update required protocol version * chore: remove logging * chore: use SNYK_OAUTH_TOKEN for CLI * chore: refresh token before calling CLI and replace oauth refresh ls command by get active user --------- Co-authored-by: Peter Schäfer <101886095+PeterSchafer@users.noreply.github.com>
1 parent b32776d commit 790bebf

6 files changed

Lines changed: 23 additions & 11 deletions

File tree

plugin/src/main/java/io/snyk/eclipse/plugin/EnvironmentConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface EnvironmentConstants {
77
String ENV_DISABLE_ANALYTICS = "SNYK_CFG_DISABLE_ANALYTICS";
88
String ENV_INTERNAL_SNYK_OAUTH_ENABLED = "INTERNAL_SNYK_OAUTH_ENABLED";
99
String ENV_INTERNAL_OAUTH_TOKEN_STORAGE = "INTERNAL_OAUTH_TOKEN_STORAGE";
10+
String ENV_OAUTH_ACCESS_TOKEN = "SNYK_OAUTH_TOKEN";
1011
}

plugin/src/main/java/io/snyk/eclipse/plugin/runner/ProcessRunner.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.util.Optional;
1212

1313
import io.snyk.languageserver.LsRuntimeEnvironment;
14+
import io.snyk.languageserver.protocolextension.messageObjects.OAuthToken;
15+
1416
import org.eclipse.core.runtime.ILog;
1517
import org.eclipse.core.runtime.IStatus;
1618
import org.eclipse.core.runtime.MultiStatus;
@@ -19,6 +21,9 @@
1921
import org.osgi.framework.Bundle;
2022
import org.osgi.framework.FrameworkUtil;
2123

24+
import com.fasterxml.jackson.databind.DeserializationFeature;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
2227
import io.snyk.eclipse.plugin.EnvironmentConstants;
2328
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
2429
import io.snyk.eclipse.plugin.utils.SnykLogger;
@@ -122,14 +127,18 @@ private void setupProcessBuilderBase(ProcessBuilder pb) {
122127

123128
String authMethod = Preferences.getInstance().getPref(Preferences.AUTHENTICATION_METHOD);
124129
String token = Preferences.getInstance().getAuthToken();
125-
if (token != null && authMethod.equals(Preferences.AUTH_METHOD_OAUTH)) {
126-
pb.environment().put(EnvironmentConstants.ENV_INTERNAL_SNYK_OAUTH_ENABLED, "1");
127-
pb.environment().put(EnvironmentConstants.ENV_INTERNAL_OAUTH_TOKEN_STORAGE, token);
128-
pb.environment().remove(EnvironmentConstants.ENV_SNYK_TOKEN);
129-
} else {
130+
if (token != null && authMethod.equals(Preferences.AUTH_METHOD_OAUTH)) {
131+
try {
132+
ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
133+
var oauthToken = objectMapper.readValue(token, OAuthToken.class);
134+
pb.environment().put(EnvironmentConstants.ENV_OAUTH_ACCESS_TOKEN, oauthToken.getAccessToken());
135+
pb.environment().remove(EnvironmentConstants.ENV_SNYK_TOKEN);
136+
} catch (Exception e) {
137+
SnykLogger.logError(e);
138+
}
139+
} else {
130140
pb.environment().put(EnvironmentConstants.ENV_SNYK_TOKEN, token);
131-
pb.environment().remove(EnvironmentConstants.ENV_INTERNAL_OAUTH_TOKEN_STORAGE);
132-
141+
pb.environment().remove(EnvironmentConstants.ENV_OAUTH_ACCESS_TOKEN);
133142
}
134143

135144
String insecure = Preferences.getInstance().getPref(Preferences.INSECURE_KEY);

plugin/src/main/java/io/snyk/eclipse/plugin/runner/SnykCliRunner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.snyk.eclipse.plugin.exception.NotSupportedException;
44
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
55
import io.snyk.eclipse.plugin.utils.Lists;
6+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
7+
68
import org.apache.commons.lang3.SystemUtils;
79

810
import java.io.File;
@@ -58,6 +60,7 @@ private ProcessResult snykRun(List<String> arguments) {
5860
private ProcessResult snykRun(List<String> arguments, Optional<File> navigatePath) {
5961
try {
6062
checkIfTrusted(navigatePath.get());
63+
SnykExtendedLanguageClient.getInstance().refreshOAuthToken();
6164
ProcessBuilder processBuilder = createProcessBuilderByOS(arguments, Preferences.getInstance().getPath());
6265
return processRunner.run(processBuilder, navigatePath);
6366
} catch (Exception e) {

plugin/src/main/java/io/snyk/languageserver/download/LsBinaries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class LsBinaries {
66
private static final String LS_DOWNLOAD_BASE_URL = "https://static.snyk.io/snyk-ls";
7-
public static final String REQUIRED_LS_PROTOCOL_VERSION = "9";
7+
public static final String REQUIRED_LS_PROTOCOL_VERSION = "10";
88

99
public static URI getBaseUri() {
1010
return URI.create(String.format("%s/%s", LS_DOWNLOAD_BASE_URL, REQUIRED_LS_PROTOCOL_VERSION));

plugin/src/main/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public CompletableFuture<ShowDocumentResult> showDocument(ShowDocumentParams par
218218
public boolean refreshOAuthToken() {
219219
var p = Preferences.getInstance();
220220
var token = p.getAuthToken();
221-
executeCommand("snyk.oauthRefreshCommand", new ArrayList<>());
221+
executeCommand("snyk.getActiveUser", new ArrayList<>());
222222
// wait until token has changed or 2s have passed
223223
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
224224
while (token.equals(p.getAuthToken())) {

tests/src/test/java/io/snyk/eclipse/plugin/runner/ProcessRunnerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ void testOAuthEnabled() {
123123

124124
var env = builder.environment();
125125

126-
assertEquals("1", env.get(EnvironmentConstants.ENV_INTERNAL_SNYK_OAUTH_ENABLED));
127-
assertEquals(expectedToken, env.get(EnvironmentConstants.ENV_INTERNAL_OAUTH_TOKEN_STORAGE));
126+
assertEquals("configAccessToken", env.get(EnvironmentConstants.ENV_OAUTH_ACCESS_TOKEN));
128127
}
129128
}

0 commit comments

Comments
 (0)