1 package com.neuronrobotics.bowlerstudio.scripting;
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
7 import java.net.URISyntaxException;
8 import java.util.ArrayList;
10 import java.util.function.Supplier;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.swing.JFrame;
17 import javax.swing.JOptionPane;
19 import org.apache.http.HttpEntity;
20 import org.apache.http.HttpResponse;
21 import org.apache.http.NameValuePair;
22 import org.apache.http.client.ClientProtocolException;
23 import org.apache.http.client.HttpClient;
24 import org.apache.http.client.entity.UrlEncodedFormEntity;
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.impl.client.DefaultHttpClient;
27 import org.apache.http.message.BasicNameValuePair;
28 import org.apache.http.util.EntityUtils;
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.servlet.ServletContextHandler;
31 import org.eclipse.jetty.servlet.ServletHolder;
47 String[] returnData =
null;
48 private static Supplier<String>
myAPI = () -> {
49 return "1edf79fae494c232d4d2";
51 private static Supplier<String>
myname =() -> {
52 JFrame jframe =
new JFrame();
53 jframe.setAlwaysOnTop(
true);
54 String answer = JOptionPane.showInputDialog(jframe,
"Enter API secret");
61 @SuppressWarnings(
"serial")
63 public String[]
prompt(String loginID) {
64 JFrame jframe =
new JFrame();
65 jframe.setAlwaysOnTop(
true);
66 loginID = JOptionPane.showInputDialog(jframe,
"Github User Name ",loginID==
null?
"":loginID);
75 ServletContextHandler context =
new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
76 context.addServlet(
new ServletHolder(
new HttpServlet() {
80 private static final long serialVersionUID = 8089806363114431858L;
83 protected void doGet(HttpServletRequest request, HttpServletResponse response)
84 throws ServletException, IOException {
86 final String code = request.getParameter(
"code");
88 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
92 }
catch (Exception ex) {
93 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
96 response.setContentType(
"text/html;charset=UTF-8");
98 response.getWriter().println(
"");
99 response.getWriter().close();
104 server.setHandler(context);
105 server.setStopAtShutdown(
true);
109 }
catch (Exception e) {
110 throw new RuntimeException(e);
114 long start = System.currentTimeMillis();
116 while (System.currentTimeMillis() - start < 200 * 1000 && returnData ==
null) {
119 }
catch (InterruptedException e) {
126 }
catch (Exception e) {
132 }
catch (Exception e) {
139 String doRequest =
"https://github.com/login/oauth/authorize?" +
140 "client_id=" +
getMyAPI().get() +
"&"
141 +
"redirect_uri=http%3A%2F%2Flocalhost%3A"+
WEBSERVER_PORT+
"%2Fsuccess" +
"&" +
142 "response_type=code" +
"&" +
143 "login="+
id.replaceAll(
"@",
"%40") +
"&" +
144 "allow_signup=true" +
"&" +
148 for (
int i = 0; i < listOfScopes.size(); i++) {
149 String scope = listOfScopes.get(i);
150 scope = scope.replaceAll(
":",
"%3A");
152 if(i!=listOfScopes.size()-1)
155 doRequest = doRequest.trim();
162 }
catch (URISyntaxException e) {
168 private void runStep2(String
id,
final String code) {
174 HttpClient httpClient =
new DefaultHttpClient();
175 HttpPost httpPost =
new HttpPost(
"https://github.com/login/oauth/access_token");
177 List<NameValuePair> params =
new ArrayList<NameValuePair>();
178 params.add(
new BasicNameValuePair(
"client_id",
getMyAPI().
get()));
179 params.add(
new BasicNameValuePair(
"client_secret",
getName().
get()));
180 params.add(
new BasicNameValuePair(
"code",code));
182 httpPost.setEntity(
new UrlEncodedFormEntity(params,
"UTF-8"));
183 }
catch (UnsupportedEncodingException e) {
191 HttpResponse response2 = httpClient.execute(httpPost);
192 HttpEntity respEntity = response2.getEntity();
194 if (respEntity !=
null) {
196 String[] content = EntityUtils.toString(respEntity).split(
"&");
197 if(content!=
null && content.length>0) {
198 String [] keys = content[0].split(
"=");
199 if(keys!=
null && keys.length>1) {
200 String
string = keys[1];
202 returnData=
new String[] {id,
string};
207 }
catch (ClientProtocolException e) {
210 }
catch (IOException e) {
233 public static void setName(Supplier<String> mykey) {
void doStepOne(String id)
String[] prompt(String loginID)
static int WEBSERVER_PORT
String twoFactorAuthCodePrompt()
static void setMyAPI(Supplier< String > myAPI)
static IURLOpen getOpen()
static Supplier< String > myAPI
static Supplier< String > myname
static void setOpen(IURLOpen open)
static Supplier< String > getName()
static void setName(Supplier< String > mykey)
void runStep2(String id, final String code)
static Supplier< String > getMyAPI()
static List< String > getListOfScopes()
default void open(URI toOpe)