diff --git a/.gitignore b/.gitignore index d930b7d..8544e28 100644 --- a/.gitignore +++ b/.gitignore @@ -31,11 +31,11 @@ build/ .vscode/ node_modules -config bundle.js bundle.min.js worker.js src/main/javascript/node/ dist out -yarn-error.log \ No newline at end of file +yarn-error.log +gen-password.txt \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0a13c91..ee37c5f 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,11 @@ - + 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE - + org.ros Chatto @@ -15,7 +15,7 @@ A self hosted minimal E2E chat application 11 - + @@ -110,6 +110,43 @@ org.springframework.boot spring-boot-maven-plugin + + org.codehaus.mojo + properties-maven-plugin + 1.0.0 + + + initialize + + read-project-properties + + + + src/main/resources/application.properties + + + + + + + org.flywaydb + flyway-maven-plugin + 5.2.4 + + + ${spring.datasource.driverClassName} + jdbc:mysql://${chatto.datasource.url}:${chatto.datasource.port}/${chatto.datasource.database-name} + ${chatto.datasource.username} + ${chatto.datasource.password} + + com.github.eirslett frontend-maven-plugin diff --git a/src/main/java/db/migration/V3__add_default_admin.java b/src/main/java/db/migration/V3__add_default_admin.java new file mode 100644 index 0000000..3c02cdb --- /dev/null +++ b/src/main/java/db/migration/V3__add_default_admin.java @@ -0,0 +1,61 @@ +package db.migration; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.security.SecureRandom; +import java.sql.PreparedStatement; + +import org.flywaydb.core.api.migration.BaseJavaMigration; +import org.flywaydb.core.api.migration.Context; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +public class V3__add_default_admin extends BaseJavaMigration { + + private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + + private final SecureRandom random = new SecureRandom(); + + /** different dictionaries used */ + private final String ALPHA_CAPS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private final String ALPHA = "abcdefghijklmnopqrstuvwxyz"; + private final String NUMERIC = "0123456789"; + private final String SPECIAL_CHARS = "!@#$%^&*_=+-/"; + + /** + * Method will generate random string based on the parameters + * + * @param len the length of the random string + * @param dic the dictionary used to generate the password + * @return the random password + */ + public String generatePassword(int len, String dic) { + String result = ""; + for (int i = 0; i < len; i++) { + int index = random.nextInt(dic.length()); + result += dic.charAt(index); + } + return result; + } + + @Override + public void migrate(final Context context) throws Exception { + try (final PreparedStatement ps = context.getConnection() + .prepareStatement("insert into users (user_id, name, password) values (0,?,?)")) { + final String generatedPassword = generatePassword(60, ALPHA_CAPS + ALPHA + SPECIAL_CHARS); + final BufferedWriter bw = new BufferedWriter(new FileWriter("gen-password.txt")); + bw.write(generatedPassword); + bw.write("\nPlease delete this file"); + bw.close(); + ps.setString(1, "admin"); + ps.setString(2, passwordEncoder.encode(generatedPassword)); + ps.execute(); + } + + // try (final PreparedStatement ps = context.getConnection() + // .prepareStatement("insert into users_roles (user_id, role_id) values (1,0)")) { + // ps.execute(); + // } + } + +} \ No newline at end of file