/// <summary>
        /// Get the raw signin link to be sent to the user for signin for a connection name.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="connectionName">Name of the auth connection to use.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        /// <remarks>If the task completes successfully, the result contains the raw signin link.</remarks>
        public async Task <string> GetOauthSignInLinkAsync(ITurnContext turnContext, string connectionName, CancellationToken cancellationToken)
        {
            BotAssert.ContextNotNull(turnContext);
            if (string.IsNullOrWhiteSpace(connectionName))
            {
                throw new ArgumentNullException(nameof(connectionName));
            }

            var activity = turnContext.Activity;

            var tokenExchangeState = new TokenExchangeState()
            {
                ConnectionName = connectionName,
                Conversation   = new ConversationReference()
                {
                    ActivityId   = activity.Id,
                    Bot          = activity.Recipient, // Activity is from the user to the bot
                    ChannelId    = activity.ChannelId,
                    Conversation = activity.Conversation,
                    ServiceUrl   = activity.ServiceUrl,
                    User         = activity.From,
                },
                MsAppId = (_credentialProvider as MicrosoftAppCredentials)?.MicrosoftAppId,
            };

            var serializedState = JsonConvert.SerializeObject(tokenExchangeState);
            var encodedState    = Encoding.UTF8.GetBytes(serializedState);
            var state           = Convert.ToBase64String(encodedState);

            var client = await CreateOAuthApiClientAsync(turnContext).ConfigureAwait(false);

            return(await client.GetSignInLinkAsync(state, null, cancellationToken).ConfigureAwait(false));
        }
Beispiel #2
0
        public void TokenExchangeStateInits()
        {
            var connectionName = "connectionName";
            var convo          = new ConversationReference(
                "ActivityId",
                new ChannelAccount("userId"),
                new ChannelAccount("bodId"),
                new ConversationAccount(),
                "channelId",
                "serviceUrl");
            var relatesTo = new ConversationReference();
            var botUrl    = "http://localhost:3978";
            var msAppId   = "msAppId";

            var tokenExchangeState = new TokenExchangeState()
            {
                ConnectionName = connectionName,
                Conversation   = convo,
                RelatesTo      = relatesTo,
                BotUrl         = botUrl,
                MsAppId        = msAppId,
            };

            Assert.NotNull(tokenExchangeState);
            Assert.IsType <TokenExchangeState>(tokenExchangeState);
            Assert.Equal(connectionName, tokenExchangeState.ConnectionName);
            Assert.Equal(convo, tokenExchangeState.Conversation);
            Assert.Equal(relatesTo, tokenExchangeState.RelatesTo);
            Assert.Equal(botUrl, tokenExchangeState.BotUrl);
            Assert.Equal(msAppId, tokenExchangeState.MsAppId);
        }
Beispiel #3
0
        public void TokenExchangeStateInitsWithNoArgs()
        {
            var tokenExchangeState = new TokenExchangeState();

            Assert.NotNull(tokenExchangeState);
            Assert.IsType <TokenExchangeState>(tokenExchangeState);
        }
        /// <summary>
        /// Helper function to create the base64 encoded token exchange state used in GetSignInResourceAsync calls.
        /// </summary>
        /// <param name="appId">The appId to include in the token exchange state.</param>
        /// <param name="connectionName">The connectionName to include in the token exchange state.</param>
        /// <param name="activity">The <see cref="Activity"/> from which to derive the token exchange state.</param>
        /// <returns>base64 encoded token exchange state.</returns>
        protected static string CreateTokenExchangeState(string appId, string connectionName, Activity activity)
        {
            _ = appId ?? throw new ArgumentNullException(nameof(appId));
            _ = connectionName ?? throw new ArgumentNullException(nameof(connectionName));
            _ = activity ?? throw new ArgumentNullException(nameof(activity));

            var tokenExchangeState = new TokenExchangeState
            {
                ConnectionName = connectionName,
                Conversation   = activity.GetConversationReference(),
                RelatesTo      = activity.RelatesTo,
                MsAppId        = appId,
            };
            var json = JsonConvert.SerializeObject(tokenExchangeState);

            return(Convert.ToBase64String(Encoding.UTF8.GetBytes(json)));
        }
        /// <summary>
        /// Get the raw signin link to be sent to the user for signin for a connection name.
        /// </summary>
        /// <param name="turnContext">Context for the current turn of conversation with the user.</param>
        /// <param name="connectionName">Name of the auth connection to use.</param>
        /// <param name="userId">The user id that will be associated with the token.</param>
        /// <param name="finalRedirect">The final URL that the OAuth flow will redirect to.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        /// <remarks>If the task completes successfully, the result contains the raw signin link.</remarks>
        public async Task <string> GetOauthSignInLinkAsync(ITurnContext turnContext, string connectionName, string userId, string finalRedirect = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            BotAssert.ContextNotNull(turnContext);

            if (string.IsNullOrWhiteSpace(connectionName))
            {
                throw new ArgumentNullException(nameof(connectionName));
            }

            if (string.IsNullOrWhiteSpace(userId))
            {
                throw new ArgumentNullException(nameof(userId));
            }

            var tokenExchangeState = new TokenExchangeState()
            {
                ConnectionName = connectionName,
                Conversation   = new ConversationReference()
                {
                    ActivityId = null,
                    Bot        = new ChannelAccount {
                        Role = "bot"
                    },
                    ChannelId    = "directline",
                    Conversation = new ConversationAccount(),
                    ServiceUrl   = null,
                    User         = new ChannelAccount {
                        Role = "user", Id = userId,
                    },
                },
                MsAppId = (this._credentialProvider as MicrosoftAppCredentials)?.MicrosoftAppId,
            };

            var serializedState = JsonConvert.SerializeObject(tokenExchangeState);
            var encodedState    = Encoding.UTF8.GetBytes(serializedState);
            var state           = Convert.ToBase64String(encodedState);

            var client = await CreateOAuthApiClientAsync(turnContext).ConfigureAwait(false);

            return(await client.GetSignInLinkAsync(state, finalRedirect, cancellationToken).ConfigureAwait(false));
        }