Skip to content

Commit e812aff

Browse files
committed
Improve token persistence and sensitive data handling
AccessTokenManager now correctly persists rotated refresh tokens and handles expiration timestamps with nullish checks. OAuth.helper.ts avoids logging sensitive OAuth config data in plaintext and refines token data structure updates to better preserve existing fields.
1 parent 46af874 commit e812aff

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

packages/core/src/Components/APICall/AccessTokenManager.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,23 @@ class AccessTokenManager {
116116
updatedData = {
117117
...this.tokensData,
118118
auth_data: {
119-
...this.tokensData.auth_data,
119+
...(this.tokensData?.auth_data ?? {}),
120120
primary: newAccessToken,
121-
secondary: this.secondaryToken,
122-
expires_in: expirationTimestamp ? expirationTimestamp.toString() : expirationTimestamp
121+
// Persist rotated refresh_token when provided; fall back to existing
122+
secondary: (response?.data?.refresh_token ?? this.secondaryToken),
123+
// Use nullish check so 0 is preserved
124+
expires_in: (expirationTimestamp ?? undefined) !== undefined ? String(expirationTimestamp) : undefined
123125
}
124126
};
125127
} else {
126128
// Maintain old structure format
127129
updatedData = {
128130
...this.tokensData,
129131
primary: newAccessToken,
130-
expires_in: expirationTimestamp ? expirationTimestamp.toString() : expirationTimestamp
132+
expires_in: (expirationTimestamp ?? undefined) !== undefined ? String(expirationTimestamp) : undefined
131133
};
132-
// Keep secondary token if it exists
133-
if (this.secondaryToken) {
134-
updatedData.secondary = this.secondaryToken;
135-
}
134+
// Persist rotated refresh_token when provided; otherwise keep existing
135+
updatedData.secondary = (response?.data?.refresh_token ?? this.secondaryToken);
136136
}
137137

138138
const save: any = await managedVault.user(AccessCandidate.agent(this.agent.id)).set(this.keyId, JSON.stringify(updatedData));

packages/core/src/Components/APICall/OAuth.helper.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ export const handleOAuthHeaders = async (agent, config, reqConfig, logger, addit
286286
oAuthConfigString = await TemplateString(oAuthConfigString).parseTeamKeysAsync(oauthTokens.team || agent.teamId).asyncResult;
287287

288288
const oAuthConfig = JSON.parse(oAuthConfigString);
289-
console.log('oAuthConfig', oAuthConfig);
289+
// Avoid logging sensitive OAuth config in plaintext
290+
// console.log('oAuthConfig', { ...oAuthConfig, clientSecret: '***' });
290291
if (oauthTokens.service === 'oauth2_client_credentials') {
291292
const accessToken = await getClientCredentialToken(tokensData, logger, keyId, oauthTokens, config, agent, isNewStructure);
292293
headers['Authorization'] = `Bearer ${accessToken}`;
@@ -388,20 +389,26 @@ async function getClientCredentialToken(tokensData, logger, keyId, oauthTokens,
388389
// Maintain the same structure format when saving
389390
let updatedData;
390391
if (isNewStructure) {
391-
// Maintain new structure format
392+
// Maintain new structure format; preserve existing fields
393+
const parts = String(config?.data?.oauth_con_id ?? '').split('_');
394+
const prefixSuffix = parts.length > 1 ? parts[1] : parts[0];
395+
const oauthKeysPrefix = prefixSuffix ? `OAUTH_${prefixSuffix}` : undefined;
392396
updatedData = {
397+
...(tokensData || {}),
393398
auth_data: {
399+
...(tokensData?.auth_data || {}),
394400
primary: newAccessToken,
395401
expires_in: expirationTimestamp.toString()
396402
},
397-
auth_settings: tokensData.auth_settings || {
403+
auth_settings: {
404+
...(tokensData?.auth_settings || {}),
398405
type: 'oauth2',
399406
tokenURL,
400407
clientID,
401408
clientSecret,
402-
oauth_keys_prefix: `OAUTH_${config?.data?.oauth_con_id?.split('_')[1]}`,
403-
service: 'oauth2_client_credentials'
404-
}
409+
...(oauthKeysPrefix ? { oauth_keys_prefix: oauthKeysPrefix } : {}),
410+
service: 'oauth2_client_credentials',
411+
},
405412
};
406413
} else {
407414
// Maintain old structure format

0 commit comments

Comments
 (0)