'AuthState save and restore value

I am building an app using AppAuth from OpenId. I am adding biometrics on Android in order to protect the app. My goal is to encrypt the AutState object, so when I am having the app in bkg, the AuthState object is cleared and I update it when I come back to the app when decyphering.

 private fun encryptAndStoreServerToken(authResult: BiometricPrompt.AuthenticationResult) {
        try {
            authResult.cryptoObject?.cipher?.apply {
                    val test = viewModel.authState

                    val encryptedServerTokenWrapper = cryptographyManager.encryptData(viewModel.getAuthState().jsonSerializeString(), this)
                    cryptographyManager.persistCiphertextWrapperToSharedPrefs(
                        encryptedServerTokenWrapper,
                        requireContext(),
                        SHARED_PREFS_FILENAME,
                        Context.MODE_PRIVATE,
                        CIPHERTEXT_WRAPPER
                    )
                 (activity as MainActivity?)?.onLoggedInFlowCompleted()
            }


        } catch (e: Exception) {
            viewModel.loginError.set("Error Biometrics")
        }

    }

the viewModel.getAuthState() is just returning AuthState object which is used by openID to save authorization req and response.

Any idea how to save this object and restore it. I tried the above code to save and the below one to restore but it failes

private fun decryptServerTokenFromStorage(authResult: BiometricPrompt.AuthenticationResult) {
        ciphertextWrapper?.let { textWrapper ->
            authResult.cryptoObject?.cipher?.let {
                val plainText = cryptographyManager.decryptData(textWrapper.ciphertext, it)
                callback.onSuccess(AuthState.jsonDeserialize(plainText))
            }
        }
    }

The AuthState object is defined as below:

https://openid.github.io/AppAuth-Android/docs/latest/index.html?net/openid/appauth/AuthState.html

Any idea how to make it works. I really just want to save the AuthState to avoid saving all object inside and restore it.

Regards Sebastien



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source