Move gist username password to config file

This commit is contained in:
2020-10-18 19:04:44 +02:00
parent 91e5ed50b7
commit 16d5baabef
2 changed files with 29 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package de.jeyp91.gists; package de.jeyp91.gists;
import com.google.api.client.util.DateTime; import com.google.api.client.util.DateTime;
import com.google.common.io.Resources;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHeaders; import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@@ -18,16 +19,37 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Set; import java.util.Set;
public class GistProvider { public class GistProvider {
private static GistProvider prov = null; private static GistProvider prov = null;
private JSONArray gistList = null; private JSONArray gistList;
private String username;
private String password;
private GistProvider() { private GistProvider() {
this.gistList = listAllGists(); this.gistList = listAllGists();
readConfig();
}
private void readConfig() {
JSONParser jsonParser = new JSONParser();
URL url = Resources.getResource("Gist_Config.json");
JSONObject gistConfig;
try {
String jsonConfig = Resources.toString(url, StandardCharsets.UTF_8);
//Read JSON file
gistConfig = (JSONObject) jsonParser.parse(jsonConfig);
this.username = gistConfig.get("username").toString();
this.password = gistConfig.get("password").toString();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
} }
public static GistProvider getInstance() { public static GistProvider getInstance() {
@@ -68,9 +90,7 @@ public class GistProvider {
public String updateGist(String id, String file, String content) throws UnsupportedEncodingException { public String updateGist(String id, String file, String content) throws UnsupportedEncodingException {
String requestUrl = "https://api.github.com/gists/" + id; String requestUrl = "https://api.github.com/gists/" + id;
String username = "tippligawuerzburg"; String auth = this.username + ":" + this.password;
String password = "1154f448eae29212971cb82ca8850f54bff23629";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64( byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1)); auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth); String authHeader = "Basic " + new String(encodedAuth);
@@ -157,9 +177,7 @@ public class GistProvider {
private String getRawData(String requestUrl) { private String getRawData(String requestUrl) {
String username = "tippligawuerzburg"; String auth = this.username + ":" + this.password;
String password = "1154f448eae29212971cb82ca8850f54bff23629";
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64( byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(StandardCharsets.ISO_8859_1)); auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth); String authHeader = "Basic " + new String(encodedAuth);

View File

@@ -0,0 +1,4 @@
{
"username": "tippligawuerzburg",
"password": "1154f448eae29212971cb82ca8850f54bff23629"
}