Skip to content

Commit 1d194ab

Browse files
Merge pull request #98 from snyk/feat/authUserFlow
feat: update authentication user flow [ROAD-1198]
2 parents db6415c + 6324c5e commit 1d194ab

File tree

16 files changed

+477
-52
lines changed

16 files changed

+477
-52
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33

44
## [2.0.0] - Unreleased
5+
### Changes
6+
- adds configuration wizard for custom endpoints
7+
8+
## [2.0.0] - v20221007.135736
59
### Fixed
610
- malformed proxy URL
711

plugin/plugin.xml

+17
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,21 @@
234234
</super>
235235
<persistent value="false"/>
236236
</extension>
237+
238+
<extension
239+
id="wizard"
240+
name="Snyk"
241+
point="org.eclipse.ui.newWizards">
242+
<category
243+
id="io.snyk.eclipse.plugin.wizards.category.snyk"
244+
name="Snyk">
245+
</category>
246+
<wizard
247+
category="io.snyk.eclipse.plugin.wizards.category.snyk"
248+
class="io.snyk.eclipse.plugin.wizards.SnykWizard"
249+
icon="icons/patch.png"
250+
id="io.snyk.eclipse.plugin.wizards.snykwizard"
251+
name="SnykWizard">
252+
</wizard>
253+
</extension>
237254
</plugin>

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

+28-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
44
import io.snyk.eclipse.plugin.utils.SnykLogger;
55
import io.snyk.eclipse.plugin.views.SnykView;
6+
import io.snyk.eclipse.plugin.wizards.SnykWizard;
67
import io.snyk.languageserver.LsRuntimeEnvironment;
78
import io.snyk.languageserver.SnykLanguageServer;
89
import io.snyk.languageserver.download.HttpClientFactory;
@@ -18,6 +19,7 @@
1819
import org.eclipse.core.runtime.Platform;
1920
import org.eclipse.core.runtime.Status;
2021
import org.eclipse.core.runtime.jobs.Job;
22+
import org.eclipse.jface.wizard.WizardDialog;
2123
import org.eclipse.ui.IStartup;
2224
import org.eclipse.ui.IWorkbench;
2325
import org.eclipse.ui.IWorkbenchWindow;
@@ -52,21 +54,31 @@ public void earlyStartup() {
5254
Job initJob = new Job("Downloading latest Language Server release...") {
5355
@Override
5456
protected IStatus run(IProgressMonitor monitor) {
55-
try {
56-
logger.info("LS: Checking for needed download");
57-
if (downloadLS()) {
58-
logger.info("LS: Need to download");
59-
downloading = true;
60-
monitor.subTask("Starting download of Snyk Language Server");
61-
download(monitor);
57+
PlatformUI.getWorkbench().getDisplay().asyncExec(() -> {
58+
try {
59+
logger.info("LS: Checking for needed download");
60+
if (downloadLS()) {
61+
logger.info("LS: Need to download");
62+
downloading = true;
63+
monitor.subTask("Starting download of Snyk Language Server");
64+
download(monitor);
65+
}
66+
67+
monitor.subTask("Starting Snyk Language Server...");
68+
SnykLanguageServer.InitializeServer();
69+
} catch (Exception exception) {
70+
logError(exception);
6271
}
63-
64-
monitor.subTask("Starting Snyk Language Server...");
65-
SnykLanguageServer.InitializeServer();
66-
} catch (Exception exception) {
67-
logError(exception);
68-
}
69-
downloading = false;
72+
downloading = false;
73+
74+
if (Preferences.getInstance().getAuthToken().isBlank()) {
75+
SnykWizard wizard = new SnykWizard();
76+
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(), wizard);
77+
dialog.setBlockOnOpen(true);
78+
dialog.open();
79+
}
80+
});
81+
7082
return Status.OK_STATUS;
7183
}
7284
};
@@ -109,7 +121,8 @@ private boolean downloadLS() {
109121
Instant lastModified = basicFileAttributes.lastModifiedTime().toInstant();
110122
boolean needsUpdate = lastModified.isBefore(Instant.now().minus(4, ChronoUnit.DAYS))
111123
|| !Preferences.getInstance().getLspVersion().equals(LsBinaries.REQUIRED_LS_PROTOCOL_VERSION);
112-
logger.info(String.format("LS: Needs update? %s. Required LSP version=%s, actual version=%s", needsUpdate, LsBinaries.REQUIRED_LS_PROTOCOL_VERSION, Preferences.getInstance().getLspVersion()));
124+
logger.info(String.format("LS: Needs update? %s. Required LSP version=%s, actual version=%s", needsUpdate,
125+
LsBinaries.REQUIRED_LS_PROTOCOL_VERSION, Preferences.getInstance().getLspVersion()));
113126
return needsUpdate;
114127
} catch (IOException e) {
115128
SnykLogger.logError(e);

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

+3
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,8 @@ public IPreferenceStore getStore() {
157157
return store.getStore();
158158
}
159159

160+
public boolean getBooleanPref(String key) {
161+
return store.getBoolean(key, false);
162+
}
160163
}
161164

plugin/src/main/java/io/snyk/eclipse/plugin/views/MenuHandler.java

+1-29
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,16 @@
33
import org.eclipse.core.commands.AbstractHandler;
44
import org.eclipse.core.commands.ExecutionEvent;
55
import org.eclipse.core.commands.ExecutionException;
6-
import org.eclipse.core.resources.IProject;
7-
import org.eclipse.jdt.internal.core.JavaProject;
8-
import org.eclipse.jface.viewers.IStructuredSelection;
9-
import org.eclipse.ui.ISelectionService;
106
import org.eclipse.ui.IWorkbenchWindow;
117
import org.eclipse.ui.handlers.HandlerUtil;
128

13-
import io.snyk.eclipse.plugin.SnykStartup;
149
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
1510

1611
public class MenuHandler extends AbstractHandler {
1712

1813
public Object execute(ExecutionEvent event) throws ExecutionException {
19-
SnykExtendedLanguageClient.getInstance().triggerScan();
20-
2114
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
22-
ISelectionService service = window.getSelectionService();
23-
IStructuredSelection structured = (IStructuredSelection) service.getSelection();
24-
25-
Object firstElement = structured.getFirstElement();
26-
27-
if (firstElement instanceof IProject) {
28-
IProject project = (IProject) firstElement;
29-
runForProject(project.getName());
30-
}
31-
32-
if (firstElement instanceof JavaProject) {
33-
JavaProject javaproject = (JavaProject) firstElement;
34-
runForProject(javaproject.getProject().getName());
35-
}
36-
15+
SnykExtendedLanguageClient.getInstance().triggerScan(window);
3716
return null;
3817
}
39-
40-
private void runForProject(String projectName) {
41-
SnykView snykView = SnykStartup.getSnykView();
42-
if (snykView != null) {
43-
snykView.testProject(projectName);
44-
}
45-
}
4618
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/SnykView.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public void run() {
218218
abortScanning.setEnabled(true);
219219

220220
CompletableFuture.runAsync(() -> {
221-
SnykExtendedLanguageClient.getInstance().triggerScan();
221+
SnykExtendedLanguageClient.getInstance().triggerScan(null);
222222
alreadyRunning = true;
223223
List<DisplayModel> scanResult = DataProvider.INSTANCE.scanWorkspace();
224224
rootModel.children.clear();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.snyk.eclipse.plugin.wizards;
2+
3+
import org.eclipse.jface.viewers.IStructuredSelection;
4+
import org.eclipse.jface.wizard.Wizard;
5+
import org.eclipse.ui.INewWizard;
6+
import org.eclipse.ui.IWorkbench;
7+
8+
import io.snyk.languageserver.LsConfigurationUpdater;
9+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
10+
11+
public class SnykWizard extends Wizard implements INewWizard {
12+
protected SnykWizardConfigureAPIPage configureAPIPage;
13+
protected SnykWizardAuthenticatePage authenticatePage;
14+
15+
protected SnykWizardModel model;
16+
17+
protected IWorkbench workbench;
18+
protected IStructuredSelection selection;
19+
20+
public SnykWizard() {
21+
super();
22+
model = new SnykWizardModel();
23+
setNeedsProgressMonitor(true);
24+
}
25+
26+
@Override
27+
public String getWindowTitle() {
28+
return "Snyk Wizard";
29+
}
30+
31+
@Override
32+
public void addPages() {
33+
configureAPIPage = new SnykWizardConfigureAPIPage();
34+
addPage(configureAPIPage);
35+
36+
authenticatePage = new SnykWizardAuthenticatePage();
37+
addPage(authenticatePage);
38+
}
39+
40+
public void init(IWorkbench workbench, IStructuredSelection selection) {
41+
this.workbench = workbench;
42+
this.selection = selection;
43+
}
44+
45+
public boolean canFinish() {
46+
if (this.getContainer().getCurrentPage() == authenticatePage) {
47+
return true;
48+
}
49+
return false;
50+
}
51+
52+
public boolean performCancel() {
53+
model.resetPreferences();
54+
return true;
55+
}
56+
57+
public boolean performFinish() {
58+
new LsConfigurationUpdater().configurationChanged();
59+
SnykExtendedLanguageClient.getInstance().triggerAuthentication();
60+
return true;
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.snyk.eclipse.plugin.wizards;
2+
3+
import org.eclipse.jface.action.IAction;
4+
import org.eclipse.jface.viewers.ISelection;
5+
import org.eclipse.jface.viewers.IStructuredSelection;
6+
import org.eclipse.jface.wizard.WizardDialog;
7+
import org.eclipse.ui.IObjectActionDelegate;
8+
import org.eclipse.ui.IWorkbenchPart;
9+
10+
public class SnykWizardAction implements IObjectActionDelegate {
11+
IWorkbenchPart part;
12+
ISelection selection;
13+
14+
public void setActivePart(IAction action, IWorkbenchPart part) {
15+
this.part = part;
16+
}
17+
18+
public void run(IAction action) {
19+
// Instantiates and initialises the wizard
20+
SnykWizard wizard = new SnykWizard();
21+
if ((selection instanceof IStructuredSelection) || (selection == null))
22+
wizard.init(part.getSite().getWorkbenchWindow().getWorkbench(),
23+
(IStructuredSelection)selection);
24+
25+
// Instantiates the wizard container with the wizard and opens it
26+
WizardDialog dialog = new WizardDialog(part.getSite().getShell(), wizard);
27+
dialog.open();
28+
}
29+
30+
public void selectionChanged(IAction action, ISelection selection) {
31+
this.selection = selection;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.snyk.eclipse.plugin.wizards;
2+
3+
import org.eclipse.jface.wizard.WizardPage;
4+
5+
import org.eclipse.swt.SWT;
6+
import org.eclipse.swt.layout.GridData;
7+
import org.eclipse.swt.layout.GridLayout;
8+
import org.eclipse.swt.widgets.Composite;
9+
import org.eclipse.swt.widgets.Label;
10+
import org.eclipse.swt.widgets.Text;
11+
12+
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
13+
14+
import org.eclipse.swt.widgets.Button;
15+
import org.eclipse.swt.widgets.Event;
16+
import org.eclipse.swt.widgets.Listener;
17+
18+
public class SnykWizardAuthenticatePage extends WizardPage implements Listener {
19+
private Text endpoint;
20+
private Button unknownCerts;
21+
22+
public SnykWizardAuthenticatePage() {
23+
super("Snyk Wizard");
24+
setTitle("Authenticate");
25+
setDescription("Review the endpoint configuration, clicking 'Finish' will authenticate with Snyk; this will open a new browser window.");
26+
}
27+
28+
@Override
29+
public void createControl(Composite parent) {
30+
Composite composite = new Composite(parent, SWT.NONE);
31+
GridLayout gl = new GridLayout();
32+
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
33+
34+
int ncol = 2;
35+
gl.numColumns = ncol;
36+
composite.setLayout(gl);
37+
38+
Label endpointLabel = new Label(composite, SWT.NONE);
39+
endpointLabel.setText("Endpoint:");
40+
41+
endpoint = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
42+
endpoint.setLayoutData(gd);
43+
44+
createLine(composite, ncol);
45+
46+
Label unknownCertsLabel = new Label(composite, SWT.NONE);
47+
unknownCertsLabel.setText("Allow unknown certificate authorities:");
48+
49+
unknownCerts = new Button(composite, SWT.CHECK);
50+
unknownCerts.setLayoutData(gd);
51+
52+
// required to avoid an error in the system
53+
setControl(composite);
54+
setPageComplete(false);
55+
}
56+
57+
public void handleEvent(Event e) {
58+
getWizard().getContainer().updateButtons();
59+
}
60+
61+
public boolean isPageComplete() {
62+
return true;
63+
}
64+
65+
void onEnterPage() {
66+
endpoint.setText(Preferences.getInstance().getEndpoint());
67+
unknownCerts.setSelection(Preferences.getInstance().getBooleanPref(Preferences.INSECURE_KEY));
68+
}
69+
70+
private void createLine(Composite parent, int ncol) {
71+
Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.BOLD);
72+
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
73+
gridData.horizontalSpan = ncol;
74+
line.setLayoutData(gridData);
75+
}
76+
}

0 commit comments

Comments
 (0)