2021-10-26 11:42:58 +03:00
|
|
|
package com.example.testmobileapp
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import android.os.Bundle
|
2021-11-07 13:49:19 +02:00
|
|
|
import android.util.Log
|
2021-11-14 10:13:40 +02:00
|
|
|
import android.view.View
|
2021-10-26 11:42:58 +03:00
|
|
|
import androidx.activity.result.ActivityResultLauncher
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
import com.example.testmobileapp.databinding.ActivityMainBinding
|
2021-11-14 10:13:40 +02:00
|
|
|
import com.google.gson.JsonObject
|
2021-11-07 13:49:19 +02:00
|
|
|
import com.koushikdutta.ion.Ion
|
2021-10-26 11:42:58 +03:00
|
|
|
|
2021-11-07 13:49:19 +02:00
|
|
|
/**
|
|
|
|
* Test mobile app to demonstrate how other applications can use MobileAuthApp.
|
|
|
|
* Single purpose app that launches the MobileAuthApp and gets the response back (JWT).
|
|
|
|
*/
|
2021-10-26 11:42:58 +03:00
|
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
private lateinit var authLauncher: ActivityResultLauncher<Intent>
|
|
|
|
|
2021-11-14 10:13:40 +02:00
|
|
|
private lateinit var binding: ActivityMainBinding
|
|
|
|
|
2021-10-26 11:42:58 +03:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-11-14 10:13:40 +02:00
|
|
|
binding = ActivityMainBinding.inflate(layoutInflater)
|
2021-10-26 11:42:58 +03:00
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
|
|
authLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { response ->
|
|
|
|
if (response.resultCode == Activity.RESULT_OK) {
|
|
|
|
// Currently we are not actually checking whether we get a valid token.
|
|
|
|
// For testing purposes only, to make sure that we are able to get a response at all.
|
|
|
|
binding.loginTextView.text = getString(R.string.auth_success)
|
|
|
|
}
|
|
|
|
if (response.resultCode == Activity.RESULT_CANCELED) {
|
|
|
|
binding.loginTextView.text = getString(R.string.auth_failure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 10:13:40 +02:00
|
|
|
showLogin()
|
|
|
|
|
|
|
|
binding.loginOptionNfcButton.setOnClickListener { getData() }
|
2021-10-26 11:42:58 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:49:19 +02:00
|
|
|
/**
|
|
|
|
* Method that creates an intent to launch the MobileAuthApp
|
|
|
|
*/
|
2021-11-14 10:13:40 +02:00
|
|
|
private fun launchAuth(challenge: String = "challenge", originUrl: String = "baseUrl", authUrl: String = "authUrl") {
|
2021-10-26 11:42:58 +03:00
|
|
|
val launchIntent = Intent()
|
|
|
|
launchIntent.setClassName("com.tarkvaraprojekt.mobileauthapp", "com.tarkvaraprojekt.mobileauthapp.MainActivity")
|
2021-11-08 19:07:30 +02:00
|
|
|
launchIntent.putExtra("action", "auth")
|
2021-11-08 20:26:17 +02:00
|
|
|
launchIntent.putExtra("challenge", challenge)
|
2021-11-14 10:13:40 +02:00
|
|
|
launchIntent.putExtra("originUrl", originUrl)
|
2021-11-08 20:26:17 +02:00
|
|
|
launchIntent.putExtra("authUrl", authUrl)
|
2021-11-08 19:07:30 +02:00
|
|
|
launchIntent.putExtra("mobile", true)
|
2021-10-26 11:42:58 +03:00
|
|
|
authLauncher.launch(launchIntent)
|
|
|
|
}
|
2021-11-07 13:49:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for retrieving data from an endpoint.
|
|
|
|
* Ion library is used as it is very convenient for making simple GET requests.
|
|
|
|
*/
|
|
|
|
private fun getData() {
|
2021-11-08 20:22:33 +02:00
|
|
|
// Enter the server endpoint address to here
|
2021-11-14 10:13:40 +02:00
|
|
|
//val originUrl = "enter-base-url-here"
|
2021-11-16 21:30:58 +02:00
|
|
|
val originUrl = "https://5d0c-85-253-195-195.ngrok.io"
|
2021-11-14 10:13:40 +02:00
|
|
|
val url = "$originUrl/auth/challenge"
|
|
|
|
Ion.getDefault(this).conscryptMiddleware.enable(false)
|
2021-11-07 13:49:19 +02:00
|
|
|
Ion.with(applicationContext)
|
|
|
|
.load(url)
|
|
|
|
.asJsonObject()
|
|
|
|
.setCallback { _, result ->
|
|
|
|
try {
|
|
|
|
// Get data from the result and call launchAuth method
|
2021-11-16 21:30:58 +02:00
|
|
|
val challenge = result.asJsonObject["nonce"].toString().replace("\"", "")
|
|
|
|
Log.v("Challenge", challenge)
|
2021-11-14 10:13:40 +02:00
|
|
|
launchAuth(challenge, originUrl, "/auth/authentication")
|
2021-11-07 13:49:19 +02:00
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.i("GETrequest", "was unsuccessful")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 10:13:40 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 11:42:58 +03:00
|
|
|
}
|