MOB-42 Changed POST request logic

This commit is contained in:
TanelOrumaa 2021-11-08 23:52:38 +02:00
parent 1d665f02bf
commit 9b0cb1a22d
4 changed files with 38 additions and 23 deletions

View File

@ -67,6 +67,8 @@ dependencies {
'org.bouncycastle:bcprov-jdk15on:1.60', 'org.bouncycastle:bcprov-jdk15on:1.60',
'io.jsonwebtoken:jjwt-gson:0.11.2' 'io.jsonwebtoken:jjwt-gson:0.11.2'
implementation 'com.koushikdutta.ion:ion:3.1.0'
// Retrofit + Moshi Converter // Retrofit + Moshi Converter
implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.9.3' implementation 'com.squareup.moshi:moshi-kotlin:1.9.3'

View File

@ -10,6 +10,8 @@ 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
import androidx.navigation.fragment.navArgs 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.databinding.FragmentResultBinding
import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel import com.tarkvaraprojekt.mobileauthapp.model.ParametersViewModel
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
@ -58,27 +60,36 @@ class ResultFragment : Fragment() {
* Makes a POST request to the backend server with a tokenItem * Makes a POST request to the backend server with a tokenItem
*/ */
fun postToken() { fun postToken() {
val tokenData = TokenItem( val json = JsonObject()
paramsModel.token, json.addProperty("token", paramsModel.token)
paramsModel.challenge json.addProperty("challenge", paramsModel.challenge)
)
CoroutineScope(Dispatchers.Default).launch { Ion.getDefault(activity).getConscryptMiddleware().enable(false)
val response = TokenApi.retrofitService.postToken(tokenData)
Log.v("Response", response.message()) Ion.with(activity)
if (response.isSuccessful) { .load("https://6bb0-85-253-195-252.ngrok.io/auth/authentication")
Log.v("GREAAAT", "SUCCESSSS") .setJsonObjectBody(json)
//Success scenario here .asJsonObject()
} else { .setCallback { e, result ->
//Failure scenario here // do stuff with the result or error
if (args.mobile) { Log.i("Log thingy", result.toString())
createResponse(false)
} else {
//Currently for some reason the activity is not killed entirely. Must be looked into further.
requireActivity().finish()
exitProcess(0)
} }
} // 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)
// }
// }
// }
} }
/** /**

View File

@ -23,8 +23,8 @@ private val retrofit = Retrofit.Builder().addConverterFactory(MoshiConverterFact
interface TokenApiService { interface TokenApiService {
@Headers("Content-Type: application/json") @Headers("Content-Type: application/json")
@POST("auth/authentication") @POST("/auth/authentication")
suspend fun postToken(@Body data: TokenItem): Response<TokenItem> suspend fun postToken(@Body data: String): Response<TokenItem>
} }
object TokenApi { object TokenApi {

View File

@ -19,7 +19,9 @@ class AuthenticationController {
@PostMapping("authentication", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]) @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. // Create Spring Security Authentication object with supplied token as credentials.
val auth = PreAuthenticatedAuthenticationToken(null, authToken) val auth = PreAuthenticatedAuthenticationToken(null, authToken)