I've successfully implemented Google SafetyNet API, even have a successful response. Problem is that the JWSResult
from AttestationResponse
is a hashed string, whereas my expectation was to get a JSON in response.
May I ask where do I need to first look for problems?
Here is the code where attest()
is called:
fun callSafetyNetAttentationApi(context: Activity, callback: SafetyNetCallback) {
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS) {
val nonce: ByteArray? = getRequestNonce("Safety Net Sample: " + System.currentTimeMillis())
val client = SafetyNet.getClient(context)
nonce?.let {
val task: Task<AttestationResponse> = client.attest(it, BuildConfig.SAFETY_NET_KEY)
task.addOnSuccessListener { response -> safetyNetSuccess(response, callback) }
.addOnFailureListener { callback.isDeviceTrusted(false) }
} ?: kotlin.run {
callback.isDeviceTrusted(false)
}
} else {
MaterialDialog.Builder(context)
.title("The app cannot be used")
.content("Please update Google Play Services and try again.")
.cancelable(false)
.positiveText("Dismiss")
.onPositive { dialog, which -> context.finish() }
.show()
}
}
This is a typical JSON response that you'll receive after performing
safetyNetClient.attest(nonce, apiKey)
:Here
foo.bar.zar
is a base64 encoded string, something likeaisnfaksdf.8439hundf.ghbadsjn
, where each part corresponds to:You need to take the
bar
and Base64 decode that in order to get the SafetyNet result JSON:Then construct java object using the JSON library you like, e.g. GSON:
Where
SafetyNetApiModel
is:Have a look at this for reference.