1010
1111import com .sun .net .httpserver .*;
1212import material .DestinyAPI ;
13+ import utils .StringUtils ;
1314
1415import javax .net .ssl .KeyManagerFactory ;
1516import javax .net .ssl .SSLContext ;
2728
2829public class OAuthFlow {
2930
31+ // keytool -genkey -dname "cn=dec 4234, ou=github/JavaDestinyAPI, o=ou=github/JavaDestinyAPI, c=US" -keyalg RSA -alias alias -keystore keystore.jks -storepass mypassword -keypass mypassword -validity 360 -keysize 2048
32+
3033 private String queryParameters = "empty" ;
3134 private volatile boolean hasQueryBeenReturned = false ;
3235
36+ /**
37+ * Initiate the OAuthFlow class which goes through the following steps
38+ *
39+ * 1. Opens the OAuth page on the user's default browser
40+ * 2. Creates and HTTPS localhost server to receive that information
41+ * 3. Extracts the oauth code from the query parameters
42+ * 4.
43+ * @param port
44+ */
3345 public void initOAuthFlow (int port ) {
3446 setTokens (port );
3547 }
3648
3749 public void initOAuthFlowIfNeeded (int port ) {
3850 if (!DestinyAPI .hasOauthManager () || DestinyAPI .getAccessToken () == null ) {
39- initOAuthFlow (8080 );
51+ initOAuthFlow (port );
4052 }
4153 }
4254
43- public void setTokens (int serverPort ) {
55+ private void setTokens (int serverPort ) {
4456 openOAuthPage ();
4557
4658 startSecureServer (serverPort );
4759
4860 String rawCode = getRawCode (queryParameters );
4961
50- System .out .println (rawCode );
51-
5262 DestinyAPI .getHttpUtils ().setTokenViaAuth (rawCode );
5363 }
5464
55- public String getRawCode (String queryInput ) {
65+ private String getRawCode (String queryInput ) {
5666 String codeString = queryInput .split ("&" )[0 ];
5767
5868 return codeString .split ("=" )[1 ];
5969 }
6070
61- public void openOAuthPage () {
71+ private void openOAuthPage () {
6272 if (Desktop .isDesktopSupported () && Desktop .getDesktop ().isSupported (Desktop .Action .BROWSE )) {
6373 try {
6474 Desktop .getDesktop ().browse (new URI ("https://www.bungie.net/en/OAuth/Authorize?client_id=" + DestinyAPI .getClientId () + "&response_type=code" ));
@@ -68,17 +78,29 @@ public void openOAuthPage() {
6878 }
6979 }
7080
71- public void startSecureServer (int port ) {
81+ private void startSecureServer (int port ) {
7282 HttpsServer server = null ;
7383 final String [] queryParams = {"" };
84+ String filePath = new File (Paths .get ("" ).toAbsolutePath ().toString ()).getPath () + "\\ keystore.jks" ;
85+ File file = new File (filePath );
7486
7587 SSLContext sslContext = null ;
7688 try {
7789 server = HttpsServer .create (new InetSocketAddress (port ), 0 );
7890 sslContext = SSLContext .getInstance ("TLS" );
7991 char [] password = "mypassword" .toCharArray ();
8092 KeyStore ks = KeyStore .getInstance ("JKS" );
81- FileInputStream fis = new FileInputStream (new File (Paths .get ("" ).toAbsolutePath ().toString ()).getPath () + "\\ keystore.jks" );
93+
94+ file .delete ();
95+
96+ // Generate a new key store
97+ // StringUtils.executeCommandLine("keytool -delete -alias alias -keystore keystore.jks");
98+ StringUtils .executeCommandLine ("keytool -genkey -dname \" cn=dec 4234, ou=github/JavaDestinyAPI, o=ou=github/JavaDestinyAPI, c=US\" -ext san=dns:www.dev.dec4234.net -keyalg RSA -alias alias -keystore keystore.jks -storepass mypassword -keypass mypassword -validity 360 -keysize 2048" );
99+
100+ // Sleep to allow for the keystore to be generated
101+ Thread .sleep (1000 );
102+
103+ FileInputStream fis = new FileInputStream (filePath );
82104 ks .load (fis , password );
83105
84106 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
@@ -131,76 +153,13 @@ public void configure(HttpsParameters httpsParameters) {
131153 }
132154
133155 server .stop (0 );
156+ file .delete (); // Delete keystore
134157 }
135158
136- public String getQueryParameters (HttpsExchange exchange ) {
159+ private String getQueryParameters (HttpsExchange exchange ) {
137160 queryParameters = exchange .getRequestURI ().getQuery ();
138161 hasQueryBeenReturned = true ;
139162
140163 return queryParameters ;
141164 }
142-
143- @ Deprecated
144- public String startHttpServer (int port ) {
145- String response = null ;
146-
147- try (ServerSocket serverSocket = new ServerSocket (8080 )) {
148- boolean hasConnected = false ;
149-
150- while (true ) {
151- Socket socket = serverSocket .accept ();
152-
153- try {
154- try (InputStream inputStream = socket .getInputStream ()) { // ARM
155- if (!hasConnected ) {
156- readInputHeaders (inputStream ).forEach (s -> {
157- System .out .println (s );
158- });
159-
160- // String requestHeader = getHeaderToArray(raw).split("\n")[0].replace("GET ", "").replace(" HTTP/1.1", "");
161-
162- // System.out.println(requestHeader);
163-
164- String httpResponse = "HTTP/1.1 200 OK\r \n \r \n You can now close this window." ;
165- socket .getOutputStream ().write (httpResponse .getBytes (StandardCharsets .UTF_8 ));
166-
167- hasConnected = true ;
168- }
169- }
170- } catch (MalformedURLException ex ) {
171- System .err .println (socket .getLocalAddress () + " is not a parseable URL" );
172- } catch (IOException ex ) {
173- System .err .println (ex .getMessage ());
174- }
175- }
176-
177- } catch (Exception ex ) {
178- System .out .println (ex .getMessage ());
179- }
180-
181- return response ;
182- }
183-
184- private List <String > readInputHeaders (InputStream inputStream ) {
185- BufferedReader br = new BufferedReader (new InputStreamReader (inputStream ));
186-
187- List <String > headers = new ArrayList <>();
188-
189- while (true ) {
190- String s = null ;
191- try {
192- s = br .readLine ();
193- } catch (IOException e ) {
194- e .printStackTrace ();
195- }
196-
197- if (s == null || s .trim ().length () == 0 ) {
198- break ;
199- }
200-
201- headers .add (s );
202- }
203-
204- return headers ;
205- }
206165}
0 commit comments