@microsoft/agents-hosting 1.1.0-alpha.2 → 1.1.0-alpha.58

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.
Files changed (150) hide show
  1. package/dist/package.json +10 -6
  2. package/dist/src/activityWireCompat.js +8 -3
  3. package/dist/src/activityWireCompat.js.map +1 -1
  4. package/dist/src/agent-client/agentClient.js +7 -3
  5. package/dist/src/agent-client/agentClient.js.map +1 -1
  6. package/dist/src/agent-client/agentResponseHandler.js +6 -2
  7. package/dist/src/agent-client/agentResponseHandler.js.map +1 -1
  8. package/dist/src/app/agentApplication.d.ts +26 -11
  9. package/dist/src/app/agentApplication.js +94 -86
  10. package/dist/src/app/agentApplication.js.map +1 -1
  11. package/dist/src/app/agentApplicationBuilder.d.ts +2 -2
  12. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  13. package/dist/src/app/agentApplicationOptions.d.ts +9 -2
  14. package/dist/src/app/appRoute.d.ts +7 -0
  15. package/dist/src/app/attachmentDownloader.d.ts +13 -3
  16. package/dist/src/app/attachmentDownloader.js +16 -3
  17. package/dist/src/app/attachmentDownloader.js.map +1 -1
  18. package/dist/src/app/{authorization.d.ts → auth/authorization.d.ts} +33 -139
  19. package/dist/src/app/auth/authorization.js +188 -0
  20. package/dist/src/app/auth/authorization.js.map +1 -0
  21. package/dist/src/app/auth/authorizationManager.d.ts +71 -0
  22. package/dist/src/app/auth/authorizationManager.js +170 -0
  23. package/dist/src/app/auth/authorizationManager.js.map +1 -0
  24. package/dist/src/app/auth/handlerStorage.d.ts +36 -0
  25. package/dist/src/app/auth/handlerStorage.js +62 -0
  26. package/dist/src/app/auth/handlerStorage.js.map +1 -0
  27. package/dist/src/app/auth/handlers/agenticAuthorization.d.ts +97 -0
  28. package/dist/src/app/auth/handlers/agenticAuthorization.js +145 -0
  29. package/dist/src/app/auth/handlers/agenticAuthorization.js.map +1 -0
  30. package/dist/src/app/auth/handlers/azureBotAuthorization.d.ts +222 -0
  31. package/dist/src/app/auth/handlers/azureBotAuthorization.js +428 -0
  32. package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -0
  33. package/dist/src/app/auth/handlers/index.d.ts +2 -0
  34. package/dist/src/app/auth/handlers/index.js +19 -0
  35. package/dist/src/app/auth/handlers/index.js.map +1 -0
  36. package/dist/src/app/auth/index.d.ts +2 -0
  37. package/dist/src/app/auth/index.js +19 -0
  38. package/dist/src/app/auth/index.js.map +1 -0
  39. package/dist/src/app/auth/types.d.ts +104 -0
  40. package/dist/src/app/auth/types.js +24 -0
  41. package/dist/src/app/auth/types.js.map +1 -0
  42. package/dist/src/app/index.d.ts +2 -3
  43. package/dist/src/app/index.js +2 -3
  44. package/dist/src/app/index.js.map +1 -1
  45. package/dist/src/app/inputFileDownloader.d.ts +10 -3
  46. package/dist/src/app/routeList.d.ts +1 -1
  47. package/dist/src/app/routeList.js +22 -5
  48. package/dist/src/app/routeList.js.map +1 -1
  49. package/dist/src/app/streaming/streamingResponse.d.ts +11 -1
  50. package/dist/src/app/streaming/streamingResponse.js +17 -2
  51. package/dist/src/app/streaming/streamingResponse.js.map +1 -1
  52. package/dist/src/app/turnState.d.ts +2 -38
  53. package/dist/src/app/turnState.js +1 -46
  54. package/dist/src/app/turnState.js.map +1 -1
  55. package/dist/src/auth/MemoryCache.d.ts +16 -0
  56. package/dist/src/auth/MemoryCache.js +58 -0
  57. package/dist/src/auth/MemoryCache.js.map +1 -0
  58. package/dist/src/auth/authConfiguration.d.ts +44 -2
  59. package/dist/src/auth/authConfiguration.js +218 -53
  60. package/dist/src/auth/authConfiguration.js.map +1 -1
  61. package/dist/src/auth/authConstants.d.ts +11 -0
  62. package/dist/src/auth/authConstants.js +15 -0
  63. package/dist/src/auth/authConstants.js.map +1 -0
  64. package/dist/src/auth/authProvider.d.ts +23 -0
  65. package/dist/src/auth/connections.d.ts +40 -0
  66. package/dist/src/auth/connections.js +7 -0
  67. package/dist/src/auth/connections.js.map +1 -0
  68. package/dist/src/auth/index.d.ts +2 -0
  69. package/dist/src/auth/index.js +2 -0
  70. package/dist/src/auth/index.js.map +1 -1
  71. package/dist/src/auth/jwt-middleware.js +31 -18
  72. package/dist/src/auth/jwt-middleware.js.map +1 -1
  73. package/dist/src/auth/msalConnectionManager.d.ts +63 -0
  74. package/dist/src/auth/msalConnectionManager.js +124 -0
  75. package/dist/src/auth/msalConnectionManager.js.map +1 -0
  76. package/dist/src/auth/msalTokenProvider.d.ts +31 -0
  77. package/dist/src/auth/msalTokenProvider.js +167 -16
  78. package/dist/src/auth/msalTokenProvider.js.map +1 -1
  79. package/dist/src/baseAdapter.d.ts +10 -25
  80. package/dist/src/baseAdapter.js +2 -15
  81. package/dist/src/baseAdapter.js.map +1 -1
  82. package/dist/src/cloudAdapter.d.ts +40 -23
  83. package/dist/src/cloudAdapter.js +132 -56
  84. package/dist/src/cloudAdapter.js.map +1 -1
  85. package/dist/src/connector-client/connectorClient.d.ts +9 -0
  86. package/dist/src/connector-client/connectorClient.js +39 -9
  87. package/dist/src/connector-client/connectorClient.js.map +1 -1
  88. package/dist/src/index.d.ts +0 -1
  89. package/dist/src/index.js +0 -1
  90. package/dist/src/index.js.map +1 -1
  91. package/dist/src/oauth/index.d.ts +0 -1
  92. package/dist/src/oauth/index.js +0 -1
  93. package/dist/src/oauth/index.js.map +1 -1
  94. package/dist/src/oauth/userTokenClient.d.ts +30 -13
  95. package/dist/src/oauth/userTokenClient.js +64 -26
  96. package/dist/src/oauth/userTokenClient.js.map +1 -1
  97. package/dist/src/oauth/userTokenClient.types.d.ts +19 -6
  98. package/dist/src/turnContext.d.ts +7 -1
  99. package/dist/src/turnContext.js +11 -4
  100. package/dist/src/turnContext.js.map +1 -1
  101. package/package.json +10 -6
  102. package/src/activityWireCompat.ts +8 -3
  103. package/src/agent-client/agentClient.ts +9 -3
  104. package/src/agent-client/agentResponseHandler.ts +5 -2
  105. package/src/app/agentApplication.ts +98 -79
  106. package/src/app/agentApplicationBuilder.ts +2 -2
  107. package/src/app/agentApplicationOptions.ts +10 -2
  108. package/src/app/appRoute.ts +8 -0
  109. package/src/app/attachmentDownloader.ts +18 -3
  110. package/src/app/auth/authorization.ts +252 -0
  111. package/src/app/auth/authorizationManager.ts +213 -0
  112. package/src/app/auth/handlerStorage.ts +61 -0
  113. package/src/app/auth/handlers/agenticAuthorization.ts +194 -0
  114. package/src/app/auth/handlers/azureBotAuthorization.ts +599 -0
  115. package/src/app/auth/handlers/index.ts +2 -0
  116. package/src/app/auth/index.ts +2 -0
  117. package/src/app/auth/types.ts +111 -0
  118. package/src/app/index.ts +2 -3
  119. package/src/app/inputFileDownloader.ts +11 -3
  120. package/src/app/routeList.ts +24 -5
  121. package/src/app/streaming/streamingResponse.ts +20 -3
  122. package/src/app/turnState.ts +2 -61
  123. package/src/auth/MemoryCache.ts +59 -0
  124. package/src/auth/authConfiguration.ts +258 -52
  125. package/src/auth/authConstants.ts +11 -0
  126. package/src/auth/authProvider.ts +31 -0
  127. package/src/auth/connections.ts +46 -0
  128. package/src/auth/index.ts +2 -0
  129. package/src/auth/jwt-middleware.ts +38 -21
  130. package/src/auth/msalConnectionManager.ts +150 -0
  131. package/src/auth/msalTokenProvider.ts +209 -9
  132. package/src/baseAdapter.ts +10 -29
  133. package/src/cloudAdapter.ts +192 -67
  134. package/src/connector-client/connectorClient.ts +49 -10
  135. package/src/index.ts +0 -1
  136. package/src/oauth/index.ts +0 -1
  137. package/src/oauth/userTokenClient.ts +79 -23
  138. package/src/oauth/userTokenClient.types.ts +20 -8
  139. package/src/turnContext.ts +16 -5
  140. package/dist/src/app/authorization.js +0 -387
  141. package/dist/src/app/authorization.js.map +0 -1
  142. package/dist/src/claimsIdentity.d.ts +0 -35
  143. package/dist/src/claimsIdentity.js +0 -43
  144. package/dist/src/claimsIdentity.js.map +0 -1
  145. package/dist/src/oauth/oAuthFlow.d.ts +0 -119
  146. package/dist/src/oauth/oAuthFlow.js +0 -316
  147. package/dist/src/oauth/oAuthFlow.js.map +0 -1
  148. package/src/app/authorization.ts +0 -432
  149. package/src/claimsIdentity.ts +0 -47
  150. package/src/oauth/oAuthFlow.ts +0 -378
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { debug } from '@microsoft/agents-activity'
7
+ import { TurnContext } from '../../../turnContext'
8
+ import { AuthorizationHandler, AuthorizationHandlerSettings, AuthorizationHandlerStatus, AuthorizationHandlerTokenOptions } from '../types'
9
+ import { TokenResponse } from '../../../oauth'
10
+ import { AuthProvider } from '../../../auth'
11
+
12
+ const logger = debug('agents:authorization:agentic')
13
+
14
+ /**
15
+ * Options for configuring the Agentic authorization handler.
16
+ */
17
+ export interface AgenticAuthorizationOptions {
18
+ /**
19
+ * The type of authorization handler.
20
+ * @remarks
21
+ * When using environment variables, this can be set using the `${authHandlerId}_type` variable.
22
+ */
23
+ type: 'agentic'
24
+ /**
25
+ * 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`).
28
+ */
29
+ scopes?: string[]
30
+ /**
31
+ * (Optional) An alternative connection name to use for the authorization process.
32
+ * @remarks
33
+ * When using environment variables, this can be set using the `${authHandlerId}_altBlueprintConnectionName` variable.
34
+ */
35
+ altBlueprintConnectionName?: string
36
+ }
37
+
38
+ /**
39
+ * Settings for configuring the Agentic authorization handler.
40
+ */
41
+ export interface AgenticAuthorizationSettings extends AuthorizationHandlerSettings {}
42
+
43
+ /**
44
+ * Authorization handler for Agentic authentication.
45
+ */
46
+ export class AgenticAuthorization implements AuthorizationHandler {
47
+ private _options: AgenticAuthorizationOptions
48
+ private _onSuccess?: Parameters<AuthorizationHandler['onSuccess']>[0]
49
+ private _onFailure?: Parameters<AuthorizationHandler['onFailure']>[0]
50
+
51
+ /**
52
+ * Creates an instance of the AgenticAuthorization class.
53
+ * @param id The unique identifier for the authorization handler.
54
+ * @param options The options for configuring the authorization handler.
55
+ * @param settings The settings for the authorization handler.
56
+ */
57
+ constructor (public readonly id: string, options: AgenticAuthorizationOptions, private settings: AgenticAuthorizationSettings) {
58
+ if (!this.settings.connections) {
59
+ throw new Error(this.prefix('The \'connections\' option is not available in the app options. Ensure that the app is properly configured.'))
60
+ }
61
+
62
+ this._options = this.loadOptions(options)
63
+ }
64
+
65
+ /**
66
+ * Loads and validates the authorization handler options.
67
+ */
68
+ private loadOptions (settings: AgenticAuthorizationOptions) {
69
+ const result: AgenticAuthorizationOptions = {
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
80
+ }
81
+
82
+ /**
83
+ * @inheritdoc
84
+ */
85
+ signin (): Promise<AuthorizationHandlerStatus> {
86
+ return Promise.resolve(AuthorizationHandlerStatus.IGNORED)
87
+ }
88
+
89
+ /**
90
+ * @inheritdoc
91
+ */
92
+ signout (): Promise<boolean> {
93
+ return Promise.resolve(false)
94
+ }
95
+
96
+ /**
97
+ * @inheritdoc
98
+ */
99
+ async token (context: TurnContext, options?: AuthorizationHandlerTokenOptions): Promise<TokenResponse> {
100
+ try {
101
+ const tokenResponse = this.getContext(context)
102
+ if (tokenResponse.token) {
103
+ logger.debug(this.prefix('Using cached Agentic user token'))
104
+ return tokenResponse
105
+ }
106
+
107
+ let connection: AuthProvider
108
+
109
+ if (this._options.altBlueprintConnectionName?.trim()) {
110
+ connection = this.settings.connections.getConnection(this._options.altBlueprintConnectionName)
111
+ } else {
112
+ const audience = this.getAudience(context)
113
+ connection = this.settings.connections.getTokenProvider(audience, context.activity.serviceUrl ?? '')
114
+ }
115
+
116
+ const token = await connection.getAgenticUserToken(
117
+ context.activity.getAgenticInstanceId() ?? '',
118
+ context.activity.getAgenticUser() ?? '',
119
+ options?.scopes || this._options.scopes!
120
+ )
121
+
122
+ this.setContext(context, { token })
123
+ this._onSuccess?.(context)
124
+ return { token }
125
+ } catch (error) {
126
+ const reason = 'Error retrieving Agentic user token'
127
+ logger.error(this.prefix(reason), error)
128
+ this._onFailure?.(context, `${reason}: ${(error as Error).message}`)
129
+ return { token: undefined }
130
+ }
131
+ }
132
+
133
+ /**
134
+ * @inheritdoc
135
+ */
136
+ onSuccess (callback: (context: TurnContext) => void): void {
137
+ this._onSuccess = callback
138
+ }
139
+
140
+ /**
141
+ * @inheritdoc
142
+ */
143
+ onFailure (callback: (context: TurnContext, reason?: string) => void): void {
144
+ this._onFailure = callback
145
+ }
146
+
147
+ /**
148
+ * Prefixes a message with the handler ID.
149
+ */
150
+ private prefix (message: string) {
151
+ return `[handler:${this.id}] ${message}`
152
+ }
153
+
154
+ private _key = `${AgenticAuthorization.name}/${this.id}`
155
+
156
+ /**
157
+ * Sets the authorization context in the turn state.
158
+ */
159
+ private setContext (context: TurnContext, data: TokenResponse) {
160
+ return context.turnState.set(this._key, () => data)
161
+ }
162
+
163
+ /**
164
+ * Gets the authorization context from the turn state.
165
+ */
166
+ private getContext (context: TurnContext): TokenResponse {
167
+ const result = context.turnState.get(this._key)
168
+ return result?.() ?? { token: undefined }
169
+ }
170
+
171
+ /**
172
+ * Gets the audience from the turn context.
173
+ */
174
+ private getAudience (context: TurnContext): string {
175
+ const { aud } = context.identity
176
+ if (!aud) {
177
+ throw new Error('No audience (aud) claim found in Activity.identity.')
178
+ }
179
+ return Array.isArray(aud) ? aud[0] : aud
180
+ }
181
+
182
+ /**
183
+ * Loads the OAuth scopes from the environment variables.
184
+ */
185
+ private loadScopes (value:string | undefined): string[] {
186
+ return value?.split(',').reduce<string[]>((acc, scope) => {
187
+ const trimmed = scope.trim()
188
+ if (trimmed) {
189
+ acc.push(trimmed)
190
+ }
191
+ return acc
192
+ }, []) ?? []
193
+ }
194
+ }