use of org.apache.camel.component.netty.http.HttpPrincipal in project camel by apache.
the class HttpServerChannelHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception {
HttpRequest request = (HttpRequest) messageEvent.getMessage();
LOG.debug("Message received: {}", request);
if (consumer.isSuspended()) {
// are we suspended?
LOG.debug("Consumer suspended, cannot service request {}", request);
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, SERVICE_UNAVAILABLE);
response.setChunked(false);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
}
// if its an OPTIONS request then return which methods is allowed
boolean isRestrictedToOptions = consumer.getEndpoint().getHttpMethodRestrict() != null && consumer.getEndpoint().getHttpMethodRestrict().contains("OPTIONS");
if ("OPTIONS".equals(request.getMethod().getName()) && !isRestrictedToOptions) {
String s;
if (consumer.getEndpoint().getHttpMethodRestrict() != null) {
s = "OPTIONS," + consumer.getEndpoint().getHttpMethodRestrict();
} else {
// allow them all
s = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
}
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
response.setChunked(false);
response.headers().set("Allow", s);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
}
if (consumer.getEndpoint().getHttpMethodRestrict() != null && !consumer.getEndpoint().getHttpMethodRestrict().contains(request.getMethod().getName())) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
response.setChunked(false);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
}
if ("TRACE".equals(request.getMethod().getName()) && !consumer.getEndpoint().isTraceEnabled()) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
response.setChunked(false);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
}
// must include HOST header as required by HTTP 1.1
if (!request.headers().contains(HttpHeaders.Names.HOST)) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, BAD_REQUEST);
response.setChunked(false);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
}
// is basic auth configured
NettyHttpSecurityConfiguration security = consumer.getEndpoint().getSecurityConfiguration();
if (security != null && security.isAuthenticate() && "Basic".equalsIgnoreCase(security.getConstraint())) {
String url = request.getUri();
// drop parameters from url
if (url.contains("?")) {
url = ObjectHelper.before(url, "?");
}
// we need the relative path without the hostname and port
URI uri = new URI(request.getUri());
String target = uri.getPath();
// strip the starting endpoint path so the target is relative to the endpoint uri
String path = consumer.getConfiguration().getPath();
if (path != null) {
// need to match by lower case as we want to ignore case on context-path
path = path.toLowerCase(Locale.US);
String match = target.toLowerCase(Locale.US);
if (match.startsWith(path)) {
target = target.substring(path.length());
}
}
// is it a restricted resource?
String roles;
if (security.getSecurityConstraint() != null) {
// if restricted returns null, then the resource is not restricted and we should not authenticate the user
roles = security.getSecurityConstraint().restricted(target);
} else {
// assume any roles is valid if no security constraint has been configured
roles = "*";
}
if (roles != null) {
// basic auth subject
HttpPrincipal principal = extractBasicAuthSubject(request);
// authenticate principal and check if the user is in role
Subject subject = null;
boolean inRole = true;
if (principal != null) {
subject = authenticate(security.getSecurityAuthenticator(), security.getLoginDeniedLoggingLevel(), principal);
if (subject != null) {
String userRoles = security.getSecurityAuthenticator().getUserRoles(subject);
inRole = matchesRoles(roles, userRoles);
}
}
if (principal == null || subject == null || !inRole) {
if (principal == null) {
LOG.debug("Http Basic Auth required for resource: {}", url);
} else if (subject == null) {
LOG.debug("Http Basic Auth not authorized for username: {}", principal.getUsername());
} else {
LOG.debug("Http Basic Auth not in role for username: {}", principal.getUsername());
}
// restricted resource, so send back 401 to require valid username/password
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, UNAUTHORIZED);
response.headers().set("WWW-Authenticate", "Basic realm=\"" + security.getRealm() + "\"");
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
response.setContent(ChannelBuffers.copiedBuffer(new byte[] {}));
messageEvent.getChannel().write(response).syncUninterruptibly();
messageEvent.getChannel().close();
return;
} else {
LOG.debug("Http Basic Auth authorized for username: {}", principal.getUsername());
}
}
}
// let Camel process this message
// It did the way as camel-netty component does
super.messageReceived(ctx, messageEvent);
}
use of org.apache.camel.component.netty.http.HttpPrincipal in project camel by apache.
the class HttpServerChannelHandler method extractBasicAuthSubject.
/**
* Extracts the username and password details from the HTTP basic header Authorization.
* <p/>
* This requires that the <tt>Authorization</tt> HTTP header is provided, and its using Basic.
* Currently Digest is <b>not</b> supported.
*
* @return {@link HttpPrincipal} with username and password details, or <tt>null</tt> if not possible to extract
*/
protected static HttpPrincipal extractBasicAuthSubject(HttpRequest request) {
String auth = request.headers().get("Authorization");
if (auth != null) {
String constraint = ObjectHelper.before(auth, " ");
if (constraint != null) {
if ("Basic".equalsIgnoreCase(constraint.trim())) {
String decoded = ObjectHelper.after(auth, " ");
// the decoded part is base64 encoded, so we need to decode that
ChannelBuffer buf = ChannelBuffers.copiedBuffer(decoded.getBytes());
ChannelBuffer out = Base64.decode(buf);
String userAndPw = out.toString(Charset.defaultCharset());
String username = ObjectHelper.before(userAndPw, ":");
String password = ObjectHelper.after(userAndPw, ":");
HttpPrincipal principal = new HttpPrincipal(username, password);
LOG.debug("Extracted Basic Auth principal from HTTP header: {}", principal);
return principal;
}
}
}
return null;
}
Aggregations