Skip to content

Commit

Permalink
Merge pull request #370 from jacwah/auth
Browse files Browse the repository at this point in the history
Require clients to log in
  • Loading branch information
Zomis committed Aug 23, 2015
2 parents 91e48a9 + 75a6854 commit 2749dd5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cardshifter.server.model;

import com.cardshifter.api.*;
import com.cardshifter.api.messages.*;
import com.cardshifter.api.outgoing.*;
import com.cardshifter.core.messages.*;

public class AuthorizationDecorator<T extends Message> implements MessageHandler<T> {

private final MessageHandler<T> handler;

public AuthorizationDecorator(MessageHandler<T> handler) {
this.handler = handler;
}

@Override
public void handle(T message, ClientIO client) {
if (client.isLoggedIn()) {
handler.handle(message, client);
}
else {
client.sendToClient(new ServerErrorMessage("Login required"));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.cardshifter.server.model;

import com.cardshifter.api.*;
import com.cardshifter.api.both.*;
import com.cardshifter.api.incoming.*;
import com.cardshifter.api.messages.*;
import com.cardshifter.api.outgoing.*;
import com.cardshifter.core.messages.*;
import org.apache.log4j.*;

import java.util.*;

public class HandlerManager {

private static final Logger logger = LogManager.getLogger(HandlerManager.class);

private final IncomingHandler incomingHandler;
private final CommandHandler commandHandler;

public HandlerManager(Server server) {
incomingHandler = new IncomingHandler();
commandHandler = new CommandHandler(server);

Handlers handlers = new Handlers(server);

addUnauthorizedHandler("login", LoginMessage.class, handlers::loginMessage);

addHandler("chat", ChatMessage.class, handlers::chat);
addHandler("startgame", StartGameRequest.class, handlers::play);
addHandler("inviteResponse", InviteResponse.class, handlers::inviteResponse);
addHandler("query", ServerQueryMessage.class, handlers::query);

// Directly game-related
addHandler("use", UseAbilityMessage.class, handlers::useAbility);
addHandler("requestTargets", RequestTargetsMessage.class, handlers::requestTargets);
addHandler("playerconfig", PlayerConfigMessage.class, handlers::incomingConfig);
}

public IncomingHandler getIncomingHandler() {
return incomingHandler;
}

public CommandHandler getCommandHandler() {
return commandHandler;
}

public <E extends Message> void addHandler(String command, Class<E> handler, MessageHandler<E> consumer) {
this.addUnauthorizedHandler(command, handler, new AuthorizationDecorator<>(consumer));
}

public <E extends Message> void addUnauthorizedHandler(String command, Class<E> handler, MessageHandler<E> consumer) {
this.incomingHandler.addHandler(command, handler, consumer);
}

public void handleMessage(ClientIO client, String json) {
Objects.requireNonNull(client, "Cannot handle message from a null client");
logger.info("Handle message " + client + ": " + json);

Message message;
try {
message = getIncomingHandler().parse(json);
logger.info("Parsed Message: " + message);
getIncomingHandler().perform(message, client);
} catch (Exception e) {
logger.error("Unable to parse incoming json: " + json, e);
client.sendToClient(new ServerErrorMessage(e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.cardshifter.api.incoming.StartGameRequest;
import com.cardshifter.api.incoming.UseAbilityMessage;
import com.cardshifter.api.messages.Message;
import com.cardshifter.api.outgoing.ClientDisconnectedMessage;
import com.cardshifter.api.outgoing.ServerErrorMessage;
import com.cardshifter.api.outgoing.UserStatusMessage;
import com.cardshifter.api.outgoing.UserStatusMessage.Status;
Expand All @@ -46,8 +45,7 @@ public class Server implements ClientServerInterface {
/**
* The IncomingHandler receives messages and passes them to the correct Handler
*/
private final IncomingHandler incomingHandler;
private final CommandHandler commandHandler;
private final HandlerManager handlerManager;

private final Map<Integer, ClientIO> clients = new ConcurrentHashMap<>();
private final Map<Integer, ChatArea> chats = new ConcurrentHashMap<>();
Expand All @@ -62,26 +60,9 @@ public class Server implements ClientServerInterface {
private final ChatArea mainChat;

public Server() {
this.incomingHandler = new IncomingHandler();
this.commandHandler = new CommandHandler(this);
this.handlerManager = new HandlerManager(this);
this.scheduler = Executors.newScheduledThreadPool(2, new ThreadFactoryBuilder().setNameFormat("ai-thread-%d").build());
mainChat = this.newChatRoom("Main");

Handlers handlers = new Handlers(this);

/**
* Add a handler for each type of command, message, and method in Handlers
*/
incomingHandler.addHandler("login", LoginMessage.class, handlers::loginMessage);
incomingHandler.addHandler("chat", ChatMessage.class, handlers::chat);
incomingHandler.addHandler("startgame", StartGameRequest.class, handlers::play);
incomingHandler.addHandler("inviteResponse", InviteResponse.class, handlers::inviteResponse);
incomingHandler.addHandler("query", ServerQueryMessage.class, handlers::query);

// Directly game-related
incomingHandler.addHandler("use", UseAbilityMessage.class, handlers::useAbility);
incomingHandler.addHandler("requestTargets", RequestTargetsMessage.class, handlers::requestTargets);
incomingHandler.addHandler("playerconfig", PlayerConfigMessage.class, handlers::incomingConfig);
}

/**
Expand Down Expand Up @@ -139,28 +120,18 @@ public void trySetClientName(ClientIO client, UserName userName) throws UserName
* @return Returns the IncomingHandler for the Server
*/
public IncomingHandler getIncomingHandler() {
return incomingHandler;
return handlerManager.getIncomingHandler();
}

/**
* Passes the message to incomingHandler which will parse and perform it
* Passes the message to the IncomingHandler which will parse and perform it
*
* @param client The client sending the message
* @param json The actual contents of the message
*/
@Override
public void handleMessage(ClientIO client, String json) {
Objects.requireNonNull(client, "Cannot handle message from a null client");
logger.info("Handle message " + client + ": " + json);
Message message;
try {
message = incomingHandler.parse(json);
logger.info("Parsed Message: " + message);
incomingHandler.perform(message, client);
} catch (Exception e) {
logger.error("Unable to parse incoming json: " + json, e);
client.sendToClient(new ServerErrorMessage(e.getMessage()));
}
handlerManager.handleMessage(client, json);
}

/**
Expand Down Expand Up @@ -304,7 +275,7 @@ public void stop() {
* @return The CommandHandler object
*/
public CommandHandler getCommandHandler() {
return commandHandler;
return handlerManager.getCommandHandler();
}

@Override
Expand Down

0 comments on commit 2749dd5

Please sign in to comment.