Skip to content

Commit 045fdf4

Browse files
committed
Refactor OAuth token handling for new structure support
Updated APICall, AccessTokenManager, and OAuth.helper to support both legacy and new OAuth token storage structures. Added logic to distinguish between structures, improved parameter extraction for OAuth1 signatures, and ensured correct token updates and retrievals for both formats. Also replaced usage of oauthService with oauth_con_id for more robust identification.
1 parent 26b7940 commit 045fdf4

File tree

3 files changed

+272
-95
lines changed

3 files changed

+272
-95
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ export class APICall extends Component {
5959
consumerSecret: Joi.string().allow('').label('Consumer Secret'),
6060
oauth1CallbackURL: Joi.string().allow('').label('OAuth1 Callback URL'),
6161
authenticate: Joi.string().allow('').label('Authenticate'),
62+
oauth_con_id: Joi.string().allow('').label('OAuth Connection ID'),
6263
});
6364
constructor() {
6465
super();
6566
}
6667

67-
init() {}
68+
init() { }
6869

6970
async process(input, config, agent: Agent) {
7071
await super.process(input, config, agent);
@@ -113,10 +114,9 @@ export class APICall extends Component {
113114
let Headers: any = {};
114115
let _error: any = undefined;
115116
try {
116-
if (config?.data?.oauthService && config?.data?.oauthService !== 'None') {
117-
const rootUrl = new URL(reqConfig.url).origin;
117+
if (config?.data?.oauth_con_id !== '' && config?.data?.oauth_con_id !== 'None') {
118118
const additionalParams = extractAdditionalParamsForOAuth1(reqConfig);
119-
const oauthHeaders = await generateOAuthHeaders(agent, config, reqConfig, logger, additionalParams, rootUrl);
119+
const oauthHeaders = await generateOAuthHeaders(agent, config, reqConfig, logger, additionalParams);
120120
//reqConfig.headers = { ...reqConfig.headers, ...oauthHeaders };
121121
reqConfig.headers = reqConfig.headers.concat({ ...oauthHeaders });
122122
}

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,35 @@ class AccessTokenManager {
1919
private secondaryToken: string; // refreshToken || tokenSecret
2020
private tokenUrl: string; // tokenURL to refresh accessToken
2121
private expires_in: any;
22-
private data: any; // value of key(keyId) in teamSettings that needs to be updated if required
23-
private keyId: any; // key of object in teamSettings
22+
private tokensData: any; // Full tokens data object
23+
private keyId: any; // key of object in teamSettings
2424
private logger: any; // Use to log console in debugger
2525
private agent: Agent;
26+
private isNewStructure: boolean;
2627
constructor(
2728
clientId: string,
2829
clientSecret: string,
2930
secondaryToken: string,
3031
tokenUrl: string,
3132
expires_in: any,
3233
primaryToken: string,
33-
data: any,
34+
tokensData: any,
3435
keyId: any,
3536
logger: any,
3637
agent: Agent,
38+
isNewStructure: boolean = false,
3739
) {
3840
this.clientId = clientId;
3941
this.clientSecret = clientSecret;
4042
this.primaryToken = primaryToken;
4143
this.secondaryToken = secondaryToken;
4244
this.tokenUrl = tokenUrl;
4345
this.expires_in = expires_in;
44-
this.data = data;
46+
this.tokensData = tokensData;
4547
this.keyId = keyId;
4648
this.logger = logger;
4749
this.agent = agent;
50+
this.isNewStructure = isNewStructure;
4851
}
4952

5053
async getAccessToken(): Promise<string> {
@@ -105,19 +108,46 @@ class AccessTokenManager {
105108
this.logger.debug('Access token refreshed successfully.');
106109
const expiresInMilliseconds: number = response?.data?.expires_in ? response?.data?.expires_in * 1000 : response?.data?.expires_in;
107110
const expirationTimestamp: number = expiresInMilliseconds ? new Date().getTime() + expiresInMilliseconds : expiresInMilliseconds;
108-
this.data.primary = newAccessToken;
109-
this.data.expires_in = expirationTimestamp ? expirationTimestamp?.toString() : expirationTimestamp;
110-
//const oauthTeamSettings = new OauthTeamSettings();
111-
//const save: any = await oauthTeamSettings.update({ keyId: this.keyId, data: this.data });
112111

113-
const save: any = await managedVault.user(AccessCandidate.agent(this.agent.id)).set(this.keyId, JSON.stringify(this.data));
112+
// Maintain the same structure format when saving
113+
let updatedData;
114+
if (this.isNewStructure) {
115+
// Maintain new structure format
116+
updatedData = {
117+
...this.tokensData,
118+
auth_data: {
119+
...this.tokensData.auth_data,
120+
primary: newAccessToken,
121+
secondary: this.secondaryToken,
122+
expires_in: expirationTimestamp ? expirationTimestamp.toString() : expirationTimestamp
123+
}
124+
};
125+
} else {
126+
// Maintain old structure format
127+
updatedData = {
128+
...this.tokensData,
129+
primary: newAccessToken,
130+
expires_in: expirationTimestamp ? expirationTimestamp.toString() : expirationTimestamp
131+
};
132+
// Keep secondary token if it exists
133+
if (this.secondaryToken) {
134+
updatedData.secondary = this.secondaryToken;
135+
}
136+
}
137+
138+
const save: any = await managedVault.user(AccessCandidate.agent(this.agent.id)).set(this.keyId, JSON.stringify(updatedData));
114139
if (save && save.status === 200) {
115140
console.log('Access token value is updated successfully.');
116141
this.logger.debug('Access token value is updated successfully.');
117142
} else {
118143
console.log('Warning: new access token value is not updated.');
119144
this.logger.debug('Warning: new access token value is not updated.');
120145
}
146+
147+
// Update internal tokensData reference
148+
this.tokensData = updatedData;
149+
this.primaryToken = newAccessToken;
150+
121151
return newAccessToken;
122152
} catch (error) {
123153
console.error('Failed to refresh access token:', error);

0 commit comments

Comments
 (0)