/// <inheritdoc />
        public async Task SaveAccessAndRefreshToken(Uri authority, string clientId, OAuth2TokenResponse tokenResponse)
        {
            try
            {
                await SecureStorage.SetAsync($"oauth_token_{clientId}_{authority}", JsonConvert.SerializeObject(tokenResponse)).ConfigureAwait(false);
            }
#pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception.
            catch (Exception)
#pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception.
            {
                // Possible that device doesn't support secure storage on device.
            }
        }
        internal AuthenticationResult(OAuth2TokenResponse tokenResponse, bool isExtendedLifetimeEnabled)
        {
            IdToken idToken = Auth.IdToken.Parse(tokenResponse.IdToken);

            bool isExtendedLifetimeToken = isExtendedLifetimeEnabled && tokenResponse.AccessTokenExtendedExpiresOn >
                                           DateTime.UtcNow + TimeSpan.FromMinutes(DefaultExpirationBufferInMinutes);

            var scopes = ScopeHelper.ConvertStringToLowercaseSortedSet(tokenResponse.Scope);

            AccessToken             = tokenResponse.AccessToken;
            IsExtendedLifeTimeToken = isExtendedLifetimeToken;
            UniqueId          = idToken.GetUniqueId();
            ExpiresOn         = tokenResponse.AccessTokenExpiresOn;
            ExtendedExpiresOn = tokenResponse.AccessTokenExtendedExpiresOn;
            IdToken           = tokenResponse.IdToken;
            Scopes            = scopes;
        }
        private async Task <OAuth2TokenResponse> SendHttpMessageAsync(OAuth2Client client, Uri tokenEndpoint)
        {
            UriBuilder builder = new UriBuilder(tokenEndpoint);

            builder.AppendQueryParameters(AuthenticationRequestParameters.ExtraQueryParameters);
            OAuth2TokenResponse authTokenResponse =
                await client
                .GetTokenAsync(
                    builder.Uri,
                    AuthenticationRequestParameters.RequestContext)
                .ConfigureAwait(false);

            if (string.IsNullOrEmpty(authTokenResponse.Scope))
            {
                authTokenResponse.Scope = AuthenticationRequestParameters.Scope.AsSingleString();
                AuthenticationRequestParameters.RequestContext.Logger.Info("ScopeSet was missing from the token response, so using developer provided scopes in the result");
            }

            return(authTokenResponse);
        }
        protected AuthenticationResult CacheTokenResponseAndCreateAuthenticationResult(OAuth2TokenResponse tokenResponse)
        {
            // developer passed in user object.
            AuthenticationRequestParameters.RequestContext.Logger.Info("Checking client info returned from the server..");

            var authenticationResults = new AuthenticationResult(tokenResponse, ServiceBundle.Config.IsExtendedTokenLifetimeEnabled);

            TokenCache?.SaveAccessAndRefreshToken(AuthenticationRequestParameters.Authority, AuthenticationRequestParameters.ClientId, tokenResponse);

            return(authenticationResults);
        }