|
| 1 | +package io.github.techstreet.dfscript.features; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import io.github.techstreet.dfscript.DFScript; |
| 6 | +import io.github.techstreet.dfscript.loader.Loadable; |
| 7 | +import io.github.techstreet.dfscript.script.util.AuthcodeResponse; |
| 8 | +import io.github.techstreet.dfscript.script.util.ServercodeResponse; |
| 9 | +import io.github.techstreet.dfscript.util.chat.ChatType; |
| 10 | +import io.github.techstreet.dfscript.util.chat.ChatUtil; |
| 11 | +import net.minecraft.client.util.Session; |
| 12 | +import org.apache.commons.lang3.RandomStringUtils; |
| 13 | + |
| 14 | +import java.io.BufferedReader; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.io.OutputStream; |
| 17 | +import java.net.HttpURLConnection; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +public class AuthHandler implements Loadable { |
| 22 | + private static String authCode = null; |
| 23 | + |
| 24 | + @Override |
| 25 | + public void load() { |
| 26 | + URL url; |
| 27 | + HttpURLConnection con; |
| 28 | + String commonSecret; |
| 29 | + JsonObject obj; |
| 30 | + |
| 31 | + try { |
| 32 | + // Authorization step one - Create a random clientcode |
| 33 | + url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/secret/"); |
| 34 | + con = (HttpURLConnection)url.openConnection(); |
| 35 | + con.setRequestMethod("POST"); |
| 36 | + con.setRequestProperty("Content-Type", "application/json"); |
| 37 | + con.setRequestProperty("Accept", "application/json"); |
| 38 | + con.setDoOutput(true); |
| 39 | + |
| 40 | + String clientCode = UUID.randomUUID().toString(); |
| 41 | + |
| 42 | + obj = new JsonObject(); |
| 43 | + obj.addProperty("uuid", DFScript.PLAYER_UUID); |
| 44 | + obj.addProperty("clientcode", clientCode); |
| 45 | + |
| 46 | + System.out.println(obj); |
| 47 | + |
| 48 | + try (OutputStream os = con.getOutputStream()) { |
| 49 | + byte[] input = obj.toString().getBytes("utf-8"); |
| 50 | + os.write(input, 0, input.length); |
| 51 | + } |
| 52 | + |
| 53 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) { |
| 54 | + StringBuilder response = new StringBuilder(); |
| 55 | + String responseLine; |
| 56 | + |
| 57 | + while ((responseLine = br.readLine()) != null) { |
| 58 | + response.append(responseLine.trim()); |
| 59 | + } |
| 60 | + |
| 61 | + ServercodeResponse servercodeResponse = DFScript.GSON.fromJson(response.toString(), ServercodeResponse.class); |
| 62 | + commonSecret = servercodeResponse.getServercode(); |
| 63 | + } |
| 64 | + |
| 65 | + // Authorization step two - Generate the authcode |
| 66 | + url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/secret/"); |
| 67 | + con = (HttpURLConnection)url.openConnection(); |
| 68 | + con.setRequestMethod("POST"); |
| 69 | + con.setRequestProperty("Content-Type", "application/json"); |
| 70 | + con.setRequestProperty("Accept", "application/json"); |
| 71 | + con.setDoOutput(true); |
| 72 | + |
| 73 | + obj = new JsonObject(); |
| 74 | + obj.addProperty("secret", commonSecret); |
| 75 | + obj.addProperty("uuid", DFScript.PLAYER_UUID); |
| 76 | + |
| 77 | + try (OutputStream os = con.getOutputStream()) { |
| 78 | + byte[] input = obj.toString().getBytes("utf-8"); |
| 79 | + os.write(input, 0, input.length); |
| 80 | + } |
| 81 | + |
| 82 | + try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) { |
| 83 | + StringBuilder response = new StringBuilder(); |
| 84 | + String responseLine; |
| 85 | + |
| 86 | + while ((responseLine = br.readLine()) != null) { |
| 87 | + response.append(responseLine.trim()); |
| 88 | + } |
| 89 | + |
| 90 | + AuthcodeResponse authcodeResponse = DFScript.GSON.fromJson(response.toString(), AuthcodeResponse.class); |
| 91 | + authCode = authcodeResponse.getAuthcode(); |
| 92 | + DFScript.LOGGER.info("Authorization code successfully generated: " + authCode); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static String getAuthCode() { |
| 100 | + return authCode; |
| 101 | + } |
| 102 | +} |
0 commit comments