コード例 #1
0
ファイル: ApiAttestation.cs プロジェクト: andrewkir/Devenir
        public async System.Threading.Tasks.Task AttestateAsync(Activity activity)
        {
            try
            {
                var        api = RestService.For <ApiService>("https://devenir.andrewkir.ru");
                JSONObject obj = new JSONObject();

                timestamp = DateTime.Now.Ticks.ToString();
                device_id = Secure.GetString(activity.ContentResolver, Secure.AndroidId);

                obj.Put("timestamp", timestamp);
                obj.Put("device_id", device_id);


                var response = await api.GetNonce(obj.ToString());

                if (response == null)
                {
                    AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.noInternetException));
                    return;
                }


                if (response.IsSuccessStatusCode)
                {
                    string nonce = response.Content;

                    if (GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(Application.Context) == ConnectionResult.Success)
                    {
                        // The SafetyNet Attestation API is available.
                        SafetyNetClient client = SafetyNetClass.GetClient(activity);
                        var             task   = client.Attest(Encoding.ASCII.GetBytes(nonce), activity.Resources.GetString(Resource.String.api_safetyNetKey))
                                                 .AddOnSuccessListener(activity, new OnSuccessListener(activity, timestamp, this))
                                                 .AddOnFailureListener(activity, new OnFailureListener(activity, this));
                    }
                    else
                    {
                        // Prompt user to update Google Play services
                        AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.attestationGooglePlayException));
                    }
                    return;
                }
                else
                {
                    AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.serverException) + $"failed with nonce");
                    return;
                }
            }
            catch (HttpRequestException ex)
            {
                AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.serverException) + $"https exception {ex}");
            }
            catch (Exception ex)
            {
                AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.unexpectedException));
            }
        }
コード例 #2
0
ファイル: ApiAttestation.cs プロジェクト: andrewkir/Devenir
        private async System.Threading.Tasks.Task CheckResultsAsync(string res, string timestamp, Activity activity)
        {
            try
            {
                var        api = RestService.For <ApiService>("https://devenir.andrewkir.ru");
                JSONObject obj = new JSONObject();
                obj.Put("safetynet", res);
                obj.Put("timestamp", timestamp);
                obj.Put("device_id", Secure.GetString(activity.ContentResolver, Secure.AndroidId));

                var response = await api.CreateAccessToken(obj.ToString());

                if (response == null)
                {
                    AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.noInternetException));
                    return;
                }
                if (response.IsSuccessStatusCode)
                {
                    JSONObject resp = new JSONObject(response.Content);
                    try
                    {
                        string access_token  = resp.GetString("access_token");
                        string refrest_token = resp.GetString("refresh_token");
                        SharedPrefsManager.SaveTokens(access_token, refrest_token);
                        AttestationResultEvent?.Invoke(true, null);
                    }
                    catch (JSONException)
                    {
                        AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.serverException) + $" json parsing error {response.Content}");
                    }
                }
                else
                {
                    AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.serverException) + $" attestation error {response.Content} {response.Error.Content}");
                }
            }
            catch (HttpRequestException)
            {
                AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.serviceNoConnectionException));
            }
            catch (Exception ex)
            {
                AttestationResultEvent?.Invoke(false, activity.GetString(Resource.String.unexpectedException));
            }
        }