Merge pull request #1797 from raehalme/KEYCLOAK-2041

KEYCLOAK-2041 Use sendError instead of setStatus to report errors
This commit is contained in:
Bill Burke 2015-11-20 11:50:37 -05:00
commit ac1baa059f
9 changed files with 55 additions and 20 deletions

View file

@ -57,7 +57,7 @@ public class AuthenticatedActionsHandler {
protected boolean abortTokenResponse() { protected boolean abortTokenResponse() {
if (facade.getSecurityContext() == null) { if (facade.getSecurityContext() == null) {
log.debugv("Not logged in, sending back 401: {0}",facade.getRequest().getURI()); log.debugv("Not logged in, sending back 401: {0}",facade.getRequest().getURI());
facade.getResponse().setStatus(401); facade.getResponse().sendError(401);
facade.getResponse().end(); facade.getResponse().end();
return true; return true;
} }
@ -94,7 +94,7 @@ public class AuthenticatedActionsHandler {
log.debugv("allowedOrigins did not contain origin"); log.debugv("allowedOrigins did not contain origin");
} }
facade.getResponse().setStatus(403); facade.getResponse().sendError(403);
facade.getResponse().end(); facade.getResponse().end();
return true; return true;
} }

View file

@ -174,23 +174,7 @@ public class OAuthRequestAuthenticator {
final String state = getStateCode(); final String state = getStateCode();
final String redirect = getRedirectUri(state); final String redirect = getRedirectUri(state);
if (redirect == null) { if (redirect == null) {
return new AuthChallenge() { return challenge(403);
@Override
public boolean challenge(HttpFacade exchange) {
exchange.getResponse().setStatus(403);
return true;
}
@Override
public boolean errorPage() {
return true;
}
@Override
public int getResponseCode() {
return 403;
}
};
} }
return new AuthChallenge() { return new AuthChallenge() {
@ -283,7 +267,7 @@ public class OAuthRequestAuthenticator {
@Override @Override
public boolean challenge(HttpFacade exchange) { public boolean challenge(HttpFacade exchange) {
exchange.getResponse().setStatus(code); exchange.getResponse().sendError(code);
return true; return true;
} }
}; };

View file

@ -56,6 +56,7 @@ public interface HttpFacade {
void resetCookie(String name, String path); void resetCookie(String name, String path);
void setCookie(String name, String value, String path, String domain, int maxAge, boolean secure, boolean httpOnly); void setCookie(String name, String value, String path, String domain, int maxAge, boolean secure, boolean httpOnly);
OutputStream getOutputStream(); OutputStream getOutputStream();
void sendError(int code);
void sendError(int code, String message); void sendError(int code, String message);
/** /**

View file

@ -132,6 +132,13 @@ public class JaxrsHttpFacade implements OIDCHttpFacade {
throw new IllegalStateException("Not supported yet"); throw new IllegalStateException("Not supported yet");
} }
@Override
public void sendError(int code) {
javax.ws.rs.core.Response response = responseBuilder.status(code).build();
requestContext.abortWith(response);
responseFinished = true;
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
javax.ws.rs.core.Response response = responseBuilder.status(code).entity(message).build(); javax.ws.rs.core.Response response = responseBuilder.status(code).entity(message).build();

View file

@ -170,6 +170,15 @@ public class JettyHttpFacade implements HttpFacade {
} }
} }
@Override
public void sendError(int code) {
try {
response.sendError(code);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
try { try {

View file

@ -156,6 +156,15 @@ public class ServletHttpFacade implements HttpFacade {
} }
} }
@Override
public void sendError(int code) {
try {
response.sendError(code);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
try { try {

View file

@ -95,6 +95,15 @@ class WrappedHttpServletResponse implements Response {
} }
} }
@Override
public void sendError(int code) {
try {
response.sendError(code);
} catch (IOException e) {
throw new RuntimeException("Unable to set HTTP status", e);
}
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
try { try {

View file

@ -167,6 +167,15 @@ public class CatalinaHttpFacade implements HttpFacade {
} }
} }
@Override
public void sendError(int code) {
try {
response.sendError(code);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
try { try {
@ -176,6 +185,7 @@ public class CatalinaHttpFacade implements HttpFacade {
} }
} }
@Override @Override
public void end() { public void end() {
ended = true; ended = true;

View file

@ -170,6 +170,12 @@ public class UndertowHttpFacade implements HttpFacade {
return exchange.getOutputStream(); return exchange.getOutputStream();
} }
@Override
public void sendError(int code) {
exchange.setResponseCode(code);
exchange.endExchange();
}
@Override @Override
public void sendError(int code, String message) { public void sendError(int code, String message) {
exchange.setResponseCode(code); exchange.setResponseCode(code);