Skip to content

Commit

Permalink
regression: handle cloud delete registration (#31038)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored Nov 21, 2023
1 parent 4f09b2e commit 26b3638
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
15 changes: 10 additions & 5 deletions apps/meteor/app/cloud/server/functions/getWorkspaceAccessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
* @param {boolean} save
* @returns string
*/
export async function getWorkspaceAccessToken(forceNew = false, scope = '', save = true): Promise<string> {
export async function getWorkspaceAccessToken(forceNew = false, scope = '', save = true, throwOnError = false): Promise<string> {
const { workspaceRegistered } = await retrieveRegistrationStatus();

if (!workspaceRegistered) {
Expand All @@ -29,15 +29,14 @@ export async function getWorkspaceAccessToken(forceNew = false, scope = '', save
return settings.get<string>('Cloud_Workspace_Access_Token');
}

const accessToken = await getWorkspaceAccessTokenWithScope(scope);
const accessToken = await getWorkspaceAccessTokenWithScope(scope, throwOnError);

if (save) {
await Promise.all([
Settings.updateValueById('Cloud_Workspace_Access_Token', accessToken.token),
Settings.updateValueById('Cloud_Workspace_Access_Token_Expires_At', accessToken.expiresAt),
]);
}

return accessToken.token;
}

Expand All @@ -47,11 +46,17 @@ export class CloudWorkspaceAccessTokenError extends Error {
}
}

export class CloudWorkspaceAccessTokenEmptyError extends Error {
constructor() {
super('Workspace access token is empty');
}
}

export async function getWorkspaceAccessTokenOrThrow(forceNew = false, scope = '', save = true): Promise<string> {
const token = await getWorkspaceAccessToken(forceNew, scope, save);
const token = await getWorkspaceAccessToken(forceNew, scope, save, true);

if (!token) {
throw new CloudWorkspaceAccessTokenError();
throw new CloudWorkspaceAccessTokenEmptyError();
}

return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { SystemLogger } from '../../../../server/lib/logger/system';
import { settings } from '../../../settings/server';
import { workspaceScopes } from '../oauthScopes';
import { getRedirectUri } from './getRedirectUri';
import { CloudWorkspaceAccessTokenError } from './getWorkspaceAccessToken';
import { removeWorkspaceRegistrationInfo } from './removeWorkspaceRegistrationInfo';
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';

export async function getWorkspaceAccessTokenWithScope(scope = '') {
export async function getWorkspaceAccessTokenWithScope(scope = '', throwOnError = false) {
const { workspaceRegistered } = await retrieveRegistrationStatus();

const tokenResponse = { token: '', expiresAt: new Date() };
Expand Down Expand Up @@ -58,6 +59,9 @@ export async function getWorkspaceAccessTokenWithScope(scope = '') {
if (err.response?.data?.error === 'oauth_invalid_client_credentials') {
SystemLogger.error('Server has been unregistered from cloud');
void removeWorkspaceRegistrationInfo();
if (throwOnError) {
throw new CloudWorkspaceAccessTokenError();
}
}

return tokenResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export async function removeWorkspaceRegistrationInfo() {
Settings.updateValueById('Cloud_Workspace_Registration_Client_Uri', null),
]);

await Settings.updateValueById('Show_Setup_Wizard', 'in_progress');
return true;
}

0 comments on commit 26b3638

Please sign in to comment.