Authenticating Client ID


Table of Contents

What is a User Identifier Authenticator?

Because there may be many clients interacting with the server, there must be a way to keep track of all of them and distinguish them apart from each other. A user identifier authenticator allows your server to verify whether the user client ID (defined by the interface UserIdentifierInterface) is valid. When initializing the server, the developer can enumerate the valid user identifiers along with the user identifier authenticator. Any user identifier that authenticator rejects is invalid.

Creating a UserIdentifier

A UserIdentifier is our way of identifying our clients by associating it with strings. All UserIdentifiers implement UserIdentifierInterface, which requires the implementation of the methods equals(uid2)getString()hashCode().  The method equals(uid2) returns true if uid2 represents the same user as the object calling the method. getString() returns a string representation of the UserIdentifier object, and hashCode() is a function that returns a unique hash code for the instance of the UserIdentifier. A UserIdentifier called BasicUserIdentifier is already implemented for you – it simply identifies a user by an input string.

public class SimpleIdentifier implements UserIdentifierInterface{
    
    // Every instance of this UserIdentifier will be identified with the string "string"
    this._id = "string";

    // We assume uid2 is also a UserIdentifier and compare it's string representation with ours
    @Override
    public boolean equals(Object uid2) {
        return this._id.equals(((UserIdentifierInterface) uid2).getString());
    }

    // We return the id "string" here
    @Override
    public String getString() {
        return this._id;
    }

    // In this simple implementation of a UserIdentifier, our hashcode function will be the same as the one provided by Java
    @Override
    public int hashCode() {
        this._id.hashCode();
    }
}

How to Create a User ID Authenticator

There is a User Identifier Authenticator implemented for your convenience (UserIDAuthenticator), but you can create your own authenticator by implementing the UserIdentifierInterface. The interface requires the implementation of the method isIDValid(uid), which returns a boolean value. If a server protocol calls isIDValid(uid2) on a userID and finds that it returns true, then the server will move on to start accepting jobs. Otherwise, it will discontinue connecting with the client. For example, the following class implements a user ID authenticator that regards all IDs as valid:

public class YourUserIDAuthenticator implements UserIDAuthenticatorInterface{
    // always returns true because all IDs are compatible to the server
    @Override
    public boolean isIDValid(UserIdentifierInterface uid) {
        return true;
    }
}

How Do I Use a User ID Authenticator?

It is up to your discretion on which user identifiers are compatible to your servers. That means a user identifier authenticator should be initialized when you set up your server:

// initializing your user identification authenticator
YourUserIDAuthenticator YourAuthenticator = new YourUserIDAuthenticator();

// pass the user ID authenticator into the server protocol
TIProtocol tiProtocol = new TIProtocol(ciphers, finalPort, keyStorePassword, clientVersionChecker, YourAuthenticator);

Once you’ve created your user ID authenticator, all you need to do is pass the authenticator into the server protocol, and the protocol will do the rest. You can refer to Start a Server to learn more about initializing your server.

Running Example

To simplify things a bit, in our running example application we will use the example user authenticator from above that regards all IDs as valid. We create a new file under the server of our running example and name it AcceptAllUsers.java.

To see how this user ID authenticator is used in the running example, refer to Start a Server under “Running Example.”

The next part of the running example can be found here.