public async Task <MsalTokenResponse> AcquireTokenUsingBrokerAsync(Dictionary <string, string> brokerPayload)
        {
            _androidBrokerTokenResponse = null;
            _correlationId = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.CorrelationId);
            //Need to disable warning for non awaited async call.
            try
            {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                new TaskFactory().StartNew(() => AcquireTokenInternalAsync(brokerPayload));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            }
            catch (Exception ex)
            {
                _logger.Error("Broker Operation Failed to complete.");
                if (ex is MsalException)
                {
                    throw;
                }
                else
                {
                    throw new MsalException(MsalError.AndroidBrokerOperationFailed, ex.Message, ex);
                }
            }

            await _readyForResponse.WaitAsync().ConfigureAwait(false);

            return(_androidBrokerTokenResponse);
        }
 public AndroidBroker(CoreUIParent uiParent, ICoreLogger logger)
 {
     _activity         = uiParent?.Activity;
     _logger           = logger ?? throw new ArgumentNullException(nameof(logger));
     _brokerHelper     = new AndroidBrokerHelper(Application.Context, logger);
     _readyForResponse = new SemaphoreSlim(0);
 }
Exemplo n.º 3
0
        public async Task <MsalTokenResponse> AcquireTokenUsingBrokerAsync(Dictionary <string, string> brokerPayload)
        {
            s_androidBrokerTokenResponse = null;
            s_correlationId = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.CorrelationId);

            try
            {
                // This task will kick off the broker and will block on the _readyForResponse semaphore
                // When the broker activity ends, SetBrokerResult is called, which releases the semaphore.
                await AcquireTokenInternalAsync(brokerPayload).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.Error("Broker Operation Failed to complete. In order to perform brokered authentication on android" +
                              " you need to ensure that you have installed either Intune Company Portal (Version 5.0.4689.0 or greater) or Microsoft Authenticator (6.2001.0140 or greater).");
                if (ex is MsalException)
                {
                    throw;
                }
                else
                {
                    throw new MsalClientException(MsalError.AndroidBrokerOperationFailed, ex.Message, ex);
                }
            }

            return(s_androidBrokerTokenResponse);
        }
        private async Task AcquireTokenInternalAsync(IDictionary <string, string> brokerPayload)
        {
            _brokerHelper.InitiateBrokerHandshake(_activity);

            Context mContext = Application.Context;

            brokerPayload[BrokerParameter.BrokerAccountName] = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.LoginHint);

            // Don't send silent background request if account information is not provided
            if (!string.IsNullOrEmpty(AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.BrokerAccountName)) ||
                !string.IsNullOrEmpty(AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.Username)))
            {
                _logger.Verbose("User is specified for silent token request. Starting silent broker request");
                string silentResult = _brokerHelper.GetBrokerAuthTokenSilently(brokerPayload, _activity);
                _androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(silentResult);
                _readyForResponse?.Release();
                return;
            }
            else
            {
                _logger.Verbose("User is not specified for silent token request");
            }

            _logger.Verbose("Starting Android Broker interactive authentication");

            // onActivityResult will receive the response for this activity.
            // Lauching this activity will switch to the broker app.
            Intent brokerIntent = _brokerHelper.GetIntentForInteractiveBrokerRequest(brokerPayload, _activity);

            if (brokerIntent != null)
            {
                try
                {
                    _logger.Info(
                        "Calling activity pid:" + AndroidNative.OS.Process.MyPid()
                        + " tid:" + AndroidNative.OS.Process.MyTid() + "uid:"
                        + AndroidNative.OS.Process.MyUid());

                    _activity.StartActivityForResult(brokerIntent, 1001);
                }
                catch (ActivityNotFoundException e)
                {
                    _logger.ErrorPiiWithPrefix(e, "Unable to get android activity during interactive broker request");
                }
            }

            await _readyForResponse.WaitAsync().ConfigureAwait(false);
        }
        public async Task <MsalTokenResponse> AcquireTokenUsingBrokerAsync(Dictionary <string, string> brokerPayload)
        {
            s_androidBrokerTokenResponse = null;
            s_correlationId = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.CorrelationId);

            try
            {
                // This task will kick off the broker and will block on the _readyForResponse semaphore
                // When the broker activity ends, SetBrokerResult is called, which releases the semaphore.
                await AcquireTokenInternalAsync(brokerPayload).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.Error("Android broker authentication failed.");
                HandleBrokerOperationError(ex);
                throw;
            }

            return(s_androidBrokerTokenResponse);
        }
        private async Task AcquireTokenInternalAsync(IDictionary <string, string> brokerPayload)
        {
            try
            {
                if (brokerPayload.ContainsKey(BrokerParameter.BrokerInstallUrl))
                {
                    _logger.Info("Android Broker - broker payload contains install url");

                    var appLink = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.BrokerInstallUrl);
                    _logger.Info("Android Broker - Starting ActionView activity to " + appLink);
                    _activity.StartActivity(new Intent(Intent.ActionView, AndroidNative.Net.Uri.Parse(appLink)));

                    throw new MsalClientException(
                              MsalError.BrokerApplicationRequired,
                              MsalErrorMessage.BrokerApplicationRequired);
                }
                await _brokerHelper.InitiateBrokerHandshakeAsync(_activity).ConfigureAwait(false);

                brokerPayload[BrokerParameter.BrokerAccountName] = AndroidBrokerHelper.GetValueFromBrokerPayload(brokerPayload, BrokerParameter.Username);

                // Don't send silent background request if account information is not provided
                if (brokerPayload.ContainsKey(BrokerParameter.IsSilentBrokerRequest))
                {
                    _logger.Verbose("User is specified for silent token request. Starting silent broker request.");
                    string silentResult = await _brokerHelper.GetBrokerAuthTokenSilentlyAsync(brokerPayload, _activity).ConfigureAwait(false);

                    if (!string.IsNullOrEmpty(silentResult))
                    {
                        s_androidBrokerTokenResponse = CreateMsalTokenResponseFromResult(silentResult);
                    }
                    else
                    {
                        s_androidBrokerTokenResponse = new MsalTokenResponse
                        {
                            Error            = MsalError.BrokerResponseReturnedError,
                            ErrorDescription = "Failed to acquire token silently from the broker." + MsalErrorMessage.AndroidBrokerCannotBeInvoked,
                        };
                    }
                    return;
                }
                else
                {
                    _logger.Verbose("User is not specified for silent token request");
                }

                _logger.Verbose("Starting Android Broker interactive authentication");

                // onActivityResult will receive the response for this activity.
                // Lauching this activity will switch to the broker app.

                Intent brokerIntent = await _brokerHelper
                                      .GetIntentForInteractiveBrokerRequestAsync(brokerPayload, _activity)
                                      .ConfigureAwait(false);

                if (brokerIntent != null)
                {
                    try
                    {
                        _logger.Info(
                            "Calling activity pid:" + AndroidNative.OS.Process.MyPid()
                            + " tid:" + AndroidNative.OS.Process.MyTid() + "uid:"
                            + AndroidNative.OS.Process.MyUid());

                        _activity.StartActivityForResult(brokerIntent, 1001);
                    }
                    catch (ActivityNotFoundException e)
                    {
                        _logger.ErrorPiiWithPrefix(e, "Unable to get android activity during interactive broker request");
                        throw;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorPiiWithPrefix(ex, "Broker invocation failed.");
                throw;
            }

            await s_readyForResponse.WaitAsync().ConfigureAwait(false);
        }