Ejemplo n.º 1
0
        private async Task <Bundle> GetHandShakeBundleResultFromBrokerAsync()
        {
            var bundle = _brokerHelper.CreateHandShakeOperationBundle();
            var serializedOperationBundle = SerializeBundle(bundle);

            return(await PerformContentResolverOperationAsync(ContentResolverOperation.hello, serializedOperationBundle).ConfigureAwait(false));
        }
        //In order for broker to use the V2 endpoint during authentication, MSAL must initiate a handshake with broker to specify what endpoint should be used for the request.
        public async Task InitiateBrokerHandshakeAsync()
        {
            using (_logger.LogMethodDuration())
            {
                try
                {
                    Bundle helloRequestBundle = _brokerHelper.CreateHandShakeOperationBundle();

                    Bundle helloRequestResult = await PerformAccountManagerOperationAsync(helloRequestBundle).ConfigureAwait(false);

                    if (helloRequestResult != null)
                    {
                        var bpKey = helloRequestResult.GetString(BrokerConstants.NegotiatedBPVersionKey);

                        if (!string.IsNullOrEmpty(bpKey))
                        {
                            _logger.Info("[Android broker] Using broker protocol version: " + bpKey);
                            return;
                        }

                        dynamic errorResult      = JObject.Parse(helloRequestResult.GetString(BrokerConstants.BrokerResultV2));
                        string  errorCode        = null;
                        string  errorDescription = null;

                        if (!string.IsNullOrEmpty(errorResult))
                        {
                            errorCode = errorResult[BrokerResponseConst.BrokerErrorCode]?.ToString();
                            string errorMessage = errorResult[BrokerResponseConst.BrokerErrorMessage]?.ToString();
                            errorDescription = $"[Android broker] An error occurred during hand shake with the broker. Error: {errorCode} Error Message: {errorMessage}";
                        }
                        else
                        {
                            errorCode        = BrokerConstants.BrokerUnknownErrorCode;
                            errorDescription = "[Android broker] An error occurred during hand shake with the broker, no detailed error information was returned. ";
                        }

                        _logger.Error(errorDescription);
                        throw new MsalClientException(errorCode, errorDescription);
                    }

                    throw new MsalClientException("[Android broker] Could not communicate with broker via account manager. Please ensure power optimization settings are turned off. ");
                }
                catch (Exception ex)
                {
                    _logger.Error("[Android broker] Error when trying to initiate communication with the broker. ");
                    if (ex is MsalException)
                    {
                        throw;
                    }

                    throw new MsalClientException(MsalError.BrokerApplicationRequired, MsalErrorMessage.AndroidBrokerCannotBeInvoked, ex);
                }
            }
        }