Skip to content

Commit 6a0ef1b

Browse files
committed
Get the common code
1 parent e950126 commit 6a0ef1b

File tree

10 files changed

+279
-393
lines changed

10 files changed

+279
-393
lines changed

src/main/java/io/github/techstreet/dfscript/DFScript.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,21 @@
55
import io.github.techstreet.dfscript.commands.CommandManager;
66
import io.github.techstreet.dfscript.commands.arguments.StringFuncArgumentType;
77
import io.github.techstreet.dfscript.commands.arguments.serializers.StringFuncArgumentSerializer;
8-
import io.github.techstreet.dfscript.features.AuthHandler;
98
import io.github.techstreet.dfscript.features.UpdateAlerts;
109
import io.github.techstreet.dfscript.loader.Loader;
11-
import io.github.techstreet.dfscript.screen.script.ScriptListScreen;
10+
import io.github.techstreet.dfscript.network.AuthHandler;
1211
import io.github.techstreet.dfscript.script.ScriptManager;
13-
import io.github.techstreet.dfscript.script.util.AuthcodeResponse;
14-
import io.github.techstreet.dfscript.script.util.ServercodeResponse;
15-
import io.github.techstreet.dfscript.script.util.UploadResponse;
1612
import io.github.techstreet.dfscript.util.Scheduler;
1713

18-
import java.io.BufferedReader;
19-
import java.io.ByteArrayOutputStream;
20-
import java.io.InputStreamReader;
21-
import java.io.OutputStream;
22-
import java.net.HttpURLConnection;
23-
import java.net.URL;
24-
import java.nio.file.Files;
2514
import java.util.concurrent.ExecutorService;
2615
import java.util.concurrent.Executors;
27-
import java.util.zip.GZIPOutputStream;
28-
29-
import io.github.techstreet.dfscript.util.chat.ChatType;
30-
import io.github.techstreet.dfscript.util.chat.ChatUtil;
3116
import net.fabricmc.api.ModInitializer;
3217
import net.fabricmc.fabric.api.command.v2.ArgumentTypeRegistry;
3318
import net.fabricmc.loader.api.FabricLoader;
3419
import net.minecraft.client.MinecraftClient;
35-
import net.minecraft.client.util.Session;
36-
import net.minecraft.command.argument.serialize.ConstantArgumentSerializer;
37-
import net.minecraft.command.argument.serialize.StringArgumentSerializer;
3820
import net.minecraft.util.Identifier;
39-
import org.apache.commons.codec.binary.Base64;
40-
import org.apache.commons.lang3.RandomStringUtils;
4121
import org.apache.logging.log4j.LogManager;
4222
import org.apache.logging.log4j.Logger;
43-
import org.apache.logging.log4j.core.jmx.Server;
4423

4524
public class DFScript implements ModInitializer {
4625

@@ -50,6 +29,7 @@ public class DFScript implements ModInitializer {
5029

5130
public static final String MOD_NAME = "DFScript";
5231
public static final String MOD_ID = "dfscript";
32+
public static final String BACKEND = "http://localhost:80";
5333
public static String MOD_VERSION;
5434

5535
public static String PLAYER_UUID = null;

src/main/java/io/github/techstreet/dfscript/features/AuthHandler.java

Lines changed: 0 additions & 154 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.github.techstreet.dfscript.network;
2+
3+
import com.google.gson.JsonObject;
4+
import io.github.techstreet.dfscript.DFScript;
5+
import io.github.techstreet.dfscript.loader.Loadable;
6+
import io.github.techstreet.dfscript.network.request.ServerCodeResponse;
7+
import org.apache.commons.codec.digest.DigestUtils;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStreamReader;
12+
import java.io.OutputStream;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.UUID;
17+
18+
public class AuthHandler implements Loadable {
19+
protected String commonSecret;
20+
protected boolean valid = false;
21+
22+
@Override
23+
public void load() {
24+
25+
try {
26+
regen();
27+
}
28+
catch (IOException e) {
29+
DFScript.LOGGER.error(e);
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
public void regen() throws IOException {
35+
// STEP 1: Create a client code, and get the server code
36+
try {
37+
this.commonSecret = getCommonSecret();
38+
} catch (IOException e) {
39+
DFScript.LOGGER.error(e);
40+
e.printStackTrace();
41+
return;
42+
}
43+
try {
44+
validateCode();
45+
this.valid = true;
46+
} catch (Exception e) {
47+
DFScript.LOGGER.error(e);
48+
e.printStackTrace();
49+
this.valid = false;
50+
}
51+
}
52+
53+
/**
54+
* Sends a code to the server, and generates a hash of this code and one returned by the server.
55+
* This code is to be used as the bearer token, it just has to be verified by connecting to the fake server.
56+
* @return The common code hash
57+
* @throws IOException Any error in the networking
58+
*/
59+
private String getCommonSecret() throws IOException {
60+
URL url = new URL(DFScript.BACKEND + "/user/secret/");
61+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
62+
connection.setRequestMethod("POST");
63+
connection.setRequestProperty("Content-Type", "application/json");
64+
connection.setRequestProperty("Accept", "application/json");
65+
connection.setDoOutput(true);
66+
connection.setReadTimeout(5000);
67+
connection.setConnectTimeout(5000);
68+
69+
String clientCode = UUID.randomUUID().toString();
70+
71+
JsonObject jsonObject = new JsonObject();
72+
jsonObject.addProperty("client",clientCode);
73+
74+
try (OutputStream os = connection.getOutputStream()) {
75+
byte[] input = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
76+
os.write(input, 0, input.length);
77+
}
78+
79+
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
80+
StringBuilder response = new StringBuilder();
81+
String responseLine;
82+
83+
while ((responseLine = br.readLine()) != null) {
84+
response.append(responseLine.trim());
85+
}
86+
ServerCodeResponse servercodeResponse = DFScript.GSON.fromJson(response.toString(), ServerCodeResponse.class);
87+
return DigestUtils.sha256Hex(servercodeResponse.getServerCode() + clientCode);
88+
}
89+
}
90+
91+
private void validateCode() {
92+
// TODO: validate code.
93+
}
94+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.github.techstreet.dfscript.network.request;
2+
3+
public class ServerCodeResponse {
4+
private final String serverCode = null;
5+
6+
public String getServerCode() {
7+
return serverCode;
8+
}
9+
}

0 commit comments

Comments
 (0)