protected async Task <string> PostAuthenticate(HttpResponseMessage response)
        {
            // An HTTP 401 Not Authorized error; handle if an authentication callback has been supplied
            if (OnAuthenticate != null)
            {
                // Extract the WWW-Authenticate header and determine if it represents an OAuth2 Bearer challenge
                string authenticateHeader = response.Headers.WwwAuthenticate.ElementAt(0).ToString();

                if (HttpBearerChallenge.IsBearerChallenge(authenticateHeader))
                {
                    var challenge = new HttpBearerChallenge(response.RequestMessage.RequestUri, authenticateHeader);

                    if (challenge != null)
                    {
                        // Update challenge cache
                        HttpBearerChallengeCache.GetInstance().SetChallengeForURL(response.RequestMessage.RequestUri, challenge);

                        // We have an authentication challenge, use it to get a new authorization token
                        return(await OnAuthenticate(
                                   challenge.AuthorizationServer,
                                   this.AzureEnvironment.ResourceManagerEndpoint,
                                   challenge.Scope).ConfigureAwait(false));
                    }
                }
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Removes the cached challenge for the specified URL
        /// </summary>
        /// <param name="uri"> the URL to remove its cached challenge </param>
        public void RemoveChallengeForURL(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            HttpBearerChallenge httpBearerChallenge = null;

            lock (_cacheLock)
            {
                _cache.TryRemove(uri.FullAuthority(), out httpBearerChallenge);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the challenge for the cached URL.
        /// </summary>
        /// <param name="uri"> the URL that the challenge is cached for.</param>
        /// <returns>the cached challenge or null otherwise.</returns>
        public HttpBearerChallenge GetChallengeForURL(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }

            HttpBearerChallenge value = null;

            lock (_cacheLock)
            {
                _cache.TryGetValue(uri.FullAuthority(), out value);
            }

            return(value);
        }
Esempio n. 4
0
        /// <summary>
        /// Caches the challenge for the specified URL
        /// </summary>
        /// <param name="uri"> URL corresponding to challenge as cache key </param>
        /// <param name="value"> the challenge </param>
        public void SetChallengeForURL(Uri uri, HttpBearerChallenge value)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("url");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (string.Compare(uri.FullAuthority(), value.SourceAuthority, StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new ArgumentException("Source URL and Challenge URL do not match");
            }

            lock (_cacheLock)
            {
                _cache[uri.FullAuthority()] = value;
            }
        }