server-side-user-authentication

The ability to perform custom user authentication on the server is important for many applications. By default the AppWarp server only guarantees uniqueness of users connected to a zone by username and doesn’t do any validation. Applications can perform client side verification but for applications with strict user admission control this may not sufficient. AppWarp S2 allows developers control over user admission.

When clients connect, they can also submit custom auth data string along with the username. For example

public void connectWithUserName(String userName, String authData);

Now on the server side, the zone adaptor’s handler method will be invoked which the application can override to perform custom logic.

 @Override
public void handleAddUserRequest(IUser user, String authData, HandlingResult result){
}          

User verification can be done synchronously or asynchronously. Below is an example of synchronous

                @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;
        }
}

In cases where the validation could be blocking (3rd party REST call or DB operation) it is better to do the validation asynchronously and return the adaptor callback without blocking. The application can then perform the validation in a separate thread and return the result later. Below is an example of 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 AppWarp S2 thread and return pending immediately.
        result.code = WarpResponseResultCode.AUTH_PENDING;
}