use of org.cubeengine.module.apiserver.RequestStatus.AUTHENTICATION_FAILURE in project modules-extra by CubeEngine.
the class HttpRequestHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest message) throws Exception {
InetSocketAddress inetSocketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
this.log.info("{} connected...", inetSocketAddress.getAddress().getHostAddress());
if (!this.server.isAddressAccepted(inetSocketAddress.getAddress())) {
this.log.info("Access denied!");
ctx.channel().close();
}
if (message.getDecoderResult().isFailure()) {
this.error(ctx, RequestStatus.UNKNOWN_ERROR);
this.log.info(message.getDecoderResult().cause(), "The decoder failed on this request...");
return;
}
boolean authorized = this.server.isAuthorized(inetSocketAddress.getAddress());
QueryStringDecoder qsDecoder = new QueryStringDecoder(message.getUri(), this.UTF8, true, 100);
final Parameters params = new Parameters(qsDecoder.parameters(), cm.getProviders());
User authUser = null;
if (!authorized) {
String user = params.get("user", String.class);
String pass = params.get("pass", String.class);
if (user == null || pass == null) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
Optional<User> byName = Sponge.getServiceManager().provide(UserStorageService.class).get().get(user);
if (!byName.isPresent()) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
UUID id = byName.get().getUniqueId();
// TODO make properly async
CompletableFuture<Boolean> cf = am.isPasswordSet(id).thenCompose(isSet -> am.checkPassword(id, pass).thenApply(correctPassword -> !isSet || !correctPassword));
Boolean authFailed = cf.get();
if (authFailed) {
this.error(ctx, AUTHENTICATION_FAILURE, new ApiRequestException("Could not complete authentication", 200));
return;
}
authUser = byName.get();
}
String path = qsDecoder.path().trim();
if (path.length() == 0 || "/".equals(path)) {
this.error(ctx, RequestStatus.ROUTE_NOT_FOUND);
return;
}
path = normalizePath(path);
// is this request intended to initialize a websockets connection?
if (WEBSOCKET_ROUTE.equals(path)) {
WebSocketRequestHandler handler;
if (!(ctx.pipeline().last() instanceof WebSocketRequestHandler)) {
handler = new WebSocketRequestHandler(cm, server, objectMapper, authUser);
ctx.pipeline().addLast("wsEncoder", new TextWebSocketFrameEncoder(objectMapper));
ctx.pipeline().addLast("handler", handler);
} else {
handler = (WebSocketRequestHandler) ctx.pipeline().last();
}
this.log.info("received a websocket request...");
handler.doHandshake(ctx, message);
return;
}
this.handleHttpRequest(ctx, message, path, params, authUser);
}
Aggregations