Commit bb489734 authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Merge branch 'release/0.2.1'

parents 22ead76c 07430c69
......@@ -2,7 +2,6 @@ package fr.utc.simde.payutc;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
......@@ -14,14 +13,10 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import fr.utc.simde.payutc.tools.HTTPRequest;
import fr.utc.simde.payutc.tools.NFCActivity;
import fr.utc.simde.payutc.tools.CASConnexion;
import fr.utc.simde.payutc.tools.Dialog;
......@@ -49,7 +44,7 @@ public class MainActivity extends NFCActivity {
nemopaySession = new NemopaySession();
casConnexion = new CASConnexion(nemopaySession);
sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
final String key = sharedPreferences.getString("key", "");
if (!key.equals(""))
setKey(key);
......@@ -63,8 +58,8 @@ public class MainActivity extends NFCActivity {
public boolean onLongClick(View v) {
if (!nemopaySession.isRegistered())
addKeyDialog();
else // A supprimer = embêtant si les clés sont réinitialisées
delKey();
else
unregister();
return false;
}
......@@ -73,7 +68,7 @@ public class MainActivity extends NFCActivity {
usernameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
connectDialog();
casDialog();
}
});
}
......@@ -84,10 +79,25 @@ public class MainActivity extends NFCActivity {
badgeDialog(idBadge);
}
protected void disconnect() {
nemopaySession.disconnect();
casConnexion.disconnect();
}
protected void unregister() {
nemopaySession.unregister();
disconnect();
((TextView) findViewById(R.id.text_app_registered)).setText(R.string.app_not_registred);
dialog.errorDialog(getResources().getString(R.string.key_registration), getResources().getString(R.string.key_remove_temp));
}
protected void delKey() {
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.remove("key");
edit.apply();
unregister();
}
protected void setKey(final String key) {
......@@ -103,7 +113,7 @@ public class MainActivity extends NFCActivity {
@Override
public void run() {
try {
nemopaySession.loginApp(key);
nemopaySession.loginApp(key, casConnexion);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
......@@ -132,11 +142,40 @@ public class MainActivity extends NFCActivity {
protected void connectWithCAS(final String username, final String password) throws InterruptedException {
dialog.dismiss();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_in_connection), true);
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_in_url), true);
loading.setCancelable(false);
new Thread() {
@Override
public void run() {
if (casConnexion.getUrl().equals("")) {
try {
if (nemopaySession.getCASUrl() == 200) {
Log.d(LOG_TAG, nemopaySession.getRequest().getResponse());
String url = nemopaySession.getRequest().getResponse();
casConnexion.setUrl(url.substring(1, url.length() - 1));
}
else
throw new Exception("Impossible to get CAS url");
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (casConnexion.getUrl().equals("")) {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_url));
}
else
loading.setMessage(getResources().getString(R.string.cas_in_connection));
}
});
if (casConnexion.getUrl().equals(""))
return;
try {
casConnexion.connect(username, password);
Thread.sleep(100);
......@@ -156,50 +195,52 @@ public class MainActivity extends NFCActivity {
}
});
if (casConnexion.isConnected()) {
try {
casConnexion.addService(service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
if (!casConnexion.isConnected())
return;
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
casConnexion.addService(service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));
else {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding));
}
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
if (casConnexion.isServiceAdded()) {
try {
nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));
else {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding));
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
}
});
}
});
if (!casConnexion.isServiceAdded())
return;
try {
nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
}
});
}
}.start();
}
......@@ -250,7 +291,7 @@ public class MainActivity extends NFCActivity {
}
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected));
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return;
}
......@@ -285,9 +326,9 @@ public class MainActivity extends NFCActivity {
dialog.createDialog(alertDialogBuilder, pinInput);
}
protected void connectDialog() {
protected void casDialog() {
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected));
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return;
}
......@@ -310,7 +351,7 @@ public class MainActivity extends NFCActivity {
Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show();
dialogInterface.cancel();
connectDialog();
casDialog();
}
else {
try {
......@@ -399,7 +440,8 @@ public class MainActivity extends NFCActivity {
public void onClick(DialogInterface dialogInterface, int id) {
setKey(keyInput.getText().toString());
}
});
})
.setNegativeButton(R.string.cancel, null);
dialog.createDialog(alertDialogBuilder, keyInput);
}
......
package fr.utc.simde.payutc.tools;
import android.util.Log;
/**
* Created by Samy on 24/10/2017.
*/
......@@ -18,28 +16,13 @@ public class CASConnexion {
this.username = "";
this.location = "";
this.ticket = "";
new Thread(new Runnable(){
@Override
public void run() {
try {
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);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
}).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 setUrl(final String url) { this.url = url; }
public void connect(final String username, final String password) throws Exception {
this.username = username;
......@@ -59,6 +42,12 @@ public class CASConnexion {
throw new RuntimeException("Not Connected");
}
public void disconnect() {
this.username = "";
this.location = "";
this.ticket = "";
}
public Boolean isConnected() { return !this.location.isEmpty(); }
public void addService(final String service) throws Exception {
......
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;
/**
......
......@@ -40,20 +40,6 @@ public class HTTPRequest {
this.cookies = new HashMap<String, String>();
}
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;
......@@ -78,7 +64,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
}
return getResponseCode();
......@@ -115,7 +106,12 @@ public class HTTPRequest {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
try {
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", error: " + e.getMessage());
}
catch (Exception e2) {
Log.e(LOG_TAG, e.getMessage());
}
}
return getResponseCode();
......@@ -176,9 +172,20 @@ public class HTTPRequest {
this.response = builder.toString();
}
public Map<String, String> getJsonResponse() throws IOException, JSONException { return jsonToMap(response); }
public JSONObject getJsonResponse() throws IOException, JSONException { return new JSONObject(response); }
public String getResponse() throws IOException { return response; }
public Boolean isJsonResponse() {
try {
new JSONObject(response);
}
catch (Exception e) {
return false;
}
return true;
}
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 = "";
......
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.
......@@ -50,87 +38,128 @@ public class NemopaySession {
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
public Boolean isRegistered() { return !this.name.isEmpty() && !this.key.isEmpty() && !this.session.isEmpty(); }
public void disconnect() {
this.session = "";
this.username = "";
}
public void unregister() {
this.name = "";
this.key = "";
disconnect();
}
public String getName() { return this.name; }
public String getKey() { return this.key; }
public String getUsername() { return username; }
public HTTPRequest getRequest() { return this.request; }
public void getCasUrl() throws IOException {
request("POSS3", "getCasUrl");
public int getCASUrl() throws IOException {
return 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>() {{
public int registerApp(final String name, final String description, final String service) throws IOException, JSONException {
int reponseCode = 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");
if (reponseCode == 200 && this.request.isJsonResponse())
this.key = this.request.getJsonResponse().getString("app_key");
this.request = request;
return reponseCode;
}
public void loginApp(final String key) throws Exception {
request("POSS3", "loginApp", new HashMap<String, String>() {{
public int loginApp(final String key, CASConnexion casConnexion) throws Exception {
int reponseCode = loginApp(key);
JSONObject response = getRequest().getJsonResponse();
if (response.has("config") && ((JSONObject) response.get("config")).has("cas_url"))
casConnexion.setUrl(((JSONObject) response.get("config")).getString("cas_url"));
else
throw new Exception("No correct informations");
return reponseCode;
}
public int loginApp(final String key) throws Exception {
int reponseCode = request("POSS3", "loginApp", new HashMap<String, String>() {{
put("key", key);
}});
Map<String, String> response = request.getJsonResponse();
JSONObject response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not authentified");
if (response.containsKey("sessionid") && response.containsKey("name")) {
this.session = response.get("sessionid");
this.name = response.get("name");
if (response.has("sessionid") && response.has("name")) {
this.session = response.getString("sessionid");
this.name = response.getString("name");
this.key = key;
}
else
throw new Exception("Not authentified");
throw new Exception("No correct informations");
return reponseCode;
}
public void loginBadge(final String idBadge, final String pin) throws Exception {
request("POSS3", "loginBadge2", new HashMap<String, String>() {{
public int loginBadge(final String idBadge, final String pin) throws Exception {
int reponseCode = 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 {
JSONObject response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid");
this.username = response.getString("username");
}
else
throw new Exception("No correct informations");
return reponseCode;
}
public void loginCas(final String ticket, final String service) throws Exception {
request("POSS3", "loginCas2", new HashMap<String, String>() {{
public int loginCas(final String ticket, final String service) throws Exception {
int reponseCode = request("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket);
put("service", service);
}});
Map<String, String> response = request.getJsonResponse();
JSONObject response;
if (reponseCode == 200 && this.request.isJsonResponse())
response = this.request.getJsonResponse();
else
throw new Exception("Not connected");
if (response.containsKey("sessionid") && response.containsKey("username")) {
this.session = response.get("sessionid");
this.username = response.get("username");
if (response.has("sessionid") && response.has("username")) {
this.session = response.getString("sessionid");
this.username = response.getString("username");
}
else
throw new Exception("Not connected");
}
throw new Exception("No correct informations");
public HTTPRequest getRequest() { return this.request; }
return reponseCode;
}
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);
protected int request(final String method, final String service) throws IOException { return request(method, service, new HashMap<String, String>()); }
protected int request(final String method, final String service, final Map<String, String> postArgs) throws IOException {
Log.d(LOG_TAG, "url: " + url + method + "/" + service);
request.setGet(getArgs);
request.setPost(postArgs);
request.setCookies(this.cookies);
this.request = new HTTPRequest(url + method + "/" + service);
this.request.setGet(getArgs);
this.request.setPost(postArgs);
this.request.setCookies(this.cookies);