2018-06-10 16:23:33 +02:00
|
|
|
package de.overview.wg.its.mispauth.network;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.Log;
|
|
|
|
import com.android.volley.Request;
|
|
|
|
import com.android.volley.RequestQueue;
|
|
|
|
import com.android.volley.Response;
|
|
|
|
import com.android.volley.VolleyError;
|
2018-07-19 10:12:04 +02:00
|
|
|
import com.android.volley.toolbox.HurlStack;
|
2018-06-10 16:23:33 +02:00
|
|
|
import com.android.volley.toolbox.JsonObjectRequest;
|
|
|
|
import com.android.volley.toolbox.Volley;
|
2018-07-19 10:12:04 +02:00
|
|
|
import de.overview.wg.its.mispauth.R;
|
2018-06-10 16:23:33 +02:00
|
|
|
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
|
2018-06-10 23:34:21 +02:00
|
|
|
import de.overview.wg.its.mispauth.auxiliary.ReadableError;
|
2018-06-10 18:40:41 +02:00
|
|
|
import de.overview.wg.its.mispauth.model.Organisation;
|
2018-07-19 10:12:04 +02:00
|
|
|
import de.overview.wg.its.mispauth.model.Server;
|
|
|
|
import de.overview.wg.its.mispauth.model.User;
|
2018-06-12 20:33:27 +02:00
|
|
|
import org.json.JSONArray;
|
2018-06-10 16:23:33 +02:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2018-07-19 10:12:04 +02:00
|
|
|
import javax.net.ssl.*;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.cert.Certificate;
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.security.cert.CertificateFactory;
|
|
|
|
import java.security.cert.X509Certificate;
|
2018-06-10 16:23:33 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple JSON based API to communicate with MISP-Instances
|
|
|
|
*/
|
|
|
|
public class MispRequest {
|
|
|
|
|
2018-06-10 18:40:41 +02:00
|
|
|
private static MispRequest instance;
|
|
|
|
|
|
|
|
private RequestQueue requestQueue;
|
|
|
|
private PreferenceManager preferenceManager;
|
|
|
|
private String serverUrl, apiKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param context for Volley and PreferenceManager
|
|
|
|
*/
|
2018-07-19 20:20:15 +02:00
|
|
|
private MispRequest(Context context, boolean loadSavedCredentials) {
|
2018-06-10 18:40:41 +02:00
|
|
|
requestQueue = Volley.newRequestQueue(context);
|
2018-07-19 10:12:04 +02:00
|
|
|
|
2018-07-19 20:20:15 +02:00
|
|
|
if (loadSavedCredentials) {
|
|
|
|
preferenceManager = PreferenceManager.Instance(context);
|
|
|
|
loadSavedCredentials();
|
|
|
|
}
|
2018-06-10 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void loadSavedCredentials() {
|
|
|
|
serverUrl = preferenceManager.getMyServerUrl();
|
|
|
|
apiKey = preferenceManager.getMyServerApiKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param orgId organisation ID on the MISP-Instance
|
|
|
|
* @param callback returns a single Organisation-JSON
|
|
|
|
*/
|
2018-06-10 23:34:21 +02:00
|
|
|
public void getOrganisation(int orgId, final OrganisationCallback callback) {
|
2018-06-10 18:40:41 +02:00
|
|
|
|
|
|
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
try {
|
2018-07-19 10:12:04 +02:00
|
|
|
callback.onResult(response.getJSONObject(Organisation.ROOT_KEY));
|
2018-06-10 18:40:41 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
callback.onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Request r = objectRequest(Request.Method.GET,
|
|
|
|
serverUrl + "/organisations/view/" + orgId,
|
|
|
|
null,
|
|
|
|
listener,
|
|
|
|
errorListener);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Typically used to get the organisation linked with this user
|
|
|
|
*
|
|
|
|
* @param callback return user associated with this API-Key
|
|
|
|
*/
|
2018-07-19 10:12:04 +02:00
|
|
|
public void getMyUser(final UserCallback callback) {
|
2018-06-10 18:40:41 +02:00
|
|
|
|
|
|
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
|
|
|
|
try {
|
2018-07-19 10:12:04 +02:00
|
|
|
callback.onResult(response.getJSONObject(User.ROOT_KEY));
|
2018-06-10 18:40:41 +02:00
|
|
|
return;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.onResult(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
callback.onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (serverUrl.isEmpty() || apiKey.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Request r = objectRequest(
|
|
|
|
Request.Method.GET,
|
|
|
|
serverUrl + "/users/view/me",
|
|
|
|
null,
|
|
|
|
listener,
|
|
|
|
errorListener);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param organisation The organisation that will be added
|
|
|
|
* @param callback returns complete organisation JSON
|
|
|
|
*/
|
|
|
|
public void addOrganisation(Organisation organisation, final OrganisationCallback callback) {
|
|
|
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
try {
|
2018-07-19 10:12:04 +02:00
|
|
|
callback.onResult(response.getJSONObject(Organisation.ROOT_KEY));
|
2018-06-10 18:40:41 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
callback.onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Request r = objectRequest(
|
|
|
|
Request.Method.POST,
|
|
|
|
serverUrl + "/admin/organisations/add",
|
|
|
|
organisation.toJSON(),
|
|
|
|
listener,
|
|
|
|
errorListener
|
|
|
|
);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:33:27 +02:00
|
|
|
public void getOrganisations(final OrganisationsCallback callback) {
|
|
|
|
Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>() {
|
2018-06-10 23:34:21 +02:00
|
|
|
@Override
|
2018-06-12 20:33:27 +02:00
|
|
|
public void onResponse(JSONArray response) {
|
|
|
|
|
|
|
|
JSONArray resultArray = new JSONArray();
|
|
|
|
|
|
|
|
int orgCount = response.length();
|
|
|
|
|
|
|
|
for(int i = 0; i < orgCount; i++) {
|
|
|
|
try {
|
|
|
|
resultArray.put(response.getJSONObject(i).getJSONObject("Organisation"));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.onResult(resultArray);
|
2018-06-10 23:34:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
2018-07-19 20:20:15 +02:00
|
|
|
callback.onError(error);
|
2018-06-10 23:34:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-12 20:33:27 +02:00
|
|
|
Request r = arrayRequestWithJsonObject(
|
2018-06-10 23:34:21 +02:00
|
|
|
Request.Method.GET,
|
2018-06-12 20:33:27 +02:00
|
|
|
serverUrl + "/organisations/index",
|
2018-06-10 23:34:21 +02:00
|
|
|
null,
|
|
|
|
listener,
|
|
|
|
errorListener);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
2018-07-19 10:12:04 +02:00
|
|
|
public void addUser(User user, final UserCallback callback) {
|
|
|
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
try {
|
|
|
|
callback.onResult(response.getJSONObject(User.ROOT_KEY));
|
|
|
|
return;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.onResult(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
callback.onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Request r = objectRequest(
|
|
|
|
Request.Method.POST,
|
|
|
|
serverUrl + "/admin/users/add",
|
|
|
|
user.toJSON(),
|
|
|
|
listener,
|
|
|
|
errorListener
|
|
|
|
);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addServer(Server server, final ServerCallback callback) {
|
|
|
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(JSONObject response) {
|
|
|
|
try {
|
|
|
|
callback.onResult(response.getJSONObject(Server.ROOT_KEY));
|
|
|
|
return;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.onResult(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
|
|
|
@Override
|
|
|
|
public void onErrorResponse(VolleyError error) {
|
|
|
|
callback.onError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Request r = objectRequest(
|
|
|
|
Request.Method.POST,
|
|
|
|
serverUrl + "/servers/add",
|
|
|
|
server.toJSON(),
|
|
|
|
listener,
|
|
|
|
errorListener
|
|
|
|
);
|
|
|
|
|
|
|
|
requestQueue.add(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-12 20:33:27 +02:00
|
|
|
private JsonArrayRequestWithJsonObject arrayRequestWithJsonObject(int method, String url,
|
|
|
|
@Nullable JSONObject body,
|
|
|
|
Response.Listener<JSONArray> listener,
|
|
|
|
Response.ErrorListener errorListener) {
|
2018-06-10 18:40:41 +02:00
|
|
|
|
2018-06-12 20:33:27 +02:00
|
|
|
return new JsonArrayRequestWithJsonObject(method, url, body, listener, errorListener) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
|
|
|
params.put("Authorization", apiKey);
|
|
|
|
params.put("Accept", "application/json");
|
|
|
|
params.put("Content-Type", "application/json; utf-8");
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private JsonObjectRequest objectRequest(int method, String url,
|
2018-06-10 18:40:41 +02:00
|
|
|
@Nullable JSONObject body,
|
|
|
|
Response.Listener<JSONObject> listener,
|
|
|
|
Response.ErrorListener errorListener) {
|
|
|
|
|
|
|
|
return new JsonObjectRequest(method, url, body, listener, errorListener) {
|
|
|
|
@Override
|
|
|
|
public Map<String, String> getHeaders() {
|
|
|
|
Map<String, String> params = new HashMap<>();
|
|
|
|
|
|
|
|
params.put("Authorization", apiKey);
|
|
|
|
params.put("Accept", "application/json");
|
|
|
|
params.put("Content-Type", "application/json; utf-8");
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setServerCredentials(String serverUrl, String apiKey) {
|
|
|
|
this.serverUrl = serverUrl;
|
|
|
|
this.apiKey = apiKey;
|
|
|
|
}
|
|
|
|
|
2018-07-19 20:20:15 +02:00
|
|
|
public static MispRequest Instance(Context context, boolean loadSavedCredentials) {
|
|
|
|
|
2018-06-10 18:40:41 +02:00
|
|
|
if (instance == null) {
|
2018-07-19 20:20:15 +02:00
|
|
|
instance = new MispRequest(context, loadSavedCredentials);
|
2018-06-10 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:33:27 +02:00
|
|
|
public interface OrganisationsCallback {
|
|
|
|
void onResult(JSONArray organisations);
|
2018-06-10 18:40:41 +02:00
|
|
|
void onError(VolleyError volleyError);
|
|
|
|
}
|
|
|
|
public interface OrganisationCallback {
|
|
|
|
void onResult(JSONObject organisationInformation);
|
|
|
|
void onError(VolleyError volleyError);
|
|
|
|
}
|
|
|
|
public interface UserCallback {
|
2018-07-19 10:12:04 +02:00
|
|
|
void onResult(JSONObject userInformation);
|
2018-06-10 18:40:41 +02:00
|
|
|
|
|
|
|
void onError(VolleyError volleyError);
|
|
|
|
}
|
2018-06-10 23:34:21 +02:00
|
|
|
public interface ServerCallback {
|
2018-07-19 10:12:04 +02:00
|
|
|
void onResult(JSONObject server);
|
2018-06-10 23:34:21 +02:00
|
|
|
void onError(VolleyError volleyError);
|
|
|
|
}
|
2018-06-10 16:23:33 +02:00
|
|
|
}
|