Authenticating Client ID


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.

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:

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:

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.