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

Badge connection

Connect with badge and pin if the application has the right
Verify before connected if already connected
Restructuration of the NemopaySession class (better HTTP request management)
parent 8f89d5b2
......@@ -63,6 +63,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();
return false;
}
......@@ -176,7 +178,7 @@ public class MainActivity extends NFCActivity {
if (casConnexion.isServiceAdded()) {
try {
HTTPRequest request = nemopaySession.loginCas(casConnexion.getTicket(), service);
nemopaySession.loginCas(casConnexion.getTicket(), service);
Thread.sleep(1000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
......@@ -204,21 +206,40 @@ public class MainActivity extends NFCActivity {
protected void connectWithBadge(final String idBadge, final String pin) {
dialog.dismiss();
if (nemopaySession.isRegistered()) {
final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Connexion ...", "A faire ...", true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
ringProgressDialog.dismiss();
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);
loading.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
nemopaySession.loginBadge(idBadge, pin);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}).start();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
try {
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));
else
dialog.errorDialog(getResources().getString(R.string.badge_dialog), getResources().getString(R.string.badge_error_no_rights));
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
});
}
}).start();
}
protected void badgeDialog(final String idBadge) {
......@@ -227,6 +248,11 @@ public class MainActivity extends NFCActivity {
return;
}
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.already_connected));
return;
}
final View pinView = getLayoutInflater().inflate(R.layout.dialog_badge, null);
final EditText pinInput = pinView.findViewById(R.id.input_pin);
......@@ -255,10 +281,15 @@ public class MainActivity extends NFCActivity {
}
});
dialog.createDialog(alertDialogBuilder);
dialog.createDialog(alertDialogBuilder, pinInput);
}
protected void connectDialog() {
if (nemopaySession.isConnected()) {
dialog.errorDialog(getResources().getString(R.string.cas_connection), getResources().getString(R.string.already_connected));
return;
}
final View usernameView = getLayoutInflater().inflate(R.layout.dialog_login, null);
final EditText usernameInput = usernameView.findViewById(R.id.input_username);
final EditText passwordInput = usernameView.findViewById(R.id.input_password);
......@@ -309,7 +340,7 @@ public class MainActivity extends NFCActivity {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle(R.string.key_dialog)
.setTitle(R.string.key_registration)
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
......@@ -360,7 +391,7 @@ public class MainActivity extends NFCActivity {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle(R.string.key_dialog)
.setTitle(R.string.key_registration)
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
......@@ -369,6 +400,6 @@ public class MainActivity extends NFCActivity {
}
});
dialog.createDialog(alertDialogBuilder);
dialog.createDialog(alertDialogBuilder, keyInput);
}
}
......@@ -54,42 +54,71 @@ public class HTTPRequest {
return map;
}
public int get() throws IOException {
String get = args2String(this.getArgs, true);
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 + 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"));
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();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
generateResponse();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
return this.request.getResponseCode();
return getResponseCode();
}
public int post() throws IOException {
String get = args2String(this.getArgs, true);
String post = args2String(this.postArgs);
public int post() {
String get = null;
String post = null;
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);
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"));
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();
Log.d(LOG_TAG, "code: " + Integer.toString(this.request.getResponseCode()) + ", response: " + this.getResponse());
return this.request.getResponseCode();
return getResponseCode();
}
public Map<String, List<String>> getHeaders() {
......@@ -106,18 +135,32 @@ public class HTTPRequest {
return this.request.getHeaderField(name);
}
public int getResponseCode() throws IOException {
public int getResponseCode() {
if (this.request == null)
return Integer.parseInt(null);
return this.request.getResponseCode();
try {
return this.request.getResponseCode();
}
catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
return 500;
}
public String getResponseMessage(final String name) throws IOException {
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 {
......
......@@ -33,6 +33,7 @@ public class NemopaySession {
private String session;
private String username;
private HTTPRequest request;
private Map<String, String> cookies = new HashMap<String, String>();
private final Map<String, String> getArgs = new HashMap<String, String>() {{
......@@ -52,12 +53,12 @@ public class NemopaySession {
public String getName() { return this.name; }
public String getKey() { return this.key; }
public HTTPRequest getCasUrl() throws IOException {
return construct("POSS3", "getCasUrl");
public void getCasUrl() throws IOException {
request("POSS3", "getCasUrl");
}
public HTTPRequest registerApp(final String name, final String description, final String service) throws IOException, JSONException {
HTTPRequest request = construct("KEY", "registerApplication", new HashMap<String, String>() {{
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);
......@@ -66,11 +67,11 @@ public class NemopaySession {
if (request.getResponseCode() == 200)
this.key = request.getJsonResponse().get("app_key");
return request;
this.request = request;
}
public HTTPRequest loginApp(final String key) throws Exception {
HTTPRequest request = construct("POSS3", "loginApp", new HashMap<String, String>() {{
public void loginApp(final String key) throws Exception {
request("POSS3", "loginApp", new HashMap<String, String>() {{
put("key", key);
}});
......@@ -83,12 +84,27 @@ public class NemopaySession {
}
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);
}});
return request;
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 loginCas(final String ticket, final String service) throws Exception {
HTTPRequest request = construct("POSS3", "loginCas2", new HashMap<String, String>() {{
public void loginCas(final String ticket, final String service) throws Exception {
request("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket);
put("service", service);
}});
......@@ -101,12 +117,12 @@ public class NemopaySession {
}
else
throw new Exception("Not connected");
return request;
}
protected HTTPRequest construct(final String method, final String service) throws IOException { return construct(method, service, new HashMap<String, String>()); }
protected HTTPRequest construct(final String method, final String service, final Map<String, String> postArgs) throws IOException {
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);
request.setGet(getArgs);
......@@ -115,6 +131,6 @@ public class NemopaySession {
request.post();
this.cookies = request.getCookies();
return request;
this.request = request;
}
}
......@@ -35,16 +35,20 @@
<string name="cas_error_service_linking">Erreur lors de la connexion entre le CAS et le service</string>
<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_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>
<string name="nemopay_registering">Enregistrement de l\'application</string>
<string name="nemopay_already_registered">Application déjà authentifiée</string>
<string name="nemopay_error_registering">Application non enregistrée</string>
<string name="key">Clé d\'application</string>
<string name="key_dialog">Enregistrement de l\'appareil</string>
<string name="key_registration">Enregistrement de l\'appareil</string>
<string name="key_explication">Pour pouvoir fonctionner, l\'application a besoin d\'être enregistrée sous un nom précis et daté (date automatiquement ajoutée si non détectée). Ceci permettra par la suite de donner des droits à cet appareil</string>
<string name="key_name">Nom - Date</string>
<string name="key_description">Description</string>
<string name="key_name_required">Nom requis !</string>
<string name="key_add_explication">Il est possible d\'ajouter directement une clé d\'application si celle-ci est connue</string>
<string name="already_connected">Déjà connecté !</string>
</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