Last week we released AppWarpS2, our on-premise game server. In this post, I will describe how developers can customize admission control in the game server via our server side adaptors.
Authentication involves two things – client sending its credentials and the server validating and authorizing admission. From the client-side, you can simply pass the user’s name and authentication data in the connect request as follows
public void connectWithUserName(String userName, String authData);
On the server the default behavior is to not do any validation and ignore the authentication data and allow the client admission. To alter this behavior, you will need to override the following method in the application zone’s adaptor
@Override
public void handleAddUserRequest(IUser user, String authData, HandlingResult result)
The outcome of your logic is communicated to the server (success/failure) by setting the code field of the HandlingResult parameter.
@Override
public void handleAddUserRequest(IUser user, String authData, HandlingResult result){
if(isUserValid(user.getName(), authData)){
result.code = WarpResponseResultCode.SUCCESS;
}
else{
result.code = WarpResponseResultCode.AUTH_ERROR;
}
}
Above is an example of synchronous authentication. This is easy to use but the method isUserValid can potentially block the server thread if its going to validate using a 3rd party REST call for example. This way of handling is not ideal in such cases where the validation can block the thread. For such cases, AppWarpS2 also supports asynchronous authentication. In this technique, you will return from the override method right away with a pending result code which will instruct the server to hold the client’s request and hence not block the server thread. On a separate thread the application will validate the user and once the result is known will instruct the server to send the response back to the client. Below is an illustration on how to do this.
@Override
public void handleAddUserRequest(IUser user, String authData, HandlingResult result){
// perform the validation in a separate thread.
new Thread(new Runnable() {
@Override
public void run() {
if(isUserValid(user.getName(), authData)){
izone.sendAddUserResponse(user, WarpResponseResultCode.SUCCESS, "Auth success on server");
}
else{
izone.sendAddUserResponse(user, WarpResponseResultCode.AUTH_ERROR, "Auth failed on server");
}
}
}).start();
// don't block the AppWarpS2 thread and return pending immediately.
result.code = WarpResponseResultCode.AUTH_PENDING;
}
This technique is also illustrated in our AppWarpS2 Rummy card game sample which allows client to connect via facebook. The authentication data passed by the client in the sample is the facebook token. The server uses facebook graph API to validate the token for the connecting user. In the sample we the authorization is done using the asynchronous technique described above.
Visit the AppWarpS2 page to sign-up, login and download the setup. Please send your questions and comment to support@shephertz.com
Leave A Reply