Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// If user is signed in get token, and optionally run validations on the Token.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <TokenResult> Recognize(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)
                {
                    var adapter = context.Adapter as BotFrameworkAdapter;
                    if (adapter == null)
                    {
                        throw new InvalidOperationException("OAuthPrompt.Recognize(): not supported by the current adapter");
                    }
                    var token = await adapter.GetUserToken(context, _settings.ConnectionName, matched.Value).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
            });
        }