Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Lparrish/o auth prompt (#967)
Browse files Browse the repository at this point in the history
* Initial Port of OAuthPrompt

* Unit tests started

* Unit Tests

* Completed Unit Tests

* Fix merge issue.

* Complete Auth Sample and fixes to AuthDialog

Co-authored-by: tracyboehrer <[email protected]>
  • Loading branch information
LeeParrishMSFT and tracyboehrer authored Feb 10, 2021
1 parent f24b0b9 commit d133a1e
Show file tree
Hide file tree
Showing 38 changed files with 5,304 additions and 673 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.bot.builder;

import java.util.concurrent.CompletableFuture;

import com.microsoft.bot.connector.ConnectorClient;
import com.microsoft.bot.connector.authentication.ClaimsIdentity;

/**
* Abstraction to build connector clients.
*/
public interface ConnectorClientBuilder {

/**
* Creates the connector client asynchronous.
* @param serviceUrl The service URL.
* @param claimsIdentity The claims claimsIdentity.
* @param audience The target audience for the connector.
* @return ConnectorClient instance.
*/
CompletableFuture<ConnectorClient> createConnectorClient(String serviceUrl,
ClaimsIdentity claimsIdentity,
String audience);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MT License.

package com.microsoft.bot.builder;

import java.time.Duration;

/**
* Constants used in TurnState.
*/
public final class TurnStateConstants {

private TurnStateConstants() {

}

/**
* TurnState key for the OAuth login timeout.
*/
public static final String OAUTH_LOGIN_TIMEOUT_KEY = "loginTimeout";

/**
* Name of the token polling settings key.
*/
public static final String TOKEN_POLLING_SETTINGS_KEY = "tokenPollingSettings";

/**
* Default amount of time an OAuthCard will remain active (clickable and
* actively waiting for a token). After this time: (1) the OAuthCard will not
* allow the user to click on it. (2) any polling triggered by the OAuthCard
* will stop.
*/
public static final Duration OAUTH_LOGIN_TIMEOUT_VALUE = Duration.ofMinutes(15);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.microsoft.bot.builder;

import com.microsoft.bot.connector.authentication.AppCredentials;
import com.microsoft.bot.schema.SignInResource;
import com.microsoft.bot.schema.TokenExchangeRequest;
import com.microsoft.bot.schema.TokenResponse;
import com.microsoft.bot.schema.TokenStatus;

Expand Down Expand Up @@ -39,7 +42,7 @@ CompletableFuture<TokenResponse> getUserToken(
* @return A task that represents the work queued to execute. If the task
* completes successfully, the result contains the raw signin link.
*/
CompletableFuture<String> getOauthSignInLink(TurnContext turnContext, String connectionName);
CompletableFuture<String> getOAuthSignInLink(TurnContext turnContext, String connectionName);

/**
* Get the raw signin link to be sent to the user for signin for a connection
Expand All @@ -53,7 +56,7 @@ CompletableFuture<TokenResponse> getUserToken(
* @return A task that represents the work queued to execute. If the task
* completes successfully, the result contains the raw signin link.
*/
CompletableFuture<String> getOauthSignInLink(
CompletableFuture<String> getOAuthSignInLink(
TurnContext turnContext,
String connectionName,
String userId,
Expand Down Expand Up @@ -156,4 +159,237 @@ CompletableFuture<Map<String, TokenResponse>> getAadTokens(
String[] resourceUrls,
String userId
);


/**
* Attempts to retrieve the token for a user that's in a login flow, using
* customized AppCredentials.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName Name of the auth connection to use.
* @param magicCode (Optional) Optional user entered code
* to validate.
*
* @return Token Response.
*/
CompletableFuture<TokenResponse> getUserToken(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName,
String magicCode);

/**
* Get the raw signin link to be sent to the user for signin for a
* connection name, using customized AppCredentials.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName Name of the auth connection to use.
*
* @return A CompletableFuture that represents the work queued to execute.
*
* If the CompletableFuture completes successfully, the result contains the raw signin
* link.
*/
CompletableFuture<String> getOAuthSignInLink(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName);

/**
* Get the raw signin link to be sent to the user for signin for a
* connection name, using customized AppCredentials.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName Name of the auth connection to use.
* @param userId The user id that will be associated
* with the token.
* @param finalRedirect The final URL that the OAuth flow
* will redirect to.
*
* @return A CompletableFuture that represents the work queued to execute.
*
* If the CompletableFuture completes successfully, the result contains the raw signin
* link.
*/
CompletableFuture<String> getOAuthSignInLink(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName,
String userId,
String finalRedirect);

/**
* Signs the user out with the token server, using customized
* AppCredentials.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName Name of the auth connection to use.
* @param userId User id of user to sign out.
*
* @return A CompletableFuture that represents the work queued to execute.
*/
CompletableFuture<Void> signOutUser(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName,
String userId);

/**
* Retrieves the token status for each configured connection for the given
* user, using customized AppCredentials.
*
* @param context Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param userId The user Id for which token status is
* retrieved.
* @param includeFilter Optional comma separated list of
* connection's to include. Blank will return token status for all
* configured connections.
*
* @return Array of TokenStatus.
*/
CompletableFuture<List<TokenStatus>> getTokenStatus(
TurnContext context,
AppCredentials oAuthAppCredentials,
String userId,
String includeFilter);

/**
* Retrieves Azure Active Directory tokens for particular resources on a
* configured connection, using customized AppCredentials.
*
* @param context Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName The name of the Azure Active
* Directory connection configured with this bot.
* @param resourceUrls The list of resource URLs to retrieve
* tokens for.
* @param userId The user Id for which tokens are
* retrieved. If passing in null the userId is taken from the Activity in
* the TurnContext.
*
* @return Dictionary of resourceUrl to the corresponding
* TokenResponse.
*/
CompletableFuture<Map<String, TokenResponse>> getAadTokens(
TurnContext context,
AppCredentials oAuthAppCredentials,
String connectionName,
String[] resourceUrls,
String userId);

/**
* Get the raw signin link to be sent to the user for signin for a
* connection name.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param connectionName Name of the auth connection to use.
*
* @return A CompletableFuture that represents the work queued to execute.
*
* If the CompletableFuture completes successfully, the result contains the raw signin
* link.
*/
CompletableFuture<SignInResource> getSignInResource(
TurnContext turnContext,
String connectionName);

/**
* Get the raw signin link to be sent to the user for signin for a
* connection name.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param connectionName Name of the auth connection to use.
* @param userId The user id that will be associated with
* the token.
* @param finalRedirect The final URL that the OAuth flow will
* redirect to.
*
* @return A CompletableFuture that represents the work queued to execute.
*
* If the CompletableFuture completes successfully, the result contains the raw signin
* link.
*/
CompletableFuture<SignInResource> getSignInResource(
TurnContext turnContext,
String connectionName,
String userId,
String finalRedirect);

/**
* Get the raw signin link to be sent to the user for signin for a
* connection name.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials Credentials for OAuth.
* @param connectionName Name of the auth connection to use.
* @param userId The user id that will be associated
* with the token.
* @param finalRedirect The final URL that the OAuth flow
* will redirect to.
*
* @return A CompletableFuture that represents the work queued to execute.
*
* If the CompletableFuture completes successfully, the result contains the raw signin
* link.
*/
CompletableFuture<SignInResource> getSignInResource(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName,
String userId,
String finalRedirect);

/**
* Performs a token exchange operation such as for single sign-on.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param connectionName Name of the auth connection to use.
* @param userId The user id associated with the token..
* @param exchangeRequest The exchange request details, either a
* token to exchange or a uri to exchange.
*
* @return If the CompletableFuture completes, the exchanged token is returned.
*/
CompletableFuture<TokenResponse> exchangeToken(
TurnContext turnContext,
String connectionName,
String userId,
TokenExchangeRequest exchangeRequest);

/**
* Performs a token exchange operation such as for single sign-on.
*
* @param turnContext Context for the current turn of
* conversation with the user.
* @param oAuthAppCredentials AppCredentials for OAuth.
* @param connectionName Name of the auth connection to use.
* @param userId The user id associated with the
* token..
* @param exchangeRequest The exchange request details, either
* a token to exchange or a uri to exchange.
*
* @return If the CompletableFuture completes, the exchanged token is returned.
*/
CompletableFuture<TokenResponse> exchangeToken(
TurnContext turnContext,
AppCredentials oAuthAppCredentials,
String connectionName,
String userId,
TokenExchangeRequest exchangeRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -451,9 +452,9 @@ public void TestAdapter_GetTokenStatus() {
adapter.addUserToken("ABC", channelId, userId, token, null);
adapter.addUserToken("DEF", channelId, userId, token, null);

TokenStatus[] status = adapter.getTokenStatus(turnContext, userId, null).join();
List<TokenStatus> status = adapter.getTokenStatus(turnContext, userId, null).join();
Assert.assertNotNull(status);
Assert.assertEquals(2, status.length);
Assert.assertEquals(2, status.size());
}

@Test
Expand All @@ -478,8 +479,8 @@ public void TestAdapter_GetTokenStatusWithFilter() {
adapter.addUserToken("ABC", channelId, userId, token, null);
adapter.addUserToken("DEF", channelId, userId, token, null);

TokenStatus[] status = adapter.getTokenStatus(turnContext, userId, "DEF").join();
List<TokenStatus> status = adapter.getTokenStatus(turnContext, userId, "DEF").join();
Assert.assertNotNull(status);
Assert.assertEquals(1, status.length);
Assert.assertEquals(1, status.size());
}
}
Loading

0 comments on commit d133a1e

Please sign in to comment.