@microsoft/agents-hosting 0.5.12-g2d752e9b13 → 0.5.16-g6bdf69cc43

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 (41) hide show
  1. package/dist/src/app/agentApplication.d.ts +186 -20
  2. package/dist/src/app/agentApplication.js +234 -32
  3. package/dist/src/app/agentApplication.js.map +1 -1
  4. package/dist/src/app/agentApplicationBuilder.d.ts +1 -1
  5. package/dist/src/app/agentApplicationOptions.d.ts +1 -1
  6. package/dist/src/app/appRoute.d.ts +5 -0
  7. package/dist/src/app/authorization.d.ts +293 -0
  8. package/dist/src/app/authorization.js +375 -0
  9. package/dist/src/app/authorization.js.map +1 -0
  10. package/dist/src/app/index.d.ts +1 -1
  11. package/dist/src/app/index.js +1 -1
  12. package/dist/src/app/index.js.map +1 -1
  13. package/dist/src/auth/index.d.ts +1 -0
  14. package/dist/src/auth/index.js +1 -0
  15. package/dist/src/auth/index.js.map +1 -1
  16. package/dist/src/auth/jwt-middleware.js.map +1 -1
  17. package/dist/src/auth/msalTokenCredential.d.ts +10 -0
  18. package/dist/src/auth/msalTokenCredential.js +19 -0
  19. package/dist/src/auth/msalTokenCredential.js.map +1 -0
  20. package/dist/src/auth/msalTokenProvider.d.ts +1 -0
  21. package/dist/src/auth/msalTokenProvider.js +15 -0
  22. package/dist/src/auth/msalTokenProvider.js.map +1 -1
  23. package/dist/src/oauth/oAuthFlow.d.ts +53 -9
  24. package/dist/src/oauth/oAuthFlow.js +164 -35
  25. package/dist/src/oauth/oAuthFlow.js.map +1 -1
  26. package/package.json +4 -3
  27. package/src/app/agentApplication.ts +247 -32
  28. package/src/app/agentApplicationBuilder.ts +1 -1
  29. package/src/app/agentApplicationOptions.ts +1 -1
  30. package/src/app/appRoute.ts +6 -0
  31. package/src/app/authorization.ts +418 -0
  32. package/src/app/index.ts +1 -1
  33. package/src/auth/index.ts +1 -0
  34. package/src/auth/jwt-middleware.ts +1 -1
  35. package/src/auth/msalTokenCredential.ts +14 -0
  36. package/src/auth/msalTokenProvider.ts +17 -1
  37. package/src/oauth/oAuthFlow.ts +196 -34
  38. package/dist/src/app/oauth/authorization.d.ts +0 -88
  39. package/dist/src/app/oauth/authorization.js +0 -134
  40. package/dist/src/app/oauth/authorization.js.map +0 -1
  41. package/src/app/oauth/authorization.ts +0 -160
@@ -1,160 +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 '../../logger'
8
- import { TurnState } from '../turnState'
9
- import { Storage } from '../../storage'
10
- import { OAuthFlow, TokenResponse } from '../../oauth'
11
-
12
- const logger = debug('agents:authorization')
13
-
14
- /**
15
- * Interface defining an authorization handler for OAuth flows
16
- * @interface AuthHandler
17
- */
18
- export interface AuthHandler {
19
- /** Connection name for the auth provider */
20
- name?: string,
21
- /** Whether authorization should be triggered automatically */
22
- auto?: boolean,
23
- /** The OAuth flow implementation */
24
- flow?: OAuthFlow,
25
- /** Title to display on auth cards/UI */
26
- title?: string,
27
- /** Text to display on auth cards/UI */
28
- text?: string,
29
- }
30
-
31
- /**
32
- * Options for configuring user authorization.
33
- * Contains settings to configure OAuth connections.
34
- */
35
- export interface AuthorizationHandlers extends Record<string, AuthHandler> {}
36
-
37
- /**
38
- * Class responsible for managing authorization and OAuth flows
39
- * @class Authorization
40
- */
41
- export class Authorization {
42
- _authHandlers: AuthorizationHandlers
43
-
44
- /**
45
- * Creates a new instance of UserAuthorization.
46
- * @param {Storage} storage - The storage system to use for state management.
47
- * @param {AuthorizationHandlers} authHandlers - Configuration for OAuth providers
48
- * @throws {Error} If storage is null/undefined or no auth handlers are provided
49
- */
50
- constructor (private storage: Storage, authHandlers: AuthorizationHandlers) {
51
- if (storage === undefined || storage === null) {
52
- throw new Error('Storage is required for UserAuthorization')
53
- }
54
- if (authHandlers === undefined || Object.keys(authHandlers).length === 0) {
55
- throw new Error('The authorization does not have any auth handlers')
56
- }
57
- this._authHandlers = authHandlers
58
- for (const ah in this._authHandlers) {
59
- if (this._authHandlers![ah].name === undefined && process.env[ah + '_connectionName'] === undefined) {
60
- throw new Error(`AuthHandler name ${ah}_connectionName not set in autorization and not found in env vars.`)
61
- }
62
- const currentAuthHandler = this._authHandlers![ah]
63
- currentAuthHandler.name = currentAuthHandler.name ?? process.env[ah + '_connectionName'] as string
64
- currentAuthHandler.title = currentAuthHandler.title ?? process.env[ah + '_connectionTitle'] as string
65
- currentAuthHandler.text = currentAuthHandler.text ?? process.env[ah + '_connectionText'] as string
66
- currentAuthHandler.auto = currentAuthHandler.auto ?? process.env[ah + '_connectionAuto'] === 'true'
67
- currentAuthHandler.flow = new OAuthFlow(this.storage, currentAuthHandler.name, null!, currentAuthHandler.title, currentAuthHandler.text)
68
- }
69
- logger.info('Authorization handlers configured with', this._authHandlers.length, 'handlers')
70
- }
71
-
72
- /**
73
- * Gets the token for a specific auth handler
74
- * @param {TurnContext} context - The context object for the current turn
75
- * @param {string} [authHandlerId] - Optional ID of the auth handler to use, defaults to first handler
76
- * @returns {Promise<TokenResponse>} The token response from the OAuth provider
77
- */
78
- public async getToken (context: TurnContext, authHandlerId?: string): Promise<TokenResponse> {
79
- logger.info('getToken from user token service for authHandlerId:', authHandlerId)
80
- const authHandler = this.resolverHandler(authHandlerId)
81
- return await authHandler.flow?.getUserToken(context)!
82
- }
83
-
84
- /**
85
- * Begins or continues an OAuth flow
86
- * @param {TurnContext} context - The context object for the current turn
87
- * @param {TurnState} state - The state object for the current turn
88
- * @param {string} [authHandlerId] - Optional ID of the auth handler to use, defaults to first handler
89
- * @returns {Promise<TokenResponse>} The token response from the OAuth provider
90
- */
91
- public async beginOrContinueFlow (context: TurnContext, state: TurnState, authHandlerId?: string) : Promise<TokenResponse> {
92
- logger.info('beginOrContinueFlow for authHandlerId:', authHandlerId)
93
- const flow = this.resolverHandler(authHandlerId).flow!
94
- let tokenResponse: TokenResponse | undefined
95
- if (flow.state!.flowStarted === false) {
96
- tokenResponse = await flow.beginFlow(context)
97
- } else {
98
- tokenResponse = await flow.continueFlow(context)
99
- if (tokenResponse && tokenResponse.token) {
100
- if (this._signInHandler) {
101
- await this._signInHandler(context, state, authHandlerId)
102
- }
103
- }
104
- }
105
- return tokenResponse!
106
- }
107
-
108
- /**
109
- * Gets the current state of the OAuth flow
110
- * @param {string} [authHandlerId] - Optional ID of the auth handler to check, defaults to first handler
111
- * @returns {boolean} Whether the flow has started
112
- */
113
- public getFlowState (authHandlerId?: string) : boolean {
114
- const flow = this.resolverHandler(authHandlerId).flow!
115
- return flow.state?.flowStarted!
116
- }
117
-
118
- /**
119
- * Resolves the auth handler to use based on the provided ID
120
- * @param {string} [authHandlerId] - Optional ID of the auth handler to resolve, defaults to first handler
121
- * @returns {AuthHandler} The resolved auth handler
122
- */
123
- resolverHandler = (authHandlerId?: string) : AuthHandler => {
124
- if (authHandlerId) {
125
- return this._authHandlers![authHandlerId]
126
- }
127
- return this._authHandlers![Object.keys(this._authHandlers)[0]]
128
- }
129
-
130
- /**
131
- * Signs out the current user.
132
- * This method clears the user's token and resets the SSO state.
133
- *
134
- * @param {TurnContext} context - The context object for the current turn.
135
- * @param {TurnState} state - The state object for the current turn.
136
- * @param {string} [authHandlerId] - Optional ID of the auth handler to use for sign out
137
- * @returns {Promise<void>}
138
- */
139
- async signOut (context: TurnContext, state: TurnState, authHandlerId?: string) : Promise<void> {
140
- logger.info('signOut for authHandlerId:', authHandlerId)
141
- if (authHandlerId === undefined) { // aw
142
- for (const ah in this._authHandlers) {
143
- const flow = this._authHandlers[ah].flow
144
- await flow?.signOut(context)
145
- }
146
- } else {
147
- await this.resolverHandler(authHandlerId).flow?.signOut(context)
148
- }
149
- }
150
-
151
- _signInHandler: ((context: TurnContext, state: TurnState, authHandlerId?: string) => void) | null = null
152
-
153
- /**
154
- * Sets a handler to be called when sign-in is successfully completed
155
- * @param {Function} handler - The handler function to call on successful sign-in
156
- */
157
- public onSignInSuccess (handler: (context: TurnContext, state: TurnState, authHandlerId?: string) => void) {
158
- this._signInHandler = handler
159
- }
160
- }