|
2 | 2 |
|
3 | 3 | import com.sun.net.httpserver.HttpExchange; |
4 | 4 | import com.sun.net.httpserver.HttpHandler; |
| 5 | +import org.json.JSONException; |
| 6 | +import org.json.JSONObject; |
5 | 7 | import simplexity.config.SimplexityFileHandler; |
6 | 8 | import simplexity.messages.Errors; |
7 | 9 |
|
| 10 | +import java.io.BufferedReader; |
8 | 11 | import java.io.IOException; |
| 12 | +import java.io.InputStreamReader; |
9 | 13 | import java.io.OutputStream; |
| 14 | +import java.net.HttpURLConnection; |
| 15 | +import java.net.URL; |
10 | 16 |
|
11 | 17 | public class AuthHandler implements HttpHandler { |
12 | 18 | @Override |
13 | 19 | public void handle(HttpExchange exchange) { |
14 | 20 | try { |
15 | | - String twitchCode = exchange.getRequestURI().getQuery().split("=")[1]; |
16 | | - twitchCode = twitchCode.split("&")[0]; |
17 | | - System.out.println(twitchCode); |
18 | | - SimplexityFileHandler.createTwitchFile(twitchCode); |
19 | | - String response = "<h1>HI</h1>"; |
20 | | - exchange.sendResponseHeaders(200, response.length()); |
21 | | - OutputStream os = exchange.getResponseBody(); |
22 | | - os.write(response.getBytes()); |
23 | | - os.close(); |
24 | | - } catch (IOException e) { |
| 21 | + String query = exchange.getRequestURI().getQuery(); |
| 22 | + String[] params = query.split("&"); |
| 23 | + String twitchCode = ""; |
| 24 | + for (String param : params) { |
| 25 | + if (param.startsWith("code=")) { |
| 26 | + twitchCode = param.split("=")[1]; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (!twitchCode.isEmpty()) { |
| 31 | + // Exchange the authorization code for an access token |
| 32 | + String clientId = ""; |
| 33 | + String clientSecret = ""; |
| 34 | + String redirectUri = "http://localhost:3000"; |
| 35 | + String tokenUrl = "https://id.twitch.tv/oauth2/token"; |
| 36 | + |
| 37 | + URL url = new URL(tokenUrl); |
| 38 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 39 | + connection.setRequestMethod("POST"); |
| 40 | + connection.setDoOutput(true); |
| 41 | + String postData = String.format( |
| 42 | + "client_id=%s&client_secret=%s&code=%s&grant_type=authorization_code&redirect_uri=%s", |
| 43 | + clientId, clientSecret, twitchCode, redirectUri |
| 44 | + ); |
| 45 | + |
| 46 | + try (OutputStream os = connection.getOutputStream()) { |
| 47 | + os.write(postData.getBytes()); |
| 48 | + os.flush(); |
| 49 | + } |
| 50 | + |
| 51 | + int responseCode = connection.getResponseCode(); |
| 52 | + if (responseCode == HttpURLConnection.HTTP_OK) { |
| 53 | + try (BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { |
| 54 | + String responseLine; |
| 55 | + StringBuilder responseBuilder = new StringBuilder(); |
| 56 | + while ((responseLine = responseReader.readLine()) != null) { |
| 57 | + responseBuilder.append(responseLine); |
| 58 | + } |
| 59 | + |
| 60 | + String response = responseBuilder.toString(); |
| 61 | + JSONObject json = new JSONObject(response); |
| 62 | + String accessToken = json.getString("access_token"); |
| 63 | + |
| 64 | + // Save the access token for later use |
| 65 | + SimplexityFileHandler.createTwitchFile(accessToken); |
| 66 | + |
| 67 | + String successResponse = "<h1>Authorization successful! You can close this window.</h1>"; |
| 68 | + exchange.sendResponseHeaders(200, successResponse.length()); |
| 69 | + OutputStream os = exchange.getResponseBody(); |
| 70 | + os.write(successResponse.getBytes()); |
| 71 | + os.close(); |
| 72 | + } |
| 73 | + } else { |
| 74 | + String errorResponse = "<h1>Authorization failed!</h1>"; |
| 75 | + exchange.sendResponseHeaders(500, errorResponse.length()); |
| 76 | + OutputStream os = exchange.getResponseBody(); |
| 77 | + os.write(errorResponse.getBytes()); |
| 78 | + os.close(); |
| 79 | + } |
| 80 | + } else { |
| 81 | + String errorResponse = "<h1>No authorization code found!</h1>"; |
| 82 | + exchange.sendResponseHeaders(400, errorResponse.length()); |
| 83 | + OutputStream os = exchange.getResponseBody(); |
| 84 | + os.write(errorResponse.getBytes()); |
| 85 | + os.close(); |
| 86 | + } |
| 87 | + } catch (IOException | JSONException e) { |
25 | 88 | System.out.println(Errors.CAUGHT_EXCEPTION.replace("%error%", e.getMessage())); |
26 | 89 | } |
27 | 90 | } |
|
0 commit comments