fixed testapp compatibility issue

This commit is contained in:
Henrik Lepson 2021-12-14 22:46:35 +02:00
parent e5931692b6
commit b889b9cda7
6 changed files with 51 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.ConnectivityManager
import android.nfc.NfcAdapter import android.nfc.NfcAdapter
import android.nfc.TagLostException import android.nfc.TagLostException
import android.nfc.tech.IsoDep import android.nfc.tech.IsoDep
@ -14,7 +13,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
@ -106,10 +104,28 @@ class HomeFragment : Fragment() {
try { try {
if (mobile) { if (mobile) {
// We use !! to get extras because we want an exception to be thrown when something is missing. // We use !! to get extras because we want an exception to be thrown when something is missing.
intentParams.setChallenge(requireActivity().intent.getStringExtra("challenge")!!) //intentParams.setChallenge(requireActivity().intent.getStringExtra("challenge")!!)
intentParams.setAuthUrl(requireActivity().intent.getStringExtra("authUrl")!!) intentParams.setAuthUrl(requireActivity().intent.getStringExtra("authUrl")!!)
intentParams.setOrigin(requireActivity().intent.getStringExtra("originUrl")!!) intentParams.setOrigin(requireActivity().intent.getStringExtra("originUrl")!!)
val challengeUrl = requireActivity().intent.getStringExtra("challenge")!!
val headers = requireActivity().intent.getStringExtra("headers")!!
val map: HashMap<String, String> = HashMap()
map.put("sessionId", headers)
intentParams.setHeaders(map)
Ion.getDefault(activity).conscryptMiddleware.enable(false)
Ion.with(activity)
.load(challengeUrl)
.setHeader("sessionId", headers)
.asJsonObject()
.setCallback { _, result ->
try {
val challenge = result.asJsonObject["nonce"].toString().replace("\"", "")
intentParams.setChallenge(challenge)
goToTheNextFragment(mobile) goToTheNextFragment(mobile)
} catch (e: Exception) {
Log.i("GETrequest", e.toString())
}
}
} else { //Website } else { //Website
/* /*
var challenge = requireActivity().intent.data!!.getQueryParameter("challenge")!! var challenge = requireActivity().intent.data!!.getQueryParameter("challenge")!!

View File

@ -50,14 +50,14 @@ class ResultFragment : Fragment() {
*/ */
private fun createResponse( private fun createResponse(
success: Boolean = true, success: Boolean = true,
result: String = "noResult", idCode: String = "noCode",
token: String = "noToken" name: String = "noName"
) { ) {
val responseCode = val responseCode =
if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED if (success) AppCompatActivity.RESULT_OK else AppCompatActivity.RESULT_CANCELED
val resultIntent = Intent() val resultIntent = Intent()
resultIntent.putExtra("result", result) resultIntent.putExtra("idCode", idCode)
resultIntent.putExtra("token", token) resultIntent.putExtra("name", name)
requireActivity().setResult(responseCode, resultIntent) requireActivity().setResult(responseCode, resultIntent)
requireActivity().finish() requireActivity().finish()
} }
@ -90,7 +90,11 @@ class ResultFragment : Fragment() {
} }
} else { } else {
if (args.mobile) { if (args.mobile) {
createResponse(true, result.toString(), paramsModel.token) Log.i("myResultTag", result.toString())
val userData = result.asJsonObject["userData"]
val idCode = userData.asJsonObject["idCode"].asString
val name = userData.asJsonObject["name"].asString
createResponse(true, idCode, name)
} else { } else {
requireActivity().finishAndRemoveTask() requireActivity().finishAndRemoveTask()
} }

View File

@ -43,4 +43,5 @@ dependencies {
testImplementation 'junit:junit:4.+' testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testmobileapp"> package="com.example.testmobileapp">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"

View File

@ -9,14 +9,14 @@ 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.koushikdutta.ion.Ion
import org.json.JSONObject import org.json.JSONObject
import java.net.URL
/** /**
* Base url where the requests should be made. Add yours here. It must use https. * Base url where the requests should be made. Add yours here. It must use https.
*/ */
private const val BASE_URL = "https-base-url-here" private const val BASE_URL = "https://e871-2001-7d0-88a4-b880-d085-ba91-1799-76e7.ngrok.io"
private const val AUTH_URL = "$BASE_URL/auth/login"
private const val CHALLENGE_URL = "$BASE_URL/auth/challenge"
/** /**
* Test mobile app to demonstrate how other applications can use MobileAuthApp. * Test mobile app to demonstrate how other applications can use MobileAuthApp.
@ -32,19 +32,17 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater) binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
Log.i("myLoggingStuff", URL("https://www.google.ee/?hl=et").host.toString())
authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { response -> authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { response ->
if (response.resultCode == Activity.RESULT_OK) { if (response.resultCode == Activity.RESULT_OK) {
binding.loginTextView.text = getString(R.string.auth_success) binding.loginTextView.text = getString(R.string.auth_success)
// Logs are used to show what information can be retrieved from the mobileauthapp. // Logs are used to show what information can be retrieved from the mobileauthapp.
Log.i("getResult", response.data?.getStringExtra("token").toString()) Log.i("getResult", response.data?.getStringExtra("idCode").toString())
Log.i("getResult", response.data?.getStringExtra("result").toString()) Log.i("getResult", response.data?.getStringExtra("name").toString())
var user = "" var user = ""
try { try {
val resultObject = JSONObject(response.data?.getStringExtra("result").toString()) user = response.data?.getStringExtra("name").toString()
user = resultObject.getString("principal")
} catch (e: Exception) { } catch (e: Exception) {
Log.i("getResult", "unable to retrieve name from principal") Log.i("getResult", "unable to retrieve name")
} }
showResult(user) showResult(user)
} }
@ -55,20 +53,24 @@ class MainActivity : AppCompatActivity() {
showLogin() showLogin()
binding.loginOptionNfcButton.setOnClickListener { getData() } binding.loginOptionNfcButton.setOnClickListener {
//getData()
launchAuth()
}
} }
/** /**
* Method that creates an intent to launch the MobileAuthApp * Method that creates an intent to launch the MobileAuthApp
*/ */
private fun launchAuth(challenge: String = "challenge", originUrl: String = "baseUrl", authUrl: String = "authUrl") { private fun launchAuth() {
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_URL)
launchIntent.putExtra("originUrl", originUrl) launchIntent.putExtra("originUrl", BASE_URL)
launchIntent.putExtra("authUrl", authUrl) launchIntent.putExtra("authUrl", AUTH_URL)
launchIntent.putExtra("headers","${(0..100000).random()}")
launchIntent.putExtra("mobile", true) launchIntent.putExtra("mobile", true)
authLauncher.launch(launchIntent) authLauncher.launch(launchIntent)
} }
@ -77,6 +79,7 @@ class MainActivity : AppCompatActivity() {
* Method for retrieving data from an endpoint. * Method for retrieving data from an endpoint.
* Ion library is used as it is very convenient for making simple GET requests. * Ion library is used as it is very convenient for making simple GET requests.
*/ */
/*
private fun getData() { private fun getData() {
// Enter the server endpoint address to here // Enter the server endpoint address to here
val url = "$BASE_URL/auth/challenge" val url = "$BASE_URL/auth/challenge"
@ -95,6 +98,7 @@ class MainActivity : AppCompatActivity() {
} }
} }
} }
*/
private fun showLogin() { private fun showLogin() {
binding.loginOptions.visibility = View.VISIBLE binding.loginOptions.visibility = View.VISIBLE

View File

@ -1,5 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
ext.kotlin_version = '1.4.10'
repositories { repositories {
google() google()
mavenCentral() mavenCentral()
@ -7,7 +8,7 @@ buildscript {
dependencies { dependencies {
classpath "com.android.tools.build:gradle:7.0.2" classpath "com.android.tools.build:gradle:7.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files
} }