Add git token (code + config)
This commit is contained in:
parent
24dc025507
commit
4df8f2a37b
3 changed files with 47 additions and 43 deletions
|
@ -7,7 +7,7 @@ import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration{
|
||||||
|
|
||||||
private String db_host;
|
private String db_host;
|
||||||
private int db_port;
|
private int db_port;
|
||||||
|
@ -29,33 +29,33 @@ public class Configuration {
|
||||||
|
|
||||||
private int groupJoinMinutes;
|
private int groupJoinMinutes;
|
||||||
private String groupQuitMinutes;
|
private String groupQuitMinutes;
|
||||||
|
|
||||||
|
private String git_token;
|
||||||
|
|
||||||
private File file;
|
private File _file;
|
||||||
|
|
||||||
public Configuration(String path) {
|
public Configuration(String path){
|
||||||
this.file = new File(path);
|
this._file = new File(path);
|
||||||
System.out.println("Config on " + file.getAbsolutePath());
|
System.out.println("Config on " + _file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public void load() throws Exception{
|
||||||
if (!this.file.exists())
|
if(!this._file.exists()) return;
|
||||||
return;
|
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(this.file));
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while((line = reader.readLine()) != null){
|
||||||
String[] split = line.split("=");
|
String[] split = line.split("=");
|
||||||
Field field = getClass().getDeclaredField(split[0]);
|
Field field = getClass().getDeclaredField(split[0]);
|
||||||
if (field == null)
|
if(field == null) continue;
|
||||||
continue;
|
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
injectValue(field, split[1]);
|
injectValue(field, split[1]);
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
private void injectValue(Field field, String value) throws IllegalAccessException{
|
||||||
if (field.getType().isPrimitive()) {
|
if(field.getType().isPrimitive()){
|
||||||
switch (field.getType().getName()) {
|
switch(field.getType().getName()){
|
||||||
case "boolean":
|
case "boolean":
|
||||||
field.setBoolean(this, Boolean.parseBoolean(value));
|
field.setBoolean(this, Boolean.parseBoolean(value));
|
||||||
break;
|
break;
|
||||||
|
@ -85,26 +85,24 @@ public class Configuration {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (field.getType().equals(String.class)) {
|
if(field.getType().equals(String.class)){
|
||||||
field.set(this, value);
|
field.set(this, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException(value);
|
throw new IllegalArgumentException(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() throws Exception {
|
public void save() throws Exception{
|
||||||
if (!file.exists()) {
|
if(!_file.exists()){
|
||||||
File parent = file.getParentFile();
|
File parent = _file.getParentFile();
|
||||||
if (!parent.exists())
|
if(!parent.exists()) parent.mkdirs();
|
||||||
parent.mkdirs();
|
_file.createNewFile();
|
||||||
file.createNewFile();
|
|
||||||
}
|
}
|
||||||
Field[] fields = getClass().getDeclaredFields();
|
Field[] fields = getClass().getDeclaredFields();
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
||||||
for (Field field : fields) {
|
for(Field field : fields){
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
if (field.getName().startsWith("_"))
|
if(field.getName().startsWith("_")) continue;
|
||||||
continue;
|
|
||||||
Object value = field.get(this);
|
Object value = field.get(this);
|
||||||
writer.write(field.getName() + "=" + value);
|
writer.write(field.getName() + "=" + value);
|
||||||
}
|
}
|
||||||
|
@ -112,53 +110,52 @@ public class Configuration {
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbHost() {
|
public String getDbHost(){
|
||||||
return this.db_host;
|
return this.db_host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDbPort() {
|
public int getDbPort(){
|
||||||
return this.db_port;
|
return this.db_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbUser() {
|
public String getDbUser(){
|
||||||
return this.db_user;
|
return this.db_user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbDatabase() {
|
public String getDbDatabase(){
|
||||||
return this.db_database;
|
return this.db_database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbPassword() {
|
public String getDbPassword(){
|
||||||
return this.db_password;
|
return this.db_password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystore() {
|
public String getSslKeystore(){
|
||||||
return this.ssl_keystore;
|
return this.ssl_keystore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTokenIssuer() {
|
public String getTokenIssuer(){
|
||||||
return this.token_issuer;
|
return this.token_issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTokenExpiration() {
|
public int getTokenExpiration(){
|
||||||
return this.token_expiration;
|
return this.token_expiration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystorePasswd() {
|
public String getSslKeystorePasswd(){
|
||||||
return this.ssl_keystorePasswd;
|
return this.ssl_keystorePasswd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTcpPort() {
|
public int getTcpPort(){
|
||||||
return this.tcp_port;
|
return this.tcp_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useSsl() {
|
public boolean useSsl(){
|
||||||
return this.use_ssl;
|
return this.use_ssl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsersFiles() {
|
public String getUsersFiles(){
|
||||||
if (users_files == null || users_files.trim().isEmpty())
|
if(users_files == null || users_files.trim().isEmpty()) users_files = "/tmp/users_files";
|
||||||
users_files = "/tmp/users_files";
|
|
||||||
return users_files;
|
return users_files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,4 +170,8 @@ public class Configuration {
|
||||||
public String getGroupQuitMinutes(){
|
public String getGroupQuitMinutes(){
|
||||||
return this.groupQuitMinutes;
|
return this.groupQuitMinutes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getGitToken(){
|
||||||
|
return this.git_token;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -76,7 +76,7 @@ public class Main{
|
||||||
|
|
||||||
private static void initRoutes(Router router, Configuration config){
|
private static void initRoutes(Router router, Configuration config){
|
||||||
DatabaseRepository repo = router.getDataBase();
|
DatabaseRepository repo = router.getDataBase();
|
||||||
router.register(new Register(repo, router, config.getUsersFiles()));
|
router.register(new Register(repo, router, config.getUsersFiles(), config.getGitToken()));
|
||||||
router.register(new Login(repo, router));
|
router.register(new Login(repo, router));
|
||||||
router.register(new ProfileSettings(repo));
|
router.register(new ProfileSettings(repo));
|
||||||
router.register(new ChangePassword(repo));
|
router.register(new ChangePassword(repo));
|
||||||
|
|
|
@ -36,11 +36,13 @@ public class Register extends FormResponse {
|
||||||
private String usersFilesPath;
|
private String usersFilesPath;
|
||||||
private KeyPairGenerator generator;
|
private KeyPairGenerator generator;
|
||||||
private Encoder encoder;
|
private Encoder encoder;
|
||||||
|
private String gitToken;
|
||||||
|
|
||||||
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath) {
|
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath, String gitToken){
|
||||||
this.databaseRepo = databaseRepo;
|
this.databaseRepo = databaseRepo;
|
||||||
this.router = router;
|
this.router = router;
|
||||||
usersFilesPath = initUsersFilesPath;
|
this.usersFilesPath = initUsersFilesPath;
|
||||||
|
this.gitToken = gitToken;
|
||||||
try {
|
try {
|
||||||
generator = KeyPairGenerator.getInstance("RSA");
|
generator = KeyPairGenerator.getInstance("RSA");
|
||||||
generator.initialize(2048); //a changer ?
|
generator.initialize(2048); //a changer ?
|
||||||
|
@ -120,6 +122,7 @@ public class Register extends FormResponse {
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setRequestProperty("Content-Type","application/json");
|
connection.setRequestProperty("Content-Type","application/json");
|
||||||
|
connection.setRequestProperty("Authorization","Bearer "+this.gitToken);
|
||||||
connection.setDoInput(true);
|
connection.setDoInput(true);
|
||||||
connection.setDoOutput(true);
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue