1
18 package io.undertow.servlet.handlers.security;
19
20 import static io.undertow.servlet.UndertowServletMessages.MESSAGES;
21
22 import io.undertow.security.handlers.SinglePortConfidentialityHandler;
23 import io.undertow.server.HttpHandler;
24 import io.undertow.server.HttpServerExchange;
25 import io.undertow.servlet.api.AuthorizationManager;
26 import io.undertow.servlet.api.ConfidentialPortManager;
27 import io.undertow.servlet.api.TransportGuaranteeType;
28 import io.undertow.servlet.handlers.ServletRequestContext;
29 import io.undertow.util.StatusCodes;
30
31 import javax.servlet.http.HttpServletResponse;
32 import java.net.URI;
33 import java.net.URISyntaxException;
34
35
40 public class ServletConfidentialityConstraintHandler extends SinglePortConfidentialityHandler {
41
42 private final ConfidentialPortManager portManager;
43
44 public ServletConfidentialityConstraintHandler(final ConfidentialPortManager portManager, final HttpHandler next) {
45 super(next, -1);
46 this.portManager = portManager;
47 }
48
49 @Override
50 public void handleRequest(HttpServerExchange exchange) throws Exception {
51 final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
52 final AuthorizationManager authorizationManager = servletRequestContext.getDeployment().getDeploymentInfo().getAuthorizationManager();
53
54 TransportGuaranteeType connectionGuarantee = servletRequestContext.getOriginalRequest().isSecure() ? TransportGuaranteeType.CONFIDENTIAL : TransportGuaranteeType.NONE;
55 TransportGuaranteeType transportGuarantee = authorizationManager.transportGuarantee(connectionGuarantee,
56 servletRequestContext.getTransportGuarenteeType(), servletRequestContext.getOriginalRequest());
57 servletRequestContext.setTransportGuarenteeType(transportGuarantee);
58
59 if (TransportGuaranteeType.REJECTED == transportGuarantee) {
60 HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
61 response.sendError(StatusCodes.FORBIDDEN);
62 return;
63 }
64 super.handleRequest(exchange);
65 }
66
67 @Override
68 protected boolean confidentialityRequired(HttpServerExchange exchange) {
69 TransportGuaranteeType transportGuarantee = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getTransportGuarenteeType();
70
71
72
73 return (TransportGuaranteeType.CONFIDENTIAL == transportGuarantee || TransportGuaranteeType.INTEGRAL == transportGuarantee);
74 }
75
76 @Override
77 protected URI getRedirectURI(HttpServerExchange exchange) throws URISyntaxException {
78 int port = portManager.getConfidentialPort(exchange);
79 if (port < 0) {
80 throw MESSAGES.noConfidentialPortAvailable();
81 }
82
83 return super.getRedirectURI(exchange, port);
84 }
85
86
94 protected boolean isConfidential(final HttpServerExchange exchange) {
95 ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
96 if(src != null) {
97 return src.getOriginalRequest().isSecure();
98 }
99 return super.isConfidential(exchange);
100 }
101 }
102