|
| 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 | +} |
0 commit comments