@microsoft/agents-hosting 1.1.0-alpha.5 → 1.1.0-alpha.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/package.json +10 -6
- package/dist/src/activityWireCompat.d.ts +1 -1
- package/dist/src/activityWireCompat.js +11 -3
- package/dist/src/activityWireCompat.js.map +1 -1
- package/dist/src/agent-client/agentClient.js +7 -3
- package/dist/src/agent-client/agentClient.js.map +1 -1
- package/dist/src/agent-client/agentResponseHandler.js +6 -2
- package/dist/src/agent-client/agentResponseHandler.js.map +1 -1
- package/dist/src/app/agentApplication.d.ts +28 -13
- package/dist/src/app/agentApplication.js +93 -82
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +2 -2
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +9 -2
- package/dist/src/app/appRoute.d.ts +7 -0
- package/dist/src/app/{authorization.d.ts → auth/authorization.d.ts} +33 -139
- package/dist/src/app/auth/authorization.js +188 -0
- package/dist/src/app/auth/authorization.js.map +1 -0
- package/dist/src/app/auth/authorizationManager.d.ts +71 -0
- package/dist/src/app/auth/authorizationManager.js +170 -0
- package/dist/src/app/auth/authorizationManager.js.map +1 -0
- package/dist/src/app/auth/handlerStorage.d.ts +36 -0
- package/dist/src/app/auth/handlerStorage.js +62 -0
- package/dist/src/app/auth/handlerStorage.js.map +1 -0
- package/dist/src/app/auth/handlers/agenticAuthorization.d.ts +93 -0
- package/dist/src/app/auth/handlers/agenticAuthorization.js +134 -0
- package/dist/src/app/auth/handlers/agenticAuthorization.js.map +1 -0
- package/dist/src/app/auth/handlers/azureBotAuthorization.d.ts +222 -0
- package/dist/src/app/auth/handlers/azureBotAuthorization.js +428 -0
- package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -0
- package/dist/src/app/auth/handlers/index.d.ts +2 -0
- package/dist/src/app/auth/handlers/index.js +19 -0
- package/dist/src/app/auth/handlers/index.js.map +1 -0
- package/dist/src/app/auth/index.d.ts +2 -0
- package/dist/src/app/auth/index.js +19 -0
- package/dist/src/app/auth/index.js.map +1 -0
- package/dist/src/app/auth/types.d.ts +104 -0
- package/dist/src/app/auth/types.js +24 -0
- package/dist/src/app/auth/types.js.map +1 -0
- package/dist/src/app/index.d.ts +2 -3
- package/dist/src/app/index.js +2 -3
- package/dist/src/app/index.js.map +1 -1
- package/dist/src/app/routeList.d.ts +1 -1
- package/dist/src/app/routeList.js +22 -5
- package/dist/src/app/routeList.js.map +1 -1
- package/dist/src/app/streaming/streamingResponse.js +2 -1
- package/dist/src/app/streaming/streamingResponse.js.map +1 -1
- package/dist/src/auth/MemoryCache.d.ts +16 -0
- package/dist/src/auth/MemoryCache.js +58 -0
- package/dist/src/auth/MemoryCache.js.map +1 -0
- package/dist/src/auth/authConfiguration.d.ts +44 -2
- package/dist/src/auth/authConfiguration.js +209 -53
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/authConstants.d.ts +11 -0
- package/dist/src/auth/authConstants.js +15 -0
- package/dist/src/auth/authConstants.js.map +1 -0
- package/dist/src/auth/authProvider.d.ts +23 -0
- package/dist/src/auth/connections.d.ts +41 -0
- package/dist/src/auth/connections.js +7 -0
- package/dist/src/auth/connections.js.map +1 -0
- package/dist/src/auth/index.d.ts +2 -0
- package/dist/src/auth/index.js +2 -0
- package/dist/src/auth/index.js.map +1 -1
- package/dist/src/auth/jwt-middleware.js +31 -18
- package/dist/src/auth/jwt-middleware.js.map +1 -1
- package/dist/src/auth/msalConnectionManager.d.ts +64 -0
- package/dist/src/auth/msalConnectionManager.js +148 -0
- package/dist/src/auth/msalConnectionManager.js.map +1 -0
- package/dist/src/auth/msalTokenProvider.d.ts +31 -0
- package/dist/src/auth/msalTokenProvider.js +167 -16
- package/dist/src/auth/msalTokenProvider.js.map +1 -1
- package/dist/src/baseAdapter.d.ts +10 -25
- package/dist/src/baseAdapter.js +2 -15
- package/dist/src/baseAdapter.js.map +1 -1
- package/dist/src/cloudAdapter.d.ts +40 -23
- package/dist/src/cloudAdapter.js +128 -60
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/connector-client/connectorClient.d.ts +15 -0
- package/dist/src/connector-client/connectorClient.js +49 -15
- package/dist/src/connector-client/connectorClient.js.map +1 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.js +0 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/oauth/customUserTokenAPI.d.ts +1 -0
- package/dist/src/oauth/customUserTokenAPI.js +11 -0
- package/dist/src/oauth/customUserTokenAPI.js.map +1 -0
- package/dist/src/oauth/index.d.ts +0 -1
- package/dist/src/oauth/index.js +0 -1
- package/dist/src/oauth/index.js.map +1 -1
- package/dist/src/oauth/userTokenClient.d.ts +30 -13
- package/dist/src/oauth/userTokenClient.js +60 -26
- package/dist/src/oauth/userTokenClient.js.map +1 -1
- package/dist/src/oauth/userTokenClient.types.d.ts +19 -6
- package/dist/src/turnContext.d.ts +7 -1
- package/dist/src/turnContext.js +11 -4
- package/dist/src/turnContext.js.map +1 -1
- package/package.json +10 -6
- package/src/activityWireCompat.ts +12 -4
- package/src/agent-client/agentClient.ts +9 -3
- package/src/agent-client/agentResponseHandler.ts +5 -2
- package/src/app/agentApplication.ts +98 -77
- package/src/app/agentApplicationBuilder.ts +2 -2
- package/src/app/agentApplicationOptions.ts +10 -2
- package/src/app/appRoute.ts +8 -0
- package/src/app/auth/authorization.ts +252 -0
- package/src/app/auth/authorizationManager.ts +213 -0
- package/src/app/auth/handlerStorage.ts +61 -0
- package/src/app/auth/handlers/agenticAuthorization.ts +182 -0
- package/src/app/auth/handlers/azureBotAuthorization.ts +599 -0
- package/src/app/auth/handlers/index.ts +2 -0
- package/src/app/auth/index.ts +2 -0
- package/src/app/auth/types.ts +111 -0
- package/src/app/index.ts +2 -3
- package/src/app/routeList.ts +24 -5
- package/src/app/streaming/streamingResponse.ts +2 -1
- package/src/auth/MemoryCache.ts +59 -0
- package/src/auth/authConfiguration.ts +245 -52
- package/src/auth/authConstants.ts +11 -0
- package/src/auth/authProvider.ts +31 -0
- package/src/auth/connections.ts +47 -0
- package/src/auth/index.ts +2 -0
- package/src/auth/jwt-middleware.ts +38 -21
- package/src/auth/msalConnectionManager.ts +175 -0
- package/src/auth/msalTokenProvider.ts +209 -9
- package/src/baseAdapter.ts +10 -29
- package/src/cloudAdapter.ts +189 -71
- package/src/connector-client/connectorClient.ts +59 -15
- package/src/index.ts +0 -1
- package/src/oauth/customUserTokenAPI.ts +5 -0
- package/src/oauth/index.ts +0 -1
- package/src/oauth/userTokenClient.ts +74 -22
- package/src/oauth/userTokenClient.types.ts +20 -8
- package/src/turnContext.ts +16 -5
- package/dist/src/app/authorization.js +0 -387
- package/dist/src/app/authorization.js.map +0 -1
- package/dist/src/claimsIdentity.d.ts +0 -35
- package/dist/src/claimsIdentity.js +0 -43
- package/dist/src/claimsIdentity.js.map +0 -1
- package/dist/src/oauth/oAuthFlow.d.ts +0 -119
- package/dist/src/oauth/oAuthFlow.js +0 -316
- package/dist/src/oauth/oAuthFlow.js.map +0 -1
- package/src/app/authorization.ts +0 -432
- package/src/claimsIdentity.ts +0 -47
- package/src/oauth/oAuthFlow.ts +0 -378
package/src/app/authorization.ts
DELETED
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { TurnContext } from '../turnContext'
|
|
7
|
-
import { debug } from '@microsoft/agents-activity/logger'
|
|
8
|
-
import { TurnState } from './turnState'
|
|
9
|
-
import { Storage } from '../storage'
|
|
10
|
-
import { OAuthFlow, TokenResponse, UserTokenClient } from '../oauth'
|
|
11
|
-
import { AuthConfiguration, loadAuthConfigFromEnv, MsalTokenProvider } from '../auth'
|
|
12
|
-
import jwt, { JwtPayload } from 'jsonwebtoken'
|
|
13
|
-
import { Activity } from '@microsoft/agents-activity'
|
|
14
|
-
|
|
15
|
-
const logger = debug('agents:authorization')
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Represents the state of a sign-in process.
|
|
19
|
-
* @interface SignInState
|
|
20
|
-
*/
|
|
21
|
-
export interface SignInState {
|
|
22
|
-
/** Optional activity to continue with after sign-in completion. */
|
|
23
|
-
continuationActivity?: Activity,
|
|
24
|
-
/** Identifier of the auth handler being used. */
|
|
25
|
-
handlerId?: string,
|
|
26
|
-
/** Whether the sign-in process has been completed. */
|
|
27
|
-
completed?: boolean
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Interface defining an authorization handler for OAuth flows.
|
|
32
|
-
* @interface AuthHandler
|
|
33
|
-
*/
|
|
34
|
-
export interface AuthHandler {
|
|
35
|
-
/** Connection name for the auth provider. */
|
|
36
|
-
name?: string,
|
|
37
|
-
/** The OAuth flow implementation. */
|
|
38
|
-
flow?: OAuthFlow,
|
|
39
|
-
/** Title to display on auth cards/UI. */
|
|
40
|
-
title?: string,
|
|
41
|
-
/** Text to display on auth cards/UI. */
|
|
42
|
-
text?: string,
|
|
43
|
-
|
|
44
|
-
cnxPrefix?: string
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Options for configuring user authorization.
|
|
49
|
-
* Contains settings to configure OAuth connections.
|
|
50
|
-
* @interface AuthorizationHandlers
|
|
51
|
-
*/
|
|
52
|
-
export interface AuthorizationHandlers extends Record<string, AuthHandler> {}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Class responsible for managing authorization and OAuth flows.
|
|
56
|
-
* Handles multiple OAuth providers and manages the complete authentication lifecycle.
|
|
57
|
-
*
|
|
58
|
-
* @remarks
|
|
59
|
-
* The Authorization class provides a centralized way to handle OAuth authentication
|
|
60
|
-
* flows within the agent application. It supports multiple authentication handlers,
|
|
61
|
-
* token exchange, on-behalf-of flows, and provides event handlers for success/failure scenarios.
|
|
62
|
-
*
|
|
63
|
-
* Key features:
|
|
64
|
-
* - Multiple OAuth provider support
|
|
65
|
-
* - Token caching and exchange
|
|
66
|
-
* - On-behalf-of (OBO) token flows
|
|
67
|
-
* - Sign-in success/failure event handling
|
|
68
|
-
* - Automatic configuration from environment variables
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```typescript
|
|
72
|
-
* const auth = new Authorization(storage, {
|
|
73
|
-
* 'microsoft': {
|
|
74
|
-
* name: 'Microsoft',
|
|
75
|
-
* title: 'Sign in with Microsoft',
|
|
76
|
-
* text: 'Please sign in'
|
|
77
|
-
* }
|
|
78
|
-
* });
|
|
79
|
-
*
|
|
80
|
-
* auth.onSignInSuccess(async (context, state) => {
|
|
81
|
-
* await context.sendActivity('Welcome! You are now signed in.');
|
|
82
|
-
* });
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
*/
|
|
86
|
-
export class Authorization {
|
|
87
|
-
/**
|
|
88
|
-
* Dictionary of configured authentication handlers.
|
|
89
|
-
* @public
|
|
90
|
-
*/
|
|
91
|
-
authHandlers: AuthorizationHandlers
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Creates a new instance of Authorization.
|
|
95
|
-
*
|
|
96
|
-
* @remarks
|
|
97
|
-
* The constructor initializes all configured auth handlers and sets up OAuth flows.
|
|
98
|
-
* It automatically configures handler properties from environment variables if not provided:
|
|
99
|
-
* - Connection name: {handlerId}_connectionName
|
|
100
|
-
* - Connection title: {handlerId}_connectionTitle
|
|
101
|
-
* - Connection text: {handlerId}_connectionText
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```typescript
|
|
105
|
-
* const auth = new Authorization(storage, {
|
|
106
|
-
* 'microsoft': {
|
|
107
|
-
* name: 'Microsoft',
|
|
108
|
-
* title: 'Sign in with Microsoft'
|
|
109
|
-
* },
|
|
110
|
-
* 'google': {
|
|
111
|
-
* // Will use GOOGLE_connectionName from env vars
|
|
112
|
-
* }
|
|
113
|
-
* });
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* @param storage - The storage system to use for state management.
|
|
117
|
-
* @param authHandlers - Configuration for OAuth providers.
|
|
118
|
-
* @throws {Error} If storage is null/undefined or no auth handlers are provided.
|
|
119
|
-
*
|
|
120
|
-
*/
|
|
121
|
-
constructor (private storage: Storage, authHandlers: AuthorizationHandlers, userTokenClient: UserTokenClient) {
|
|
122
|
-
if (storage === undefined || storage === null) {
|
|
123
|
-
throw new Error('Storage is required for UserAuthorization')
|
|
124
|
-
}
|
|
125
|
-
if (authHandlers === undefined || Object.keys(authHandlers).length === 0) {
|
|
126
|
-
throw new Error('The authorization does not have any auth handlers')
|
|
127
|
-
}
|
|
128
|
-
this.authHandlers = authHandlers
|
|
129
|
-
for (const ah in this.authHandlers) {
|
|
130
|
-
if (this.authHandlers![ah].name === undefined && process.env[ah + '_connectionName'] === undefined) {
|
|
131
|
-
throw new Error(`AuthHandler name ${ah}_connectionName not set in autorization and not found in env vars.`)
|
|
132
|
-
}
|
|
133
|
-
const currentAuthHandler = this.authHandlers![ah]
|
|
134
|
-
currentAuthHandler.name = currentAuthHandler.name ?? process.env[ah + '_connectionName'] as string
|
|
135
|
-
currentAuthHandler.title = currentAuthHandler.title ?? process.env[ah + '_connectionTitle'] as string
|
|
136
|
-
currentAuthHandler.text = currentAuthHandler.text ?? process.env[ah + '_connectionText'] as string
|
|
137
|
-
currentAuthHandler.cnxPrefix = currentAuthHandler.cnxPrefix ?? process.env[ah + '_cnxPrefix'] as string
|
|
138
|
-
currentAuthHandler.flow = new OAuthFlow(this.storage, currentAuthHandler.name, userTokenClient, currentAuthHandler.title, currentAuthHandler.text)
|
|
139
|
-
}
|
|
140
|
-
logger.info('Authorization handlers configured with', Object.keys(this.authHandlers).length, 'handlers')
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Gets the token for a specific auth handler.
|
|
145
|
-
*
|
|
146
|
-
* @param context - The context object for the current turn.
|
|
147
|
-
* @param authHandlerId - ID of the auth handler to use.
|
|
148
|
-
* @returns A promise that resolves to the token response from the OAuth provider.
|
|
149
|
-
* @throws {Error} If the auth handler is not configured.
|
|
150
|
-
*
|
|
151
|
-
* @remarks
|
|
152
|
-
* This method retrieves an existing token for the specified auth handler.
|
|
153
|
-
* The token may be cached and will be retrieved from the OAuth provider if needed.
|
|
154
|
-
*
|
|
155
|
-
* @example
|
|
156
|
-
* ```typescript
|
|
157
|
-
* const tokenResponse = await auth.getToken(context, 'microsoft');
|
|
158
|
-
* if (tokenResponse.token) {
|
|
159
|
-
* console.log('User is authenticated');
|
|
160
|
-
* }
|
|
161
|
-
* ```
|
|
162
|
-
*
|
|
163
|
-
* @public
|
|
164
|
-
*/
|
|
165
|
-
public async getToken (context: TurnContext, authHandlerId: string): Promise<TokenResponse> {
|
|
166
|
-
logger.info('getToken from user token service for authHandlerId:', authHandlerId)
|
|
167
|
-
const authHandler = this.getAuthHandlerOrThrow(authHandlerId)
|
|
168
|
-
return await authHandler.flow?.getUserToken(context)!
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Gets the auth handler by ID or throws an error if not found.
|
|
173
|
-
*
|
|
174
|
-
* @param authHandlerId - ID of the auth handler to retrieve.
|
|
175
|
-
* @returns The auth handler instance.
|
|
176
|
-
* @throws {Error} If the auth handler with the specified ID is not configured.
|
|
177
|
-
* @private
|
|
178
|
-
*/
|
|
179
|
-
private getAuthHandlerOrThrow (authHandlerId: string): AuthHandler {
|
|
180
|
-
if (!Object.prototype.hasOwnProperty.call(this.authHandlers, authHandlerId)) {
|
|
181
|
-
throw new Error(`AuthHandler with ID ${authHandlerId} not configured`)
|
|
182
|
-
}
|
|
183
|
-
return this.authHandlers[authHandlerId]
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Exchanges a token for a new token with different scopes.
|
|
188
|
-
*
|
|
189
|
-
* @param context - The context object for the current turn.
|
|
190
|
-
* @param scopes - Array of scopes to request for the new token.
|
|
191
|
-
* @param authHandlerId - ID of the auth handler to use.
|
|
192
|
-
* @returns A promise that resolves to the exchanged token response.
|
|
193
|
-
* @throws {Error} If the auth handler is not configured.
|
|
194
|
-
*
|
|
195
|
-
* @remarks
|
|
196
|
-
* This method handles token exchange scenarios, particularly for on-behalf-of (OBO) flows.
|
|
197
|
-
* It checks if the current token is exchangeable (e.g., has audience starting with 'api://')
|
|
198
|
-
* and performs the appropriate token exchange using MSAL.
|
|
199
|
-
*
|
|
200
|
-
* @example
|
|
201
|
-
* ```typescript
|
|
202
|
-
* const exchangedToken = await auth.exchangeToken(
|
|
203
|
-
* context,
|
|
204
|
-
* ['https://graph.microsoft.com/.default'],
|
|
205
|
-
* 'microsoft'
|
|
206
|
-
* );
|
|
207
|
-
* ```
|
|
208
|
-
*
|
|
209
|
-
* @public
|
|
210
|
-
*/
|
|
211
|
-
public async exchangeToken (context: TurnContext, scopes: string[], authHandlerId: string): Promise<TokenResponse> {
|
|
212
|
-
logger.info('exchangeToken from user token service for authHandlerId:', authHandlerId)
|
|
213
|
-
const authHandler = this.getAuthHandlerOrThrow(authHandlerId)
|
|
214
|
-
const tokenResponse = await authHandler.flow?.getUserToken(context)!
|
|
215
|
-
if (this.isExchangeable(tokenResponse.token)) {
|
|
216
|
-
return await this.handleObo(context, tokenResponse.token!, scopes, authHandler.cnxPrefix)
|
|
217
|
-
}
|
|
218
|
-
return tokenResponse
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Checks if a token is exchangeable for an on-behalf-of flow.
|
|
223
|
-
*
|
|
224
|
-
* @param token - The token to check.
|
|
225
|
-
* @returns True if the token is exchangeable, false otherwise.
|
|
226
|
-
* @private
|
|
227
|
-
*/
|
|
228
|
-
private isExchangeable (token: string | undefined): boolean {
|
|
229
|
-
if (!token || typeof token !== 'string') {
|
|
230
|
-
return false
|
|
231
|
-
}
|
|
232
|
-
const payload = jwt.decode(token) as JwtPayload
|
|
233
|
-
return payload?.aud?.indexOf('api://') === 0
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Handles on-behalf-of token exchange using MSAL.
|
|
238
|
-
*
|
|
239
|
-
* @param context - The context object for the current turn.
|
|
240
|
-
* @param token - The token to exchange.
|
|
241
|
-
* @param scopes - Array of scopes to request for the new token.
|
|
242
|
-
* @returns A promise that resolves to the exchanged token response.
|
|
243
|
-
* @private
|
|
244
|
-
*/
|
|
245
|
-
private async handleObo (context: TurnContext, token: string, scopes: string[], cnxPrefix?: string): Promise<TokenResponse> {
|
|
246
|
-
const msalTokenProvider = new MsalTokenProvider()
|
|
247
|
-
let authConfig: AuthConfiguration = context.adapter.authConfig
|
|
248
|
-
if (cnxPrefix) {
|
|
249
|
-
authConfig = loadAuthConfigFromEnv(cnxPrefix)
|
|
250
|
-
}
|
|
251
|
-
const newToken = await msalTokenProvider.acquireTokenOnBehalfOf(authConfig, scopes, token)
|
|
252
|
-
return { token: newToken }
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Begins or continues an OAuth flow.
|
|
257
|
-
*
|
|
258
|
-
* @param context - The context object for the current turn.
|
|
259
|
-
* @param state - The state object for the current turn.
|
|
260
|
-
* @param authHandlerId - ID of the auth handler to use.
|
|
261
|
-
* @returns A promise that resolves to the token response from the OAuth provider.
|
|
262
|
-
* @throws {Error} If the auth handler is not configured.
|
|
263
|
-
*
|
|
264
|
-
* @remarks
|
|
265
|
-
* This method manages the complete OAuth authentication flow:
|
|
266
|
-
* - If no flow is active, it begins a new OAuth flow and shows the sign-in card
|
|
267
|
-
* - If a flow is active, it continues the flow and processes the authentication response
|
|
268
|
-
* - Handles success/failure callbacks and updates the sign-in state accordingly
|
|
269
|
-
*
|
|
270
|
-
* The method automatically manages the sign-in state and continuation activities,
|
|
271
|
-
* allowing the conversation to resume after successful authentication.
|
|
272
|
-
*
|
|
273
|
-
* @example
|
|
274
|
-
* ```typescript
|
|
275
|
-
* const tokenResponse = await auth.beginOrContinueFlow(context, state, 'microsoft');
|
|
276
|
-
* if (tokenResponse && tokenResponse.token) {
|
|
277
|
-
* // User is now authenticated
|
|
278
|
-
* await context.sendActivity('Authentication successful!');
|
|
279
|
-
* }
|
|
280
|
-
* ```
|
|
281
|
-
*
|
|
282
|
-
* @public
|
|
283
|
-
*/
|
|
284
|
-
public async beginOrContinueFlow (context: TurnContext, state: TurnState, authHandlerId: string, secRoute: boolean = true) : Promise<TokenResponse> {
|
|
285
|
-
const authHandler = this.getAuthHandlerOrThrow(authHandlerId)
|
|
286
|
-
logger.info('beginOrContinueFlow for authHandlerId:', authHandlerId)
|
|
287
|
-
const signInState: SignInState | undefined = state.getValue('user.__SIGNIN_STATE_') || { continuationActivity: undefined, handlerId: undefined, completed: false }
|
|
288
|
-
const flow = authHandler.flow!
|
|
289
|
-
let tokenResponse: TokenResponse | undefined
|
|
290
|
-
tokenResponse = await authHandler.flow?.getUserToken(context)
|
|
291
|
-
|
|
292
|
-
if (tokenResponse?.token && tokenResponse.token.length > 0) {
|
|
293
|
-
delete authHandler.flow?.state.eTag
|
|
294
|
-
authHandler.flow!.state.flowStarted = false
|
|
295
|
-
await authHandler.flow?.setFlowState(context, authHandler.flow.state)
|
|
296
|
-
if (secRoute) {
|
|
297
|
-
return tokenResponse!
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if (flow.state === null || flow.state?.flowStarted === false || flow.state?.flowStarted === undefined) {
|
|
302
|
-
tokenResponse = await flow.beginFlow(context)
|
|
303
|
-
if (secRoute && tokenResponse?.token === undefined) {
|
|
304
|
-
signInState!.continuationActivity = context.activity
|
|
305
|
-
signInState!.handlerId = authHandlerId
|
|
306
|
-
state.setValue('user.__SIGNIN_STATE_', signInState)
|
|
307
|
-
}
|
|
308
|
-
} else {
|
|
309
|
-
tokenResponse = await flow.continueFlow(context)
|
|
310
|
-
if (tokenResponse && tokenResponse.token) {
|
|
311
|
-
if (this._signInSuccessHandler) {
|
|
312
|
-
await this._signInSuccessHandler(context, state, authHandlerId)
|
|
313
|
-
}
|
|
314
|
-
if (secRoute) {
|
|
315
|
-
state.deleteValue('user.__SIGNIN_STATE_')
|
|
316
|
-
}
|
|
317
|
-
} else {
|
|
318
|
-
logger.warn('Failed to complete OAuth flow, no token received')
|
|
319
|
-
if (this._signInFailureHandler) {
|
|
320
|
-
await this._signInFailureHandler(context, state, authHandlerId, 'Failed to complete the OAuth flow')
|
|
321
|
-
}
|
|
322
|
-
// signInState!.completed = false
|
|
323
|
-
// state.setValue('user.__SIGNIN_STATE_', signInState)
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return tokenResponse!
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Signs out the current user.
|
|
331
|
-
*
|
|
332
|
-
* @param context - The context object for the current turn.
|
|
333
|
-
* @param state - The state object for the current turn.
|
|
334
|
-
* @param authHandlerId - Optional ID of the auth handler to use for sign out. If not provided, signs out from all handlers.
|
|
335
|
-
* @returns A promise that resolves when sign out is complete.
|
|
336
|
-
* @throws {Error} If the specified auth handler is not configured.
|
|
337
|
-
*
|
|
338
|
-
* @remarks
|
|
339
|
-
* This method clears the user's token and resets the authentication state.
|
|
340
|
-
* If no specific authHandlerId is provided, it signs out from all configured handlers.
|
|
341
|
-
* This ensures complete cleanup of authentication state across all providers.
|
|
342
|
-
*
|
|
343
|
-
* @example
|
|
344
|
-
* ```typescript
|
|
345
|
-
* // Sign out from specific handler
|
|
346
|
-
* await auth.signOut(context, state, 'microsoft');
|
|
347
|
-
*
|
|
348
|
-
* // Sign out from all handlers
|
|
349
|
-
* await auth.signOut(context, state);
|
|
350
|
-
* ```
|
|
351
|
-
*
|
|
352
|
-
* @public
|
|
353
|
-
*/
|
|
354
|
-
async signOut (context: TurnContext, state: TurnState, authHandlerId?: string) : Promise<void> {
|
|
355
|
-
logger.info('signOut for authHandlerId:', authHandlerId)
|
|
356
|
-
if (authHandlerId === undefined) { // aw
|
|
357
|
-
for (const ah in this.authHandlers) {
|
|
358
|
-
const flow = this.authHandlers[ah].flow
|
|
359
|
-
await flow?.signOut(context)
|
|
360
|
-
}
|
|
361
|
-
} else {
|
|
362
|
-
const authHandler = this.getAuthHandlerOrThrow(authHandlerId)
|
|
363
|
-
await authHandler.flow?.signOut(context)
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Private handler for successful sign-in events.
|
|
369
|
-
* @private
|
|
370
|
-
*/
|
|
371
|
-
_signInSuccessHandler: ((context: TurnContext, state: TurnState, authHandlerId?: string) => Promise<void>) | null = null
|
|
372
|
-
|
|
373
|
-
/**
|
|
374
|
-
* Sets a handler to be called when sign-in is successfully completed.
|
|
375
|
-
*
|
|
376
|
-
* @param handler - The handler function to call on successful sign-in.
|
|
377
|
-
*
|
|
378
|
-
* @remarks
|
|
379
|
-
* This method allows you to register a callback that will be invoked whenever
|
|
380
|
-
* a user successfully completes the authentication process. The handler receives
|
|
381
|
-
* the turn context, state, and the ID of the auth handler that was used.
|
|
382
|
-
*
|
|
383
|
-
* @example
|
|
384
|
-
* ```typescript
|
|
385
|
-
* auth.onSignInSuccess(async (context, state, authHandlerId) => {
|
|
386
|
-
* await context.sendActivity(`Welcome! You signed in using ${authHandlerId}.`);
|
|
387
|
-
* // Perform any post-authentication setup
|
|
388
|
-
* });
|
|
389
|
-
* ```
|
|
390
|
-
*
|
|
391
|
-
* @public
|
|
392
|
-
*/
|
|
393
|
-
public onSignInSuccess (handler: (context: TurnContext, state: TurnState, authHandlerId?: string) => Promise<void>) {
|
|
394
|
-
this._signInSuccessHandler = handler
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
/**
|
|
398
|
-
* Private handler for failed sign-in events.
|
|
399
|
-
* @private
|
|
400
|
-
*/
|
|
401
|
-
_signInFailureHandler: ((context: TurnContext, state: TurnState, authHandlerId?: string, errorMessage?: string) => Promise<void>) | null = null
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Sets a handler to be called when sign-in fails.
|
|
405
|
-
*
|
|
406
|
-
* @param handler - The handler function to call on sign-in failure.
|
|
407
|
-
*
|
|
408
|
-
* @remarks
|
|
409
|
-
* This method allows you to register a callback that will be invoked whenever
|
|
410
|
-
* a user's authentication attempt fails. The handler receives the turn context,
|
|
411
|
-
* state, auth handler ID, and an optional error message describing the failure.
|
|
412
|
-
*
|
|
413
|
-
* Common failure scenarios include:
|
|
414
|
-
* - User cancels the authentication process
|
|
415
|
-
* - Invalid credentials or expired tokens
|
|
416
|
-
* - Network connectivity issues
|
|
417
|
-
* - OAuth provider errors
|
|
418
|
-
*
|
|
419
|
-
* @example
|
|
420
|
-
* ```typescript
|
|
421
|
-
* auth.onSignInFailure(async (context, state, authHandlerId, errorMessage) => {
|
|
422
|
-
* await context.sendActivity(`Sign-in failed: ${errorMessage || 'Unknown error'}`);
|
|
423
|
-
* await context.sendActivity('Please try signing in again.');
|
|
424
|
-
* });
|
|
425
|
-
* ```
|
|
426
|
-
*
|
|
427
|
-
* @public
|
|
428
|
-
*/
|
|
429
|
-
public onSignInFailure (handler: (context: TurnContext, state: TurnState, authHandlerId?: string, errorMessage?: string) => Promise<void>) {
|
|
430
|
-
this._signInFailureHandler = handler
|
|
431
|
-
}
|
|
432
|
-
}
|
package/src/claimsIdentity.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the MIT License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Represents a claim with a type and value.
|
|
8
|
-
*/
|
|
9
|
-
export interface Claim {
|
|
10
|
-
readonly type: string;
|
|
11
|
-
readonly value: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Represents an identity with a collection of claims.
|
|
16
|
-
*/
|
|
17
|
-
export class ClaimsIdentity {
|
|
18
|
-
/**
|
|
19
|
-
* Creates a new instance of the ClaimsIdentity class.
|
|
20
|
-
* @param claims The collection of claims associated with the identity.
|
|
21
|
-
* @param authenticationType The type of authentication used, or a boolean indicating if the identity is authenticated.
|
|
22
|
-
*/
|
|
23
|
-
constructor (public readonly claims: Claim[], private readonly authenticationType?: string | boolean) {}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Indicates whether the identity is authenticated.
|
|
27
|
-
* @returns True if the identity is authenticated; otherwise, false.
|
|
28
|
-
*/
|
|
29
|
-
get isAuthenticated (): boolean {
|
|
30
|
-
if (typeof this.authenticationType === 'boolean') {
|
|
31
|
-
return this.authenticationType
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return this.authenticationType != null
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Gets the value of a claim by its type.
|
|
39
|
-
* @param claimType The type of the claim to retrieve.
|
|
40
|
-
* @returns The value of the claim, or null if the claim is not found.
|
|
41
|
-
*/
|
|
42
|
-
getClaimValue (claimType: string): string | null {
|
|
43
|
-
const claim = this.claims.find((c) => c.type === claimType)
|
|
44
|
-
|
|
45
|
-
return claim?.value ?? null
|
|
46
|
-
}
|
|
47
|
-
}
|