Merge pull request #8 from TanelOrumaa/integrateNFC

The UI and NFC class now work together. Iteration 2 related functionality.
This commit is contained in:
Henrik Lepson 2021-10-10 13:55:19 +03:00 committed by GitHub
commit 57ed281aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 97 additions and 14 deletions

View File

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tarkvaraprojekt.mobileauthapp">
<uses-permission android:name="android.permission.NFC" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -1,15 +1,23 @@
package com.tarkvaraprojekt.mobileauthapp
import android.app.Activity
import android.content.Context
import android.nfc.NfcAdapter
import android.nfc.tech.IsoDep
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.tarkvaraprojekt.mobileauthapp.NFC.Comms
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentAuthBinding
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
import java.lang.Exception
import kotlin.concurrent.thread
/**
* Fragment that asks the user to detect the ID card with mobile NFC chip.
@ -37,25 +45,61 @@ class AuthFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
timer = object : CountDownTimer(90000, 1000) {
timer = object : CountDownTimer((timeRemaining * 1000).toLong(), 1000) {
override fun onTick(p0: Long) {
binding!!.timeCounter.text = getString(R.string.time_left, timeRemaining)
timeRemaining--
if (timeRemaining == 0) {
binding!!.timeCounter.text = getString(R.string.no_time)
} else {
binding!!.timeCounter.text = getString(R.string.time_left, timeRemaining)
}
}
override fun onFinish() {
binding!!.timeCounter.text = getString(R.string.no_time)
Thread.sleep(750)
goToTheStart()
}
}.start()
binding!!.nextButton.setOnClickListener { goToNextFragment() }
binding!!.cancelButton.setOnClickListener { goToTheStart() }
val adapter = NfcAdapter.getDefaultAdapter(activity)
if (adapter != null)
getInfoFromIdCard(adapter)
}
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))
viewModel.setUserFirstName(response[1])
viewModel.setUserLastName(response[0])
viewModel.setUserIdentificationNumber(response[2])
requireActivity().runOnUiThread{
binding!!.timeCounter.text = getString(R.string.data_read)
}
} catch (e: Exception) {
requireActivity().runOnUiThread {
binding!!.timeCounter.text = getString(R.string.no_success)
}
// Gives user some time to read the error message
Thread.sleep(1000)
goToTheStart()
} finally {
adapter.disableReaderMode(activity)
}
}
}, NfcAdapter.FLAG_READER_NFC_A, null)
}
private fun goToNextFragment() {
//Dummy data for now
viewModel.setUserFirstName("John")
viewModel.setUserLastName("Doe")
viewModel.setUserIdentificationNumber("012345678910")
timer.cancel()
findNavController().navigate(R.id.action_authFragment_to_userFragment)
}

View File

@ -1,7 +1,9 @@
package com.tarkvaraprojekt.mobileauthapp
import android.nfc.NfcAdapter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.tarkvaraprojekt.mobileauthapp.databinding.ActivityMainBinding

View File

@ -95,6 +95,10 @@ public class Comms {
keyMAC = keys[1];
}
public byte[] getAuthenticationCertificate() {
return new byte[0];
}
/**
* Calculates the message authentication code
*

View File

@ -34,6 +34,9 @@ class PinFragment : Fragment() {
binding!!.nextButton.setOnClickListener { goToNextFragment() }
binding!!.cancelButton.setOnClickListener { goToTheStart() }
// Currently PIN 1 is not required and thus this step is immediately skipped.
// In the future the UI flow will be changed in the nav_graph.
goToNextFragment()
}
private fun goToNextFragment() {
@ -44,8 +47,11 @@ class PinFragment : Fragment() {
)
findNavController().navigate(R.id.action_pinFragment_to_canFragment)
} else {
Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT)
.show()
// Currently it is not important to enter PIN1 so we will allow the user to leave this field empty
//Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT)
// .show()
viewModel.setUserPin("1234")
findNavController().navigate(R.id.action_pinFragment_to_canFragment)
}
}

View File

@ -13,8 +13,8 @@ import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
/**
* Fragment that is used to display the persons name and national identification number.
* Currently needed in order to test that the app is working because the results at the moment
* are not sent to some other website or app.
* Currently needed in order to test that the app is working and information is read
* from the ID card via NFC.
*/
class UserFragment : Fragment() {

View File

@ -0,0 +1,22 @@
package com.tarkvaraprojekt.mobileauthapp.auth
import android.nfc.tech.IsoDep
import com.tarkvaraprojekt.mobileauthapp.NFC.Comms
import java.math.BigInteger
class Authenticator(val comms : Comms) {
public fun authenticate(nonce: BigInteger, challengeUrl: String, pin1: String) {
// Ask PIN 1 from the user and get the authentication certificate from the ID card.
val authenticationCertificate : ByteArray = comms.getAuthenticationCertificate();
// Create the authentication token (OpenID X509)
// Hash the authentication token.
// Send the authentication token hash to the ID card for signing and get signed authentication token as response.
// Return the signed authentication token.
}
}

View File

@ -10,18 +10,21 @@
<string name="pin_fragment">Palun sisesta PIN 1</string>
<string name="enter_pin">PIN 1</string>
<string name="example_pin">Näide. 1234</string>
<string name="length_pin">PIN1 lubatud pikkus on 4-12</string>
<string name="length_pin">PIN 1 lubatud pikkus on 4..12</string>
<!-- string resources for CanFragment -->
<string name="example_can">Näide. 123456</string>
<string name="text_can">CAN</string>
<string name="enter_can">Sisesta ID kaardi CAN (Card Access Number)</string>
<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 resources for AuthFragment layout -->
<string name="auth_instruction_text">ID kaardiga ühenduse loomiseks pane kaart vastu telefoni</string>
<string name="time_left">Aega on jäänud %d sek</string>
<string name="no_time">Aeg on otsas</string>
<string name="no_success">Vale CAN</string>
<!-- string resources for UserFragment layout -->
<string name="user_name_label">NIMI</string>