Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(install-provider): do not duplicate installation if tokens werent… #1497

Merged
merged 1 commit into from Jun 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 39 additions & 34 deletions packages/oauth/src/install-provider.ts
Expand Up @@ -238,40 +238,45 @@ export class InstallProvider {
'if it has a refresh token';
throw new UnknownError(errorMessage);
}
const installationUpdates: TokenRefreshInstallationUpdates = {
...queryResult as Installation<'v2', boolean>,
};
const refreshResponses: OAuthV2TokenRefreshResponse[] = await this.refreshExpiringTokens(tokensToRefresh);
refreshResponses.forEach((refreshResp) => {
const tokenType = refreshResp.token_type;

// Update Authorization
if (tokenType === 'bot') {
authResult.botToken = refreshResp.access_token;
authResult.botRefreshToken = refreshResp.refresh_token;
authResult.botTokenExpiresAt = currentUTCSec + refreshResp.expires_in;
}

if (tokenType === 'user') {
authResult.userToken = refreshResp.access_token;
authResult.userRefreshToken = refreshResp.refresh_token;
authResult.userTokenExpiresAt = currentUTCSec + refreshResp.expires_in;
}

// Update Installation
const botOrUser = installationUpdates[tokenType];
if (botOrUser !== undefined) {
this.logger.debug(`Saving ${tokenType} token and its refresh token in InstallationStore`);
botOrUser.token = refreshResp.access_token;
botOrUser.refreshToken = refreshResp.refresh_token;
botOrUser.expiresAt = currentUTCSec + refreshResp.expires_in;
} else {
const errorMessage = `Unexpected data structure detected. The data returned by your InstallationStore#fetchInstallation() method must have ${tokenType} at top-level`;
throw new UnknownError(errorMessage);
}
});
await this.installationStore.storeInstallation(installationUpdates);
this.logger.debug('Refreshed tokens have been saved in InstallationStore');
if (refreshResponses.length) {
const installationUpdates: TokenRefreshInstallationUpdates = {
...queryResult as Installation<'v2', boolean>,
};
refreshResponses.forEach((refreshResp) => {
const tokenType = refreshResp.token_type;

// Update Authorization
if (tokenType === 'bot') {
authResult.botToken = refreshResp.access_token;
authResult.botRefreshToken = refreshResp.refresh_token;
authResult.botTokenExpiresAt = currentUTCSec + refreshResp.expires_in;
}

if (tokenType === 'user') {
authResult.userToken = refreshResp.access_token;
authResult.userRefreshToken = refreshResp.refresh_token;
authResult.userTokenExpiresAt = currentUTCSec + refreshResp.expires_in;
}

// Update Installation
const botOrUser = installationUpdates[tokenType];
if (botOrUser !== undefined) {
this.logger.debug(`Saving ${tokenType} token and its refresh token in InstallationStore`);
botOrUser.token = refreshResp.access_token;
botOrUser.refreshToken = refreshResp.refresh_token;
botOrUser.expiresAt = currentUTCSec + refreshResp.expires_in;
} else {
const errorMessage = `Unexpected data structure detected. The data returned by your InstallationStore#fetchInstallation() method must have ${tokenType} at top-level`;
throw new UnknownError(errorMessage);
}
});
await this.installationStore.storeInstallation(installationUpdates);
this.logger.debug('Refreshed tokens have been saved in InstallationStore');
}
else {
this.logger.debug('No tokens were refreshed');
}
}
}

Expand Down Expand Up @@ -325,7 +330,7 @@ export class InstallProvider {
): Promise<void> {
if (installOptions === undefined && this.installUrlOptions === undefined) {
const errorMessage = 'To enable the built-in install path handler, you need to pass InstallURLOptions to InstallProvider. ' +
"If you're using @slack/bolt, please upgrade the framework to the latest version.";
"If you're using @slack/bolt, please upgrade the framework to the latest version.";
throw new GenerateInstallUrlError(errorMessage);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down