private string CreateDeviceCodeRequestUriString()
        {
            var deviceCodeRequestParameters = new DictionaryRequestParameters(this.resource, this.clientKey);

            if (this.callState != null && this.callState.CorrelationId != Guid.Empty)
            {
                deviceCodeRequestParameters[OAuthParameter.CorrelationId] = this.callState.CorrelationId.ToString();
            }

            IDictionary <string, string> adalIdParameters = AdalIdHelper.GetAdalIdParameters();

            foreach (KeyValuePair <string, string> kvp in adalIdParameters)
            {
                deviceCodeRequestParameters[kvp.Key] = kvp.Value;
            }

            if (!string.IsNullOrWhiteSpace(extraQueryParameters))
            {
                // Checks for extraQueryParameters duplicating standard parameters
                Dictionary <string, string> kvps = EncodingHelper.ParseKeyValueList(extraQueryParameters, '&', false, this.callState);
                foreach (KeyValuePair <string, string> kvp in kvps)
                {
                    if (deviceCodeRequestParameters.ContainsKey(kvp.Key))
                    {
                        throw new AdalException(AdalError.DuplicateQueryParameter, string.Format(CultureInfo.CurrentCulture, AdalErrorMessage.DuplicateQueryParameterTemplate, kvp.Key));
                    }
                }

                deviceCodeRequestParameters.ExtraQueryParameter = extraQueryParameters;
            }

            return(new Uri(new Uri(this.authenticator.DeviceCodeUri), "?" + deviceCodeRequestParameters).AbsoluteUri);
        }
        private DictionaryRequestParameters CreateAuthorizationRequest(string loginHint)
        {
            var authorizationRequestParameters = new DictionaryRequestParameters(this.Resource, this.ClientKey);

            authorizationRequestParameters[OAuthParameter.ResponseType] = OAuthResponseType.Code;
            authorizationRequestParameters[OAuthParameter.HasChrome]    = "1";
            authorizationRequestParameters[OAuthParameter.RedirectUri]  = this.redirectUriRequestParameter;

            if (!string.IsNullOrWhiteSpace(loginHint))
            {
                authorizationRequestParameters[OAuthParameter.LoginHint] = loginHint;
            }

            if (!string.IsNullOrWhiteSpace(claims))
            {
                authorizationRequestParameters["claims"] = claims;
            }

            if (this.CallState != null && this.CallState.CorrelationId != Guid.Empty)
            {
                authorizationRequestParameters[OAuthParameter.CorrelationId] = this.CallState.CorrelationId.ToString();
            }

            if (this.authorizationParameters != null)
            {
                platformInformation.AddPromptBehaviorQueryParameter(this.authorizationParameters, authorizationRequestParameters);
            }

            IDictionary <string, string> adalIdParameters = AdalIdHelper.GetAdalIdParameters();

            foreach (KeyValuePair <string, string> kvp in adalIdParameters)
            {
                authorizationRequestParameters[kvp.Key] = kvp.Value;
            }


            if (!string.IsNullOrWhiteSpace(extraQueryParameters))
            {
                // Checks for extraQueryParameters duplicating standard parameters
                Dictionary <string, string> kvps = EncodingHelper.ParseKeyValueList(extraQueryParameters, '&', false, this.CallState);
                foreach (KeyValuePair <string, string> kvp in kvps)
                {
                    if (authorizationRequestParameters.ContainsKey(kvp.Key))
                    {
                        throw new AdalException(AdalError.DuplicateQueryParameter, string.Format(CultureInfo.CurrentCulture, AdalErrorMessage.DuplicateQueryParameterTemplate, kvp.Key));
                    }
                }

                authorizationRequestParameters.ExtraQueryParameter = extraQueryParameters;
            }

            return(authorizationRequestParameters);
        }
Exemple #3
0
        public static void AdalIdTest()
        {
            var adalParameters = AdalIdHelper.GetAdalIdParameters();

            Assert.AreEqual(4, adalParameters.Count);
            Assert.IsNotNull(adalParameters[AdalIdParameter.Product]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.Version]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.CpuPlatform]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.OS]);
            Assert.IsFalse(adalParameters.ContainsKey(AdalIdParameter.DeviceModel));
            adalParameters = AdalIdHelper.GetAdalIdParameters();

            Assert.AreEqual(4, adalParameters.Count);
            Assert.IsNotNull(adalParameters[AdalIdParameter.Product]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.Version]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.CpuPlatform]);
            Assert.IsNotNull(adalParameters[AdalIdParameter.OS]);
            Assert.IsFalse(adalParameters.ContainsKey(AdalIdParameter.DeviceModel));
        }
        private async Task <T> GetResponseAsync <T>(bool respondToDeviceAuthChallenge)
        {
            T typedResponse = default(T);
            IHttpWebResponse response;

            try
            {
                IDictionary <string, string> adalIdHeaders = AdalIdHelper.GetAdalIdParameters();
                foreach (KeyValuePair <string, string> kvp in adalIdHeaders)
                {
                    this.Client.Headers[kvp.Key] = kvp.Value;
                }
                //add pkeyauth header
                this.Client.Headers[DeviceAuthHeaderName] = DeviceAuthHeaderValue;
                using (response = await this.Client.GetResponseAsync().ConfigureAwait(false))
                {
                    typedResponse = EncodingHelper.DeserializeResponse <T>(response.ResponseString);
                }
            }
            catch (HttpRequestWrapperException ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    Resiliency = true;

                    _callState.Logger.Information(this.CallState, "Network timeout, Exception type: " + ex.InnerException.GetType());
                    _callState.Logger.InformationPii(this.CallState, "Network timeout, Exception message: " + ex.InnerException.Message);
                }

                if (!Resiliency && ex.WebResponse == null)
                {
                    _callState.Logger.Error(CallState, ex);
                    _callState.Logger.ErrorPii(CallState, ex);
                    throw new AdalServiceException(AdalError.Unknown, ex);
                }

                //check for resiliency
                if (!Resiliency && (int)ex.WebResponse.StatusCode >= 500 && (int)ex.WebResponse.StatusCode < 600)
                {
                    _callState.Logger.Information(this.CallState,
                                                  "HttpStatus code: " + ex.WebResponse.StatusCode + ", Exception type: " + ex.InnerException?.GetType());

                    _callState.Logger.InformationPii(this.CallState,
                                                     "HttpStatus code: " + ex.WebResponse.StatusCode + ", Exception message: " + ex.InnerException?.Message);

                    Resiliency = true;
                }

                if (Resiliency)
                {
                    if (RetryOnce)
                    {
                        await Task.Delay(DelayTimePeriodMilliSeconds).ConfigureAwait(false);

                        RetryOnce = false;

                        var msg = "Retrying one more time..";
                        _callState.Logger.Information(this.CallState, msg);
                        _callState.Logger.InformationPii(this.CallState, msg);

                        return(await this.GetResponseAsync <T>(respondToDeviceAuthChallenge).ConfigureAwait(false));
                    }

                    _callState.Logger.Information(CallState,
                                                  "Retry Failed, Exception type: " + ex.InnerException?.GetType());
                    _callState.Logger.InformationPii(CallState,
                                                     "Retry Failed, Exception message: " + ex.InnerException?.Message);
                }

                if (!this.IsDeviceAuthChallenge(ex.WebResponse, respondToDeviceAuthChallenge))
                {
                    TokenResponse        tokenResponse = TokenResponse.CreateFromErrorResponse(ex.WebResponse);
                    string[]             errorCodes    = tokenResponse.ErrorCodes ?? new[] { ex.WebResponse.StatusCode.ToString() };
                    AdalServiceException serviceEx     = new AdalServiceException(tokenResponse.Error,
                                                                                  tokenResponse.ErrorDescription,
                                                                                  errorCodes, ex);

                    if (ex.WebResponse.StatusCode == HttpStatusCode.BadRequest &&
                        tokenResponse.Error == AdalError.InteractionRequired)
                    {
                        throw new AdalClaimChallengeException(tokenResponse.Error, tokenResponse.ErrorDescription, ex, tokenResponse.Claims);
                    }

                    throw serviceEx;
                }

                //attempt device auth
                return(await HandleDeviceAuthChallenge <T>(ex.WebResponse).ConfigureAwait(false));
            }

            return(typedResponse);
        }