integrating with REO auth
This commit is contained in:
parent
6b4cbecd24
commit
a9265db1f0
|
@ -0,0 +1,39 @@
|
|||
package com.rusefi.tools.online;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class HttpUtil {
|
||||
public static String RUSEFI_ONLINE_JSON_API_PREFIX = "https://rusefi.com/online/api.php?method=";
|
||||
|
||||
// todo: migrate proxy http jscon API server to TLS
|
||||
public static final String RUSEFI_PROXY_JSON_PROTOCOL = "http://";
|
||||
/**
|
||||
* hostname of PROXY server, not primary rusEFI web server - those are two separate hosts at the moment
|
||||
*/
|
||||
public static String RUSEFI_PROXY_HOSTNAME = System.getProperty("RUSEFI_PROXY_URL", "proxy.rusefi.com");
|
||||
|
||||
public static <T> T getJsonResponse(HttpResponse response) throws IOException, ParseException {
|
||||
HttpEntity entity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
System.out.println("responseString=" + responseString);
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
return (T) parser.parse(responseString);
|
||||
}
|
||||
|
||||
public static HttpResponse executeGet(String url) throws IOException {
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
System.out.println("Connecting to " + url);
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
return httpclient.execute(httpget);
|
||||
}
|
||||
}
|
|
@ -1,16 +1,10 @@
|
|||
package com.rusefi.tools.online;
|
||||
|
||||
import com.rusefi.server.UserDetails;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -23,18 +17,11 @@ public class ProxyClient {
|
|||
|
||||
@NotNull
|
||||
public static List<UserDetails> getOnlineUsers(int httpPort) throws IOException {
|
||||
HttpClient httpclient = new DefaultHttpClient();
|
||||
String url = "http://" + LOCALHOST + ":" + httpPort + LIST_PATH;
|
||||
System.out.println("Connecting to " + url);
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
HttpResponse httpResponse = httpclient.execute(httpget);
|
||||
HttpResponse httpResponse = HttpUtil.executeGet(HttpUtil.RUSEFI_PROXY_JSON_PROTOCOL + HttpUtil.RUSEFI_PROXY_HOSTNAME + ":" + httpPort + LIST_PATH);
|
||||
|
||||
HttpEntity entity = httpResponse.getEntity();
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
JSONParser parser = new JSONParser();
|
||||
List<UserDetails> userLists = new ArrayList<>();
|
||||
try {
|
||||
JSONArray array = (JSONArray) parser.parse(responseString);
|
||||
JSONArray array = HttpUtil.getJsonResponse(httpResponse);
|
||||
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject element = (JSONObject) array.get(i);
|
||||
|
@ -48,4 +35,5 @@ public class ProxyClient {
|
|||
}
|
||||
return userLists;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -13,5 +13,6 @@
|
|||
<orderEntry type="library" name="httpclient" level="project" />
|
||||
<orderEntry type="library" name="json-simple" level="project" />
|
||||
<orderEntry type="module" module-name="shared_io" />
|
||||
<orderEntry type="module" module-name="io" />
|
||||
</component>
|
||||
</module>
|
|
@ -3,7 +3,6 @@ package com.rusefi.tools.online;
|
|||
import com.rusefi.shared.FileUtil;
|
||||
import com.rusefi.tune.xml.Msq;
|
||||
import com.rusefi.ui.AuthTokenPanel;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
|
@ -13,10 +12,8 @@ import org.apache.http.entity.mime.MultipartEntity;
|
|||
import org.apache.http.entity.mime.content.FileBody;
|
||||
import org.apache.http.entity.mime.content.StringBody;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import javax.swing.*;
|
||||
|
@ -47,12 +44,8 @@ public class Online {
|
|||
System.out.println("response=" + response);
|
||||
System.out.println("code " + response.getStatusLine().getStatusCode());
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
System.out.println("responseString=" + responseString);
|
||||
JSONObject object = HttpUtil.getJsonResponse(response);
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
JSONObject object = (JSONObject) parser.parse(responseString);
|
||||
System.out.println("object=" + object);
|
||||
JSONArray info = (JSONArray) object.get("info");
|
||||
JSONArray error = (JSONArray) object.get("error");
|
||||
|
|
|
@ -11,6 +11,7 @@ import com.rusefi.io.LinkManager;
|
|||
import com.rusefi.io.commands.HelloCommand;
|
||||
import com.rusefi.proxy.NetworkConnector;
|
||||
import com.rusefi.server.*;
|
||||
import com.rusefi.tools.online.HttpUtil;
|
||||
import com.rusefi.tools.online.ProxyClient;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -40,7 +41,7 @@ public class ServerTest {
|
|||
|
||||
@Before
|
||||
public void setTestCertificate() throws MalformedURLException {
|
||||
NetworkConnector.RUSEFI_PROXY_HOSTNAME = ProxyClient.LOCALHOST;
|
||||
HttpUtil.RUSEFI_PROXY_HOSTNAME = ProxyClient.LOCALHOST;
|
||||
|
||||
rusEFISSLContext.init("certificate/test_pkcs12.jks", "password");
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ import com.rusefi.io.IoStream;
|
|||
import com.rusefi.io.commands.HelloCommand;
|
||||
import com.rusefi.io.tcp.BinaryProtocolProxy;
|
||||
import com.rusefi.io.tcp.TcpIoStream;
|
||||
import com.rusefi.proxy.NetworkConnector;
|
||||
import com.rusefi.server.ApplicationRequest;
|
||||
import com.rusefi.server.rusEFISSLContext;
|
||||
import com.rusefi.tools.online.HttpUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
@ -26,7 +26,7 @@ public class LocalApplicationProxy {
|
|||
* @param authenticatorPort local port we would bind for TunerStudio to connect to
|
||||
*/
|
||||
static void startAndRun(Logger logger, int serverPortForRemoteUsers, ApplicationRequest applicationRequest, int authenticatorPort) throws IOException {
|
||||
IoStream authenticatorToProxyStream = new TcpIoStream(logger, rusEFISSLContext.getSSLSocket(NetworkConnector.RUSEFI_PROXY_HOSTNAME, serverPortForRemoteUsers));
|
||||
IoStream authenticatorToProxyStream = new TcpIoStream(logger, rusEFISSLContext.getSSLSocket(HttpUtil.RUSEFI_PROXY_HOSTNAME, serverPortForRemoteUsers));
|
||||
LocalApplicationProxy localApplicationProxy = new LocalApplicationProxy(logger, applicationRequest);
|
||||
localApplicationProxy.run(authenticatorToProxyStream);
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.rusefi.io.tcp.TcpIoStream;
|
|||
import com.rusefi.server.ControllerInfo;
|
||||
import com.rusefi.server.SessionDetails;
|
||||
import com.rusefi.server.rusEFISSLContext;
|
||||
import com.rusefi.tools.online.HttpUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -16,7 +17,6 @@ import java.io.IOException;
|
|||
* Connector between rusEFI ECU and rusEFI server
|
||||
*/
|
||||
public class NetworkConnector {
|
||||
public static String RUSEFI_PROXY_HOSTNAME = System.getProperty("RUSEFI_PROXY_URL", "proxy.rusefi.com");
|
||||
|
||||
@NotNull
|
||||
public static SessionDetails runNetworkConnector(int serverPortForControllers, IoStream targetEcuSocket, final Logger logger, String authToken) throws IOException {
|
||||
|
@ -28,7 +28,7 @@ public class NetworkConnector {
|
|||
|
||||
SessionDetails deviceSessionDetails = new SessionDetails(ci, authToken, SessionDetails.createOneTimeCode());
|
||||
|
||||
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(rusEFISSLContext.getSSLSocket(RUSEFI_PROXY_HOSTNAME, serverPortForControllers),
|
||||
BaseBroadcastingThread baseBroadcastingThread = new BaseBroadcastingThread(rusEFISSLContext.getSSLSocket(HttpUtil.RUSEFI_PROXY_HOSTNAME, serverPortForControllers),
|
||||
deviceSessionDetails,
|
||||
logger) {
|
||||
@Override
|
||||
|
|
|
@ -1,11 +1,39 @@
|
|||
package com.rusefi.server;
|
||||
|
||||
import com.rusefi.tools.online.HttpUtil;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class JsonUserDetailsResolver implements Function<String, UserDetails> {
|
||||
@Override
|
||||
@Nullable
|
||||
public UserDetails apply(String authToken) {
|
||||
// todo
|
||||
return new UserDetails("", 0);
|
||||
|
||||
try {
|
||||
HttpResponse response = HttpUtil.executeGet(HttpUtil.RUSEFI_ONLINE_JSON_API_PREFIX + "getUserByToken&rusefi_token=" + authToken);
|
||||
JSONObject json = HttpUtil.getJsonResponse(response);
|
||||
System.out.println("String " + json);
|
||||
Object getUserByToken = json.get("getUserByToken");
|
||||
if (getUserByToken instanceof String) {
|
||||
System.out.println("AUTH FAILED: Result " + getUserByToken);
|
||||
return null;
|
||||
}
|
||||
JSONObject details = (JSONObject) getUserByToken;
|
||||
String id = (String) details.get("ID");
|
||||
String name = (String) details.get("NAME");
|
||||
|
||||
UserDetails result = new UserDetails(name, Integer.parseInt(id));
|
||||
System.out.println("AUTH SUCCESS " + result);
|
||||
return result;
|
||||
} catch (IOException | ParseException e) {
|
||||
System.out.println("JsonUserDetailsResolver error" + e);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,6 @@ public class JsonUserDetailsResolverSandbox {
|
|||
public static void main(String[] args) {
|
||||
JsonUserDetailsResolver resolver = new JsonUserDetailsResolver();
|
||||
|
||||
System.out.println(resolver.apply("haba-haba"));
|
||||
System.out.println("UserDetails result: " + resolver.apply("haba-haba"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue