Skip to content

Authenticated Mesh Network

gcflames5 edited this page Aug 26, 2014 · 3 revisions

The AuthenticatedMesh behaves exactly like a Mesh, however, in order for the connection to be accepted, an AuthenticationPacket must be sent and accepted by the receiver's Authenticator.

The first thing we need to do is create an extension of AuthenticationPacket. In this example, I will be authenticating user by a username and password:

public class UserAuthenticationPacket extends AuthenticationPacket {

    //If you choose not to use the encrypted socket I recommend that
    //you use some method of encryption in this packet
    private String username, password;

    public UserAuthenticationPacket() {} //necessary for all packets

    public UserAuthenticationPacket(String username, String password){
        this.username = username;
        this.password = password;
    }

    public String getUsername(){ return username; }

    public String getPassword(){ return password; }

}

Next we have to build an Authenticator that takes in an AuthenticationPacket and determines whether or not it is valid:

public class UserAuthenticator implements Authenticator {
    
    public boolean authenticate(AuthenticationPacket authenticationPacket) {
         //Code that could ping a website, check a database, etc.
    }

}

Now we can make the AuthenticatedMesh. It needs an instance of an Authenticator, an instance of AuthenticationPacket, a port, a list of IPs that are in the mesh:

AuthenticatedMesh mesh = new AuthenticatedMesh(new UserAuthenticator(), new UserAuthenticationPacket("Username", "Password"), 1337, ip);
try { manager.initialize(); } 
catch (IOException e) { /* handle exception */ }

Clone this wiki locally