Skip to content

Commit 7d8360c

Browse files
author
Yevhenii Voevodin
authored
csrf init (eclipse-che#5120)
1 parent 63b440a commit 7d8360c

5 files changed

Lines changed: 60 additions & 4 deletions

File tree

ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/app/AppContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
import org.eclipse.che.ide.resource.Path;
2525

2626
import java.util.List;
27+
import java.util.Map;
2728

2829
/**
2930
* Represents current context of the IDE application.
3031
*
3132
* @author Vitaly Parfonov
3233
* @author Artem Zatsarynnyi
3334
* @author Vlad Zhukovskyi
35+
* @author Yevhenii Voevodin
3436
*/
3537
public interface AppContext {
3638

@@ -185,7 +187,7 @@ public interface AppContext {
185187
FactoryDto getFactory();
186188

187189
void setFactory(FactoryDto factory);
188-
190+
189191
String getWorkspaceId();
190192

191193
/**
@@ -218,4 +220,13 @@ public interface AppContext {
218220
* @return identifier
219221
*/
220222
String getAppId();
223+
224+
/**
225+
* Returns context properties, key-value storage that allows to store
226+
* data in the context for plugins and extensions.
227+
*
228+
* @return a modifiable properties map
229+
* @since 5.11.0
230+
*/
231+
Map<String, String> getProperties();
221232
}

ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
import org.eclipse.che.ide.statepersistance.AppStateManager;
5151

5252
import java.util.ArrayList;
53+
import java.util.HashMap;
5354
import java.util.List;
55+
import java.util.Map;
5456

5557
import static com.google.common.base.Preconditions.checkState;
5658
import static com.google.common.collect.Lists.newArrayList;
@@ -95,6 +97,7 @@ public class AppContextImpl implements AppContext,
9597
private Path projectsRoot;
9698
private ActiveRuntime runtime;
9799
private ResourceManager resourceManager;
100+
private Map<String, String> properties;
98101

99102
/**
100103
* List of actions with parameters which comes from startup URL.
@@ -463,4 +466,12 @@ public String getAppId() {
463466
public ActiveRuntime getActiveRuntime() {
464467
return runtime;
465468
}
469+
470+
@Override
471+
public Map<String, String> getProperties() {
472+
if (properties == null) {
473+
properties = new HashMap<>();
474+
}
475+
return properties;
476+
}
466477
}

ide/commons-gwt/src/main/java/org/eclipse/che/ide/rest/AsyncRequestFactory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public AsyncRequest createRequest(RequestBuilder.Method method, String url, List
213213
protected AsyncRequest doCreateRequest(RequestBuilder.Method method, String url, Object dtoBody, boolean async) {
214214
Preconditions.checkNotNull(method, "Request method should not be a null");
215215

216-
AsyncRequest asyncRequest = new AsyncRequest(method, url, async);
216+
AsyncRequest asyncRequest = newAsyncRequest(method, url, async);
217217
if (dtoBody != null) {
218218
if (dtoBody instanceof List) {
219219
asyncRequest.data(dtoFactory.toJson((List)dtoBody));
@@ -239,6 +239,20 @@ protected AsyncRequest doCreateRequest(RequestBuilder.Method method, String url,
239239
return asyncRequest;
240240
}
241241

242+
/**
243+
* A factory method which creates a new instance of {@link AsyncRequest}.
244+
*
245+
* @param method
246+
* the request method
247+
* @param url
248+
* the url to go to
249+
* @param async
250+
* whether this request is asynchronous in terms of Everrest polling strategy
251+
*/
252+
protected AsyncRequest newAsyncRequest(RequestBuilder.Method method, String url, boolean async) {
253+
return new AsyncRequest(method, url, async);
254+
}
255+
242256
/**
243257
* Creates new GET request to the specified {@code url}.
244258
*

plugins/plugin-machine/che-plugin-machine-ssh-client/src/main/java/org/eclipse/che/ide/ext/ssh/client/upload/UploadSshKeyPresenter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class UploadSshKeyPresenter implements UploadSshKeyView.ActionDelegate {
3636
private String restContext;
3737
private NotificationManager notificationManager;
3838
private AsyncCallback<Void> callback;
39+
private AppContext appContext;
3940

4041
@Inject
4142
public UploadSshKeyPresenter(UploadSshKeyView view,
@@ -47,6 +48,7 @@ public UploadSshKeyPresenter(UploadSshKeyView view,
4748
this.constant = constant;
4849
this.restContext = appContext.getMasterEndpoint();
4950
this.notificationManager = notificationManager;
51+
this.appContext = appContext;
5052
}
5153

5254
/** Show dialog. */
@@ -71,7 +73,15 @@ public void onUploadClicked() {
7173
return;
7274
}
7375
view.setEncoding(FormPanel.ENCODING_MULTIPART);
74-
view.setAction(restContext + "/ssh");
76+
77+
String action = restContext + "/ssh";
78+
79+
String csrfToken = appContext.getProperties().get("X-CSRF-Token");
80+
if (csrfToken != null) {
81+
action += "?X-CSRF-Token=" + csrfToken;
82+
}
83+
84+
view.setAction(action);
7585
view.submit();
7686
}
7787

wsagent/che-core-ssh-key-ide/src/main/java/org/eclipse/che/plugin/ssh/key/client/upload/UploadSshKeyPresenter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class UploadSshKeyPresenter implements UploadSshKeyView.ActionDelegate {
3636
private String restContext;
3737
private NotificationManager notificationManager;
3838
private AsyncCallback<Void> callback;
39+
private AppContext appContext;
3940

4041
@Inject
4142
public UploadSshKeyPresenter(UploadSshKeyView view,
@@ -47,6 +48,7 @@ public UploadSshKeyPresenter(UploadSshKeyView view,
4748
this.constant = constant;
4849
this.restContext = appContext.getMasterEndpoint();
4950
this.notificationManager = notificationManager;
51+
this.appContext = appContext;
5052
}
5153

5254
/** Show dialog. */
@@ -73,7 +75,15 @@ public void onUploadClicked() {
7375
return;
7476
}
7577
view.setEncoding(FormPanel.ENCODING_MULTIPART);
76-
view.setAction(restContext + "/ssh");
78+
79+
String action = restContext + "/ssh";
80+
81+
String csrfToken = appContext.getProperties().get("X-CSRF-Token");
82+
if (csrfToken != null) {
83+
action += "?X-CSRF-Token=" + csrfToken;
84+
}
85+
86+
view.setAction(action);
7787
view.submit();
7888
}
7989

0 commit comments

Comments
 (0)