Commit 22ead76c authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'release/0.2'

parents 2204b77c 54d95874
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -7,15 +7,13 @@ import android.util.Log;
*/
public class CASConnexion {
private static final String LOG_TAG = "CASConnexion";
private static final String casUrl = "https://api.nemopay.net/services/POSS3/getCasUrl?system_id=payutc";
private static final String service = "http://localhost";
private String url;
private String username;
private String location;
private String ticket;
public CASConnexion() {
private static final String LOG_TAG = "_CASConnexion";
private static String url;
private static String username;
private static String location;
private static String ticket;
public CASConnexion(final NemopaySession nemopaySession) {
this.url = "";
this.username = "";
this.location = "";
......@@ -25,7 +23,7 @@ public class CASConnexion {
@Override
public void run() {
try {
HTTPRequest http = new HTTPRequest(casUrl);
HTTPRequest http = new HTTPRequest("https://api.nemopay.net/services/POSS3/getCasUrl?system_id=payutc"); // Remettre le getCasUrl
http.post();
url = http.getResponse();
url = url.substring(1, url.length() - 1);
......@@ -36,7 +34,11 @@ public class CASConnexion {
}).start();
}
public void setUsername(final String username) { this.username = username; }
public String getUsername() { return this.username; }
public String getTicket() { return this.ticket; }
public String getUrl() { return this.url; }
public void connect(final String username, final String password) throws Exception {
......@@ -44,26 +46,22 @@ public class CASConnexion {
this.location = "";
this.ticket = "";
if (this.url.isEmpty() || username.isEmpty() || password.isEmpty()) {
if (this.url.isEmpty() || username.isEmpty() || password.isEmpty())
throw new RuntimeException("Elements required");
}
HTTPRequest http = new HTTPRequest(this.url + "v1/tickets/");
http.setArg("username", username);
http.setArg("password", password);
HTTPRequest request = new HTTPRequest(this.url + "v1/tickets/");
request.addPost("username", username);
request.addPost("password", password);
if (http.post() == 201) {
Log.d(LOG_TAG, http.getHeader("Location"));
this.location = http.getHeader("Location");
}
if (request.post() == 201)
this.location = request.getHeader("Location");
else
throw new RuntimeException("Not Connected");
}
public Boolean isConnected() { return !this.location.isEmpty(); }
public void addService() throws Exception {
public void addService(final String service) throws Exception {
this.ticket = "";
if (!isConnected())
......@@ -72,13 +70,11 @@ public class CASConnexion {
if (this.url.isEmpty() || this.username.isEmpty() || this.location.isEmpty())
throw new RuntimeException("Elements required");
HTTPRequest http = new HTTPRequest(this.location);
http.setArg("service", this.service);
HTTPRequest request = new HTTPRequest(this.location);
request.addPost("service", service);
if (http.post() == 200) {
this.ticket = http.getResponse();
Log.d(LOG_TAG, this.ticket);
}
if (request.post() == 200)
this.ticket = request.getResponse();
else
throw new RuntimeException("Service not added");
}
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import fr.utc.simde.payutc.MainActivity;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 25/10/2017.
*/
public class Dialog {
private static final String LOG_TAG = "_Dialog";
private static Activity activity;
private static AlertDialog alertDialog;
private static AlertDialog.Builder alertDialogBuilder;
public Dialog(final Activity activity) {
this.activity = activity;
}
public void dismiss() {
if (this.alertDialog != null)
this.alertDialog.dismiss();
}
public Boolean isShowing() { return this.alertDialog != null && this.alertDialog.isShowing(); }
public void createDialog() { createDialog((EditText) null); }
public void createDialog(AlertDialog.Builder alertDialogBuilder) { createDialog(alertDialogBuilder, null); }
public void createDialog(AlertDialog.Builder alertDialogBuilder, final EditText input) { this.alertDialogBuilder = alertDialogBuilder; createDialog(input); }
public void createDialog(final EditText input) {
dismiss();
this.alertDialog = alertDialogBuilder.create();
this.alertDialog.show();
// Auto open keyboard
if (input != null) {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
input.requestFocus();
input.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}.start();
}
}
public void errorDialog(final String title, final String message) {
this.alertDialogBuilder = new AlertDialog.Builder(this.activity);
this.alertDialogBuilder
.setTitle(title)
.setMessage(message)
.setCancelable(true)
.setNegativeButton(R.string.ok, null);
createDialog();
}
}
......@@ -6,6 +6,9 @@ package fr.utc.simde.payutc.tools;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
......@@ -14,58 +17,108 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HTTPRequest {
public static final String LOG_TAG = "HTTPRequest";
public String url;
public HttpURLConnection request;
public String response;
private static final String LOG_TAG = "_HTTPRequest";
private String url;
private HttpURLConnection request;
private String response;
static Map<String, String> args;
static Map<String, String> cookies;
private Map<String, String> postArgs;
private Map<String, String> getArgs;
private Map<String, String> cookies;
public HTTPRequest(final String url) {
this.url = url;
this.request = null;
this.response = "";
this.args = new HashMap<String, String>();
this.postArgs = new HashMap<String, String>();
this.getArgs = new HashMap<String, String>();
this.cookies = new HashMap<String, String>();
}
public int get() throws IOException {
String data = "?" + args2String(this.args);
Log.d(LOG_TAG, "get: " + this.url + data);
public static Map<String, String> jsonToMap(String t) throws JSONException {
Map<String, String> map = new HashMap<String, String>();
JSONObject jObject = new JSONObject(t);
Iterator<?> keys = jObject.keys();
while (keys.hasNext()){
String key = (String) keys.next();
String value = jObject.getString(key);
map.put(key, value);
}
return map;
}
public int get() {
String get = null;
try {
get = args2String(this.getArgs, true);
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
Log.d(LOG_TAG, "get: " + this.url + get);
this.request = (HttpURLConnection) (new URL(this.url + data)).openConnection();
this.request.setRequestMethod("GET");
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
this.request.setDoOutput(true);
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
try {
this.request = (HttpURLConnection) (new URL(this.url + get)).openConnection();
this.request.setRequestMethod("GET");
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
this.request.setDoOutput(true);
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
generateResponse();
return this.request.getResponseCode();
generateResponse();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
return getResponseCode();
}
public int post() throws IOException {
String data = args2String(this.args);
Log.d(LOG_TAG, "post: " + this.url + ", data: " + data);
public int post() {
String get = null;
String post = null;
this.request = (HttpURLConnection) (new URL(this.url)).openConnection();
this.request.setRequestMethod("POST");
this.request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
this.request.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
this.request.setDoInput(true);
this.request.setDoOutput(true);
this.request.getOutputStream().write(data.getBytes());
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
try {
get = args2String(this.getArgs, true);
post = args2String(this.postArgs);
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
Log.d(LOG_TAG, "post: " + this.url + get + ", data: " + post);
try {
this.request = (HttpURLConnection) (new URL(this.url + get)).openConnection();
this.request.setRequestMethod("POST");
this.request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
this.request.setRequestProperty("charset", "utf-8");
this.request.setRequestProperty("Content-Length", Integer.toString(post.getBytes().length));
this.request.setRequestProperty("Cookie", getCookiesHeader());
this.request.setUseCaches(false);
this.request.setDoInput(true);
this.request.setDoOutput(true);
this.request.getOutputStream().write(post.getBytes());
updateCookies(this.request.getHeaderFields().get("Set-Cookie"));
generateResponse();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
generateResponse();
return this.request.getResponseCode();
return getResponseCode();
}
public Map<String, List<String>> getHeaders() {
......@@ -82,11 +135,32 @@ public class HTTPRequest {
return this.request.getHeaderField(name);
}
public String getResponseMessage(final String name) throws IOException {
public int getResponseCode() {
if (this.request == null)
return Integer.parseInt(null);
try {
return this.request.getResponseCode();
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
return 500;
}
public String getResponseMessage(final String name) {
if (this.request == null)
return null;
return this.request.getResponseMessage();
try {
return this.request.getResponseMessage();
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
return "";
}
protected void generateResponse() throws IOException {
......@@ -102,28 +176,41 @@ public class HTTPRequest {
this.response = builder.toString();
}
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); }
public String getResponse() throws IOException { return response; }
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException {
protected String args2String(Map<String, String> args) throws UnsupportedEncodingException { return args2String(args, false); }
protected String args2String(Map<String, String> args, Boolean isGet) throws UnsupportedEncodingException {
String data = "";
for (String arg : args.keySet())
data += (URLEncoder.encode(arg, "UTF-8") + "=" + URLEncoder.encode(args.get(arg), "UTF-8") + "&");
return data.substring(0, data.equals("") ? 0 : data.length() - 1);
return data.equals("") ? "" : (isGet ? "?" : "") + data.substring(0, data.length() - 1);
}
public void setGet(Map<String, String> args) {
this.getArgs = args;
}
public void setPost(Map<String, String> args) {
this.postArgs = args;
}
public void addGet(final String key, final String value) {
this.getArgs.put(key, value);
}
public void setArg(final String key, final String value) {
this.args.put(key, value);
public void addPost(final String key, final String value) {
this.postArgs.put(key, value);
}
public Boolean delArg(final String key) {
if (this.args.containsKey(key))
this.args.remove(key);
else
return false;
public Map<String, String> getCookies() {
return this.cookies;
}
return true;
public void setCookies(Map<String, String> cookies) {
this.cookies = cookies;
}
synchronized String getCookiesHeader() {
......
......@@ -23,7 +23,7 @@ import android.widget.Toast;
import fr.utc.simde.payutc.R;
public abstract class NFCActivity extends Activity {
private static final String LOG_TAG = "NFCActivity";
private static final String LOG_TAG = "_NFCActivity";
private static NfcAdapter NFCAdapter;
private static AlertDialog.Builder alertDialogBuilder;
......@@ -75,7 +75,6 @@ public abstract class NFCActivity extends Activity {
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals(NFCAdapter.ACTION_TAG_DISCOVERED)) {
Log.d(LOG_TAG, "Lecture d'un ID NFC");
String idBadge = ByteArrayToHexString(((Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)).getId());
Log.d(LOG_TAG, "ID: " + idBadge);
onIdentification(idBadge);
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import fr.utc.simde.payutc.MainActivity;
import fr.utc.simde.payutc.R;
/**
* Created by Samy on 24/10/2017.
*/
public class NemopaySession {
private static final String LOG_TAG = "_NemopaySession";
private static final String url = "https://api.nemopay.net/services/";
private String name;
private String key;
private String session;
private String username;
public NemopaySession(final String session, final String username) {
this.session = session;
this.username = username;
private HTTPRequest request;
private Map<String, String> cookies = new HashMap<String, String>();
private final Map<String, String> getArgs = new HashMap<String, String>() {{
put("system_id", "payutc");
}};
public NemopaySession() {
this.name = "";
this.key = "";
this.session = "";
this.username = "";
}
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
public Boolean isRegistered() { return !this.name.isEmpty() && !this.key.isEmpty() && !this.session.isEmpty(); }
public String getName() { return this.name; }
public String getKey() { return this.key; }
public void getCasUrl() throws IOException {
request("POSS3", "getCasUrl");
}
public void registerApp(final String name, final String description, final String service) throws IOException, JSONException {
request("KEY", "registerApplication", new HashMap<String, String>() {{
put("app_url", service);
put("app_name", name);
put("app_desc", description);
}});
if (request.getResponseCode() == 200)
this.key = request.getJsonResponse().get("app_key");
this.request = request;
}
public void loginApp(final String key) throws Exception {
request("POSS3", "loginApp", new HashMap<String, String>() {{
put("key", key);
}});
Map<String, String> response = request.getJsonResponse();
if (response.containsKey("sessionid") && response.containsKey("name")) {
this.session = response.get("sessionid");
this.name = response.get("name");
this.key = key;
}
else
throw new Exception("Not authentified");
}
public void loginBadge(final String idBadge, final String pin) throws Exception {
request("POSS3", "loginBadge2", new HashMap<String, String>() {{
put("badge_id", idBadge);
put("pin", pin);
}});
Map<String, String> response = request.getJsonResponse();
if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid");
this.username = response.get("username");
}
else {
throw new Exception("Not connected");
}
}
public void loginCas(final String ticket, final String service) throws Exception {
request("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket);
put("service", service);
}});
Map<String, String> response = request.getJsonResponse();
if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid");
this.username = response.get("username");
}
else
throw new Exception("Not connected");
}
public HTTPRequest getRequest() { return this.request; }
protected void request(final String method, final String service) throws IOException { request(method, service, new HashMap<String, String>()); }
protected void request(final String method, final String service, final Map<String, String> postArgs) throws IOException {
HTTPRequest request = new HTTPRequest(url + method + "/" + service);
Log.d(LOG_TAG, "url: " + url + method + "/" + service);