Skip to content

Get CSRF token from local storage #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,17 @@ public final class Constants {
/**
* The constant CSRF_DEFAULT_COOKIE_NAME.
*/
public static final String CSRF_DEFAULT_COOKIE_NAME= "XSRF-TOKEN";
public static final String CSRF_DEFAULT_COOKIE_NAME = "XSRF-TOKEN";

/**
* The constant CSRF_DEFAULT_LOCAL_STORAGE_KEY
*/
public static final String CSRF_DEFAULT_LOCAL_STORAGE_KEY = "XSRF-TOKEN";

/**
* The constant CSRF_DEFAULT_HEADER_NAME.
*/
public static final String CSRF_DEFAULT_HEADER_NAME= "X-XSRF-TOKEN";
public static final String CSRF_DEFAULT_HEADER_NAME = "X-XSRF-TOKEN";

/**
* The constant OPERATION_ATTRIBUTE.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,21 @@ public static class Csrf {
*/
private boolean enabled;

/**
* Use Local storage.
*/
private boolean useLocalStorage;

/**
* The Cookie name.
*/
private String cookieName = Constants.CSRF_DEFAULT_COOKIE_NAME;

/**
* The Local storage key.
*/
private String localStorageKey = Constants.CSRF_DEFAULT_LOCAL_STORAGE_KEY;

/**
* The Header name.
*/
Expand All @@ -145,6 +155,24 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

/**
* Use Local storage boolean.
*
* @return the boolean
*/
public boolean isUseLocalStorage() {
return useLocalStorage;
}

/**
* Sets useLocalStorage.
*
* @param useLocalStorage the use local storage
*/
public void setUseLocalStorage(boolean useLocalStorage) {
this.useLocalStorage = useLocalStorage;
}

/**
* Gets cookie name.
*
Expand All @@ -163,6 +191,24 @@ public void setCookieName(String cookieName) {
this.cookieName = cookieName;
}

/**
* Gets local storage key.
*
* @return the cookie name
*/
public String getLocalStorageKey() {
return localStorageKey;
}

/**
* Sets local storage key.
*
* @param localStorageKey the local storage key
*/
public void setLocalStorageKey(String localStorageKey) {
this.localStorageKey = localStorageKey;
}

/**
* Gets header name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
html = overwriteSwaggerDefaultUrl(html);
}
if (swaggerUiConfig.isCsrfEnabled()) {
html = addCSRF(html);
if (swaggerUiConfig.getCsrf().isUseLocalStorage()) {
html = addCSRFLocalStorage(html);
} else {
html = addCSRF(html);
}
}
if (swaggerUiConfig.getSyntaxHighlight() != null) {
html = addSyntaxHighlight(html);
Expand Down Expand Up @@ -174,6 +178,26 @@ protected String addCSRF(String html) {
return html.replace(PRESETS, stringBuilder.toString());
}

/**
* Add csrf string.
*
* @param html the html
* @return the string
*/
protected String addCSRFLocalStorage(String html) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("requestInterceptor: (request) => {\n");
stringBuilder.append("const value = window.localStorage.getItem('");
stringBuilder.append(swaggerUiConfig.getCsrf().getLocalStorageKey() + "');\n");
stringBuilder.append("request.headers['");
stringBuilder.append(swaggerUiConfig.getCsrf().getHeaderName());
stringBuilder.append("'] = value;\n");
stringBuilder.append("return request;\n");
stringBuilder.append("},\n");
stringBuilder.append(PRESETS);
return html.replace(PRESETS, stringBuilder.toString());
}

/**
* Add syntax highlight string.
*
Expand Down