Exemple #1
0
        public void TokenPollingSettingsInits()
        {
            var timeout  = 1000;
            var interval = 600000;

            var tokenPollingSettings = new TokenPollingSettings()
            {
                Timeout  = timeout,
                Interval = interval,
            };

            Assert.NotNull(tokenPollingSettings);
            Assert.IsType <TokenPollingSettings>(tokenPollingSettings);
            Assert.Equal(timeout, tokenPollingSettings.Timeout);
            Assert.Equal(interval, tokenPollingSettings.Interval);
        }
        private JObject MakeProperties(int?timeout, int?interval)
        {
            var settings = new TokenPollingSettings();

            if (timeout.HasValue)
            {
                settings.Timeout = timeout.Value;
            }

            if (interval.HasValue)
            {
                settings.Interval = interval.Value;
            }

            var properties = new JObject();

            properties[TurnStateConstants.TokenPollingSettingsKey] = JObject.FromObject(settings);

            return(properties);
        }
        private static async Task PollForTokenAsync(BotFrameworkAdapter adapter, ILogger logger, ITurnContext turnContext, Activity activity, string connectionName, CancellationToken cancellationToken)
        {
            TokenResponse tokenResponse           = null;
            bool          shouldEndPolling        = false;
            var           pollingTimeout          = TurnStateConstants.OAuthLoginTimeoutValue;
            var           pollingRequestsInterval = PollingInterval;
            var           loginTimeout            = turnContext.TurnState.Get <object>(TurnStateConstants.OAuthLoginTimeoutKey);
            bool          sentToken = false;

            // Override login timeout with value set from the OAuthPrompt or by the developer
            if (turnContext.TurnState.ContainsKey(TurnStateConstants.OAuthLoginTimeoutKey))
            {
                pollingTimeout = (TimeSpan)turnContext.TurnState.Get <object>(TurnStateConstants.OAuthLoginTimeoutKey);
            }

            var stopwatch   = Stopwatch.StartNew();
            var oauthClient = turnContext.TurnState.Get <OAuthClient>();

            while (stopwatch.Elapsed < pollingTimeout && !shouldEndPolling)
            {
                tokenResponse = await adapter.GetUserTokenAsync(turnContext, oauthClient?.Credentials as AppCredentials, connectionName, null, cancellationToken).ConfigureAwait(false);

                if (tokenResponse != null)
                {
                    // This can be used to short-circuit the polling loop.
                    if (tokenResponse.Properties != null)
                    {
                        JToken tokenPollingSettingsToken          = null;
                        TokenPollingSettings tokenPollingSettings = null;
                        tokenResponse.Properties.TryGetValue(TurnStateConstants.TokenPollingSettingsKey, out tokenPollingSettingsToken);

                        if (tokenPollingSettingsToken != null)
                        {
                            tokenPollingSettings = tokenPollingSettingsToken.ToObject <TokenPollingSettings>();

                            if (tokenPollingSettings != null)
                            {
                                logger.LogInformation($"PollForTokenAsync received new polling settings: timeout={tokenPollingSettings.Timeout}, interval={tokenPollingSettings.Interval}", tokenPollingSettings);
                                shouldEndPolling        = tokenPollingSettings.Timeout <= 0 ? true : shouldEndPolling;                                                            // Timeout now and stop polling
                                pollingRequestsInterval = tokenPollingSettings.Interval > 0 ? TimeSpan.FromMilliseconds(tokenPollingSettings.Interval) : pollingRequestsInterval; // Only overrides if it is set.
                            }
                        }
                    }

                    // once there is a token, send it to the bot and stop polling
                    if (tokenResponse.Token != null)
                    {
                        var tokenResponseActivityEvent = CreateTokenResponse(turnContext.Activity.GetConversationReference(), tokenResponse.Token, connectionName);
                        var identity = turnContext.TurnState.Get <IIdentity>(BotFrameworkAdapter.BotIdentityKey) as ClaimsIdentity;
                        var callback = turnContext.TurnState.Get <BotCallbackHandler>();
                        await adapter.ProcessActivityAsync(identity, tokenResponseActivityEvent, callback, cancellationToken).ConfigureAwait(false);

                        shouldEndPolling = true;
                        sentToken        = true;

                        logger.LogInformation("PollForTokenAsync completed with a token", turnContext.Activity);
                    }
                }

                if (!shouldEndPolling)
                {
                    await Task.Delay(pollingRequestsInterval).ConfigureAwait(false);
                }
            }

            if (!sentToken)
            {
                logger.LogInformation("PollForTokenAsync completed without receiving a token", turnContext.Activity);
            }

            stopwatch.Stop();
        }