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

Merge branch 'feature/connection' into develop

parents 425c34d7 519e6e57
......@@ -14,9 +14,15 @@ import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import fr.utc.simde.payutc.tools.NFCActivity;
import fr.utc.simde.payutc.tools.CASConnexion;
......@@ -45,7 +51,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);
......@@ -114,7 +120,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());
......@@ -143,11 +149,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);
......@@ -167,50 +202,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;
try {
casConnexion.addService(service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
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));
}
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));
}
});
}
});
if (casConnexion.isServiceAdded()) {
try {
nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
if (!casConnexion.isServiceAdded())
return;
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();
}
});
}
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();
}
......
......@@ -18,28 +18,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;
......
......@@ -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;
......@@ -186,12 +172,12 @@ 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 {
jsonToMap(response);
new JSONObject(response);
}
catch (Exception e) {
return false;
......
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.
......@@ -67,93 +55,111 @@ public class NemopaySession {
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 && request.isJsonResponse())
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;
if (request.getResponseCode() == 200 && request.isJsonResponse())
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("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;
if (request.getResponseCode() == 200 && request.isJsonResponse())
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("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;
if (request.getResponseCode() == 200 && request.isJsonResponse())
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("No correct informations");
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);
request.post();
int reponseCode = this.request.post();
this.cookies = request.getCookies();
this.request = request;
return reponseCode;
}
}
......@@ -26,9 +26,11 @@
<string name="nfc_availability">L\'application nécessite que le NFC soit disponible sur le téléphone pour que celle-ci fonctionne correctement. Elle ne pourra donc être utilisée sur ce téléphone.</string>
<string name="nfc_accessibility">L\'application nécessite que le NFC soit activé pour fonctionner correctement.</string>
<string name="cas_connection">Connexion via CAS</string>
<string name="cas_in_url">Récupération du lien CAS...</string>
<string name="cas_in_connection">Connexion au CAS...</string>
<string name="cas_in_service_adding">Ajout du service au CAS...</string>
<string name="cas_in_service_connection">Connexion au service via le CAS...</string>
<string name="cas_error_url">Impossible de joindre le CAS</string>
<string name="cas_error_connection">Login et/ou mot de passe non reconnu par le CAS</string>
<string name="cas_error_service_adding">Une erreur a été détectée lors de l\'ajout du service</string>
<string name="cas_error_service_connection">Erreur lors de la connexion au service depuis le CAS</string>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment