/// <summary>
        /// Get a token for a user signed in.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <TokenResult> GetUserToken(ITurnContext context)
        {
            var adapter = context.Adapter as BotFrameworkAdapter;

            if (adapter == null)
            {
                throw new InvalidOperationException("OAuthPrompt.GetUserToken(): not supported by the current adapter");
            }

            var token = await adapter.GetUserToken(context, _settings.ConnectionName, null).ConfigureAwait(false);

            TokenResult tokenResult = null;

            if (token == null)
            {
                tokenResult = new TokenResult()
                {
                    Status = PromptStatus.NotRecognized
                };
            }
            else
            {
                tokenResult = new TokenResult()
                {
                    Status        = PromptStatus.Recognized,
                    TokenResponse = token
                };
            }
            if (_promptValidator != null)
            {
                await _promptValidator(context, tokenResult).ConfigureAwait(false);
            }
            return(tokenResult);
        }
Exemplo n.º 2
0
        /// <summary>
        /// If user is signed in get token, and optionally run validations on the Token.
        /// </summary>
        /// <param name="context">Context for the current turn of the conversation with the user.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task <TokenResult> RecognizeAsync(ITurnContext context)
        {
            if (IsTokenResponseEvent(context))
            {
                var tokenResponseObject = context.Activity.Value as JObject;
                var tokenResponse       = tokenResponseObject?.ToObject <TokenResponse>();
                return(new TokenResult
                {
                    Status = PromptStatus.Recognized,
                    TokenResponse = tokenResponse,
                });
            }
            else if (context.Activity.Type == ActivityTypes.Message)
            {
                var matched = magicCodeRegex.Match(context.Activity.Text);
                if (matched.Success)
                {
                    if (!(context.Adapter is BotFrameworkAdapter adapter))
                    {
                        throw new InvalidOperationException("OAuthPrompt.Recognize(): not supported by the current adapter");
                    }

                    var token = await adapter.GetUserTokenAsync(context, _settings.ConnectionName, matched.Value, default(CancellationToken)).ConfigureAwait(false);

                    var tokenResult = new TokenResult
                    {
                        Status        = PromptStatus.Recognized,
                        TokenResponse = token,
                    };

                    if (_promptValidator != null)
                    {
                        await _promptValidator(context, tokenResult).ConfigureAwait(false);
                    }

                    return(tokenResult);
                }
            }

            return(new TokenResult {
                Status = PromptStatus.NotRecognized
            });
        }