private void OverrideOperatorUrls(ProviderMetadata metadata)
        {
            if (OperatorUrls == null)
            {
                return;
            }

            OperatorUrls.OverrideUrls(metadata);
        }
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="forceCacheBypass"></param>
        /// <returns></returns>
        public async Task <ProviderMetadata> RetrieveProviderMetada(string url, bool forceCacheBypass = false)
        {
            if (url == null)
            {
                Log.Warning("Provider metadata was defaulted as no url found");
                return(ProviderMetadata.Default);
            }

            // Get value from cache, if it is expired we don't want to remove it because we will use it as a fallback if the call to the provider metadata endpoint fails
            // If it is expired and the call to the provider metadata endpoint succeeds then expired value will be overwritten in the cache anyway
            ProviderMetadata cached = null;

            if (!forceCacheBypass)
            {
                cached = await GetCachedValueAsync <ProviderMetadata>(url, false);

                if (cached != null && !cached.HasExpired)
                {
                    return(cached);
                }
            }

            ProviderMetadata metadata = null;

            try
            {
                var response = await _client.GetAsync(url, null);

                if ((int)response.StatusCode < 400)
                {
                    metadata = JsonConvert.DeserializeObject <ProviderMetadata>(response.Content);
                    await AddCachedValueAsync(url, metadata).ConfigureAwait(false);
                }
                else if (cached != null)
                {
                    metadata = cached;
                }
            }
            catch (Exception e)
            {
                // If an error occurred while fetching the metadata we don't want to cause the discovery to fail completely as the
                // info in the discovery response should be enough to make successful calls in the event that a provider supplies
                // malformed metadata
                if (cached != null)
                {
                    metadata = cached;
                }
                Log.Warning(() => cached != null ? $"Falling back to expired cached provider metadata, url={url}, reason={e.Message}" : $"Falling back to default provider metadata, url={url}, reason={e.Message}");
            }

            return(metadata ?? ProviderMetadata.Default);
        }
        /// <summary>
        /// Replaces URLs from the discovery response with URLs from the provider metadata.
        /// This allows providers to use temporary urls while the main url is down for maintenance.
        /// </summary>
        /// <param name="metadata">Metadata to get overriding urls from</param>
        internal void OverrideUrls(ProviderMetadata metadata)
        {
            if (metadata == null)
            {
                return;
            }

            AuthorizationUrl = metadata.AuthorizationEndpoint ?? AuthorizationUrl;
            RequestTokenUrl  = metadata.TokenEndpoint ?? RequestTokenUrl;
            UserInfoUrl      = metadata.UserInfoEndpoint ?? UserInfoUrl;
            PremiumInfoUrl   = metadata.PremiumInfoEndpoint ?? PremiumInfoUrl;
            JWKSUrl          = metadata.JwksUri ?? JWKSUrl;
            RefreshTokenUrl  = metadata.RefreshEndpoint ?? RefreshTokenUrl;
            RevokeTokenUrl   = metadata.RevokeEndpoint ?? RevokeTokenUrl;
        }
Esempio n. 4
0
        /// <summary>
        /// <inheritdoc/>
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="forceCacheBypass"></param>
        /// <returns></returns>
        public async Task <ProviderMetadata> RetrieveProviderMetada(string url, bool forceCacheBypass = false)
        {
            if (url == null)
            {
                Log.Warning("Provider metadata was defaulted as no url found");
                return(ProviderMetadata.Default);
            }

            ProviderMetadata metadata = null;

            var response = await _client.GetAsync(url, null);

            if ((int)response.StatusCode < 400)
            {
                metadata = JsonConvert.DeserializeObject <ProviderMetadata>(response.Content);
            }

            return(metadata ?? ProviderMetadata.Default);
        }