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

Preparation to rights

Add right management
Print rights needed if 403 error
Cleaning
CAS Logout 
parent 7e2aae79
......@@ -41,7 +41,7 @@ public class MainActivity extends NFCActivity {
setContentView(R.layout.activity_main);
dialog = new Dialog(MainActivity.this);
nemopaySession = new NemopaySession();
nemopaySession = new NemopaySession(MainActivity.this);
casConnexion = new CASConnexion(nemopaySession);
sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
......@@ -89,7 +89,7 @@ public class MainActivity extends NFCActivity {
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));
dialog.errorDialog(getString(R.string.key_registration), getString(R.string.key_remove_temp));
}
protected void delKey() {
......@@ -102,11 +102,11 @@ public class MainActivity extends NFCActivity {
protected void setKey(final String key) {
if (nemopaySession.isRegistered()) {
dialog.errorDialog(getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_already_registered));
dialog.errorDialog(getString(R.string.nemopay_connection), getString(R.string.nemopay_already_registered));
return;
}
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_authentification), true);
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_authentification), true);
loading.setCancelable(false);
new Thread() {
......@@ -132,7 +132,7 @@ public class MainActivity extends NFCActivity {
((TextView) findViewById(R.id.text_app_registered)).setText(nemopaySession.getName().substring(0, nemopaySession.getName().length() - (nemopaySession.getName().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? 13 : 0)));
}
else
dialog.errorDialog(getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_error_registering));
dialog.errorDialog(getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
}
});
}
......@@ -142,7 +142,7 @@ 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_url), true);
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.cas_connection), getString(R.string.cas_in_url), true);
loading.setCancelable(false);
new Thread() {
@Override
......@@ -166,10 +166,10 @@ public class MainActivity extends NFCActivity {
public void run() {
if (casConnexion.getUrl().equals("")) {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_url));
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_url));
}
else
loading.setMessage(getResources().getString(R.string.cas_in_connection));
loading.setMessage(getString(R.string.cas_in_connection));
}
});
......@@ -187,10 +187,10 @@ public class MainActivity extends NFCActivity {
@Override
public void run() {
if (casConnexion.isConnected())
loading.setMessage(getResources().getString(R.string.cas_in_service_adding));
loading.setMessage(getString(R.string.cas_in_service_adding));
else {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_connection));
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_connection));
}
}
});
......@@ -210,10 +210,10 @@ public class MainActivity extends NFCActivity {
public void run() {
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));
loading.setMessage(getString(R.string.nemopay_connection));
else {
loading.dismiss();
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_adding));
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_adding));
}
}
});
......@@ -234,7 +234,7 @@ public class MainActivity extends NFCActivity {
loading.dismiss();
if (!nemopaySession.isConnected())
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.cas_error_service_linking));
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.cas_error_service_linking));
else if (!nemopaySession.isRegistered())
keyDialog();
else
......@@ -251,7 +251,7 @@ public class MainActivity extends NFCActivity {
if (!nemopaySession.isRegistered() || nemopaySession.isConnected())
return;
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.badge_dialog), getResources().getString(R.string.badge_recognization), true);
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.badge_dialog), getString(R.string.badge_recognization), true);
loading.setCancelable(false);
new Thread(new Runnable() {
@Override
......@@ -272,9 +272,9 @@ public class MainActivity extends NFCActivity {
if (nemopaySession.isConnected())
Toast.makeText(MainActivity.this, "Tout est bon !", Toast.LENGTH_SHORT).show();
else if (nemopaySession.getRequest().getResponseCode() == 400)
dialog.errorDialog(getResources().getString(R.string.badge_dialog), getResources().getString(R.string.badge_pin_error_not_recognized));
dialog.errorDialog(getString(R.string.badge_dialog), getString(R.string.badge_pin_error_not_recognized));
else
dialog.errorDialog(getResources().getString(R.string.badge_dialog), getResources().getString(R.string.badge_error_no_rights));
dialog.errorDialog(getString(R.string.badge_dialog), getString(R.string.badge_error_no_rights) + ".\n" + nemopaySession.needRights(MainActivity.this));
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
......@@ -286,12 +286,12 @@ public class MainActivity extends NFCActivity {
protected void badgeDialog(final String idBadge) {
if (!nemopaySession.isRegistered()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.badge_app_not_registered));
dialog.errorDialog(getString(R.string.badge_connection), getString(R.string.badge_app_not_registered));
return;
}
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
dialog.errorDialog(getString(R.string.badge_connection), getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return;
}
......@@ -328,7 +328,7 @@ public class MainActivity extends NFCActivity {
protected void casDialog() {
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected) + " " + nemopaySession.getUsername());
dialog.errorDialog(getString(R.string.cas_connection), getString(R.string.already_connected) + " " + nemopaySession.getUsername());
return;
}
......@@ -395,7 +395,7 @@ public class MainActivity extends NFCActivity {
else {
dialogInterface.cancel();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_registering), true);
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getString(R.string.nemopay_connection), getString(R.string.nemopay_registering), true);
loading.setCancelable(false);
new Thread() {
@Override
......@@ -413,7 +413,7 @@ public class MainActivity extends NFCActivity {
loading.dismiss();
if (nemopaySession.getKey().isEmpty())
dialog.errorDialog(getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_error_registering));
dialog.errorDialog(getString(R.string.nemopay_connection), getString(R.string.nemopay_error_registering));
else
setKey(nemopaySession.getKey());
}
......
......@@ -46,6 +46,9 @@ public class CASConnexion {
this.username = "";
this.location = "";
this.ticket = "";
HTTPRequest request = new HTTPRequest(this.url + "logout");
request.post();
}
public Boolean isConnected() { return !this.location.isEmpty(); }
......
package fr.utc.simde.payutc.tools;
import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import fr.utc.simde.payutc.R;
import static java.lang.System.in;
/**
* Created by Samy on 24/10/2017.
*/
......@@ -16,23 +27,30 @@ import java.util.Map;
public class NemopaySession {
private static final String LOG_TAG = "_NemopaySession";
private static final String url = "https://api.nemopay.net/services/";
private static Map<String, String> allRights = new HashMap<String, String>();
private String name;
private String key;
private String session;
private String username;
private HTTPRequest request;
private Map<String, String> cookies = new HashMap<String, String>();
private String[] rightsNeeded;
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() {
public NemopaySession(Activity activity) {
this.name = "";
this.key = "";
this.session = "";
this.username = "";
String[] keys = activity.getResources().getStringArray(R.array.rights_keys);
String[] values = activity.getResources().getStringArray(R.array.rights_values);
for (int i = 0; i < Math.min(keys.length, values.length); ++i)
this.allRights.put(keys[i], values[i]);
}
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
......@@ -109,7 +127,9 @@ public class NemopaySession {
int reponseCode = request("POSS3", "loginBadge2", new HashMap<String, String>() {{
put("badge_id", idBadge);
put("pin", pin);
}});
}}, new String[]{
"sale"
});
JSONObject response;
if (reponseCode == 200 && this.request.isJsonResponse())
......@@ -149,8 +169,31 @@ public class NemopaySession {
return reponseCode;
}
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 {
public String needRights(Activity activity) {
String result;
if (this.rightsNeeded.length == 0)
return activity.getString(R.string.no_need_rights);
else if (this.rightsNeeded.length == 1)
result = activity.getString(R.string.no_right);
else
result = activity.getString(R.string.no_rights);
for (String right : this.rightsNeeded) {
if (allRights.containsKey(right))
result += " " + allRights.get(right) + ",";
else {
result += " " + right + ",";
Log.e(LOG_TAG, "\"" + right + "\" does not exist");
}
}
return result.substring(0, result.length() - 1) + ".";
}
protected int request(final String method, final String service) throws IOException { return request(method, service, new HashMap<String, String>(), new String[]{}); }
protected int request(final String method, final String service, final String[] rightsNeeded) throws IOException { return request(method, service, new HashMap<String, String>(), rightsNeeded); }
protected int request(final String method, final String service, final Map<String, String> postArgs) throws IOException { return request(method, service, postArgs, new String[]{}); }
protected int request(final String method, final String service, final Map<String, String> postArgs, final String[] rightsNeeded) throws IOException {
Log.d(LOG_TAG, "url: " + url + method + "/" + service);
this.request = new HTTPRequest(url + method + "/" + service);
this.request.setGet(getArgs);
......@@ -159,6 +202,7 @@ public class NemopaySession {
int reponseCode = this.request.post();
this.cookies = request.getCookies();
this.rightsNeeded = rightsNeeded;
return reponseCode;
}
......
......@@ -38,7 +38,7 @@
<string name="badge_connection">Connexion via badge</string>
<string name="badge_app_not_registered">Il est nécessaire que l\'application soit enregistrée pour permettre la connexion via badge</string>
<string name="badge_recognization">Authentification du badge...</string>
<string name="badge_error_no_rights">Impossible de vérifier le badge, l\'application n\'a pas les droits nécessaires</string>
<string name="badge_error_no_rights">Impossible de vérifier le badge</string>
<string name="badge_pin_error_not_recognized">Badge ou code PIN non reconnu</string>
<string name="nemopay_connection">Connexion à Nemopay</string>
<string name="nemopay_authentification">Authentification de l\'application</string>
......@@ -54,4 +54,17 @@
<string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string>
<string name="key_remove_temp">L\'application a été désauthentifiée temporairement. Il suffit de relancer l\'application pour qu\'elle soit de nouveau connectée</string>
<string name="already_connected">Déjà connecté sous</string>
<string name="no_right">Il est nécessaire d\'avoir au moins le droit suivant:</string>
<string name="no_rights">Il est nécessaire d\'avoir au moins les droits suivants:</string>
<string name="no_need_rights">Aucun droit spécifique n\'est nécessaire</string>
<string-array name="rights_keys">
<item>all</item>
<item>sale</item>
</string-array>
<string-array name="rights_values">
<item>Tous les droits</item>
<item>Vente physique</item>
</string-array>
</resources>
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