MOB-40 Changed header type from string to map and now will be used in requests

This commit is contained in:
TanelOrumaa 2021-12-12 20:05:18 +02:00
parent d92656d982
commit 13a0a9430f
4 changed files with 86 additions and 38 deletions

View File

@ -25,6 +25,7 @@ import com.tarkvaraprojekt.mobileauthapp.NFC.Comms
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentHomeBinding import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentHomeBinding
import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
import org.json.JSONObject
import java.lang.Exception import java.lang.Exception
import java.lang.RuntimeException import java.lang.RuntimeException
import java.net.URL import java.net.URL
@ -91,7 +92,8 @@ class HomeFragment : Fragment() {
*/ */
private fun goToTheNextFragment(mobile: Boolean = false) { private fun goToTheNextFragment(mobile: Boolean = false) {
(activity as MainActivity).menuAvailable = false (activity as MainActivity).menuAvailable = false
val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(auth = true, mobile = mobile) val action =
HomeFragmentDirections.actionHomeFragmentToCanFragment(auth = true, mobile = mobile)
findNavController().navigate(action) findNavController().navigate(action)
} }
@ -117,28 +119,39 @@ class HomeFragment : Fragment() {
intentParams.setAuthUrl(requireActivity().intent.data!!.getQueryParameter("authUrl")!!) intentParams.setAuthUrl(requireActivity().intent.data!!.getQueryParameter("authUrl")!!)
intentParams.setOrigin(requireActivity().intent.data!!.getQueryParameter("originUrl")!!) intentParams.setOrigin(requireActivity().intent.data!!.getQueryParameter("originUrl")!!)
*/ */
var getAuthChallengeUrl = requireActivity().intent.data!!.getQueryParameter("getAuthChallengeUrl")!! var getAuthChallengeUrl =
getAuthChallengeUrl = getAuthChallengeUrl.substring(1, getAuthChallengeUrl.length - 1) requireActivity().intent.data!!.getQueryParameter("getAuthChallengeUrl")!!
var postAuthTokenUrl = requireActivity().intent.data!!.getQueryParameter("postAuthTokenUrl")!! getAuthChallengeUrl =
getAuthChallengeUrl.substring(1, getAuthChallengeUrl.length - 1)
var postAuthTokenUrl =
requireActivity().intent.data!!.getQueryParameter("postAuthTokenUrl")!!
postAuthTokenUrl = postAuthTokenUrl.substring(1, postAuthTokenUrl.length - 1) postAuthTokenUrl = postAuthTokenUrl.substring(1, postAuthTokenUrl.length - 1)
val headers = requireActivity().intent.data!!.getQueryParameter("headers")!! val headers =
getHeaders(requireActivity().intent.data!!.getQueryParameter("headers")!!)
intentParams.setAuthUrl(postAuthTokenUrl) intentParams.setAuthUrl(postAuthTokenUrl)
val address = "https://" + URL(getAuthChallengeUrl).host val address = "https://" + URL(getAuthChallengeUrl).host
intentParams.setOrigin(address) intentParams.setOrigin(address)
Log.w("ORIGIN", intentParams.origin)
intentParams.setHeaders(headers) intentParams.setHeaders(headers)
Ion.getDefault(activity).conscryptMiddleware.enable(false) Ion.getDefault(activity).conscryptMiddleware.enable(false)
Ion.with(activity) val ion = Ion.with(activity)
.load(getAuthChallengeUrl) .load(getAuthChallengeUrl)
// Set headers.
for ((header, value) in intentParams.headers) {
ion.setHeader(header, value)
}
ion
.asJsonObject() .asJsonObject()
.setCallback { _, result -> .setCallback { _, result ->
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().replace("\"", "") val challenge =
result.asJsonObject["nonce"].toString().replace("\"", "")
intentParams.setChallenge(challenge) intentParams.setChallenge(challenge)
goToTheNextFragment(mobile) goToTheNextFragment(mobile)
} catch (e: Exception) { } catch (e: Exception) {
Log.i("GETrequest", "was unsuccessful") Log.i("GETrequest", "was unsuccessful" + e.message)
throw RuntimeException() throw RuntimeException()
} }
} }
@ -159,7 +172,7 @@ class HomeFragment : Fragment() {
} else { } else {
message.setMessage(getString(R.string.problem_other)) message.setMessage(getString(R.string.problem_other))
} }
message.setPositiveButton(getString(R.string.continue_button)) {_, _ -> message.setPositiveButton(getString(R.string.continue_button)) { _, _ ->
val resultIntent = Intent() val resultIntent = Intent()
requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent)
requireActivity().finish() requireActivity().finish()
@ -196,6 +209,17 @@ class HomeFragment : Fragment() {
} }
} }
private fun getHeaders(headersString: String): Map<String, String> {
val headers = HashMap<String, String>()
val headersStringFormatted = headersString.substring(1, headersString.length - 1)
val headersJsonObject = JSONObject(headersStringFormatted)
for (name in headersJsonObject.keys()) {
headers[name] = headersJsonObject[name].toString()
}
return headers
}
/** /**
* Displays texts that inform the user whether the CAN and PIN 1 are saved on the device or not. * Displays texts that inform the user whether the CAN and PIN 1 are saved on the device or not.
* This might help the user to save some time as checking menu is not necessary unless the user * This might help the user to save some time as checking menu is not necessary unless the user
@ -222,7 +246,7 @@ class HomeFragment : Fragment() {
val dialog = MaterialAlertDialogBuilder(requireContext()) val dialog = MaterialAlertDialogBuilder(requireContext())
.setTitle(title) .setTitle(title)
.setMessage(message) .setMessage(message)
.setPositiveButton(R.string.return_text){_, _ -> } .setPositiveButton(R.string.return_text) { _, _ -> }
.show() .show()
val title = dialog.findViewById<TextView>(R.id.alertTitle) val title = dialog.findViewById<TextView>(R.id.alertTitle)
title?.textSize = 24F title?.textSize = 24F
@ -242,11 +266,17 @@ class HomeFragment : Fragment() {
binding.detectionActionText.text = getString(R.string.action_detect_unavailable) binding.detectionActionText.text = getString(R.string.action_detect_unavailable)
binding.homeActionButton.text = getString(R.string.add_can_text) binding.homeActionButton.text = getString(R.string.add_can_text)
binding.homeActionButton.setOnClickListener { binding.homeActionButton.setOnClickListener {
val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(saving = true, fromhome = true) val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(
saving = true,
fromhome = true
)
findNavController().navigate(action) findNavController().navigate(action)
} }
binding.homeHelpButton.setOnClickListener { binding.homeHelpButton.setOnClickListener {
displayMessage(getString(R.string.can_question), getString(R.string.can_explanation)) displayMessage(
getString(R.string.can_question),
getString(R.string.can_explanation)
)
} }
binding.homeActionButton.visibility = View.VISIBLE binding.homeActionButton.visibility = View.VISIBLE
binding.homeHelpButton.visibility = View.VISIBLE binding.homeHelpButton.visibility = View.VISIBLE
@ -293,13 +323,15 @@ class HomeFragment : Fragment() {
findNavController().navigate(action) findNavController().navigate(action)
} }
} catch (e: Exception) { } catch (e: Exception) {
when(e) { when (e) {
is TagLostException -> requireActivity().runOnUiThread { is TagLostException -> requireActivity().runOnUiThread {
binding.detectionActionText.text = getString(R.string.id_card_removed_early) binding.detectionActionText.text =
getString(R.string.id_card_removed_early)
reset() reset()
} }
else -> requireActivity().runOnUiThread { else -> requireActivity().runOnUiThread {
binding.detectionActionText.text = getString(R.string.nfc_reading_error) binding.detectionActionText.text =
getString(R.string.nfc_reading_error)
viewModel.deleteCan(requireContext()) viewModel.deleteCan(requireContext())
canState() canState()
reset() reset()

View File

@ -11,9 +11,11 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.navArgs import androidx.navigation.fragment.navArgs
import com.google.gson.JsonObject import com.google.gson.JsonObject
import com.google.gson.JsonParser
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 org.json.JSONObject
/** /**
* 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
@ -46,8 +48,13 @@ class ResultFragment : Fragment() {
/** /**
* 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, result: String = "noResult", token: String = "noToken") { private fun createResponse(
val responseCode = if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED success: Boolean = true,
result: String = "noResult",
token: String = "noToken"
) {
val responseCode =
if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED
val resultIntent = Intent() val resultIntent = Intent()
resultIntent.putExtra("result", result) resultIntent.putExtra("result", result)
resultIntent.putExtra("token", token) resultIntent.putExtra("token", token)
@ -60,29 +67,35 @@ class ResultFragment : Fragment() {
*/ */
fun postToken() { fun postToken() {
val json = JsonObject() val json = JsonObject()
json.addProperty("token", paramsModel.token) json.addProperty("auth-token", paramsModel.token)
json.addProperty("headers", paramsModel.headers)
Ion.getDefault(activity).conscryptMiddleware.enable(false) Ion.getDefault(activity).conscryptMiddleware.enable(false)
Ion.with(activity) val ion = Ion.with(activity)
.load(paramsModel.authUrl) .load(paramsModel.authUrl)
.setJsonObjectBody(json) for ((header, value) in paramsModel.headers) {
.asJsonObject() ion.setHeader(header, value)
.setCallback { e, result -> }
if (result == null) {
if (args.mobile) { ion
createResponse(false) .setJsonObjectBody(json)
} else { .asJsonObject()
requireActivity().finishAndRemoveTask() .setCallback { e, result ->
} if (result == null) {
if (args.mobile) {
createResponse(false)
} else { } else {
if (args.mobile) { requireActivity().finishAndRemoveTask()
createResponse(true, result.toString(), paramsModel.token) }
} else { } else {
requireActivity().finishAndRemoveTask() if (args.mobile) {
} createResponse(true, result.toString(), paramsModel.token)
} else {
requireActivity().finishAndRemoveTask()
} }
} }
}
} }
override fun onDestroy() { override fun onDestroy() {

View File

@ -1,5 +1,7 @@
package com.tarkvaraprojekt.mobileauthapp.model package com.tarkvaraprojekt.mobileauthapp.model
import android.util.Log
import android.util.Log.WARN
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
class ParametersViewModel: ViewModel() { class ParametersViewModel: ViewModel() {
@ -16,7 +18,7 @@ class ParametersViewModel: ViewModel() {
private var _origin: String = "" private var _origin: String = ""
val origin get() = _origin val origin get() = _origin
private var _headers: String = "" private var _headers: Map<String, String> = HashMap<String, String>()
val headers get() =_headers val headers get() =_headers
fun setChallenge(newChallenge: String) { fun setChallenge(newChallenge: String) {
@ -35,7 +37,8 @@ class ParametersViewModel: ViewModel() {
_origin = newOrigin _origin = newOrigin
} }
fun setHeaders(newHeaders: String) { fun setHeaders(newHeaders: Map<String, String>) {
Log.i("HEADERS", newHeaders.toList().toString())
_headers = newHeaders _headers = newHeaders
} }
} }

View File

@ -67,7 +67,7 @@
<string name="gender_label">SEX</string> <string name="gender_label">SEX</string>
<!-- string resources for ResultFragment layout--> <!-- string resources for ResultFragment layout-->
<string name="result_text">Controlling the created token</string> <string name="result_text">Checking the created token</string>
<string name="result_info">The app will close automatically</string> <string name="result_info">The app will close automatically</string>
<!-- menu --> <!-- menu -->