Skip to content

Commit

Permalink
chore: using a string instead of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustrb committed Oct 21, 2024
1 parent 0db731e commit 7d7c08e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export async function getWorkspaceAccessToken(forceNew = false, scope = '', save

// Note: If no scope is given, it means we should assume the default scope, we store the default scopes
// in the global variable workspaceScopes.
const scopes = scope === '' ? [...workspaceScopes] : [scope];
const scopes = scope === '' ? workspaceScopes.join(' ') : scope;

const workspaceCredentials = await WorkspaceCredentials.getCredentialByScopes(scopes);
const workspaceCredentials = await WorkspaceCredentials.getCredentialByScope(scopes);
if (workspaceCredentials && !hasWorkspaceAccessTokenExpired(workspaceCredentials) && !forceNew) {
return workspaceCredentials.accessToken;
}

const accessToken = await getWorkspaceAccessTokenWithScope(scope, throwOnError);

if (save) {
await WorkspaceCredentials.updateCredentialByScopes({
scopes: accessToken.scopes,
await WorkspaceCredentials.updateCredentialByScope({
scope: accessToken.scope,
accessToken: accessToken.token,
expirationDate: accessToken.expiresAt,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
type WorkspaceAccessTokenWithScope = {
token: string;
expiresAt: Date;
scopes: string[];
scope: string;
};

export async function getWorkspaceAccessTokenWithScope(scope = '', throwOnError = false): Promise<WorkspaceAccessTokenWithScope> {
const { workspaceRegistered } = await retrieveRegistrationStatus();

const tokenResponse = { token: '', expiresAt: new Date(), scopes: [] };
const tokenResponse = { token: '', expiresAt: new Date(), scope: '' };

if (!workspaceRegistered) {
return tokenResponse;
Expand All @@ -29,7 +29,9 @@ export async function getWorkspaceAccessTokenWithScope(scope = '', throwOnError
return tokenResponse;
}

const scopes = scope === '' ? workspaceScopes.join(' ') : scope;
if (scope === '') {
scope = workspaceScopes.join(' ');
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const client_secret = settings.get<string>('Cloud_Workspace_Client_Secret');
Expand All @@ -40,7 +42,7 @@ export async function getWorkspaceAccessTokenWithScope(scope = '', throwOnError
const body = new URLSearchParams();
body.append('client_id', client_id);
body.append('client_secret', client_secret);
body.append('scope', scopes);
body.append('scope', scope);
body.append('grant_type', 'client_credentials');
body.append('redirect_uri', redirectUri);

Expand All @@ -66,7 +68,7 @@ export async function getWorkspaceAccessTokenWithScope(scope = '', throwOnError
return {
token: payload.access_token,
expiresAt,
scopes: scope === '' ? [...workspaceScopes] : [scope],
scope: payload.scope,
};
} catch (err: any) {
if (err instanceof CloudWorkspaceAccessTokenError) {
Expand Down
24 changes: 8 additions & 16 deletions apps/meteor/server/models/raw/WorkspaceCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,33 @@ export class WorkspaceCredentialsRaw extends BaseRaw<IWorkspaceCredentials> impl
}

protected modelIndexes(): IndexDescription[] {
return [{ key: { scopes: 1, expirationDate: 1, accessToken: 1 }, unique: true }];
return [{ key: { scope: 1, expirationDate: 1, accessToken: 1 }, unique: true }];
}

getCredentialByScopes(scopes: string[] = []): Promise<IWorkspaceCredentials | null> {
const query: Filter<IWorkspaceCredentials> = {
scopes: {
$eq: scopes,
},
};
getCredentialByScope(scope = ''): Promise<IWorkspaceCredentials | null> {
const query: Filter<IWorkspaceCredentials> = { scope };

return this.findOne(query);
}

updateCredentialByScopes({
scopes,
updateCredentialByScope({
scope,
accessToken,
expirationDate,
}: {
scopes: string[];
scope: string;
accessToken: string;
expirationDate: Date;
}): Promise<UpdateResult> {
const record = {
$set: {
scopes,
scope,
accessToken,
expirationDate,
},
};

const query: Filter<IWorkspaceCredentials> = {
scopes: {
$eq: scopes,
},
};
const query: Filter<IWorkspaceCredentials> = { scope };

return this.updateOne(query, record, { upsert: true });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-typings/src/ee/IWorkspaceCredentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { IRocketChatRecord } from '../IRocketChatRecord';

export interface IWorkspaceCredentials extends IRocketChatRecord {
_id: string;
scopes: string[];
scope: string;
expirationDate: Date;
accessToken: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { DeleteResult, UpdateResult } from 'mongodb';
import type { IBaseModel } from './IBaseModel';

export interface IWorkspaceCredentialsModel extends IBaseModel<IWorkspaceCredentials> {
getCredentialByScopes(scopes?: string[]): Promise<IWorkspaceCredentials | null>;
updateCredentialByScopes(credentials: { scopes: string[]; accessToken: string; expirationDate: Date }): Promise<UpdateResult>;
getCredentialByScope(scope?: string): Promise<IWorkspaceCredentials | null>;
updateCredentialByScope(credentials: { scope: string; accessToken: string; expirationDate: Date }): Promise<UpdateResult>;
removeAllCredentials(): Promise<DeleteResult>;
}

0 comments on commit 7d7c08e

Please sign in to comment.