Skip to content

Commit

Permalink
Add invoke auth refresh subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Oct 7, 2024
1 parent 09cbc1e commit 83617d5
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion packages/cli/src/oclif/commands/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,63 @@ class InvokeCommand extends BaseCommand {
}
}

async refreshOAuth2(appDefinition, authData) {
startSpinner('Invoking authentication.oauth2Config.refreshAccessToken');

const newAuthData = await localAppCommand({
command: 'execute',
method: 'authentication.oauth2Config.refreshAccessToken',
bundle: {
authData,
},
});

endSpinner();
return newAuthData;
}

async refreshSessionAuth(appDefinition, authData) {
startSpinner('Invoking authentication.sessionConfig.perform');

const sessionData = await localAppCommand({
command: 'execute',
method: 'authentication.sessionConfig.perform',
bundle: {
authData,
},
});

endSpinner();
return sessionData;
}

async refreshAuth(appDefinition, authData) {
const authentication = appDefinition.authentication;
if (!authentication) {
console.warn(
"Your integration doesn't seem to need authentication. " +
"If that isn't true, the app definition should have " +
'an `authentication` object at the root level.'
);
return null;
}
if (_.isEmpty(authData)) {
throw new Error(
'No auth data found in the .env file. Run `zapier invoke auth start` first to initialize the auth data.'
);
}
switch (authentication.type) {
case 'oauth2':
return this.refreshOAuth2(appDefinition, authData);
case 'session':
return this.refreshSessionAuth(appDefinition, authData);
default:
throw new Error(
`This command doesn't support refreshing authentication type "${authentication.type}".`
);
}
}

async promptForField(
field,
appDefinition,
Expand Down Expand Up @@ -906,7 +963,6 @@ class InvokeCommand extends BaseCommand {
isTestingAuth: true,
};
switch (actionKey) {
// TODO: Add 'refresh' command
case 'start': {
const newAuthData = await this.startAuth(appDefinition);
if (_.isEmpty(newAuthData)) {
Expand All @@ -918,6 +974,17 @@ class InvokeCommand extends BaseCommand {
);
return;
}
case 'refresh': {
const newAuthData = await this.refreshAuth(appDefinition, authData);
if (_.isEmpty(newAuthData)) {
return;
}
await appendEnv(newAuthData, AUTH_FIELD_ENV_PREFIX);
console.warn(
'Auth data has been refreshed and appended to .env file. Run `zapier invoke auth test` to test it.'
);
return;
}
case 'test': {
const output = await testAuth(authData, meta, zcacheTestObj);
console.log(JSON.stringify(output, null, 2));
Expand Down

0 comments on commit 83617d5

Please sign in to comment.