/
CreateAccountCommand.java
38 lines (34 loc) · 1.38 KB
/
CreateAccountCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package de.cubeside.globalserver.command;
import de.cubeside.globalserver.GlobalServer;
import de.cubeside.globalserver.ServerCommand;
import de.iani.cubesideutils.commands.ArgsParser;
import java.security.SecureRandom;
public class CreateAccountCommand extends ServerCommand {
public CreateAccountCommand() {
super("createaccount");
}
@Override
public void execute(GlobalServer server, ArgsParser args) {
if (args.remaining() != 1) {
GlobalServer.LOGGER.info("/createaccount <name>");
return;
}
String accountName = args.getNext().toLowerCase().trim();
if (server.getAccount(accountName) != null) {
GlobalServer.LOGGER.info("Account " + accountName + " already exists!");
return;
}
String password = createRandomPassword(32);
server.addAccount(accountName, password);
GlobalServer.LOGGER.info("Account " + accountName + " created with password: " + password);
}
public static String createRandomPassword(int length) {
SecureRandom random = new SecureRandom();
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-+";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(chars.charAt(random.nextInt(chars.length())));
}
return sb.toString();
}
}