Search in sources :

Example 1 with HandshakeData

use of com.corundumstudio.socketio.HandshakeData in project netty-socketio by mrniko.

the class AuthorizeHandler method authorize.

private boolean authorize(ChannelHandlerContext ctx, Channel channel, String origin, Map<String, List<String>> params, FullHttpRequest req) throws IOException {
    Map<String, List<String>> headers = new HashMap<String, List<String>>(req.headers().names().size());
    for (String name : req.headers().names()) {
        List<String> values = req.headers().getAll(name);
        headers.put(name, values);
    }
    HandshakeData data = new HandshakeData(req.headers(), params, (InetSocketAddress) channel.remoteAddress(), req.uri(), origin != null && !origin.equalsIgnoreCase("null"));
    boolean result = false;
    try {
        result = configuration.getAuthorizationListener().isAuthorized(data);
    } catch (Exception e) {
        log.error("Authorization error", e);
    }
    if (!result) {
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        log.debug("Handshake unauthorized, query params: {} headers: {}", params, headers);
        return false;
    }
    UUID sessionId = this.generateOrGetSessionIdFromRequest(req.headers());
    List<String> transportValue = params.get("transport");
    if (transportValue == null) {
        log.error("Got no transports for request {}", req.uri());
        HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
        channel.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
        return false;
    }
    Transport transport = Transport.byName(transportValue.get(0));
    if (!configuration.getTransports().contains(transport)) {
        Map<String, Object> errorData = new HashMap<String, Object>();
        errorData.put("code", 0);
        errorData.put("message", "Transport unknown");
        channel.attr(EncoderHandler.ORIGIN).set(origin);
        channel.writeAndFlush(new HttpErrorMessage(errorData));
        return false;
    }
    ClientHead client = new ClientHead(sessionId, ackManager, disconnectable, storeFactory, data, clientsBox, transport, disconnectScheduler, configuration);
    channel.attr(ClientHead.CLIENT).set(client);
    clientsBox.addClient(client);
    String[] transports = {};
    if (configuration.getTransports().contains(Transport.WEBSOCKET)) {
        transports = new String[] { "websocket" };
    }
    AuthPacket authPacket = new AuthPacket(sessionId, transports, configuration.getPingInterval(), configuration.getPingTimeout());
    Packet packet = new Packet(PacketType.OPEN);
    packet.setData(authPacket);
    client.send(packet);
    client.schedulePingTimeout();
    log.debug("Handshake authorized for sessionId: {}, query params: {} headers: {}", sessionId, params, headers);
    return true;
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HashMap(java.util.HashMap) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) IOException(java.io.IOException) HttpErrorMessage(com.corundumstudio.socketio.messages.HttpErrorMessage) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) HandshakeData(com.corundumstudio.socketio.HandshakeData) List(java.util.List) UUID(java.util.UUID) Transport(com.corundumstudio.socketio.Transport)

Aggregations

HandshakeData (com.corundumstudio.socketio.HandshakeData)1 Transport (com.corundumstudio.socketio.Transport)1 HttpErrorMessage (com.corundumstudio.socketio.messages.HttpErrorMessage)1 AuthPacket (com.corundumstudio.socketio.protocol.AuthPacket)1 Packet (com.corundumstudio.socketio.protocol.Packet)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 UUID (java.util.UUID)1