Ejemplo n.º 1
0
 public OnFailureListener(Activity activity, ApiAttestation sender)
 {
     this.activity = activity;
     this.sender   = sender;
 }
Ejemplo n.º 2
0
 public OnSuccessListener(Activity activity, string timestamp, ApiAttestation sender)
 {
     this.activity  = activity;
     this.timestamp = timestamp;
     this.sender    = sender;
 }
Ejemplo n.º 3
0
        private async System.Threading.Tasks.Task RefreshTokenAsync(Activity activity)
        {
            try
            {
                var    api           = RestService.For <ApiService>(PATH);
                string refresh_token = SharedPrefsManager.GetTokens().refresh_token;
                if (refresh_token != null && refresh_token != "")
                {
                    var response = await api.RefreshToken("Bearer " + refresh_token, deviceId);

                    if (response == null)
                    {
                        RequestResultEvent?.Invoke(false, null, activity.GetString(Resource.String.noInternetException));
                        return;
                    }
                    if (response.IsSuccessStatusCode)
                    {
                        JSONObject resp = new JSONObject(response.Content);
                        try
                        {
                            string access_token      = resp.GetString("access_token");
                            string refresh_token_new = resp.GetString("refresh_token");
                            SharedPrefsManager.SaveTokens(access_token, refresh_token_new);
                            //Toast.MakeText(Application.Context, "Успешное обновление access_token и refresh_token", ToastLength.Short).Show();
                            RequestResultEvent?.Invoke(true, null, null);
                            return;
                        }
                        catch (JSONException)
                        {
                            RequestResultEvent?.Invoke(false, null, activity.GetString(Resource.String.latexServerException));
                            return;
                        }
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity)
                    {
                        //Toast.MakeText(Application.Context, $"Переаттестация! Истёк refresh token", ToastLength.Short).Show();
                        var attest = new ApiAttestation();
                        attest.AddOnResultListener(delegate(bool res, string exception)
                        {
                            if (res)
                            {
                                RequestResultEvent?.Invoke(res, null, null);
                            }
                            else
                            {
                                RequestResultEvent?.Invoke(res, null, exception);
                            }
                        });
                        await attest.AttestateAsync(activity);
                    }
                }
                else
                {
                    var attest = new ApiAttestation();
                    attest.AddOnResultListener(delegate(bool res, string exception)
                    {
                        if (res)
                        {
                            RequestResultEvent?.Invoke(res, null, null);
                        }
                        else
                        {
                            RequestResultEvent?.Invoke(res, null, exception);
                        }
                    });
                    await attest.AttestateAsync(activity);
                }
            }
            catch (HttpRequestException)
            {
                RequestResultEvent?.Invoke(false, null, activity.GetString(Resource.String.latexNoConnectionException));
            }
            catch (Exception ex)
            {
                RequestResultEvent?.Invoke(false, null, activity.GetString(Resource.String.latexNoConnectionException));
            }
        }