From eca3f924681d8d1d01bf528f787360517b4601a7 Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Sun, 7 Nov 2021 13:49:19 +0200 Subject: [PATCH] MOB-41 small changes to the test app --- .../mobileauthapp/AuthFragment.kt | 70 ++++++++++--------- .../mobileauthapp/ResultFragment.kt | 4 +- TestMobileApp/app/build.gradle | 2 +- .../com/example/testmobileapp/MainActivity.kt | 31 +++++++- .../app/src/main/res/values-en/strings.xml | 9 +++ .../app/src/main/res/values-et/strings.xml | 9 +++ .../app/src/main/res/values/strings.xml | 8 +-- 7 files changed, 93 insertions(+), 40 deletions(-) create mode 100644 TestMobileApp/app/src/main/res/values-en/strings.xml create mode 100644 TestMobileApp/app/src/main/res/values-et/strings.xml diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt index 7e6b4aa..cb97a5a 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/AuthFragment.kt @@ -70,40 +70,44 @@ class AuthFragment : Fragment() { } private fun getInfoFromIdCard(adapter: NfcAdapter) { - adapter.enableReaderMode(activity, { tag -> - timer.cancel() - requireActivity().runOnUiThread { - binding!!.timeCounter.text = getString(R.string.card_detected) - } - val card = IsoDep.get(tag) - card.timeout = 32768 - card.use { - try { - val comms = Comms(it, viewModel.userCan) - val response = comms.readPersonalData(byteArrayOf(1, 2, 6, 3, 4, 8)) - viewModel.setUserFirstName(response[1]) - viewModel.setUserLastName(response[0]) - viewModel.setUserIdentificationNumber(response[2]) - viewModel.setGender(response[3]) - viewModel.setCitizenship(response[4]) - viewModel.setExpiration(response[5]) - requireActivity().runOnUiThread{ - binding!!.timeCounter.text = getString(R.string.data_read) - } - } catch (e: Exception) { - requireActivity().runOnUiThread { - binding!!.timeCounter.text = getString(R.string.no_success) - } - // If the CAN is wrong we will also delete the saved CAN so that the user won't use it again. - viewModel.deleteCan(requireContext()) - // Gives user some time to read the error message - Thread.sleep(1000) - goToTheStart() - } finally { - adapter.disableReaderMode(activity) + if (args.reading) { + adapter.enableReaderMode(activity, { tag -> + timer.cancel() + requireActivity().runOnUiThread { + binding!!.timeCounter.text = getString(R.string.card_detected) } - } - }, NfcAdapter.FLAG_READER_NFC_A, null) + val card = IsoDep.get(tag) + card.timeout = 32768 + card.use { + try { + val comms = Comms(it, viewModel.userCan) + val response = comms.readPersonalData(byteArrayOf(1, 2, 6, 3, 4, 8)) + viewModel.setUserFirstName(response[1]) + viewModel.setUserLastName(response[0]) + viewModel.setUserIdentificationNumber(response[2]) + viewModel.setGender(response[3]) + viewModel.setCitizenship(response[4]) + viewModel.setExpiration(response[5]) + requireActivity().runOnUiThread { + binding!!.timeCounter.text = getString(R.string.data_read) + } + } catch (e: Exception) { + requireActivity().runOnUiThread { + binding!!.timeCounter.text = getString(R.string.no_success) + } + // If the CAN is wrong we will also delete the saved CAN so that the user won't use it again. + viewModel.deleteCan(requireContext()) + // Gives user some time to read the error message + Thread.sleep(1000) + goToTheStart() + } finally { + adapter.disableReaderMode(activity) + } + } + }, NfcAdapter.FLAG_READER_NFC_A, null) + } else { //We want to create a JWT instead of reading the info from the card. + goToNextFragment() + } } private fun goToNextFragment() { diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt index 5c45db8..9d01c8f 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt @@ -38,7 +38,9 @@ class ResultFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding!!.resultBackButton.setOnClickListener { - createResponse() + if (!args.mobile) { + createResponse() + } } } diff --git a/TestMobileApp/app/build.gradle b/TestMobileApp/app/build.gradle index dcc7861..4645a41 100644 --- a/TestMobileApp/app/build.gradle +++ b/TestMobileApp/app/build.gradle @@ -35,7 +35,7 @@ android { } dependencies { - + implementation 'com.koushikdutta.ion:ion:3.1.0' implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' diff --git a/TestMobileApp/app/src/main/java/com/example/testmobileapp/MainActivity.kt b/TestMobileApp/app/src/main/java/com/example/testmobileapp/MainActivity.kt index 7e9532a..8d9e9e0 100644 --- a/TestMobileApp/app/src/main/java/com/example/testmobileapp/MainActivity.kt +++ b/TestMobileApp/app/src/main/java/com/example/testmobileapp/MainActivity.kt @@ -4,10 +4,16 @@ import android.app.Activity import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import android.util.Log import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts import com.example.testmobileapp.databinding.ActivityMainBinding +import com.koushikdutta.ion.Ion +/** + * Test mobile app to demonstrate how other applications can use MobileAuthApp. + * Single purpose app that launches the MobileAuthApp and gets the response back (JWT). + */ class MainActivity : AppCompatActivity() { private lateinit var authLauncher: ActivityResultLauncher @@ -32,10 +38,33 @@ class MainActivity : AppCompatActivity() { } - private fun launchAuth() { + /** + * Method that creates an intent to launch the MobileAuthApp + */ + private fun launchAuth(arg: String = "nothing") { val launchIntent = Intent() launchIntent.setClassName("com.tarkvaraprojekt.mobileauthapp", "com.tarkvaraprojekt.mobileauthapp.MainActivity") launchIntent.putExtra("auth", true) + launchIntent.putExtra("nonce", arg) // Currently nothing authLauncher.launch(launchIntent) } + + /** + * Method for retrieving data from an endpoint. + * Ion library is used as it is very convenient for making simple GET requests. + */ + private fun getData() { + val url = "real-address-here" + Ion.with(applicationContext) + .load(url) + .asJsonObject() + .setCallback { _, result -> + try { + // Get data from the result and call launchAuth method + launchAuth() + } catch (e: Exception) { + Log.i("GETrequest", "was unsuccessful") + } + } + } } \ No newline at end of file diff --git a/TestMobileApp/app/src/main/res/values-en/strings.xml b/TestMobileApp/app/src/main/res/values-en/strings.xml new file mode 100644 index 0000000..86688c5 --- /dev/null +++ b/TestMobileApp/app/src/main/res/values-en/strings.xml @@ -0,0 +1,9 @@ + + + TestMobileApp + Login + Choose login method + NFC auth + Successful response + Response failed + \ No newline at end of file diff --git a/TestMobileApp/app/src/main/res/values-et/strings.xml b/TestMobileApp/app/src/main/res/values-et/strings.xml new file mode 100644 index 0000000..0daf8b4 --- /dev/null +++ b/TestMobileApp/app/src/main/res/values-et/strings.xml @@ -0,0 +1,9 @@ + + + TestMobileApp + Logi sisse + Vali sobiv meetod + NFC auth + Vastus kätte saadud + Vastust ei õnnestunud kätte saada + \ No newline at end of file diff --git a/TestMobileApp/app/src/main/res/values/strings.xml b/TestMobileApp/app/src/main/res/values/strings.xml index 81cd201..2353f0b 100644 --- a/TestMobileApp/app/src/main/res/values/strings.xml +++ b/TestMobileApp/app/src/main/res/values/strings.xml @@ -1,8 +1,8 @@ TestMobileApp - Logi sisse - Vali sobiv meetod + Login + Choose login method NFC auth - Autentimine õnnestus - Autentimine ebaõnnestus + Successful response + Response failed \ No newline at end of file