Commit 67c09a8e authored by Nastuzzi Samy's avatar Nastuzzi Samy
Browse files

Key Management

Store key in SharedPreferences
Auto authentification
Can create one key
Registered verification
parent eb954b0f
......@@ -4,18 +4,19 @@ 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;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
......@@ -28,12 +29,12 @@ import fr.utc.simde.payutc.tools.NemopaySession;
public class MainActivity extends NFCActivity {
private static final String LOG_TAG = "_MainActivity";
private static final String service = "http://assos.utc.fr";
private static Boolean registered = false;
private static final String service = "https://assos.utc.fr";
private static Dialog dialog;
private static NemopaySession nemopaySession;
private static CASConnexion casConnexion;
private static SharedPreferences sharedPreferences;
private static TextView AppConfigText;
private static TextView AppRegisteredText;
......@@ -47,6 +48,16 @@ public class MainActivity extends NFCActivity {
dialog = new Dialog(MainActivity.this);
nemopaySession = new NemopaySession();
casConnexion = new CASConnexion(nemopaySession);
sharedPreferences = getSharedPreferences("payutc", Activity.MODE_PRIVATE);
/*
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("key", "");
edit.remove("key");
edit.apply();
*/
final String key = sharedPreferences.getString("key", "");
if (!key.equals(""))
setKey(key);
AppConfigText = findViewById(R.id.text_app_config);
AppRegisteredText = findViewById(R.id.text_app_registered);
......@@ -65,9 +76,43 @@ public class MainActivity extends NFCActivity {
badgeDialog(idBadge);
}
protected void setRegistered(boolean registered) {
this.registered = registered;
AppRegisteredText.setText(registered ? R.string.app_registred : R.string.app_not_registred);
public void setKey(final String key) {
if (nemopaySession.isRegistered()) {
dialog.errorDialog(getResources().getString(R.string.nemopay_connection), getResources().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);
loading.setCancelable(false);
new Thread() {
@Override
public void run() {
try {
nemopaySession.loginApp(key);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (nemopaySession.isRegistered()) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", key);
editor.apply();
((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));
}
});
}
}.start();
}
protected void connectWithCAS(final String username, final String password) throws InterruptedException {
......@@ -148,7 +193,7 @@ public class MainActivity extends NFCActivity {
protected void connectWithBadge(final String idBadge, final String pin) {
dialog.dismiss();
if (registered) {
if (nemopaySession.isRegistered()) {
final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this, "Connexion ...", "A faire ...", true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
......@@ -157,6 +202,7 @@ public class MainActivity extends NFCActivity {
try {
Thread.sleep(2000);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
ringProgressDialog.dismiss();
}
......@@ -165,7 +211,7 @@ public class MainActivity extends NFCActivity {
}
protected void badgeDialog(final String idBadge) {
if (!registered) {
if (!nemopaySession.isRegistered()) {
dialog.errorDialog(getResources().getString(R.string.badge_connection), getResources().getString(R.string.badge_app_not_registered));
return;
}
......@@ -179,22 +225,22 @@ public class MainActivity extends NFCActivity {
.setView(pinView)
.setCancelable(true)
.setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
public void onClick(DialogInterface dialogInterface, int id) {
if (pinInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.pin_required, Toast.LENGTH_SHORT).show();
dialog.cancel();
dialogInterface.cancel();
badgeDialog(idBadge);
}
else {
connectWithBadge(idBadge, pinInput.getText().toString());
dialog.cancel();
dialogInterface.cancel();
}
}
})
.setNeutralButton(R.string.no_pin, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
public void onClick(DialogInterface dialogInterface, int id) {
connectWithBadge(idBadge, "0000");
dialog.cancel();
dialogInterface.cancel();
}
});
......@@ -214,28 +260,28 @@ public class MainActivity extends NFCActivity {
.setView(usernameView)
.setCancelable(false)
.setPositiveButton(R.string.connexion, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
public void onClick(DialogInterface dialogInterface, int id) {
if (usernameInput.getText().toString().equals("") || passwordInput.getText().toString().equals("")) {
if (!usernameInput.getText().toString().equals(""))
casConnexion.setUsername(usernameInput.getText().toString());
Toast.makeText(MainActivity.this, R.string.username_and_password_required, Toast.LENGTH_SHORT).show();
dialog.cancel();
dialogInterface.cancel();
connectDialog();
}
else {
try {
connectWithCAS(usernameInput.getText().toString(), passwordInput.getText().toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
dialog.cancel();
dialogInterface.cancel();
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
public void onClick(DialogInterface dialogInterface, int id) {
dialogInterface.cancel();
}
});
......@@ -243,33 +289,57 @@ public class MainActivity extends NFCActivity {
}
protected void keyDialog() {
final View usernameView = getLayoutInflater().inflate(R.layout.dialog_key, null);
final EditText keyInput = usernameView.findViewById(R.id.input_key);
final View keyView = getLayoutInflater().inflate(R.layout.dialog_key, null);
final EditText nameInput = keyView.findViewById(R.id.input_name);
final EditText descriptionInput = keyView.findViewById(R.id.input_description);
final String date = new SimpleDateFormat("yyyy/MM/dd", Locale.FRANCE).format(new Date());
keyInput.setText("Téléphone de " + casConnexion.getUsername() + " - " + date);
nameInput.setText("Téléphone de " + casConnexion.getUsername() + " - " + date);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setTitle(R.string.key_dialog)
.setView(usernameView)
.setView(keyView)
.setCancelable(false)
.setPositiveButton(R.string.register, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (keyInput.getText().toString().equals("")) {
public void onClick(DialogInterface dialogInterface, int id) {
if (nameInput.getText().toString().equals("")) {
Toast.makeText(MainActivity.this, R.string.key_name_required, Toast.LENGTH_SHORT).show();
dialog.cancel();
dialogInterface.cancel();
keyDialog();
}
else {
/* AddKey */
Log.d(LOG_TAG, "name: " + keyInput.getText().toString() + (keyInput.getText().toString().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? "" : " - " + date));
dialog.cancel();
keyDialog();
dialogInterface.cancel();
final ProgressDialog loading = ProgressDialog.show(MainActivity.this, getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_registering), true);
loading.setCancelable(false);
new Thread() {
@Override
public void run() {
try {
nemopaySession.registerApp(nameInput.getText().toString() + (nameInput.getText().toString().matches("^.* - ([0-9]{4})([/-])([0-9]{2})\\2([0-9]{2})$") ? "" : " - " + date), descriptionInput.getText().toString(), service);
Thread.sleep(100);
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
loading.dismiss();
if (nemopaySession.getKey().isEmpty())
dialog.errorDialog(getResources().getString(R.string.nemopay_connection), getResources().getString(R.string.nemopay_error_registering));
else
setKey(nemopaySession.getKey());
}
});
}
}.start();
}
}
});
dialog.createDialog(alertDialogBuilder, keyInput);
dialog.createDialog(alertDialogBuilder, nameInput);
}
}
......@@ -29,7 +29,7 @@ public class HTTPRequest {
private Map<String, String> postArgs;
private Map<String, String> getArgs;
private static Map<String, String> cookies;
private Map<String, String> cookies;
public HTTPRequest(final String url) {
this.url = url;
......@@ -78,6 +78,7 @@ public class HTTPRequest {
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);
......@@ -161,6 +162,14 @@ public class HTTPRequest {
this.postArgs.put(key, value);
}
public Map<String, String> getCookies() {
return this.cookies;
}
public void setCookies(Map<String, String> cookies) {
this.cookies = cookies;
}
synchronized String getCookiesHeader() {
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;
......@@ -15,6 +18,9 @@ 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.
*/
......@@ -22,27 +28,65 @@ import java.util.Objects;
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;
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 = "";
this.key = "";
}
public Boolean isConnected() { return !this.session.isEmpty() && !this.username.isEmpty(); }
public Boolean isRegistered() { return !this.key.isEmpty() && !this.session.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 HTTPRequest getCasUrl() throws IOException {
return construct("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>() {{
put("app_url", service);
put("app_name", name);
put("app_desc", description);
}});
if (request.getResponseCode() == 200)
this.key = request.getJsonResponse().get("app_key");
return request;
}
public HTTPRequest loginApp(final String key) throws Exception {
HTTPRequest request = construct("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");
return request;
}
public HTTPRequest loginCas(final String ticket, final String service) throws Exception {
HTTPRequest request = construct("POSS3", "loginCas2", new HashMap<String, String>() {{
put("ticket", ticket);
......@@ -67,8 +111,10 @@ public class NemopaySession {
Log.d(LOG_TAG, "url: " + url + method + "/" + service);
request.setGet(getArgs);
request.setPost(postArgs);
request.setCookies(this.cookies);
request.post();
this.cookies = request.getCookies();
return request;
}
}
......@@ -14,11 +14,20 @@
android:layout_marginBottom="10dp" />
<EditText
android:id="@+id/input_key"
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:inputType="textPersonName"
android:hint="@string/key_name" />
<EditText
android:id="@+id/input_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:inputType="textCapSentences|textAutoCorrect"
android:hint="@string/key_description" />
</LinearLayout>
\ No newline at end of file
......@@ -36,8 +36,13 @@
<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="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_dialog">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>
</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