From 9b0cb1a22da7460184096ec6df39e1a974967df9 Mon Sep 17 00:00:00 2001 From: TanelOrumaa Date: Mon, 8 Nov 2021 23:52:38 +0200 Subject: [PATCH] MOB-42 Changed POST request logic --- MobileAuthApp/app/build.gradle | 2 + .../mobileauthapp/ResultFragment.kt | 51 +++++++++++-------- .../mobileauthapp/network/TokenApiService.kt | 4 +- .../web/rest/AuthenticationController.kt | 4 +- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/MobileAuthApp/app/build.gradle b/MobileAuthApp/app/build.gradle index 8cff52f..bc4a034 100644 --- a/MobileAuthApp/app/build.gradle +++ b/MobileAuthApp/app/build.gradle @@ -67,6 +67,8 @@ dependencies { 'org.bouncycastle:bcprov-jdk15on:1.60', 'io.jsonwebtoken:jjwt-gson:0.11.2' + implementation 'com.koushikdutta.ion:ion:3.1.0' + // Retrofit + Moshi Converter implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' implementation 'com.squareup.moshi:moshi-kotlin:1.9.3' diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt index d2eb5fc..6eddd05 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/ResultFragment.kt @@ -10,6 +10,8 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.navArgs +import com.google.gson.JsonObject +import com.koushikdutta.ion.Ion import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentResultBinding import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel @@ -58,27 +60,36 @@ class ResultFragment : Fragment() { * Makes a POST request to the backend server with a tokenItem */ fun postToken() { - val tokenData = TokenItem( - paramsModel.token, - paramsModel.challenge - ) - CoroutineScope(Dispatchers.Default).launch { - val response = TokenApi.retrofitService.postToken(tokenData) - Log.v("Response", response.message()) - if (response.isSuccessful) { - Log.v("GREAAAT", "SUCCESSSS") - //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) + val json = JsonObject() + json.addProperty("token", paramsModel.token) + json.addProperty("challenge", paramsModel.challenge) + + Ion.getDefault(activity).getConscryptMiddleware().enable(false) + + Ion.with(activity) + .load("https://6bb0-85-253-195-252.ngrok.io/auth/authentication") + .setJsonObjectBody(json) + .asJsonObject() + .setCallback { e, result -> + // do stuff with the result or error + Log.i("Log thingy", result.toString()) } - } - } +// 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) +// } +// } +// } } /** diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt index 7e7d70f..67b952b 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt @@ -23,8 +23,8 @@ private val retrofit = Retrofit.Builder().addConverterFactory(MoshiConverterFact interface TokenApiService { @Headers("Content-Type: application/json") - @POST("auth/authentication") - suspend fun postToken(@Body data: TokenItem): Response + @POST("/auth/authentication") + suspend fun postToken(@Body data: String): Response } object TokenApi { diff --git a/demoBackend/src/main/kotlin/com/tarkvaratehnika/demobackend/web/rest/AuthenticationController.kt b/demoBackend/src/main/kotlin/com/tarkvaratehnika/demobackend/web/rest/AuthenticationController.kt index dc93dd0..68a3f5b 100644 --- a/demoBackend/src/main/kotlin/com/tarkvaratehnika/demobackend/web/rest/AuthenticationController.kt +++ b/demoBackend/src/main/kotlin/com/tarkvaratehnika/demobackend/web/rest/AuthenticationController.kt @@ -19,7 +19,9 @@ class AuthenticationController { @PostMapping("authentication", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]) - fun authenticate(@RequestBody authToken : AuthTokenDTO): Authentication { + fun authenticate(@RequestBody body : String): Authentication { + val parts = body.split("\"") + val authToken = AuthTokenDTO(parts[3], parts[7]) // Create Spring Security Authentication object with supplied token as credentials. val auth = PreAuthenticatedAuthenticationToken(null, authToken)