From 8637a4182afa292e2e0733ef92d4310c5bd8e21e Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Fri, 15 Oct 2021 22:12:06 +0300 Subject: [PATCH] MOB-39 can can be now stored on the phone --- MobileAuthApp/app/build.gradle | 3 ++ .../mobileauthapp/CanFragment.kt | 31 +++++++++++++++- .../mobileauthapp/HomeFragment.kt | 3 +- .../mobileauthapp/menu/SettingsFragment.kt | 2 +- .../mobileauthapp/model/SmartCardViewModel.kt | 36 +++++++++++++++++++ .../app/src/main/res/navigation/nav_graph.xml | 4 ++- .../app/src/main/res/values-en/strings.xml | 3 ++ .../app/src/main/res/values-et/strings.xml | 2 ++ .../app/src/main/res/values/strings.xml | 3 ++ 9 files changed, 83 insertions(+), 4 deletions(-) diff --git a/MobileAuthApp/app/build.gradle b/MobileAuthApp/app/build.gradle index 6f6625a..5cd7f6a 100644 --- a/MobileAuthApp/app/build.gradle +++ b/MobileAuthApp/app/build.gradle @@ -59,4 +59,7 @@ dependencies { //For cryptography implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.69' + //SecureDataStoring + implementation("androidx.security:security-crypto:1.0.0") + } \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt index cd971c6..19179f1 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt @@ -1,5 +1,7 @@ package com.tarkvaraprojekt.mobileauthapp +import android.app.AlertDialog +import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -35,6 +37,9 @@ class CanFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + if (viewModel.userCan.length == 6) { + skip() + } if (args.saving) { binding!!.nextButton.text = getString(R.string.save_text) } @@ -42,6 +47,12 @@ class CanFragment : Fragment() { binding!!.cancelButton.setOnClickListener { goToTheStart() } } + // If CAN is already set + private fun skip() { + findNavController().navigate(R.id.action_canFragment_to_authFragment) + } + + // Might need some rework, must break it up and make logic better. private fun goToNextFragment() { val enteredCan = binding!!.canEditText.editText?.text.toString() if (enteredCan.length != 6) { @@ -52,9 +63,27 @@ class CanFragment : Fragment() { binding!!.canEditText.editText?.text.toString() ) if (args.saving) { + viewModel.storeCan(requireContext()) findNavController().navigate(R.id.action_canFragment_to_settingsFragment) } else { - findNavController().navigate(R.id.action_canFragment_to_authFragment) + val canStoreQuestion: AlertDialog? = activity?.let { frag -> + val builder = AlertDialog.Builder(frag) + builder.apply { + setPositiveButton(R.string.save_text) { _, _ -> + viewModel.storeCan( + requireContext() + ) + findNavController().navigate(R.id.action_canFragment_to_authFragment) + } + setNegativeButton(R.string.deny_text) { _, _ -> + findNavController().navigate(R.id.action_canFragment_to_authFragment) + } + } + builder.setMessage(R.string.can_save_request) + builder.setTitle(R.string.save_can_title) + builder.create() + } + canStoreQuestion?.show() } } } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt index bdfaa69..600fdd8 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -1,6 +1,7 @@ package com.tarkvaraprojekt.mobileauthapp import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -27,7 +28,7 @@ class HomeFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - + viewModel.checkCan(requireContext()) binding!!.beginButton.setOnClickListener { goToNextFragment() } } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt index 88f4642..a488cb6 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/menu/SettingsFragment.kt @@ -43,7 +43,7 @@ class SettingsFragment : Fragment() { findNavController().navigate(action) } else { // If can in ViewModel is 6 we know that we can only delete it. - viewModel.setUserCan("") + viewModel.deleteCan(requireContext()) binding!!.canSaved.text = getString(R.string.saved_can, "puudub") binding!!.canMenuAction.text = getString(R.string.can_add) } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt index 4674831..9bfa178 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/model/SmartCardViewModel.kt @@ -1,6 +1,10 @@ package com.tarkvaraprojekt.mobileauthapp.model +import android.content.Context +import android.content.SharedPreferences import androidx.lifecycle.ViewModel +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys class SmartCardViewModel: ViewModel() { @@ -47,4 +51,36 @@ class SmartCardViewModel: ViewModel() { _userIdentificationNumber = newUserIdentificationNumber } + + private fun getSharedPreferences(context: Context): SharedPreferences { + val masterKeyAlias: String = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) + return EncryptedSharedPreferences.create( + "user_creds", + masterKeyAlias, + context, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + } + + fun storeCan(context: Context) { + val sharedPreferences: SharedPreferences = getSharedPreferences(context) + sharedPreferences.edit().putString("CAN", userCan).apply() + } + + fun checkCan(context: Context) { + val sharedPreferences: SharedPreferences = getSharedPreferences(context) + val foundCan = sharedPreferences.getString("CAN", null) + foundCan?.let { + _userCan = it + } + } + + // Must be called from AuthFragment as well, when CAN is wrong. + fun deleteCan(context: Context) { + val sharedPreferences: SharedPreferences = getSharedPreferences(context) + sharedPreferences.edit().remove("CAN").apply() + _userCan = "" + } + } \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/res/navigation/nav_graph.xml b/MobileAuthApp/app/src/main/res/navigation/nav_graph.xml index bf8d07b..d01406f 100644 --- a/MobileAuthApp/app/src/main/res/navigation/nav_graph.xml +++ b/MobileAuthApp/app/src/main/res/navigation/nav_graph.xml @@ -49,7 +49,9 @@ app:popUpToInclusive="true" /> + app:destination="@id/settingsFragment" + app:popUpTo="@id/settingsFragment" + app:popUpToInclusive="true"/> EDASI KATKESTA SALVESTA + EI Palun sisesta PIN 1 @@ -22,6 +23,8 @@ CANi pikkus on vale Kaart on tuvastatud. Hoia kaarti vastu telefoni. Andmed loetud. Võid edasi minna. + Praegu ei ole rakenduses CANi salvestatud. Kas sa soovid sisestatud CANi salvestada? Sellisel juhul sisestatakse see järgmisel korral automaatselt. Salvestatud CANi saab alati menüüs muuta ja kustutada. + Salvesta CAN ID kaardiga ühenduse loomiseks pane kaart vastu telefoni diff --git a/MobileAuthApp/app/src/main/res/values-et/strings.xml b/MobileAuthApp/app/src/main/res/values-et/strings.xml index 351a9bf..5e9226e 100644 --- a/MobileAuthApp/app/src/main/res/values-et/strings.xml +++ b/MobileAuthApp/app/src/main/res/values-et/strings.xml @@ -7,6 +7,7 @@ EDASI KATKESTA SALVESTA + EI Palun sisesta PIN 1 @@ -21,6 +22,7 @@ CANi pikkus on vale Kaart on tuvastatud. Hoia kaarti vastu telefoni. Andmed loetud. Võid edasi minna. + Praegu ei ole rakenduses CANi salvestatud. Kas sa soovid sisestatud CANi salvestada? Sellisel juhul sisestatakse see järgmisel korral automaatselt. Salvestatud CANi saab alati menüüs muuta ja kustutada. Salvesta CAN ID kaardiga ühenduse loomiseks pane kaart vastu telefoni diff --git a/MobileAuthApp/app/src/main/res/values/strings.xml b/MobileAuthApp/app/src/main/res/values/strings.xml index 92302a2..e3cb6f2 100644 --- a/MobileAuthApp/app/src/main/res/values/strings.xml +++ b/MobileAuthApp/app/src/main/res/values/strings.xml @@ -6,6 +6,7 @@ EDASI KATKESTA SALVESTA + EI Palun sisesta PIN 1 @@ -20,6 +21,8 @@ CANi pikkus on vale Kaart on tuvastatud. Hoia kaarti vastu telefoni. Andmed loetud. Võid edasi minna. + Praegu ei ole rakenduses CANi salvestatud. Kas sa soovid sisestatud CANi salvestada? Sellisel juhul sisestatakse see järgmisel korral automaatselt. Salvestatud CANi saab alati menüüs muuta ja kustutada. + Salvesta CAN ID kaardiga ühenduse loomiseks pane kaart vastu telefoni