Example #1
0
        /// <summary>
        /// Refreshes the expired access token.
        /// </summary>
        /// <param name="httpClient">The <see cref="HttpClient"/> to perform the send operation.</param>
        /// <returns>A task that represents the asynchronous refresh operation.</returns>
        private static async Task <string> InternalRefreshToken(HttpClient httpClient)
        {
            // Prepare the refresh request
            var endpoint       = Uris.GetRefreshTokenUri();
            var refreshRequest = new JsonObject
            {
                { "grant_type", "refresh_token" },
                { "refresh_token", Settings.Instance.GetValue(Settings.RefreshToken, string.Empty) }
            };

            using (var request = new HttpRequestMessage(endpoint.Method, WebHelper.AppendNonce(endpoint.Uri)))
            {
                // Setup the request
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Content = refreshRequest.AsHttpContent();

                // Send the request to the server
                var socketTimeout = httpClient.Timeout.Add(TimeSpan.FromSeconds(5));
                var response      = await WebHelper.SendRequest(httpClient, request, socketTimeout);

                using (response)
                {
                    // Parse the server response
                    var responseContent = await WebHelper.ParseResponse(response);

                    // Parse the new access token data
                    var accessToken = responseContent.GetItemOrDefault("access_token").GetStringValueOrDefault(string.Empty);
                    var expiresIn   = responseContent.GetItemOrDefault("expires_in").GetIntValueOrDefault(24 * 60 * 60);

                    // If all of the fields are returned
                    if (!string.IsNullOrEmpty(accessToken))
                    {
                        // Update access token
                        Settings.Instance.SetValue(Settings.AccessToken, accessToken);
                        Settings.Instance.SetValue(Settings.AccessTokenExpires, DateTime.UtcNow.AddSeconds(expiresIn));
                        return(accessToken);
                    }

                    // Report error
                    throw new RemoteServerException(
                              HttpStatusCode.Unauthorized,
                              Localization.ErrorDialogTitle,
                              Localization.ErrorUnauthorized,
                              null);
                }
            }
        }
        /// <summary>
        /// Retries the login process after user consent to change session.
        /// </summary>
        /// <param name="request">The request to retry.</param>
        /// <param name="response">The response received from the server.</param>
        private async void RetryLogin(JsonObject request, JsonValue response)
        {
            // Ask the user consent to override session
            var consent = await App.DisplayAlert(
                Localization.ConfirmationDialogTitle,
                Localization.ConfirmationSesionChange,
                Localization.ButtonConfirm,
                Localization.Cancel);

            // If the user consent received
            if (consent)
            {
                // Modify the request based on server response
                var grantType = response.GetItemOrDefault("grant_type").GetStringValueOrDefault(string.Empty);
                if (!string.IsNullOrEmpty(grantType))
                {
                    request["grant_type"] = grantType;
                }

                var state = response.GetItemOrDefault("state").GetStringValueOrDefault(string.Empty);
                if (!string.IsNullOrEmpty(state))
                {
                    request["state"] = state;
                }

                // Resent the request to the server
                request.Add("override", true);
                WebHelper.SendAsync(
                    Uris.GetLoginUri(),
                    request.AsHttpContent(),
                    this.ProcessLoginResult,
                    () => this.IsBusy = false);
            }
            else
            {
                // Cancel the task
                this.UserName = string.Empty;
                this.Password = string.Empty;
                this.OnPropertyChanged(nameof(this.Password));
                this.IsBusy = false;
            }
        }