This is a simple security provider based on Minecraft sessions.
ExecutorService executorService = Executors.newSingleThreadExecutor();
SimpleSecurityProvider securityProvider = new SimpleSecurityProvider("sign key", 0L); // TODO: Replace with your own key and salt.
MinecraftAPI client = MinecraftAPI.client(executorService);
MinecraftAPI server = MinecraftAPI.server(executorService, securityProvider);
client.join(
"access token",
"uuid",
"username"
).thenApply(serverId -> server.hasJoined("username", serverId)
.thenApply(securityProvider::generateToken)
.thenAccept(System.out::println) // TODO: Send token to client.
).join();
executorService.shutdown();
- It's your work sending the username and serverId to the server.
- It's your work sending the token to the client.
Optional<String> hid = securityProvider.getHidIfValid(token);
//or
boolean valid = securityProvider.verifyToken(token);
Each time the client uses the token, you should regenerate it.
securityProvider.getHidIfValid(token).map(securityProvider::generateToken);
All the initialization should be done in the same scope (e.g. in the main method). The initialization should be done in the following order:
- Read the key and salt from a file with no read/write access to other users.
- Create the security provider.
- Return the security provider.
The all in same scope is important to remove as soon as possible the key and salt from the memory.
- The key is a random string of any length. The salt is a random long. Create strong random values.
- The salt must not change. The key can change, but it will invalidate all tokens.