@microsoft/agents-hosting 0.6.17-g5d9066ba37 → 0.6.21-g3c2261b2fc

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 (34) hide show
  1. package/dist/package.json +2 -2
  2. package/dist/src/app/agentApplication.js +7 -3
  3. package/dist/src/app/agentApplication.js.map +1 -1
  4. package/dist/src/app/authorization.d.ts +2 -2
  5. package/dist/src/app/authorization.js +2 -2
  6. package/dist/src/app/authorization.js.map +1 -1
  7. package/dist/src/auth/jwt-middleware.js +3 -3
  8. package/dist/src/auth/jwt-middleware.js.map +1 -1
  9. package/dist/src/auth/msalTokenProvider.js +3 -3
  10. package/dist/src/auth/msalTokenProvider.js.map +1 -1
  11. package/dist/src/baseAdapter.d.ts +5 -0
  12. package/dist/src/baseAdapter.js +4 -0
  13. package/dist/src/baseAdapter.js.map +1 -1
  14. package/dist/src/cloudAdapter.d.ts +2 -1
  15. package/dist/src/cloudAdapter.js +6 -8
  16. package/dist/src/cloudAdapter.js.map +1 -1
  17. package/dist/src/oauth/oAuthFlow.d.ts +2 -2
  18. package/dist/src/oauth/oAuthFlow.js +20 -12
  19. package/dist/src/oauth/oAuthFlow.js.map +1 -1
  20. package/dist/src/oauth/userTokenClient.d.ts +3 -2
  21. package/dist/src/oauth/userTokenClient.js +54 -48
  22. package/dist/src/oauth/userTokenClient.js.map +1 -1
  23. package/dist/src/storage/memoryStorage.js +3 -3
  24. package/dist/src/storage/memoryStorage.js.map +1 -1
  25. package/package.json +2 -2
  26. package/src/app/agentApplication.ts +5 -2
  27. package/src/app/authorization.ts +3 -3
  28. package/src/auth/jwt-middleware.ts +3 -3
  29. package/src/auth/msalTokenProvider.ts +3 -3
  30. package/src/baseAdapter.ts +6 -0
  31. package/src/cloudAdapter.ts +6 -10
  32. package/src/oauth/oAuthFlow.ts +18 -12
  33. package/src/oauth/userTokenClient.ts +55 -43
  34. package/src/storage/memoryStorage.ts +3 -3
@@ -15,14 +15,12 @@ const logger = debug('agents:user-token-client')
15
15
  */
16
16
  export class UserTokenClient {
17
17
  client: AxiosInstance
18
- msAppId: string
19
18
  /**
20
19
  * Creates a new instance of UserTokenClient.
21
20
  * @param token The token to use for authentication.
22
21
  * @param msAppId The Microsoft application ID.
23
22
  */
24
- constructor (token: string, appId: string) {
25
- this.msAppId = appId
23
+ constructor (private msAppId: string) {
26
24
  const baseURL = 'https://api.botframework.com'
27
25
  const axiosInstance = axios.create({
28
26
  baseURL,
@@ -31,7 +29,35 @@ export class UserTokenClient {
31
29
  'User-Agent': getProductInfo(),
32
30
  }
33
31
  })
34
- axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`
32
+ // axiosInstance.defaults.headers.common.Authorization = `Bearer ${token}`
33
+ axiosInstance.interceptors.response.use(
34
+ (config) => {
35
+ const { status, statusText, config: requestConfig } = config
36
+ logger.debug('Response: ', {
37
+ status,
38
+ statusText,
39
+ host: axiosInstance.getUri(),
40
+ url: requestConfig?.url,
41
+ data: config.config.data,
42
+ method: requestConfig?.method,
43
+ })
44
+ return config
45
+ },
46
+ (error) => {
47
+ const { code, status, message, stack, response } = error
48
+ if (status !== 404) {
49
+ const errorDetails = {
50
+ code,
51
+ host: axiosInstance.getUri(),
52
+ url: error.config.url,
53
+ method: error.config.method,
54
+ data: error.config.data,
55
+ message: message + JSON.stringify(response?.data),
56
+ stack,
57
+ }
58
+ return Promise.reject(errorDetails)
59
+ }
60
+ })
35
61
  this.client = axiosInstance
36
62
  }
37
63
 
@@ -44,16 +70,12 @@ export class UserTokenClient {
44
70
  * @returns A promise that resolves to the user token.
45
71
  */
46
72
  async getUserToken (connectionName: string, channelId: string, userId: string, code?: string) : Promise<TokenResponse> {
47
- try {
48
- const params = { connectionName, channelId, userId, code }
49
- const response = await this.client.get('/api/usertoken/GetToken', { params })
73
+ const params = { connectionName, channelId, userId, code }
74
+ const response = await this.client.get('/api/usertoken/GetToken', { params })
75
+ if (response?.data) {
50
76
  return response.data as TokenResponse
51
- } catch (error: any) {
52
- if (error.response?.status !== 404) {
53
- logger.error(error)
54
- }
55
- return { token: undefined }
56
77
  }
78
+ return { token: undefined }
57
79
  }
58
80
 
59
81
  /**
@@ -64,14 +86,9 @@ export class UserTokenClient {
64
86
  * @returns A promise that resolves when the sign-out operation is complete.
65
87
  */
66
88
  async signOut (userId: string, connectionName: string, channelId: string) : Promise<void> {
67
- try {
68
- const params = { userId, connectionName, channelId }
69
- const response = await this.client.delete('/api/usertoken/SignOut', { params })
70
- if (response.status !== 200) {
71
- throw new Error('Failed to sign out')
72
- }
73
- } catch (error: any) {
74
- logger.error(error)
89
+ const params = { userId, connectionName, channelId }
90
+ const response = await this.client.delete('/api/usertoken/SignOut', { params })
91
+ if (response.status !== 200) {
75
92
  throw new Error('Failed to sign out')
76
93
  }
77
94
  }
@@ -84,22 +101,17 @@ export class UserTokenClient {
84
101
  * @returns A promise that resolves to the signing resource.
85
102
  */
86
103
  async getSignInResource (msAppId: string, connectionName: string, conversation: ConversationReference, relatesTo?: ConversationReference) : Promise<SignInResource> {
87
- try {
88
- const tokenExchangeState = {
89
- connectionName,
90
- conversation,
91
- relatesTo,
92
- msAppId
93
- }
94
- const tokenExchangeStateNormalized = normalizeTokenExchangeState(tokenExchangeState)
95
- const state = Buffer.from(JSON.stringify(tokenExchangeStateNormalized)).toString('base64')
96
- const params = { state }
97
- const response = await this.client.get('/api/botsignin/GetSignInResource', { params })
98
- return response.data as SignInResource
99
- } catch (error: any) {
100
- logger.error(error)
101
- throw error
104
+ const tokenExchangeState = {
105
+ connectionName,
106
+ conversation,
107
+ relatesTo,
108
+ msAppId
102
109
  }
110
+ const tokenExchangeStateNormalized = normalizeTokenExchangeState(tokenExchangeState)
111
+ const state = Buffer.from(JSON.stringify(tokenExchangeStateNormalized)).toString('base64')
112
+ const params = { state }
113
+ const response = await this.client.get('/api/botsignin/GetSignInResource', { params })
114
+ return response.data as SignInResource
103
115
  }
104
116
 
105
117
  /**
@@ -111,15 +123,11 @@ export class UserTokenClient {
111
123
  * @returns A promise that resolves to the exchanged token.
112
124
  */
113
125
  async exchangeTokenAsync (userId: string, connectionName: string, channelId: string, tokenExchangeRequest: TokenExchangeRequest) : Promise<TokenResponse> {
114
- try {
115
- const params = { userId, connectionName, channelId }
116
- const response = await this.client.post('/api/usertoken/exchange', tokenExchangeRequest, { params })
126
+ const params = { userId, connectionName, channelId }
127
+ const response = await this.client.post('/api/usertoken/exchange', tokenExchangeRequest, { params })
128
+ if (response?.data) {
117
129
  return response.data as TokenResponse
118
- } catch (error: any) {
119
- logger.error(error)
120
- if (error.response?.data) {
121
- logger.error(error.response.data)
122
- }
130
+ } else {
123
131
  return { token: undefined }
124
132
  }
125
133
  }
@@ -169,4 +177,8 @@ export class UserTokenClient {
169
177
  const response = await this.client.post('/api/usertoken/GetAadTokens', resourceUrls, { params })
170
178
  return response.data as Record<string, TokenResponse>
171
179
  }
180
+
181
+ public updateAuthToken (token: string): void {
182
+ this.client.defaults.headers.common.Authorization = `Bearer ${token}`
183
+ }
172
184
  }
@@ -65,7 +65,7 @@ export class MemoryStorage implements Storage {
65
65
 
66
66
  const data: StoreItem = {}
67
67
  for (const key of keys) {
68
- logger.info(`Reading key: ${key}`)
68
+ logger.debug(`Reading key: ${key}`)
69
69
  const item = this.memory[key]
70
70
  if (item) {
71
71
  data[key] = JSON.parse(item)
@@ -93,7 +93,7 @@ export class MemoryStorage implements Storage {
93
93
  }
94
94
 
95
95
  for (const [key, newItem] of Object.entries(changes)) {
96
- logger.info(`Writing key: ${key}`)
96
+ logger.debug(`Writing key: ${key}`)
97
97
  const oldItemStr = this.memory[key]
98
98
  if (!oldItemStr || newItem.eTag === '*' || !newItem.eTag) {
99
99
  this.saveItem(key, newItem)
@@ -115,7 +115,7 @@ export class MemoryStorage implements Storage {
115
115
  * @returns A promise that resolves when the delete operation is complete
116
116
  */
117
117
  async delete (keys: string[]): Promise<void> {
118
- logger.info(`Deleting keys: ${keys.join(', ')}`)
118
+ logger.debug(`Deleting keys: ${keys.join(', ')}`)
119
119
  for (const key of keys) {
120
120
  delete this.memory[key]
121
121
  }