fixed app not closing bug, when started from website

This commit is contained in:
Henrik Lepson 2021-11-14 10:13:40 +02:00
parent 636beeb7f3
commit 168c9be010
13 changed files with 116 additions and 61 deletions

View File

@ -69,6 +69,7 @@ class AuthFragment : Fragment() {
goToTheStart() goToTheStart()
} }
}.start() }.start()
//binding!!.nextButton.visibility = View.INVISIBLE
binding!!.nextButton.setOnClickListener { goToNextFragment() } binding!!.nextButton.setOnClickListener { goToNextFragment() }
binding!!.cancelButton.setOnClickListener { goToTheStart() } binding!!.cancelButton.setOnClickListener { goToTheStart() }
val adapter = NfcAdapter.getDefaultAdapter(activity) val adapter = NfcAdapter.getDefaultAdapter(activity)
@ -140,8 +141,7 @@ class AuthFragment : Fragment() {
} else { } else {
if (!args.mobile) { if (!args.mobile) {
//Currently for some reason the activity is not killed entirely. Must be looked into further. //Currently for some reason the activity is not killed entirely. Must be looked into further.
requireActivity().finish() requireActivity().finishAndRemoveTask()
exitProcess(0)
} else { } else {
val resultIntent = Intent() val resultIntent = Intent()
requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent)

View File

@ -127,10 +127,14 @@ class CanFragment : Fragment() {
// TODO: Needs special handling when the app is launched with intent. Temporary solution at the moment. // TODO: Needs special handling when the app is launched with intent. Temporary solution at the moment.
if (args.saving) { if (args.saving) {
findNavController().navigate(R.id.action_canFragment_to_settingsFragment) findNavController().navigate(R.id.action_canFragment_to_settingsFragment)
} else if (args.auth) { } else if (args.auth || args.mobile) {
val resultIntent = Intent() if (args.mobile) {
requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) val resultIntent = Intent()
requireActivity().finish() requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent)
requireActivity().finish()
} else {
requireActivity().finishAndRemoveTask()
}
} else { } else {
findNavController().navigate(R.id.action_canFragment_to_homeFragment) findNavController().navigate(R.id.action_canFragment_to_homeFragment)
} }

View File

@ -60,8 +60,6 @@ class HomeFragment : Fragment() {
intentParams.setAuthUrl(requireActivity().intent.getStringExtra("authUrl")!!) intentParams.setAuthUrl(requireActivity().intent.getStringExtra("authUrl")!!)
intentParams.setOrigin(requireActivity().intent.getStringExtra("originUrl")!!) intentParams.setOrigin(requireActivity().intent.getStringExtra("originUrl")!!)
} else { //Website } else { //Website
// Currently the test website won't send the authUrl parameter
//Log.i("intentDebugging", requireActivity().intent.data.toString())
var challenge = requireActivity().intent.data!!.getQueryParameter("challenge")!! var challenge = requireActivity().intent.data!!.getQueryParameter("challenge")!!
// TODO: Since due to encoding plus gets converted to space, temporary solution is to replace it back. // TODO: Since due to encoding plus gets converted to space, temporary solution is to replace it back.
challenge = challenge.replace(" ", "+") challenge = challenge.replace(" ", "+")

View File

@ -128,10 +128,14 @@ class PinFragment : Fragment() {
private fun goToTheStart() { private fun goToTheStart() {
if (args.saving) { if (args.saving) {
findNavController().navigate(R.id.action_canFragment_to_settingsFragment) findNavController().navigate(R.id.action_canFragment_to_settingsFragment)
} else if (args.auth) { } else if (args.auth || args.mobile) {
val resultIntent = Intent() if (args.mobile) {
requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) val resultIntent = Intent()
requireActivity().finish() requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent)
requireActivity().finish()
} else {
requireActivity().finishAndRemoveTask()
}
} else { } else {
findNavController().navigate(R.id.action_canFragment_to_homeFragment) findNavController().navigate(R.id.action_canFragment_to_homeFragment)
} }

View File

@ -14,15 +14,6 @@ import com.google.gson.JsonObject
import com.koushikdutta.ion.Ion import com.koushikdutta.ion.Ion
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentResultBinding import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentResultBinding
import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
import com.tarkvaraprojekt.mobileauthapp.network.BASE_URL
import com.tarkvaraprojekt.mobileauthapp.network.TokenApi
import com.tarkvaraprojekt.mobileauthapp.network.TokenApiService
import com.tarkvaraprojekt.mobileauthapp.network.TokenItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.system.exitProcess
/** /**
* ResultFragment is used to create a JWT and to send response to the website/application * ResultFragment is used to create a JWT and to send response to the website/application
@ -48,12 +39,8 @@ class ResultFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
binding!!.resultBackButton.setOnClickListener { binding!!.resultBackButton.visibility = View.GONE
// if (args.mobile) { postToken()
// createResponse()
// }
postToken()
}
} }
/** /**
@ -64,40 +51,40 @@ class ResultFragment : Fragment() {
json.addProperty("token", paramsModel.token) json.addProperty("token", paramsModel.token)
json.addProperty("challenge", paramsModel.challenge) json.addProperty("challenge", paramsModel.challenge)
Ion.getDefault(activity).getConscryptMiddleware().enable(false) Ion.getDefault(activity).conscryptMiddleware.enable(false)
Ion.with(activity) Ion.with(activity)
.load(paramsModel.origin + paramsModel.authUrl) .load(paramsModel.origin + paramsModel.authUrl)
.setJsonObjectBody(json) .setJsonObjectBody(json)
.asJsonObject() .asJsonObject()
.setCallback { e, result -> .setCallback { e, result ->
// do stuff with the result or error // do stuff with the result or error
Log.i("Log thingy", result.toString()) if (result == null) {
// TODO: Set auth message failed and close the app
Log.i("Log thingy fail", "result was null")
if (args.mobile) {
createResponse(false)
} else {
requireActivity().finishAndRemoveTask()
}
} else {
Log.i("Log thingy success", result.toString())
if (args.mobile) {
createResponse(true, result.toString(), paramsModel.token)
} else {
requireActivity().finishAndRemoveTask()
}
}
} }
// CoroutineScope(Dispatchers.Default).launch {
// val response = TokenApi.retrofitService.postToken(jsonBody)
// Log.v("Response", response.message())
// if (response.isSuccessful) {
// //Success scenario here
// } else {
// //Failure scenario here
// if (args.mobile) {
// createResponse(false)
// } else {
// //Currently for some reason the activity is not killed entirely. Must be looked into further.
// requireActivity().finish()
// exitProcess(0)
// }
// }
// }
} }
/** /**
* Only used when the MobileAuthApp was launched by an app. Not for website use. * Only used when the MobileAuthApp was launched by an app. Not for website use.
*/ */
private fun createResponse(success: Boolean = true) { private fun createResponse(success: Boolean = true, result: String = "noResult", token: String = "noToken") {
val responseCode = if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED val responseCode = if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED
val resultIntent = Intent() val resultIntent = Intent()
resultIntent.putExtra("result", result)
resultIntent.putExtra("token", token)
requireActivity().setResult(responseCode, resultIntent) requireActivity().setResult(responseCode, resultIntent)
requireActivity().finish() requireActivity().finish()
} }

View File

@ -56,8 +56,8 @@
<string name="clear_button">FORGET</string> <string name="clear_button">FORGET</string>
<!-- string resources for ResultFragment layout--> <!-- string resources for ResultFragment layout-->
<string name="result_text">See Fragment vastutab vastuse tagastamise eest.</string> <string name="result_text">Controlling the created token</string>
<string name="result_info">Hiljem sulgeb rakendus automaatselt.</string> <string name="result_info">Wait for the app to close</string>
<!-- menu --> <!-- menu -->
<string name="menu_settings_title">Settings</string> <string name="menu_settings_title">Settings</string>

View File

@ -55,8 +55,8 @@
<string name="gender_label">SUGU</string> <string name="gender_label">SUGU</string>
<!-- string resources for ResultFragment layout--> <!-- string resources for ResultFragment layout-->
<string name="result_text">See Fragment vastutab vastuse tagastamise eest.</string> <string name="result_text">Tulemust kontrollitakse</string>
<string name="result_info">Hiljem sulgeb rakendus automaatselt.</string> <string name="result_info">Rakendus sulgeb ennast ise</string>
<!-- menu --> <!-- menu -->
<string name="menu_settings_title">Seaded</string> <string name="menu_settings_title">Seaded</string>

View File

@ -55,8 +55,8 @@
<string name="clear_button">FORGET</string> <string name="clear_button">FORGET</string>
<!-- string resources for ResultFragment layout--> <!-- string resources for ResultFragment layout-->
<string name="result_text">See Fragment vastutab vastuse tagastamise eest.</string> <string name="result_text">Controlling the created token</string>
<string name="result_info">Hiljem sulgeb rakendus automaatselt.</string> <string name="result_info">Wait for the app to close</string>
<!-- menu --> <!-- menu -->
<string name="menu_settings_title">Settings</string> <string name="menu_settings_title">Settings</string>

View File

@ -5,9 +5,11 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.View
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import com.example.testmobileapp.databinding.ActivityMainBinding import com.example.testmobileapp.databinding.ActivityMainBinding
import com.google.gson.JsonObject
import com.koushikdutta.ion.Ion import com.koushikdutta.ion.Ion
/** /**
@ -18,9 +20,11 @@ class MainActivity : AppCompatActivity() {
private lateinit var authLauncher: ActivityResultLauncher<Intent> private lateinit var authLauncher: ActivityResultLauncher<Intent>
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater) binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { response -> authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { response ->
@ -34,19 +38,21 @@ class MainActivity : AppCompatActivity() {
} }
} }
binding.loginOptionNfcButton.setOnClickListener { launchAuth() } showLogin()
//binding.loginOptionNfcButton.setOnClickListener { getData() }
binding.loginOptionNfcButton.setOnClickListener { getData() }
} }
/** /**
* Method that creates an intent to launch the MobileAuthApp * Method that creates an intent to launch the MobileAuthApp
*/ */
private fun launchAuth(challenge: String = "challenge", authUrl: String = "authUrl") { private fun launchAuth(challenge: String = "challenge", originUrl: String = "baseUrl", authUrl: String = "authUrl") {
val launchIntent = Intent() val launchIntent = Intent()
launchIntent.setClassName("com.tarkvaraprojekt.mobileauthapp", "com.tarkvaraprojekt.mobileauthapp.MainActivity") launchIntent.setClassName("com.tarkvaraprojekt.mobileauthapp", "com.tarkvaraprojekt.mobileauthapp.MainActivity")
launchIntent.putExtra("action", "auth") launchIntent.putExtra("action", "auth")
launchIntent.putExtra("challenge", challenge) launchIntent.putExtra("challenge", challenge)
launchIntent.putExtra("originUrl", originUrl)
launchIntent.putExtra("authUrl", authUrl) launchIntent.putExtra("authUrl", authUrl)
launchIntent.putExtra("mobile", true) launchIntent.putExtra("mobile", true)
authLauncher.launch(launchIntent) authLauncher.launch(launchIntent)
@ -58,8 +64,10 @@ class MainActivity : AppCompatActivity() {
*/ */
private fun getData() { private fun getData() {
// Enter the server endpoint address to here // Enter the server endpoint address to here
val baseUrl = "enter-base-url-here" //val originUrl = "enter-base-url-here"
val url = "$baseUrl/auth/challenge" val originUrl = "https-origin-url-here"
val url = "$originUrl/auth/challenge"
Ion.getDefault(this).conscryptMiddleware.enable(false)
Ion.with(applicationContext) Ion.with(applicationContext)
.load(url) .load(url)
.asJsonObject() .asJsonObject()
@ -67,10 +75,27 @@ class MainActivity : AppCompatActivity() {
try { try {
// Get data from the result and call launchAuth method // Get data from the result and call launchAuth method
val challenge = result.asJsonObject["nonce"].toString() val challenge = result.asJsonObject["nonce"].toString()
launchAuth(challenge, baseUrl) launchAuth(challenge, originUrl, "/auth/authentication")
} catch (e: Exception) { } catch (e: Exception) {
Log.i("GETrequest", "was unsuccessful") Log.i("GETrequest", "was unsuccessful")
} }
} }
} }
private fun showLogin() {
binding.loginOptions.visibility = View.VISIBLE
}
private fun showResult(resultObject: String, token: String) {
binding.loginOptions.visibility = View.GONE
binding.resultLayout.visibility = View.VISIBLE
binding.resultObject.text = resultObject
binding.resultToken.text = token
binding.buttonForget.setOnClickListener {
binding.resultObject.text = ""
binding.resultToken.text = ""
binding.resultLayout.visibility = View.GONE
binding.loginOptions.visibility = View.VISIBLE
}
}
} }

View File

@ -26,7 +26,8 @@
android:layout_margin="12dp" android:layout_margin="12dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/login_text_view" app:layout_constraintTop_toBottomOf="@id/login_text_view"
app:layout_constraintEnd_toEndOf="parent"> app:layout_constraintEnd_toEndOf="parent"
android:visibility="gone">
<TextView <TextView
android:id="@+id/choose_method_text_view" android:id="@+id/choose_method_text_view"
@ -46,4 +47,37 @@
</LinearLayout> </LinearLayout>
<LinearLayout
android:id="@+id/result_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:visibility="gone">
<TextView
android:id="@+id/result_object"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textSize="18sp"/>
<TextView
android:id="@+id/result_token"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textSize="18sp"/>
<TextView
android:id="@+id/button_forget"
android:text="@string/forget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -6,4 +6,5 @@
<string name="method_nfc">NFC auth</string> <string name="method_nfc">NFC auth</string>
<string name="auth_success">Successful response</string> <string name="auth_success">Successful response</string>
<string name="auth_failure">Response failed</string> <string name="auth_failure">Response failed</string>
<string name="forget_button">Forget</string>
</resources> </resources>

View File

@ -6,4 +6,5 @@
<string name="method_nfc">NFC auth</string> <string name="method_nfc">NFC auth</string>
<string name="auth_success">Vastus kätte saadud</string> <string name="auth_success">Vastus kätte saadud</string>
<string name="auth_failure">Vastust ei õnnestunud kätte saada</string> <string name="auth_failure">Vastust ei õnnestunud kätte saada</string>
<string name="forget_button">Unusta</string>
</resources> </resources>

View File

@ -5,4 +5,5 @@
<string name="method_nfc">NFC auth</string> <string name="method_nfc">NFC auth</string>
<string name="auth_success">Successful response</string> <string name="auth_success">Successful response</string>
<string name="auth_failure">Response failed</string> <string name="auth_failure">Response failed</string>
<string name="forget_button">Forget</string>
</resources> </resources>