From d1b225d38288efe496d78460719f1f2ae013f565 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sun, 13 Sep 2020 01:27:45 +0900 Subject: [PATCH] Add backend authentication to App. Change all references of sigonasr2 to the username of the App. --- DivaBot/src/sig/DrawCanvas.java | 10 ++-- DivaBot/src/sig/MyRobot.java | 87 +++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/DivaBot/src/sig/DrawCanvas.java b/DivaBot/src/sig/DrawCanvas.java index aaba43f..4900bc7 100644 --- a/DivaBot/src/sig/DrawCanvas.java +++ b/DivaBot/src/sig/DrawCanvas.java @@ -134,20 +134,20 @@ public class DrawCanvas extends JPanel implements KeyListener,ComponentListener, romanizedname = currentSong.romanized_name; englishname = currentSong.english_name; artist = currentSong.artist; - JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); + JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/"+MyRobot.USERNAME+"/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); if (obj.has("cool")) { bestPlay = new Result(MyRobot.p.songname,difficulty,obj.getInt("cool"),obj.getInt("fine"),obj.getInt("safe"),obj.getInt("sad"),obj.getInt("worst"),(float)obj.getDouble("percent")); lastScore = obj.getDouble("score"); } else { bestPlay = null; } - obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/playcount/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); + obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/playcount/"+MyRobot.USERNAME+"/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); plays = obj.getInt("playcount"); - obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/songpasscount/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); + obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/songpasscount/"+MyRobot.USERNAME+"/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); passes = obj.getInt("passcount"); - obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/songfccount/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); + obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/songfccount/"+MyRobot.USERNAME+"/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); fcCount = obj.getInt("fccount"); - /*obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/sigonasr2"); + /*obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/"+MyRobot.USERNAME); lastRating = overallrating; overallrating = (int)obj.getDouble("rating"); if (lastRatingnot share it with others!!!", "XXXXX-XXXXX-XXXXX"); + if (enteredValue==null) { + System.exit(0); + } + if (!enteredValue.isEmpty()) { + do { + username = JOptionPane.showInputDialog("Please enter your Project DivaR username:",""); + } while (username.isEmpty()); + if (username==null) { + System.exit(0); + } + success = SendAuthenticationRequest(enteredValue, username); + } + } while (!success); + } else { + String[] data = FileUtils.readFromFile("authToken.txt"); + String username = data[0]; + String authenticationToken = data[1]; + boolean success = SendAuthenticationRequest(data[1], data[0]); + if (!success) { + authentication_token.delete(); + AuthenticateUser(); + } + } + } + + private static boolean SendAuthenticationRequest(String authenticationToken, String username) { + HttpClient httpclient = HttpClients.createDefault(); + HttpPost httppost = new HttpPost("http://projectdivar.com/authenticateuser"); + List params = new ArrayList(); + params.add(new BasicNameValuePair("username", username)); + params.add(new BasicNameValuePair("authenticationToken", authenticationToken)); + try { + httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + HttpResponse response = null; + try { + response = httpclient.execute(httppost); + } catch (IOException e) { + e.printStackTrace(); + } + HttpEntity entity = response.getEntity(); + if (entity != null) { + try (InputStream instream = entity.getContent()) { + Scanner s = new Scanner(instream).useDelimiter("\\A"); + String result = s.hasNext() ? s.next() : ""; + System.out.println(result); + if (result.equalsIgnoreCase("\"authentication success!\"")) { + FileUtils.writetoFile(new String[] {username,authenticationToken,"The App Authentication token is used to record your scores and verify who you are! Do not share it with others!!!"}, "authToken.txt", false); + USERNAME=username; + AUTHTOKEN=authenticationToken; + return true; + } else { + JOptionPane.showMessageDialog(null,"Authentication Failed!\n\nPlease check your credentials and try again!"); + } + instream.close(); + } catch (UnsupportedOperationException | IOException e) { + e.printStackTrace(); + } + } + return false; + } + boolean textFailPixel(BufferedImage img) { Color failPixel = new Color(img.getRGB(0, 0)); //System.out.println(failPixel); @@ -190,7 +267,7 @@ public class MyRobot{ void BotMain() { try { - JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/sigonasr2"); + JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/"+USERNAME); p.lastRating = p.overallrating; p.overallrating = (int)obj.getDouble("rating"); if (p.lastRating params = new ArrayList(); params.add(new BasicNameValuePair("song", r.songName)); - params.add(new BasicNameValuePair("username", "sigonasr2")); - params.add(new BasicNameValuePair("authentication_token", "sig")); + params.add(new BasicNameValuePair("username", USERNAME)); + params.add(new BasicNameValuePair("authentication_token", AUTHTOKEN)); params.add(new BasicNameValuePair("difficulty", r.difficulty)); params.add(new BasicNameValuePair("cool", Integer.toString(r.cool))); params.add(new BasicNameValuePair("fine", Integer.toString(r.fine))); @@ -362,8 +439,8 @@ public class MyRobot{ results.clear(); try { - JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/sigonasr2"); - JSONObject obj2 = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/sigonasr2/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); + JSONObject obj = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/rating/"+USERNAME); + JSONObject obj2 = FileUtils.readJsonFromUrl("http://45.33.13.215:4501/bestplay/"+USERNAME+"/"+URLEncoder.encode(MyRobot.p.songname, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")+"/"+difficulty); p.lastRating = p.overallrating; if (obj2.has("score")) { double newScore = obj2.getDouble("score");