Implementation part of #3

This commit is contained in:
Rohan Sircar 2020-06-02 19:02:08 +05:30
parent 36ba88a546
commit 1789b29ee0
3 changed files with 103 additions and 5 deletions

2
.gitignore vendored
View File

@ -31,7 +31,6 @@ build/
.vscode/ .vscode/
node_modules node_modules
config
bundle.js bundle.js
bundle.min.js bundle.min.js
worker.js worker.js
@ -39,3 +38,4 @@ src/main/javascript/node/
dist dist
out out
yarn-error.log yarn-error.log
gen-password.txt

43
pom.xml
View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version> <version>2.1.8.RELEASE</version>
<relativePath/> <relativePath />
</parent> </parent>
<groupId>org.ros</groupId> <groupId>org.ros</groupId>
<artifactId>Chatto</artifactId> <artifactId>Chatto</artifactId>
@ -15,7 +15,7 @@
<description>A self hosted minimal E2E chat application</description> <description>A self hosted minimal E2E chat application</description>
<properties> <properties>
<java.version>11</java.version> <java.version>11</java.version>
<gruntArg> </gruntArg> <gruntArg></gruntArg>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@ -110,6 +110,43 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/application.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.2.4</version>
<!-- <executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions> -->
<configuration>
<driver>${spring.datasource.driverClassName}</driver>
<url>jdbc:mysql://${chatto.datasource.url}:${chatto.datasource.port}/${chatto.datasource.database-name}</url>
<user>${chatto.datasource.username}</user>
<password>${chatto.datasource.password}</password>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>com.github.eirslett</groupId> <groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId> <artifactId>frontend-maven-plugin</artifactId>

View File

@ -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();
// }
}
}