Skip to content

Commit

Permalink
KEYCLOAK-5664 (keycloak#4604)
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst authored Nov 7, 2017
1 parent 1db3134 commit b1a05df
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ public Response createUser(final MultivaluedMap<String, String> formData) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}

String cookieStateChecker = getCsrfCookie();
String formStateChecker = formData.getFirst("stateChecker");
csrfCheck(cookieStateChecker, formStateChecker);
csrfCheck(formData);

String username = formData.getFirst("username");
String password = formData.getFirst("password");
Expand Down Expand Up @@ -183,7 +181,7 @@ private Response createWelcomePage(String successMessage, String errorMessage) {
map.put("localUser", isLocal);

if (isLocal) {
String stateChecker = updateCsrfChecks();
String stateChecker = setCsrfCookie();
map.put("stateChecker", stateChecker);
}
}
Expand Down Expand Up @@ -242,25 +240,23 @@ private boolean isLocalAddress(InetAddress inetAddress) {
return inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress();
}

private String updateCsrfChecks() {
String stateChecker = getCsrfCookie();
if (stateChecker != null) {
return stateChecker;
} else {
stateChecker = Base64Url.encode(KeycloakModelUtils.generateSecret());
String cookiePath = uriInfo.getPath();
boolean secureOnly = uriInfo.getRequestUri().getScheme().equalsIgnoreCase("https");
CookieHelper.addCookie(KEYCLOAK_STATE_CHECKER, stateChecker, cookiePath, null, null, -1, secureOnly, true);
return stateChecker;
}
private String setCsrfCookie() {
String stateChecker = Base64Url.encode(KeycloakModelUtils.generateSecret());
String cookiePath = uriInfo.getPath();
boolean secureOnly = uriInfo.getRequestUri().getScheme().equalsIgnoreCase("https");
CookieHelper.addCookie(KEYCLOAK_STATE_CHECKER, stateChecker, cookiePath, null, null, -1, secureOnly, true);
return stateChecker;
}

private String getCsrfCookie() {
private void csrfCheck(final MultivaluedMap<String, String> formData) {
String formStateChecker = formData.getFirst("stateChecker");
Cookie cookie = headers.getCookies().get(KEYCLOAK_STATE_CHECKER);
return cookie==null ? null : cookie.getValue();
}
if (cookie == null) {
throw new ForbiddenException();
}

String cookieStateChecker = cookie.getValue();

private void csrfCheck(String cookieStateChecker, String formStateChecker) {
if (cookieStateChecker == null || !cookieStateChecker.equals(formStateChecker)) {
throw new ForbiddenException();
}
Expand Down

0 comments on commit b1a05df

Please sign in to comment.