Skip to content

Commit 630ad8c

Browse files
feat: support oauth authentication flow (#132)
Co-authored-by: Bastian Doetsch <bastian.doetsch@snyk.io>
1 parent 63d5311 commit 630ad8c

13 files changed

Lines changed: 255 additions & 276 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ public interface EnvironmentConstants {
55
String ENV_SNYK_TOKEN = "SNYK_TOKEN";
66
String ENV_SNYK_ORG = "SNYK_CFG_ORG";
77
String ENV_DISABLE_ANALYTICS = "SNYK_CFG_DISABLE_ANALYTICS";
8+
String ENV_INTERNAL_SNYK_OAUTH_ENABLED = "INTERNAL_SNYK_OAUTH_ENABLED";
9+
String ENV_INTERNAL_OAUTH_TOKEN_STORAGE = "INTERNAL_OAUTH_TOKEN_STORAGE";
810
}

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package io.snyk.eclipse.plugin;
22

3-
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
4-
import io.snyk.eclipse.plugin.utils.SnykLogger;
5-
import io.snyk.eclipse.plugin.views.SnykView;
6-
import io.snyk.eclipse.plugin.wizards.SnykWizard;
7-
import io.snyk.languageserver.LsRuntimeEnvironment;
8-
import io.snyk.languageserver.SnykLanguageServer;
9-
import io.snyk.languageserver.download.HttpClientFactory;
10-
import io.snyk.languageserver.download.LsBinaries;
11-
import io.snyk.languageserver.download.LsDownloader;
3+
import static io.snyk.eclipse.plugin.utils.SnykLogger.logError;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.URISyntaxException;
8+
import java.nio.file.Files;
9+
import java.nio.file.attribute.BasicFileAttributes;
10+
import java.time.Instant;
11+
import java.time.temporal.ChronoUnit;
1212

13-
import org.apache.http.impl.client.CloseableHttpClient;
14-
import org.apache.http.impl.client.HttpClients;
15-
import org.eclipse.core.net.proxy.IProxyData;
1613
import org.eclipse.core.runtime.ILog;
1714
import org.eclipse.core.runtime.IProgressMonitor;
1815
import org.eclipse.core.runtime.IStatus;
@@ -26,15 +23,15 @@
2623
import org.eclipse.ui.PartInitException;
2724
import org.eclipse.ui.PlatformUI;
2825

29-
import java.io.File;
30-
import java.io.IOException;
31-
import java.net.URISyntaxException;
32-
import java.nio.file.Files;
33-
import java.nio.file.attribute.BasicFileAttributes;
34-
import java.time.Instant;
35-
import java.time.temporal.ChronoUnit;
36-
37-
import static io.snyk.eclipse.plugin.utils.SnykLogger.logError;
26+
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
27+
import io.snyk.eclipse.plugin.utils.SnykLogger;
28+
import io.snyk.eclipse.plugin.views.SnykView;
29+
import io.snyk.eclipse.plugin.wizards.SnykWizard;
30+
import io.snyk.languageserver.LsRuntimeEnvironment;
31+
import io.snyk.languageserver.SnykLanguageServer;
32+
import io.snyk.languageserver.download.HttpClientFactory;
33+
import io.snyk.languageserver.download.LsBinaries;
34+
import io.snyk.languageserver.download.LsDownloader;
3835

3936
public class SnykStartup implements IStartup {
4037
private LsRuntimeEnvironment runtimeEnvironment;

plugin/src/main/java/io/snyk/eclipse/plugin/properties/preferences/ApiClient.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import io.snyk.languageserver.LsRuntimeEnvironment;
77
import io.snyk.languageserver.download.HttpClientFactory;
8+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
9+
import io.snyk.languageserver.protocolextension.messageObjects.OAuthToken;
810

911
import org.apache.http.client.methods.HttpGet;
1012
import org.apache.http.client.protocol.HttpClientContext;
@@ -42,7 +44,14 @@ public boolean checkSnykCodeEnablement() {
4244
url += "?org=" + org;
4345
}
4446
var httpGet = new HttpGet(endpoint + url);
45-
httpGet.addHeader("Authorization", "token " + prefs.getAuthToken());
47+
if (prefs.getPref(Preferences.AUTHENTICATION_METHOD).equals(Preferences.AUTH_METHOD_TOKEN)) {
48+
httpGet.addHeader("Authorization", "token " + prefs.getAuthToken());
49+
} else {
50+
// first refresh token
51+
SnykExtendedLanguageClient.getInstance().refreshOAuthToken();
52+
var oauthToken = objectMapper.readValue(prefs.getAuthToken(), OAuthToken.class);
53+
httpGet.addHeader("Authorization", "bearer " + oauthToken.getAccessToken());
54+
}
4655
httpGet.addHeader("Content-Type", "application/json");
4756
var response = httpClient.execute(httpGet, context);
4857

plugin/src/main/java/io/snyk/eclipse/plugin/properties/preferences/Preferences.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public static synchronized Preferences getInstance(PreferenceStore store) {
4646
public static final String ENABLE_TELEMETRY = EnvironmentConstants.ENV_DISABLE_ANALYTICS;
4747
public static final String MANAGE_BINARIES_AUTOMATICALLY = "SNYK_CFG_MANAGE_BINARIES_AUTOMATICALLY";
4848
public static final String ORGANIZATION_KEY = EnvironmentConstants.ENV_SNYK_ORG;
49-
49+
public static final String AUTHENTICATION_METHOD = "AUTHENTICATION_METHOD";
50+
public static final String AUTH_METHOD_TOKEN = "token";
51+
public static final String AUTH_METHOD_OAUTH = "oauth";
5052

5153
private final PreferenceStore store;
5254

@@ -76,6 +78,11 @@ public static synchronized Preferences getInstance(PreferenceStore store) {
7678
if (getPref(LSP_VERSION) == null) {
7779
store(LSP_VERSION, "1");
7880
}
81+
82+
if (getPref(AUTHENTICATION_METHOD) == null || getPref(AUTHENTICATION_METHOD).isBlank()) {
83+
store(AUTHENTICATION_METHOD, AUTH_METHOD_TOKEN);
84+
}
85+
7986
if (getPref(LS_BINARY_KEY) == null || getPref(LS_BINARY_KEY).equals("")) {
8087
store(LS_BINARY_KEY, getDefaultLsPath());
8188
}

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

Lines changed: 0 additions & 53 deletions
This file was deleted.

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

Lines changed: 0 additions & 161 deletions
This file was deleted.

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ProcessRunner {
3131

3232
private static final String HOME = System.getProperty("user.home");
3333
private static final String DEFAULT_MAC_PATH = "/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin:" + HOME + "/bin:"
34-
+ HOME + "/.cargo/bin:" + System.getenv("GOPATH") + "/bin" + System.getenv("GOROOT") + "/bin";
34+
+ HOME + "/.cargo/bin:" + System.getenv("GOPATH") + "/bin" + System.getenv("GOROOT") + "/bin";
3535
private static final String DEFAULT_LINUX_PATH = DEFAULT_MAC_PATH;
3636
private static final String DEFAULT_WIN_PATH = "";
3737

@@ -89,8 +89,8 @@ private ProcessBuilder getProcessBuilder(List<String> params, Optional<String> p
8989
// TODO: move to runtimeEnvironment
9090
if (path.isPresent() && !path.get().isBlank()) {
9191
pb.environment().put("PATH",
92-
path.map(p -> p + File.pathSeparator + defaultPathForOS).orElse(defaultPathForOS)
93-
+ File.pathSeparator + System.getenv("PATH"));
92+
path.map(p -> p + File.pathSeparator + defaultPathForOS).orElse(defaultPathForOS)
93+
+ File.pathSeparator + System.getenv("PATH"));
9494
}
9595
return pb;
9696
}
@@ -120,9 +120,14 @@ private void setupProcessBuilderBase(ProcessBuilder pb) {
120120
}
121121
}
122122

123+
String authMethod = Preferences.getInstance().getPref(Preferences.AUTHENTICATION_METHOD);
123124
String token = Preferences.getInstance().getAuthToken();
124-
if (token != null)
125-
pb.environment().put(EnvironmentConstants.ENV_SNYK_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+
} else {
129+
pb.environment().put(EnvironmentConstants.ENV_SNYK_TOKEN, token);
130+
}
126131

127132
String insecure = Preferences.getInstance().getPref(Preferences.INSECURE_KEY);
128133
if (insecure != null && insecure.equalsIgnoreCase("true"))
@@ -150,7 +155,7 @@ public ProcessBuilder createWinProcessBuilder(List<String> params, Optional<Stri
150155
ProcessBuilder pb = new ProcessBuilder(cmd);
151156
setupProcessBuilderBase(pb);
152157
pb.environment().put("PATH", path.map(p -> p + ";" + DEFAULT_WIN_PATH).orElse(DEFAULT_WIN_PATH)
153-
+ File.pathSeparator + System.getenv("PATH"));
158+
+ File.pathSeparator + System.getenv("PATH"));
154159

155160
// debug logging on windows machines
156161
IStatus[] statuses = new IStatus[] {

0 commit comments

Comments
 (0)