@microsoft/agents-hosting 1.5.0-beta.15.gdfa61e3bf0 → 1.5.0-beta.19.g3a0f01fc43
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 +2 -2
- package/dist/src/app/agentApplication.d.ts +2 -2
- package/dist/src/app/agentApplication.js +3 -5
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/auth/authorization.js +10 -7
- package/dist/src/app/auth/authorization.js.map +1 -1
- package/dist/src/app/auth/authorizationManager.d.ts +18 -5
- package/dist/src/app/auth/authorizationManager.js +245 -43
- package/dist/src/app/auth/authorizationManager.js.map +1 -1
- package/dist/src/app/auth/handlerStorage.js +3 -1
- package/dist/src/app/auth/handlerStorage.js.map +1 -1
- package/dist/src/app/auth/handlers/agenticAuthorization.d.ts +19 -16
- package/dist/src/app/auth/handlers/agenticAuthorization.js +16 -32
- package/dist/src/app/auth/handlers/agenticAuthorization.js.map +1 -1
- package/dist/src/app/auth/handlers/azureBotAuthorization.d.ts +43 -69
- package/dist/src/app/auth/handlers/azureBotAuthorization.js +37 -65
- package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -1
- package/dist/src/app/auth/types.d.ts +100 -1
- package/dist/src/auth/authConfiguration.d.ts +61 -0
- package/dist/src/auth/authConfiguration.js +50 -1
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/msalConnectionManager.js +20 -0
- package/dist/src/auth/msalConnectionManager.js.map +1 -1
- package/dist/src/auth/msalTokenCredential.js +3 -0
- package/dist/src/auth/msalTokenCredential.js.map +1 -1
- package/dist/src/auth/msalTokenProvider.js +11 -4
- package/dist/src/auth/msalTokenProvider.js.map +1 -1
- package/dist/src/cloudAdapter.js +7 -0
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/errorHelper.js +14 -0
- package/dist/src/errorHelper.js.map +1 -1
- package/package.json +2 -2
- package/src/app/agentApplication.ts +7 -8
- package/src/app/auth/authorization.ts +10 -7
- package/src/app/auth/authorizationManager.ts +282 -43
- package/src/app/auth/handlerStorage.ts +3 -1
- package/src/app/auth/handlers/agenticAuthorization.ts +31 -43
- package/src/app/auth/handlers/azureBotAuthorization.ts +73 -129
- package/src/app/auth/types.ts +102 -1
- package/src/auth/authConfiguration.ts +57 -0
- package/src/auth/msalConnectionManager.ts +22 -0
- package/src/auth/msalTokenCredential.ts +4 -0
- package/src/auth/msalTokenProvider.ts +11 -4
- package/src/cloudAdapter.ts +8 -0
- package/src/errorHelper.ts +16 -0
|
@@ -3,16 +3,23 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { Activity,
|
|
6
|
+
import { Activity, ExceptionHelper } from '@microsoft/agents-activity'
|
|
7
|
+
import { debug } from '@microsoft/agents-activity/logger'
|
|
7
8
|
import { AgentApplication } from '../agentApplication'
|
|
8
9
|
import { AgenticAuthorization, AzureBotAuthorization } from './handlers'
|
|
9
10
|
import { TurnContext } from '../../turnContext'
|
|
10
11
|
import { HandlerStorage } from './handlerStorage'
|
|
12
|
+
import { Errors } from '../../errorHelper'
|
|
11
13
|
import { ActiveAuthorizationHandler, AuthorizationHandlerStatus, AuthorizationHandler, AuthorizationHandlerSettings, AuthorizationOptions } from './types'
|
|
12
14
|
import { Connections } from '../../auth/connections'
|
|
15
|
+
import { envParser, envParserUtils } from '../../auth'
|
|
13
16
|
|
|
14
17
|
const logger = debug('agents:authorization:manager')
|
|
15
18
|
|
|
19
|
+
const AGENTIC = 'AgenticUserAuthorization'
|
|
20
|
+
const AGENTIC_LEGACY = 'agentic'
|
|
21
|
+
const AZURE_BOT = 'AzureBotUserAuthorization'
|
|
22
|
+
|
|
16
23
|
/**
|
|
17
24
|
* Active handler information used by the AuthorizationManager.
|
|
18
25
|
*/
|
|
@@ -52,53 +59,113 @@ export class AuthorizationManager {
|
|
|
52
59
|
private _handlers: Record<string, AuthorizationHandler> = {}
|
|
53
60
|
|
|
54
61
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @param app The agent application instance.
|
|
62
|
+
* Environment variable configuration for the latest format.
|
|
57
63
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
64
|
+
private _envLatest = {
|
|
65
|
+
key: {
|
|
66
|
+
prefix: 'AgentApplication__UserAuthorization__Handlers__',
|
|
67
|
+
separator: '__Settings__',
|
|
68
|
+
create (id: string, prop: string) {
|
|
69
|
+
return `${this.prefix}${id}${this.separator}${prop}`
|
|
70
|
+
},
|
|
71
|
+
extract (envKey: string) {
|
|
72
|
+
// Substring: AgentApplication__UserAuthorization__Handlers__<id>__Settings__<prop>
|
|
73
|
+
// position —————————————————————————————————————————> start^ ^end ^prop
|
|
74
|
+
const start = this.prefix.length
|
|
75
|
+
const end = envKey.toUpperCase().indexOf(this.separator.toUpperCase())
|
|
76
|
+
if (end === -1) {
|
|
77
|
+
return { id: undefined, prop: undefined }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const id = envKey.substring(start, end)
|
|
81
|
+
const prop = envKey.substring(end + this.separator.length)
|
|
82
|
+
return { id, prop }
|
|
74
83
|
}
|
|
75
|
-
}
|
|
84
|
+
},
|
|
85
|
+
parser: envParser({
|
|
86
|
+
// Common
|
|
87
|
+
type: envParserUtils.bypass,
|
|
88
|
+
|
|
89
|
+
// Azure Bot
|
|
90
|
+
azureBotOAuthConnectionName: envParserUtils.bypass,
|
|
91
|
+
title: envParserUtils.bypass,
|
|
92
|
+
text: envParserUtils.bypass,
|
|
93
|
+
invalidSignInRetryMessage: envParserUtils.bypass,
|
|
94
|
+
invalidSignInRetryMessageFormat: envParserUtils.bypass,
|
|
95
|
+
invalidSignInRetryMaxExceededMessage: envParserUtils.bypass,
|
|
96
|
+
oboConnectionName: envParserUtils.bypass,
|
|
97
|
+
enableSso (value) {
|
|
98
|
+
return { value: value !== 'false' }
|
|
99
|
+
},
|
|
100
|
+
invalidSignInRetryMax (value) {
|
|
101
|
+
return { value: parseInt(value) }
|
|
102
|
+
},
|
|
103
|
+
oboScopes (value) {
|
|
104
|
+
return this.scopes(value)
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
// Agentic
|
|
108
|
+
altBlueprintConnectionName: envParserUtils.bypass,
|
|
109
|
+
scopes (value) {
|
|
110
|
+
if (value.includes(',')) {
|
|
111
|
+
return { value: value.split(',').map(s => s.trim()).filter(Boolean) }
|
|
112
|
+
}
|
|
113
|
+
return { value: value.split(/\s+/).filter(Boolean) }
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
76
116
|
}
|
|
77
117
|
|
|
78
118
|
/**
|
|
79
|
-
*
|
|
119
|
+
* Environment variable configuration for the legacy format.
|
|
80
120
|
*/
|
|
81
|
-
private
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
121
|
+
private _envLegacy = {
|
|
122
|
+
key: {
|
|
123
|
+
separator: '_',
|
|
124
|
+
},
|
|
125
|
+
parser: envParser({
|
|
126
|
+
// Common
|
|
127
|
+
type: envParserUtils.redirect(this._envLatest.parser, 'type'),
|
|
128
|
+
|
|
129
|
+
// Azure Bot
|
|
130
|
+
connectionName: envParserUtils.redirect(this._envLatest.parser, 'azureBotOAuthConnectionName'),
|
|
131
|
+
connectionTitle: envParserUtils.redirect(this._envLatest.parser, 'title'),
|
|
132
|
+
connectionText: envParserUtils.redirect(this._envLatest.parser, 'text'),
|
|
133
|
+
maxAttempts: envParserUtils.redirect(this._envLatest.parser, 'invalidSignInRetryMax'),
|
|
134
|
+
messages_invalidCode: envParserUtils.redirect(this._envLatest.parser, 'invalidSignInRetryMessage'),
|
|
135
|
+
messages_invalidCodeFormat: envParserUtils.redirect(this._envLatest.parser, 'invalidSignInRetryMessageFormat'),
|
|
136
|
+
messages_maxAttemptsExceeded: envParserUtils.redirect(this._envLatest.parser, 'invalidSignInRetryMaxExceededMessage'),
|
|
137
|
+
obo_connection: envParserUtils.redirect(this._envLatest.parser, 'oboConnectionName'),
|
|
138
|
+
obo_scopes: envParserUtils.redirect(this._envLatest.parser, 'oboScopes'),
|
|
139
|
+
enableSso: envParserUtils.redirect(this._envLatest.parser, 'enableSso'),
|
|
140
|
+
|
|
141
|
+
// Agentic
|
|
142
|
+
scopes: envParserUtils.redirect(this._envLatest.parser, 'scopes'),
|
|
143
|
+
altBlueprintConnectionName: envParserUtils.redirect(this._envLatest.parser, 'altBlueprintConnectionName')
|
|
144
|
+
})
|
|
145
|
+
}
|
|
86
146
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Creates an instance of the AuthorizationManager.
|
|
149
|
+
* @param app The agent application instance.
|
|
150
|
+
*/
|
|
151
|
+
constructor (private app: AgentApplication<any>, private connections: Connections) {
|
|
152
|
+
this.createHandlers()
|
|
153
|
+
|
|
154
|
+
if (this.handlers.length === 0 && app.options.authorization !== undefined) {
|
|
155
|
+
throw ExceptionHelper.generateException(Error, Errors.NoAuthHandlersConfigured)
|
|
91
156
|
}
|
|
92
157
|
|
|
93
|
-
|
|
158
|
+
for (const [id, handler] of Object.entries(this._handlers)) {
|
|
159
|
+
logger.debug('auth handler "%s" type=%s scopes=%o', id, handler.type, handler.scopes)
|
|
160
|
+
}
|
|
94
161
|
}
|
|
95
162
|
|
|
96
163
|
/**
|
|
97
164
|
* Gets the registered authorization handlers.
|
|
98
165
|
* @returns A record of authorization handlers by their IDs.
|
|
99
166
|
*/
|
|
100
|
-
public get handlers ():
|
|
101
|
-
return this._handlers
|
|
167
|
+
public get handlers (): AuthorizationHandler[] {
|
|
168
|
+
return Object.values(this._handlers)
|
|
102
169
|
}
|
|
103
170
|
|
|
104
171
|
/**
|
|
@@ -108,6 +175,10 @@ export class AuthorizationManager {
|
|
|
108
175
|
* @returns The result of the authorization process.
|
|
109
176
|
*/
|
|
110
177
|
public async process (context: TurnContext, getHandlerIds: GetHandlerIds): Promise<AuthorizationManagerProcessResult> {
|
|
178
|
+
if (this.handlers.length === 0) {
|
|
179
|
+
return { authorized: true, context }
|
|
180
|
+
}
|
|
181
|
+
|
|
111
182
|
const storage = new HandlerStorage(this.app.options.storage!, context)
|
|
112
183
|
|
|
113
184
|
let active = await this.active(storage, getHandlerIds)
|
|
@@ -124,8 +195,13 @@ export class AuthorizationManager {
|
|
|
124
195
|
const sharedContext = new TurnContext(context)
|
|
125
196
|
|
|
126
197
|
for (const handler of handlers) {
|
|
198
|
+
if (handler.scopes?.length) {
|
|
199
|
+
logger.debug('invoking auth handler "%s" scopes=[%s]', handler.id, handler.scopes.join(','))
|
|
200
|
+
} else {
|
|
201
|
+
logger.debug('invoking auth handler "%s"', handler.id)
|
|
202
|
+
}
|
|
127
203
|
const status = await this.signin(storage, handler, sharedContext, active?.data)
|
|
128
|
-
logger.debug(
|
|
204
|
+
logger.debug('auth handler "%s" sign-in status=%s', handler.id, status)
|
|
129
205
|
|
|
130
206
|
if (status === AuthorizationHandlerStatus.IGNORED) {
|
|
131
207
|
await storage.delete()
|
|
@@ -147,7 +223,7 @@ export class AuthorizationManager {
|
|
|
147
223
|
}
|
|
148
224
|
|
|
149
225
|
if (status !== AuthorizationHandlerStatus.APPROVED) {
|
|
150
|
-
throw
|
|
226
|
+
throw ExceptionHelper.generateException(Error, Errors.UnexpectedRegistrationStatus, undefined, { status })
|
|
151
227
|
}
|
|
152
228
|
|
|
153
229
|
await storage.delete()
|
|
@@ -194,7 +270,7 @@ export class AuthorizationManager {
|
|
|
194
270
|
return await handler.signin(context, active)
|
|
195
271
|
} catch (cause) {
|
|
196
272
|
await storage.delete()
|
|
197
|
-
throw
|
|
273
|
+
throw ExceptionHelper.generateException(Error, Errors.FailedToSignIn, cause as Error)
|
|
198
274
|
}
|
|
199
275
|
}
|
|
200
276
|
|
|
@@ -202,16 +278,19 @@ export class AuthorizationManager {
|
|
|
202
278
|
* Maps an array of handler IDs to their corresponding handler instances.
|
|
203
279
|
*/
|
|
204
280
|
private mapHandlers (ids: string[]): AuthorizationHandler[] {
|
|
205
|
-
|
|
281
|
+
const unknownHandlers: string[] = []
|
|
206
282
|
const handlers = ids.map(id => {
|
|
207
|
-
|
|
208
|
-
|
|
283
|
+
const handler = this.handlers.find(e => e.id.toLowerCase() === id.toLowerCase())
|
|
284
|
+
if (!handler) {
|
|
285
|
+
unknownHandlers.push(id)
|
|
209
286
|
}
|
|
210
|
-
return
|
|
211
|
-
})
|
|
212
|
-
|
|
213
|
-
|
|
287
|
+
return handler
|
|
288
|
+
}).filter((handler) => handler !== undefined)
|
|
289
|
+
|
|
290
|
+
if (unknownHandlers.length > 0) {
|
|
291
|
+
throw ExceptionHelper.generateException(Error, Errors.AuthHandlersNotFound, undefined, { handlerIds: unknownHandlers.join(', ') })
|
|
214
292
|
}
|
|
293
|
+
|
|
215
294
|
return handlers
|
|
216
295
|
}
|
|
217
296
|
|
|
@@ -221,4 +300,164 @@ export class AuthorizationManager {
|
|
|
221
300
|
private prefix (id: string, message: string) {
|
|
222
301
|
return `[handler:${id}] ${message}`
|
|
223
302
|
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Creates authorization handlers based on the application configuration and environment variables.
|
|
306
|
+
*/
|
|
307
|
+
private createHandlers () {
|
|
308
|
+
let legacyMessage = ''
|
|
309
|
+
const settings: AuthorizationHandlerSettings = { storage: this.app.options.storage!, connections: this.connections }
|
|
310
|
+
let runtimeEntries = Object.entries(this.app.options.authorization ?? {})
|
|
311
|
+
const result = { latest: {}, legacy: {} } as {
|
|
312
|
+
latest: Record<string, Record<string, any> | undefined>;
|
|
313
|
+
legacy: Record<string, Record<string, any> | undefined>;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
for (const [envKey, envValue] of Object.entries(process.env)) {
|
|
317
|
+
if (!envValue?.trim()) {
|
|
318
|
+
continue
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const upperEnvKey = envKey.toUpperCase()
|
|
322
|
+
|
|
323
|
+
// Legacy: extract handler ID, handler options key and its value, and assign it to the correct runtime handler ID.
|
|
324
|
+
if (!upperEnvKey.startsWith(this._envLatest.key.prefix.toUpperCase())) {
|
|
325
|
+
const [id] = runtimeEntries.find(([id]) => upperEnvKey.startsWith(`${id.toUpperCase()}${this._envLegacy.key.separator}`)) ?? []
|
|
326
|
+
if (!id) {
|
|
327
|
+
continue
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const prop = envKey.substring(id.length + this._envLegacy.key.separator.length)
|
|
331
|
+
if (!prop) {
|
|
332
|
+
continue
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const { key, value } = this._envLegacy.parser.parse(prop as any, envValue)
|
|
336
|
+
if (!key) {
|
|
337
|
+
continue
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
legacyMessage += ` ${envKey}= # Use ${this._envLatest.key.create(id, key)} instead.\n`
|
|
341
|
+
|
|
342
|
+
result.legacy[id] ??= {}
|
|
343
|
+
result.legacy[id][key] = value
|
|
344
|
+
continue
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Latest: extract handler ID, handler options key and its value, and assign it to the correct latest handler ID.
|
|
348
|
+
const { id, prop } = this._envLatest.key.extract(envKey)
|
|
349
|
+
if (!id || !prop) {
|
|
350
|
+
continue
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const { key, value } = this._envLatest.parser.parse(prop as any, envValue)
|
|
354
|
+
if (!key) {
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Find existing handler ID case-insensitively to avoid duplicates, but keep the original casing for later processing.
|
|
359
|
+
// This allows users to specify environment variables in a case-insensitive way, while ensuring that the handler IDs are treated in a case-sensitive way internally, to avoid issues when referencing them later.
|
|
360
|
+
// For example, if the user specifies AGENTAPPLICATION__USERAUTHORIZATION__HANDLERS__GRAPH__SETTINGS__AZUREBOTOAUTHCONNECTIONNAME and agentapplication__userauthorization__handlers__graph__settings__azurebotoauthconnectionname, we want to treat them as the same handler ID "graph", and not create two separate handlers "GRAPH" and "graph".
|
|
361
|
+
// Note: the first environment variable that is processed will determine the casing of the handler ID, and any subsequent environment variable that matches the same handler ID case-insensitively will be treated as referring to the same handler, regardless of its casing.
|
|
362
|
+
const existingId = id in result.latest ? id : Object.keys(result.latest).find(e => e.toLowerCase() === id.toLowerCase())
|
|
363
|
+
const realId = existingId ?? id
|
|
364
|
+
result.latest[realId] ??= {}
|
|
365
|
+
result.latest[realId][key] = value
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (legacyMessage.length > 0) {
|
|
369
|
+
logger.warn('Deprecated environment variables detected, update to the latest format: (case-insensitive)', `[\n${legacyMessage}]`)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Fix types
|
|
373
|
+
const fixTypes = (entries: [string, any][]) => entries
|
|
374
|
+
.map(([id, options]) => [id, { ...options, type: this.fixType(id, options?.type) }] as [string, AuthorizationOptions[string]])
|
|
375
|
+
|
|
376
|
+
runtimeEntries = fixTypes(runtimeEntries)
|
|
377
|
+
const latestEntries = fixTypes(Object.entries(result.latest))
|
|
378
|
+
const legacyEntries = fixTypes(Object.entries(result.legacy))
|
|
379
|
+
|
|
380
|
+
const registeredHandlers = new Set()
|
|
381
|
+
for (const [id] of [...runtimeEntries, ...latestEntries]) {
|
|
382
|
+
if (registeredHandlers.has(id.toLowerCase())) {
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Find entries case-insensitively for later processing
|
|
387
|
+
const [, runtime] = runtimeEntries.find(([key]) => key.toLowerCase() === id.toLowerCase()) ?? []
|
|
388
|
+
const [, latest] = latestEntries.find(([key]) => key.toLowerCase() === id.toLowerCase()) ?? []
|
|
389
|
+
const [, legacy] = legacyEntries.find(([key]) => key.toLowerCase() === id.toLowerCase()) ?? []
|
|
390
|
+
|
|
391
|
+
if (runtime !== undefined && latest !== undefined) {
|
|
392
|
+
logger.warn(this.prefix(id, 'Both runtime and latest environment variable configurations detected. Runtime configuration will take precedence over latest environment variables.'))
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Normalize runtime legacy options to latest format
|
|
396
|
+
if (runtime?.type === AZURE_BOT) {
|
|
397
|
+
runtime.azureBotOAuthConnectionName ??= runtime.name
|
|
398
|
+
runtime.invalidSignInRetryMax ??= runtime.maxAttempts
|
|
399
|
+
runtime.invalidSignInRetryMessage ??= runtime.messages?.invalidCode
|
|
400
|
+
runtime.invalidSignInRetryMessageFormat ??= runtime.messages?.invalidCodeFormat
|
|
401
|
+
runtime.invalidSignInRetryMaxExceededMessage ??= runtime.messages?.maxAttemptsExceeded
|
|
402
|
+
runtime.oboConnectionName ??= runtime.obo?.connection
|
|
403
|
+
runtime.oboScopes ??= runtime.obo?.scopes
|
|
404
|
+
delete runtime.name
|
|
405
|
+
delete runtime.maxAttempts
|
|
406
|
+
delete runtime.messages
|
|
407
|
+
delete runtime.obo
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Helper to remove undefined options
|
|
411
|
+
const prune = <T extends Record<string, any>>(obj: T) => {
|
|
412
|
+
const entries = Object.entries(obj).filter(([, e]) => e !== undefined)
|
|
413
|
+
return Object.fromEntries(entries) as T
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Priority: runtime > latest > legacy
|
|
417
|
+
// Pruning done individually to avoid overwriting with undefined when merging.
|
|
418
|
+
const options: AuthorizationOptions[string] = runtime ? { ...prune(legacy || {}), ...prune(runtime) } : prune(latest || {})
|
|
419
|
+
|
|
420
|
+
if (!settings.storage) {
|
|
421
|
+
throw ExceptionHelper.generateException(Error, Errors.StorageRequiredForAuthorization)
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (options.type === AGENTIC) {
|
|
425
|
+
this._handlers[id] = new AgenticAuthorization(id, options, settings)
|
|
426
|
+
} else if (options.type === AZURE_BOT) {
|
|
427
|
+
// Set default values if not provided
|
|
428
|
+
options.title ||= 'Sign-in'
|
|
429
|
+
options.text ||= 'Please sign-in to continue'
|
|
430
|
+
options.oboScopes ??= []
|
|
431
|
+
options.enableSso = options.enableSso !== false // default value is true if undefined.
|
|
432
|
+
|
|
433
|
+
this._handlers[id] = new AzureBotAuthorization(id, options, settings)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
registeredHandlers.add(id.toLowerCase())
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Fixes the handler type based on the provided type string, supporting both latest and legacy formats.
|
|
442
|
+
*/
|
|
443
|
+
private fixType (handlerId: string, type: AuthorizationOptions[string]['type'] | string) {
|
|
444
|
+
if (!type) {
|
|
445
|
+
return AZURE_BOT
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (type.toLowerCase() === AGENTIC_LEGACY.toLowerCase()) {
|
|
449
|
+
logger.warn(this.prefix(handlerId, 'The \'agentic\' type is deprecated. Please use \'AgenticUserAuthorization\' instead.'))
|
|
450
|
+
return AGENTIC
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (type.toLowerCase() === AGENTIC.toLowerCase()) {
|
|
454
|
+
return AGENTIC
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (type.toLowerCase() === AZURE_BOT.toLowerCase()) {
|
|
458
|
+
return AZURE_BOT
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
throw ExceptionHelper.generateException(Error, Errors.UnsupportedAuthHandlerType, undefined, { handlerType: type })
|
|
462
|
+
}
|
|
224
463
|
}
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
import { ActiveAuthorizationHandler } from './types'
|
|
7
7
|
import { TurnContext } from '../../turnContext'
|
|
8
8
|
import { Storage } from '../../storage'
|
|
9
|
+
import { ExceptionHelper } from '@microsoft/agents-activity'
|
|
10
|
+
import { Errors } from '../../errorHelper'
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Storage manager for handler state.
|
|
@@ -25,7 +27,7 @@ export class HandlerStorage<TActiveHandler extends ActiveAuthorizationHandler =
|
|
|
25
27
|
const channelId = this.context.activity.channelId?.trim()
|
|
26
28
|
const userId = this.context.activity.from?.id?.trim()
|
|
27
29
|
if (!channelId || !userId) {
|
|
28
|
-
throw
|
|
30
|
+
throw ExceptionHelper.generateException(Error, Errors.ChannelIdAndFromIdRequired)
|
|
29
31
|
}
|
|
30
32
|
return `auth/${channelId}/${userId}`
|
|
31
33
|
}
|
|
@@ -3,34 +3,42 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { ExceptionHelper } from '@microsoft/agents-activity'
|
|
7
|
+
import { debug } from '@microsoft/agents-activity/logger'
|
|
7
8
|
import { TurnContext } from '../../../turnContext'
|
|
8
9
|
import { AuthorizationHandler, AuthorizationHandlerSettings, AuthorizationHandlerStatus, AuthorizationHandlerTokenOptions } from '../types'
|
|
9
10
|
import { TokenResponse } from '../../../oauth'
|
|
10
11
|
import { AuthProvider } from '../../../auth'
|
|
12
|
+
import { Errors } from '../../../errorHelper'
|
|
11
13
|
|
|
12
14
|
const logger = debug('agents:authorization:agentic')
|
|
13
15
|
|
|
14
16
|
/**
|
|
15
17
|
* Options for configuring the Agentic authorization handler.
|
|
18
|
+
* @remarks
|
|
19
|
+
* Properties can be configured via environment variables (case-insensitive).
|
|
20
|
+
* Use the format: `AgentApplication__UserAuthorization__handlers__{handlerId}__settings__{propertyName}`
|
|
21
|
+
* where `{handlerId}` is the handler's unique identifier and `{propertyName}` matches the property name.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```env
|
|
25
|
+
* # For a handler with id "myAuth":
|
|
26
|
+
* AgentApplication__UserAuthorization__handlers__myAuth__settings__type=AgenticUserAuthorization
|
|
27
|
+
* AgentApplication__UserAuthorization__handlers__myAuth__settings__scopes=api://scope1 api://scope2
|
|
28
|
+
* ```
|
|
16
29
|
*/
|
|
17
30
|
export interface AgenticAuthorizationOptions {
|
|
18
31
|
/**
|
|
19
32
|
* The type of authorization handler.
|
|
20
|
-
* @remarks
|
|
21
|
-
* When using environment variables, this can be set using the `${authHandlerId}_type` variable.
|
|
22
33
|
*/
|
|
23
|
-
type: 'agentic'
|
|
34
|
+
type: 'AgenticUserAuthorization' | 'agentic'
|
|
24
35
|
/**
|
|
25
36
|
* The scopes required for the authorization.
|
|
26
|
-
* @remarks
|
|
27
|
-
* When using environment variables, this can be set using the `${authHandlerId}_scopes` variable (comma-separated values, e.g. `scope1,scope2`).
|
|
37
|
+
* @remarks When set via environment variable, use comma or space-separated values (e.g. `scope1,scope2` or `scope1 scope2`).
|
|
28
38
|
*/
|
|
29
39
|
scopes?: string[]
|
|
30
40
|
/**
|
|
31
|
-
*
|
|
32
|
-
* @remarks
|
|
33
|
-
* When using environment variables, this can be set using the `${authHandlerId}_altBlueprintConnectionName` variable.
|
|
41
|
+
* An alternative connection name to use for the authorization process.
|
|
34
42
|
*/
|
|
35
43
|
altBlueprintConnectionName?: string
|
|
36
44
|
}
|
|
@@ -44,39 +52,32 @@ export interface AgenticAuthorizationSettings extends AuthorizationHandlerSettin
|
|
|
44
52
|
* Authorization handler for Agentic authentication.
|
|
45
53
|
*/
|
|
46
54
|
export class AgenticAuthorization implements AuthorizationHandler {
|
|
47
|
-
private _options: AgenticAuthorizationOptions
|
|
48
55
|
private _onSuccess?: Parameters<AuthorizationHandler['onSuccess']>[0]
|
|
49
56
|
private _onFailure?: Parameters<AuthorizationHandler['onFailure']>[0]
|
|
50
57
|
|
|
51
58
|
/**
|
|
52
59
|
* Creates an instance of the AgenticAuthorization class.
|
|
53
60
|
* @param id The unique identifier for the authorization handler.
|
|
54
|
-
* @param options The options for configuring the authorization handler.
|
|
61
|
+
* @param options The options for configuring the authorization handler (must be fully resolved).
|
|
55
62
|
* @param settings The settings for the authorization handler.
|
|
56
63
|
*/
|
|
57
|
-
constructor (public readonly id: string, options: AgenticAuthorizationOptions, private settings: AgenticAuthorizationSettings) {
|
|
64
|
+
constructor (public readonly id: string, private options: AgenticAuthorizationOptions, private settings: AgenticAuthorizationSettings) {
|
|
58
65
|
if (!this.settings.connections) {
|
|
59
|
-
throw
|
|
66
|
+
throw ExceptionHelper.generateException(Error, Errors.ConnectionsOptionNotAvailable)
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
|
|
69
|
+
if (!options.scopes || options.scopes.length === 0) {
|
|
70
|
+
throw ExceptionHelper.generateException(Error, Errors.AtLeastOneScopeRequired)
|
|
71
|
+
}
|
|
63
72
|
}
|
|
64
73
|
|
|
74
|
+
readonly type = 'agentic'
|
|
75
|
+
|
|
65
76
|
/**
|
|
66
|
-
*
|
|
77
|
+
* The scopes configured for this handler.
|
|
67
78
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
type: 'agentic',
|
|
71
|
-
altBlueprintConnectionName: settings.altBlueprintConnectionName ?? (process.env[`${this.id}_altBlueprintConnectionName`]),
|
|
72
|
-
scopes: settings.scopes ?? this.loadScopes(process.env[`${this.id}_scopes`]),
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!result.scopes || result.scopes.length === 0) {
|
|
76
|
-
throw new Error(this.prefix('At least one scope must be specified for the Agentic authorization handler.'))
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return result
|
|
79
|
+
get scopes (): string[] | undefined {
|
|
80
|
+
return this.options.scopes
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
/**
|
|
@@ -98,7 +99,7 @@ export class AgenticAuthorization implements AuthorizationHandler {
|
|
|
98
99
|
*/
|
|
99
100
|
async token (context: TurnContext, options?: AuthorizationHandlerTokenOptions): Promise<TokenResponse> {
|
|
100
101
|
try {
|
|
101
|
-
const scopes = options?.scopes || this.
|
|
102
|
+
const scopes = options?.scopes || this.options.scopes!
|
|
102
103
|
|
|
103
104
|
const tokenResponse = this.getContext(context, scopes)
|
|
104
105
|
if (tokenResponse.token) {
|
|
@@ -108,8 +109,8 @@ export class AgenticAuthorization implements AuthorizationHandler {
|
|
|
108
109
|
|
|
109
110
|
let connection: AuthProvider
|
|
110
111
|
|
|
111
|
-
if (this.
|
|
112
|
-
connection = this.settings.connections.getConnection(this.
|
|
112
|
+
if (this.options.altBlueprintConnectionName?.trim()) {
|
|
113
|
+
connection = this.settings.connections.getConnection(this.options.altBlueprintConnectionName)
|
|
113
114
|
} else {
|
|
114
115
|
connection = this.settings.connections.getTokenProvider(context.identity, context.activity.serviceUrl ?? '')
|
|
115
116
|
}
|
|
@@ -173,17 +174,4 @@ export class AgenticAuthorization implements AuthorizationHandler {
|
|
|
173
174
|
const result = context.turnState.get(`${this._key}:${scopes.join(';')}`)
|
|
174
175
|
return result?.() ?? { token: undefined }
|
|
175
176
|
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Loads the OAuth scopes from the environment variables.
|
|
179
|
-
*/
|
|
180
|
-
private loadScopes (value:string | undefined): string[] {
|
|
181
|
-
return value?.split(',').reduce<string[]>((acc, scope) => {
|
|
182
|
-
const trimmed = scope.trim()
|
|
183
|
-
if (trimmed) {
|
|
184
|
-
acc.push(trimmed)
|
|
185
|
-
}
|
|
186
|
-
return acc
|
|
187
|
-
}, []) ?? []
|
|
188
|
-
}
|
|
189
177
|
}
|