Search in sources :

Example 1 with AUTHENTICATION_FAILURE

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);
}
Also used : EMPTY_BUFFER(io.netty.buffer.Unpooled.EMPTY_BUFFER) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Unpooled(io.netty.buffer.Unpooled) UserStorageService(org.spongepowered.api.service.user.UserStorageService) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ApiRequestException(org.cubeengine.module.apiserver.exception.ApiRequestException) CONTENT_TYPE(io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE) CommandManager(org.cubeengine.libcube.service.command.CommandManager) Charset(java.nio.charset.Charset) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) HTTP_1_1(io.netty.handler.codec.http.HttpVersion.HTTP_1_1) JsonNode(com.fasterxml.jackson.databind.JsonNode) CLOSE(io.netty.channel.ChannelFutureListener.CLOSE) CLOSE_ON_FAILURE(io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE) Log(org.cubeengine.logscribe.Log) User(org.spongepowered.api.entity.living.player.User) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Sponge(org.spongepowered.api.Sponge) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) WEBSOCKET_ROUTE(org.cubeengine.module.apiserver.WebSocketRequestHandler.WEBSOCKET_ROUTE) UUID(java.util.UUID) InetSocketAddress(java.net.InetSocketAddress) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Authorization(org.cubeengine.module.authorization.Authorization) AUTHENTICATION_FAILURE(org.cubeengine.module.apiserver.RequestStatus.AUTHENTICATION_FAILURE) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Optional(java.util.Optional) User(org.spongepowered.api.entity.living.player.User) InetSocketAddress(java.net.InetSocketAddress) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) ApiRequestException(org.cubeengine.module.apiserver.exception.ApiRequestException) UUID(java.util.UUID)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 EMPTY_BUFFER (io.netty.buffer.Unpooled.EMPTY_BUFFER)1 CLOSE (io.netty.channel.ChannelFutureListener.CLOSE)1 CLOSE_ON_FAILURE (io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 CONTENT_TYPE (io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 HTTP_1_1 (io.netty.handler.codec.http.HttpVersion.HTTP_1_1)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 InetSocketAddress (java.net.InetSocketAddress)1 Charset (java.nio.charset.Charset)1 HashMap (java.util.HashMap)1