Google Safety Net Attestation API未在模拟器上提供响应

时间:2017-11-20 12:16:29

标签: android safetynet

我们正在尝试将安全网API添加到我们的应用中。 当我们在真实设备上进行测试时,一切正常,但是在仿真器上进行测试时,安全网服务器没有响应。 API的目的是检测模拟设备,因此不知道为什么它不能在模拟器上工作。

以下是API调用:

SafetyNet.getClient(activity).attest(getRequestNonce(nonceData), "<Key-here>").addOnSuccessListener(activity, new OnSuccessListener() {
    public void onSuccess(AttestationResponse response) {
        String[] jwtParts = response.getJwsResult().split("\\.");
        if(jwtParts.length == 3) {
            String sharedpreferences = new String(Base64.decode(jwtParts[1], 0));
            SharedPreferences editor = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor1 = editor.edit();
            editor1.putString("decodedPayload", sharedpreferences);
            editor1.commit();
            Log.d("ContentValues", "The Safety net response is: " + sharedpreferences);
        } else {
            SharedPreferences sharedpreferences1 = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor2 = sharedpreferences1.edit();
            editor2.putString("decodedPayload", "CND");
            editor2.commit();
            Log.d("ContentValues", "The safety net response could not be decoded");
        }

    }
}).addOnFailureListener(activity, new OnFailureListener() {
    public void onFailure(@NonNull Exception e) {
        if(e instanceof ApiException) {
            ApiException apiException = (ApiException)e;
            Log.d("ContentValues", "Error while fetching safety net result: " + ((ApiException)e).getStatusCode() + " " + ((ApiException)e).getStatusMessage());
            SharedPreferences sharedpreferences = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor = sharedpreferences.edit();
            editor.putString("decodedPayload", "ERR");
            editor.commit();
            Log.d("ContentValues", "The safety net response could not be decoded");
        } else {
            Log.d("ContentValues", "Unknown Error while fetching safety net results: " + e.getMessage());
        }

    }
});

}

即使在等待30秒之后,所有处理程序都没有得到响应。 有人可以帮忙吗。

2 个答案:

答案 0 :(得分:1)

您运行的模拟器很可能没有安装Google Play服务。由于Google Play服务部门负责实施证明,因此Attestation只会在没有Google Play服务的设备上工作。

使用Attestation或任何其他Google Play服务API时,您应该check that Google Play Services is installed, and is at a version that you need (optimally, you should check that it's at the latest version)

从历史上看,官方AVD图像不包括Google Play服务,您可以在技术上将其加载。但是,2017年4月左右,there are now official AVD images that include Play Store(以及Google Play服务)。

答案 1 :(得分:0)

这是一个较晚的响应,但也许将来有人需要。 尝试使用SafetyNet附带的其他侦听器,即使SafetyNet返回成功,所有完成的侦听器都不会响应。 另外,您还可以在Android Device Verification API信息中心的Google Cloud Platform中检查api调用以及成功,失败情况。

        SafetyNet.getClient(this).attest(generateOneTimeRequestNonce(), "API_KEY")
                .addOnFailureListener { e ->
                    Log.i(TAG, "SafetyNet callback fail")
                }
                .addOnSuccessListener { resp ->
                    Log.i(TAG, "SafetyNet callback success")
                    val response = parseJsonWebSignature(resp.jwsResult)!!

                    when {
                        response.isCtsProfileMatch -> {
                            //profile of the device running your app matches the profile of a device that has passed Android compatibility testing.

                        }
                        response.isBasicIntegrity -> {
                            //then the device running your app likely wasn't tampered with, but the device has not necessarily passed Android compatibility testing.

                        }
                        else -> {
                            //handle fail, maybe warn user device is unsupported or in compromised state? (this is up to you!)

                        }
                    }
                }