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

Commit d133a1e

Browse files
Lparrish/o auth prompt (#967)
* 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 <tracyboehrer@users.noreply.github.com>
1 parent f24b0b9 commit d133a1e

38 files changed

Lines changed: 5304 additions & 673 deletions

File tree

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/BotFrameworkAdapter.java

Lines changed: 649 additions & 486 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.bot.builder;
5+
6+
import java.util.concurrent.CompletableFuture;
7+
8+
import com.microsoft.bot.connector.ConnectorClient;
9+
import com.microsoft.bot.connector.authentication.ClaimsIdentity;
10+
11+
/**
12+
* Abstraction to build connector clients.
13+
*/
14+
public interface ConnectorClientBuilder {
15+
16+
/**
17+
* Creates the connector client asynchronous.
18+
* @param serviceUrl The service URL.
19+
* @param claimsIdentity The claims claimsIdentity.
20+
* @param audience The target audience for the connector.
21+
* @return ConnectorClient instance.
22+
*/
23+
CompletableFuture<ConnectorClient> createConnectorClient(String serviceUrl,
24+
ClaimsIdentity claimsIdentity,
25+
String audience);
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MT License.
3+
4+
package com.microsoft.bot.builder;
5+
6+
import java.time.Duration;
7+
8+
/**
9+
* Constants used in TurnState.
10+
*/
11+
public final class TurnStateConstants {
12+
13+
private TurnStateConstants() {
14+
15+
}
16+
17+
/**
18+
* TurnState key for the OAuth login timeout.
19+
*/
20+
public static final String OAUTH_LOGIN_TIMEOUT_KEY = "loginTimeout";
21+
22+
/**
23+
* Name of the token polling settings key.
24+
*/
25+
public static final String TOKEN_POLLING_SETTINGS_KEY = "tokenPollingSettings";
26+
27+
/**
28+
* Default amount of time an OAuthCard will remain active (clickable and
29+
* actively waiting for a token). After this time: (1) the OAuthCard will not
30+
* allow the user to click on it. (2) any polling triggered by the OAuthCard
31+
* will stop.
32+
*/
33+
public static final Duration OAUTH_LOGIN_TIMEOUT_VALUE = Duration.ofMinutes(15);
34+
}

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/UserTokenProvider.java

Lines changed: 238 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
package com.microsoft.bot.builder;
55

6+
import com.microsoft.bot.connector.authentication.AppCredentials;
7+
import com.microsoft.bot.schema.SignInResource;
8+
import com.microsoft.bot.schema.TokenExchangeRequest;
69
import com.microsoft.bot.schema.TokenResponse;
710
import com.microsoft.bot.schema.TokenStatus;
811

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

4447
/**
4548
* Get the raw signin link to be sent to the user for signin for a connection
@@ -53,7 +56,7 @@ CompletableFuture<TokenResponse> getUserToken(
5356
* @return A task that represents the work queued to execute. If the task
5457
* completes successfully, the result contains the raw signin link.
5558
*/
56-
CompletableFuture<String> getOauthSignInLink(
59+
CompletableFuture<String> getOAuthSignInLink(
5760
TurnContext turnContext,
5861
String connectionName,
5962
String userId,
@@ -156,4 +159,237 @@ CompletableFuture<Map<String, TokenResponse>> getAadTokens(
156159
String[] resourceUrls,
157160
String userId
158161
);
162+
163+
164+
/**
165+
* Attempts to retrieve the token for a user that's in a login flow, using
166+
* customized AppCredentials.
167+
*
168+
* @param turnContext Context for the current turn of
169+
* conversation with the user.
170+
* @param oAuthAppCredentials AppCredentials for OAuth.
171+
* @param connectionName Name of the auth connection to use.
172+
* @param magicCode (Optional) Optional user entered code
173+
* to validate.
174+
*
175+
* @return Token Response.
176+
*/
177+
CompletableFuture<TokenResponse> getUserToken(
178+
TurnContext turnContext,
179+
AppCredentials oAuthAppCredentials,
180+
String connectionName,
181+
String magicCode);
182+
183+
/**
184+
* Get the raw signin link to be sent to the user for signin for a
185+
* connection name, using customized AppCredentials.
186+
*
187+
* @param turnContext Context for the current turn of
188+
* conversation with the user.
189+
* @param oAuthAppCredentials AppCredentials for OAuth.
190+
* @param connectionName Name of the auth connection to use.
191+
*
192+
* @return A CompletableFuture that represents the work queued to execute.
193+
*
194+
* If the CompletableFuture completes successfully, the result contains the raw signin
195+
* link.
196+
*/
197+
CompletableFuture<String> getOAuthSignInLink(
198+
TurnContext turnContext,
199+
AppCredentials oAuthAppCredentials,
200+
String connectionName);
201+
202+
/**
203+
* Get the raw signin link to be sent to the user for signin for a
204+
* connection name, using customized AppCredentials.
205+
*
206+
* @param turnContext Context for the current turn of
207+
* conversation with the user.
208+
* @param oAuthAppCredentials AppCredentials for OAuth.
209+
* @param connectionName Name of the auth connection to use.
210+
* @param userId The user id that will be associated
211+
* with the token.
212+
* @param finalRedirect The final URL that the OAuth flow
213+
* will redirect to.
214+
*
215+
* @return A CompletableFuture that represents the work queued to execute.
216+
*
217+
* If the CompletableFuture completes successfully, the result contains the raw signin
218+
* link.
219+
*/
220+
CompletableFuture<String> getOAuthSignInLink(
221+
TurnContext turnContext,
222+
AppCredentials oAuthAppCredentials,
223+
String connectionName,
224+
String userId,
225+
String finalRedirect);
226+
227+
/**
228+
* Signs the user out with the token server, using customized
229+
* AppCredentials.
230+
*
231+
* @param turnContext Context for the current turn of
232+
* conversation with the user.
233+
* @param oAuthAppCredentials AppCredentials for OAuth.
234+
* @param connectionName Name of the auth connection to use.
235+
* @param userId User id of user to sign out.
236+
*
237+
* @return A CompletableFuture that represents the work queued to execute.
238+
*/
239+
CompletableFuture<Void> signOutUser(
240+
TurnContext turnContext,
241+
AppCredentials oAuthAppCredentials,
242+
String connectionName,
243+
String userId);
244+
245+
/**
246+
* Retrieves the token status for each configured connection for the given
247+
* user, using customized AppCredentials.
248+
*
249+
* @param context Context for the current turn of
250+
* conversation with the user.
251+
* @param oAuthAppCredentials AppCredentials for OAuth.
252+
* @param userId The user Id for which token status is
253+
* retrieved.
254+
* @param includeFilter Optional comma separated list of
255+
* connection's to include. Blank will return token status for all
256+
* configured connections.
257+
*
258+
* @return Array of TokenStatus.
259+
*/
260+
CompletableFuture<List<TokenStatus>> getTokenStatus(
261+
TurnContext context,
262+
AppCredentials oAuthAppCredentials,
263+
String userId,
264+
String includeFilter);
265+
266+
/**
267+
* Retrieves Azure Active Directory tokens for particular resources on a
268+
* configured connection, using customized AppCredentials.
269+
*
270+
* @param context Context for the current turn of
271+
* conversation with the user.
272+
* @param oAuthAppCredentials AppCredentials for OAuth.
273+
* @param connectionName The name of the Azure Active
274+
* Directory connection configured with this bot.
275+
* @param resourceUrls The list of resource URLs to retrieve
276+
* tokens for.
277+
* @param userId The user Id for which tokens are
278+
* retrieved. If passing in null the userId is taken from the Activity in
279+
* the TurnContext.
280+
*
281+
* @return Dictionary of resourceUrl to the corresponding
282+
* TokenResponse.
283+
*/
284+
CompletableFuture<Map<String, TokenResponse>> getAadTokens(
285+
TurnContext context,
286+
AppCredentials oAuthAppCredentials,
287+
String connectionName,
288+
String[] resourceUrls,
289+
String userId);
290+
291+
/**
292+
* Get the raw signin link to be sent to the user for signin for a
293+
* connection name.
294+
*
295+
* @param turnContext Context for the current turn of
296+
* conversation with the user.
297+
* @param connectionName Name of the auth connection to use.
298+
*
299+
* @return A CompletableFuture that represents the work queued to execute.
300+
*
301+
* If the CompletableFuture completes successfully, the result contains the raw signin
302+
* link.
303+
*/
304+
CompletableFuture<SignInResource> getSignInResource(
305+
TurnContext turnContext,
306+
String connectionName);
307+
308+
/**
309+
* Get the raw signin link to be sent to the user for signin for a
310+
* connection name.
311+
*
312+
* @param turnContext Context for the current turn of
313+
* conversation with the user.
314+
* @param connectionName Name of the auth connection to use.
315+
* @param userId The user id that will be associated with
316+
* the token.
317+
* @param finalRedirect The final URL that the OAuth flow will
318+
* redirect to.
319+
*
320+
* @return A CompletableFuture that represents the work queued to execute.
321+
*
322+
* If the CompletableFuture completes successfully, the result contains the raw signin
323+
* link.
324+
*/
325+
CompletableFuture<SignInResource> getSignInResource(
326+
TurnContext turnContext,
327+
String connectionName,
328+
String userId,
329+
String finalRedirect);
330+
331+
/**
332+
* Get the raw signin link to be sent to the user for signin for a
333+
* connection name.
334+
*
335+
* @param turnContext Context for the current turn of
336+
* conversation with the user.
337+
* @param oAuthAppCredentials Credentials for OAuth.
338+
* @param connectionName Name of the auth connection to use.
339+
* @param userId The user id that will be associated
340+
* with the token.
341+
* @param finalRedirect The final URL that the OAuth flow
342+
* will redirect to.
343+
*
344+
* @return A CompletableFuture that represents the work queued to execute.
345+
*
346+
* If the CompletableFuture completes successfully, the result contains the raw signin
347+
* link.
348+
*/
349+
CompletableFuture<SignInResource> getSignInResource(
350+
TurnContext turnContext,
351+
AppCredentials oAuthAppCredentials,
352+
String connectionName,
353+
String userId,
354+
String finalRedirect);
355+
356+
/**
357+
* Performs a token exchange operation such as for single sign-on.
358+
*
359+
* @param turnContext Context for the current turn of
360+
* conversation with the user.
361+
* @param connectionName Name of the auth connection to use.
362+
* @param userId The user id associated with the token..
363+
* @param exchangeRequest The exchange request details, either a
364+
* token to exchange or a uri to exchange.
365+
*
366+
* @return If the CompletableFuture completes, the exchanged token is returned.
367+
*/
368+
CompletableFuture<TokenResponse> exchangeToken(
369+
TurnContext turnContext,
370+
String connectionName,
371+
String userId,
372+
TokenExchangeRequest exchangeRequest);
373+
374+
/**
375+
* Performs a token exchange operation such as for single sign-on.
376+
*
377+
* @param turnContext Context for the current turn of
378+
* conversation with the user.
379+
* @param oAuthAppCredentials AppCredentials for OAuth.
380+
* @param connectionName Name of the auth connection to use.
381+
* @param userId The user id associated with the
382+
* token..
383+
* @param exchangeRequest The exchange request details, either
384+
* a token to exchange or a uri to exchange.
385+
*
386+
* @return If the CompletableFuture completes, the exchanged token is returned.
387+
*/
388+
CompletableFuture<TokenResponse> exchangeToken(
389+
TurnContext turnContext,
390+
AppCredentials oAuthAppCredentials,
391+
String connectionName,
392+
String userId,
393+
TokenExchangeRequest exchangeRequest);
394+
159395
}

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/TestAdapterTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.junit.Assert;
1515
import org.junit.Test;
1616

17+
import java.util.List;
1718
import java.util.UUID;
1819
import java.util.concurrent.CompletableFuture;
1920
import java.util.concurrent.CompletionException;
@@ -451,9 +452,9 @@ public void TestAdapter_GetTokenStatus() {
451452
adapter.addUserToken("ABC", channelId, userId, token, null);
452453
adapter.addUserToken("DEF", channelId, userId, token, null);
453454

454-
TokenStatus[] status = adapter.getTokenStatus(turnContext, userId, null).join();
455+
List<TokenStatus> status = adapter.getTokenStatus(turnContext, userId, null).join();
455456
Assert.assertNotNull(status);
456-
Assert.assertEquals(2, status.length);
457+
Assert.assertEquals(2, status.size());
457458
}
458459

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

481-
TokenStatus[] status = adapter.getTokenStatus(turnContext, userId, "DEF").join();
482+
List<TokenStatus> status = adapter.getTokenStatus(turnContext, userId, "DEF").join();
482483
Assert.assertNotNull(status);
483-
Assert.assertEquals(1, status.length);
484+
Assert.assertEquals(1, status.size());
484485
}
485486
}

0 commit comments

Comments
 (0)