Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
import { License } from '@rocket.chat/license';
|
|
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
|
|
|
|
import { getRedirectUri } from './getRedirectUri';
|
|
import { CloudWorkspaceAccessTokenError } from './getWorkspaceAccessToken';
|
|
import { workspaceScopes } from './oauthScopes';
|
|
import { removeWorkspaceRegistrationInfo } from './removeWorkspaceRegistrationInfo';
|
|
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
|
|
import { settings } from '../../settings';
|
|
import { SystemLogger } from '../logger/system';
|
|
|
|
type WorkspaceAccessTokenWithScope = {
|
|
token: string;
|
|
expiresAt: Date;
|
|
scope: string;
|
|
};
|
|
|
|
type GetWorkspaceAccessTokenWithScopeParams = {
|
|
scope?: string;
|
|
throwOnError?: boolean;
|
|
};
|
|
|
|
export async function getWorkspaceAccessTokenWithScope({
|
|
scope = '',
|
|
throwOnError = false,
|
|
}: GetWorkspaceAccessTokenWithScopeParams): Promise<WorkspaceAccessTokenWithScope> {
|
|
const { workspaceRegistered } = await retrieveRegistrationStatus();
|
|
|
|
const tokenResponse = { token: '', expiresAt: new Date(), scope: '' };
|
|
|
|
if (!workspaceRegistered) {
|
|
return tokenResponse;
|
|
}
|
|
|
|
if (License.hasOfflineLicense()) {
|
|
return tokenResponse;
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
const client_id = settings.get<string>('Cloud_Workspace_Client_Id');
|
|
if (!client_id) {
|
|
return tokenResponse;
|
|
}
|
|
|
|
if (scope === '') {
|
|
scope = workspaceScopes.join(' ');
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
const client_secret = settings.get<string>('Cloud_Workspace_Client_Secret');
|
|
const redirectUri = getRedirectUri();
|
|
|
|
let payload;
|
|
try {
|
|
const body = new URLSearchParams();
|
|
body.append('client_id', client_id);
|
|
body.append('client_secret', client_secret);
|
|
body.append('scope', scope);
|
|
body.append('grant_type', 'client_credentials');
|
|
body.append('redirect_uri', redirectUri);
|
|
|
|
const cloudUrl = settings.get<string>('Cloud_Url');
|
|
const response = await fetch(`${cloudUrl}/api/oauth/token`, {
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
method: 'POST',
|
|
body,
|
|
// SECURITY: the URL is a default hardcoded value or an envvar/setting set by an admin. It's safe to disable this check.
|
|
ignoreSsrfValidation: true,
|
|
timeout: 5000,
|
|
});
|
|
|
|
payload = await response.json();
|
|
|
|
if (response.status >= 400) {
|
|
if (payload.error === 'oauth_invalid_client_credentials') {
|
|
throw new CloudWorkspaceAccessTokenError();
|
|
}
|
|
}
|
|
|
|
const expiresAt = new Date();
|
|
expiresAt.setSeconds(expiresAt.getSeconds() + payload.expires_in);
|
|
|
|
return {
|
|
token: payload.access_token,
|
|
expiresAt,
|
|
scope: payload.scope,
|
|
};
|
|
} catch (err: any) {
|
|
if (err instanceof CloudWorkspaceAccessTokenError) {
|
|
SystemLogger.error('Server has been unregistered from cloud');
|
|
void removeWorkspaceRegistrationInfo();
|
|
if (throwOnError) {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
return tokenResponse;
|
|
}
|