Merge pull request #21 from TanelOrumaa/testappchanges

Testappchanges to MOB-55
This commit is contained in:
TanelOrumaa 2022-01-18 00:54:23 +02:00 committed by GitHub
commit d67c815aad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 49 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

@ -47,17 +47,20 @@ 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.
* Not really the safest way of doing things, but sufficient for POC purposes.
*/ */
private fun createResponse( private fun createResponse(
success: Boolean = true, success: Boolean = true,
result: String = "noResult", idCode: String = "noCode",
token: String = "noToken" name: String = "noName",
authority: String = "noAuthority"
) { ) {
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)
resultIntent.putExtra("authority", authority)
requireActivity().setResult(responseCode, resultIntent) requireActivity().setResult(responseCode, resultIntent)
requireActivity().finish() requireActivity().finish()
} }
@ -81,6 +84,7 @@ class ResultFragment : Fragment() {
.setJsonObjectBody(json) .setJsonObjectBody(json)
.asJsonObject() .asJsonObject()
.setCallback { e, result -> .setCallback { e, result ->
Log.i("resultTag", result.toString())
if (result == null) { if (result == null) {
if (args.mobile) { if (args.mobile) {
createResponse(false) createResponse(false)
@ -89,7 +93,11 @@ class ResultFragment : Fragment() {
} }
} else { } else {
if (args.mobile) { if (args.mobile) {
createResponse(true, result.toString(), paramsModel.token) val userData = result.asJsonObject["userData"]
val idCode = userData.asJsonObject["idCode"].asString
val name = userData.asJsonObject["name"].asString
val authority = result.asJsonObject["roles"].asJsonArray[0].asJsonObject["authority"].asString
createResponse(true, idCode, name, authority)
} 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,18 +9,19 @@ 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://a0fe-2001-7d0-88ab-b880-7571-cba0-5db2-11b7.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 could potentially use MobileAuthApp.
* Single purpose app that launches the MobileAuthApp and gets the response back (JWT). * Single purpose app that launches the MobileAuthApp and gets the response back (JWT).
* Only for demo purposes.
*/ */
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -32,19 +33,18 @@ 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())
Log.i("getResult", response.data?.getStringExtra("authority").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,47 +55,27 @@ class MainActivity : AppCompatActivity() {
showLogin() showLogin()
binding.loginOptionNfcButton.setOnClickListener { getData() } binding.loginOptionNfcButton.setOnClickListener {
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)
} }
/**
* Method for retrieving data from an endpoint.
* Ion library is used as it is very convenient for making simple GET requests.
*/
private fun getData() {
// Enter the server endpoint address to here
val url = "$BASE_URL/auth/challenge"
Ion.getDefault(this).conscryptMiddleware.enable(false)
Ion.with(applicationContext)
.load(url)
.asJsonObject()
.setCallback { _, result ->
try {
// Get data from the result and call launchAuth method
val challenge = result.asJsonObject["nonce"].toString().replace("\"", "")
Log.v("Challenge", challenge)
launchAuth(challenge, BASE_URL, "/auth/authentication")
} catch (e: Exception) {
Log.i("GETrequest", "was unsuccessful")
}
}
}
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
} }