MOB-39 can can be now stored on the phone

This commit is contained in:
Henrik Lepson 2021-10-15 22:12:06 +03:00
parent 141dfb18db
commit 8637a4182a
9 changed files with 83 additions and 4 deletions

View File

@ -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")
}

View File

@ -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,10 +63,28 @@ class CanFragment : Fragment() {
binding!!.canEditText.editText?.text.toString()
)
if (args.saving) {
viewModel.storeCan(requireContext())
findNavController().navigate(R.id.action_canFragment_to_settingsFragment)
} else {
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()
}
}
}

View File

@ -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() }
}

View File

@ -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)
}

View File

@ -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 = ""
}
}

View File

@ -49,7 +49,9 @@
app:popUpToInclusive="true" />
<action
android:id="@+id/action_canFragment_to_settingsFragment"
app:destination="@id/settingsFragment" />
app:destination="@id/settingsFragment"
app:popUpTo="@id/settingsFragment"
app:popUpToInclusive="true"/>
<argument
android:name="saving"
app:argType="boolean"

View File

@ -8,6 +8,7 @@
<string name="next_text">EDASI</string>
<string name="cancel_text">KATKESTA</string>
<string name="save_text">SALVESTA</string>
<string name="deny_text">EI</string>
<!-- string resources for PinFragment -->
<string name="pin_fragment">Palun sisesta PIN 1</string>
@ -22,6 +23,8 @@
<string name="length_can">CANi pikkus on vale</string>
<string name="card_detected">Kaart on tuvastatud. Hoia kaarti vastu telefoni.</string>
<string name="data_read">Andmed loetud. Võid edasi minna.</string>
<string name="can_save_request">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.</string>
<string name="save_can_title">Salvesta CAN</string>
<!-- string resources for AuthFragment layout -->
<string name="auth_instruction_text">ID kaardiga ühenduse loomiseks pane kaart vastu telefoni</string>

View File

@ -7,6 +7,7 @@
<string name="next_text">EDASI</string>
<string name="cancel_text">KATKESTA</string>
<string name="save_text">SALVESTA</string>
<string name="deny_text">EI</string>
<!-- string resources for PinFragment -->
<string name="pin_fragment">Palun sisesta PIN 1</string>
@ -21,6 +22,7 @@
<string name="length_can">CANi pikkus on vale</string>
<string name="card_detected">Kaart on tuvastatud. Hoia kaarti vastu telefoni.</string>
<string name="data_read">Andmed loetud. Võid edasi minna.</string>
<string name="can_save_request">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.</string> <string name="save_can_title">Salvesta CAN</string>
<!-- string resources for AuthFragment layout -->
<string name="auth_instruction_text">ID kaardiga ühenduse loomiseks pane kaart vastu telefoni</string>

View File

@ -6,6 +6,7 @@
<string name="next_text">EDASI</string>
<string name="cancel_text">KATKESTA</string>
<string name="save_text">SALVESTA</string>
<string name="deny_text">EI</string>
<!-- string resources for PinFragment -->
<string name="pin_fragment">Palun sisesta PIN 1</string>
@ -20,6 +21,8 @@
<string name="length_can">CANi pikkus on vale</string>
<string name="card_detected">Kaart on tuvastatud. Hoia kaarti vastu telefoni.</string>
<string name="data_read">Andmed loetud. Võid edasi minna.</string>
<string name="can_save_request">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.</string>
<string name="save_can_title">Salvesta CAN</string>
<!-- string resources for AuthFragment layout -->
<string name="auth_instruction_text">ID kaardiga ühenduse loomiseks pane kaart vastu telefoni</string>