[FEATURE][EXPERIMENTAL] Flags in AuthProvider

This commit is contained in:
Gravit 2020-03-31 10:37:06 +07:00
parent 0692fe367f
commit 56e1920a8d
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
7 changed files with 28 additions and 8 deletions

View file

@ -21,6 +21,7 @@ public static class authResult {
String username; String username;
String error; String error;
long permissions; long permissions;
long flags;
} }
public static class authRequest { public static class authRequest {
@ -54,7 +55,7 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p
authResult result = gson.fromJson(content, authResult.class); authResult result = gson.fromJson(content, authResult.class);
if (result.username != null) if (result.username != null)
return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions)); return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions, result.flags));
else if (result.error != null) else if (result.error != null)
return authError(result.error); return authError(result.error);
else else

View file

@ -20,6 +20,7 @@ public final class MySQLAuthProvider extends AuthProvider {
private String query; private String query;
private String message; private String message;
private String[] queryParams; private String[] queryParams;
private boolean flagsEnabled;
@Override @Override
public void init(LaunchServer srv) { public void init(LaunchServer srv) {
@ -41,7 +42,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p
// Execute SQL query // Execute SQL query
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(set.getLong(2))) : authError(message); return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(
set.getLong(2), flagsEnabled ? set.getLong(3) : 0)) : authError(message);
} }
} }

View file

@ -19,6 +19,7 @@ public final class PostgreSQLAuthProvider extends AuthProvider {
private String query; private String query;
private String message; private String message;
private String[] queryParams; private String[] queryParams;
private boolean flagsEnabled;
@Override @Override
public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws SQLException, AuthException { public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws SQLException, AuthException {
@ -32,7 +33,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p
// Execute SQL query // Execute SQL query
s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT); s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(set.getLong(2))) : authError(message); return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(
set.getLong(2), flagsEnabled ? set.getLong(3) : 0)) : authError(message);
} }
} }
} }

View file

@ -22,7 +22,7 @@ public RejectAuthProvider(String message) {
} }
private String message; private String message;
private ArrayList<String> whitelist; private ArrayList<String> whitelist = new ArrayList<>();
@Override @Override
public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws AuthException { public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws AuthException {
@ -46,11 +46,20 @@ public Map<String, Command> getCommands() {
Map<String, Command> commands = new HashMap<>(); Map<String, Command> commands = new HashMap<>();
commands.put("message", new SubCommand() { commands.put("message", new SubCommand() {
@Override @Override
public void invoke(String... args) { public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
message = args[0]; message = args[0];
LogHelper.info("New reject message: %s", message); LogHelper.info("New reject message: %s", message);
} }
}); });
commands.put("whirelist.add", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
whitelist.add(args[0]);
LogHelper.info("%s added to whitelist", args[0]);
}
});
return commands; return commands;
} }
} }

View file

@ -19,6 +19,7 @@ public final class RequestAuthProvider extends AuthProvider {
private String url; private String url;
private transient Pattern pattern; private transient Pattern pattern;
private String response; private String response;
private boolean flagsEnabled;
@Override @Override
public void init(LaunchServer srv) { public void init(LaunchServer srv) {
@ -36,7 +37,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p
// Match username // Match username
Matcher matcher = pattern.matcher(currentResponse); Matcher matcher = pattern.matcher(currentResponse);
return matcher.matches() && matcher.groupCount() >= 1 ? return matcher.matches() && matcher.groupCount() >= 1 ?
new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken(), new ClientPermissions(Long.parseLong(matcher.group("permission")))) : new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken(), new ClientPermissions(
Long.parseLong(matcher.group("permissions")), flagsEnabled ? Long.parseLong(matcher.group("flags")) : 0)) :
authError(currentResponse); authError(currentResponse);
} }

View file

@ -29,6 +29,7 @@ public class UserHibernateImpl implements User {
public String serverID; public String serverID;
private String password_salt; private String password_salt;
public long permissions; public long permissions;
public long flags;
public void setPassword(String password) { public void setPassword(String password) {
password_salt = SecurityHelper.randomStringAESKey(); password_salt = SecurityHelper.randomStringAESKey();
@ -55,11 +56,12 @@ public boolean verifyPassword(String password) {
} }
public ClientPermissions getPermissions() { public ClientPermissions getPermissions() {
return new ClientPermissions(permissions); return new ClientPermissions(permissions, flags);
} }
public void setPermissions(ClientPermissions permissions) { public void setPermissions(ClientPermissions permissions) {
this.permissions = permissions.toLong(); this.permissions = permissions.permissions;
this.flags = permissions.flags;
} }
public String getAccessToken() { public String getAccessToken() {

View file

@ -90,6 +90,7 @@ public void invoke(String... args) throws Exception {
long perm = Long.parseLong(args[1]); long perm = Long.parseLong(args[1]);
boolean value = Boolean.parseBoolean(args[2]); boolean value = Boolean.parseBoolean(args[2]);
permissions.setPermission(perm, value); permissions.setPermission(perm, value);
user.setPermissions(permissions);
userDAO.update(user); userDAO.update(user);
} }
}); });
@ -106,6 +107,7 @@ public void invoke(String... args) throws Exception {
long perm = Long.parseLong(args[1]); long perm = Long.parseLong(args[1]);
boolean value = Boolean.parseBoolean(args[2]); boolean value = Boolean.parseBoolean(args[2]);
permissions.setFlag(perm, value); permissions.setFlag(perm, value);
user.setPermissions(permissions);
userDAO.update(user); userDAO.update(user);
} }
}); });