Skip to content

Commit b63c712

Browse files
committed
Make _getOrCreateRemoteProject able to specify what team the project should belong to
1 parent 4daa44e commit b63c712

File tree

1 file changed

+85
-10
lines changed
  • packages/insomnia-app/app/sync/vcs

1 file changed

+85
-10
lines changed

packages/insomnia-app/app/sync/vcs/index.ts

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,22 @@ export default class VCS {
505505
let project = await this._queryProject();
506506

507507
if (!project) {
508-
project = await this._queryCreateProject(localProject.rootDocumentId, localProject.name);
508+
project = await this._createRemoteProject(localProject.rootDocumentId, localProject.name);
509509
}
510510

511511
await this._storeProject(project);
512512
return project;
513513
}
514514

515+
async _createRemoteProject(workspaceId: string, workspaceName: string, teamId?: string) {
516+
if (teamId) {
517+
var teamKeys = await this._queryTeamMemberKeys(teamId)
518+
return this._queryCreateProject(workspaceId, workspaceName, teamId, teamKeys.memberKeys)
519+
}
520+
521+
return this._queryCreateProject(workspaceId, workspaceName)
522+
}
523+
515524
async push() {
516525
await this._getOrCreateRemoteProject();
517526
const branch = await this._getCurrentBranch();
@@ -1163,32 +1172,98 @@ export default class VCS {
11631172
return project.teams;
11641173
}
11651174

1166-
async _queryCreateProject(workspaceId: string, workspaceName: string) {
1167-
const { publicKey } = this._assertSession();
1175+
async _queryTeamMemberKeys(
1176+
teamId: string,
1177+
): Promise<{
1178+
memberKeys: {
1179+
accountId: string;
1180+
publicKey: string;
1181+
}[];
1182+
}> {
1183+
const { teamMemberKeys } = await this._runGraphQL(
1184+
`
1185+
query ($teamId: ID!) {
1186+
teamMemberKeys(teamId: $teamId) {
1187+
memberKeys {
1188+
accountId
1189+
publicKey
1190+
}
1191+
}
1192+
}
1193+
`,
1194+
{
1195+
teamId: teamId,
1196+
},
1197+
'teamMemberKeys',
1198+
);
1199+
return teamMemberKeys;
1200+
}
11681201

1202+
async _queryCreateProject(
1203+
workspaceId: string,
1204+
workspaceName: string,
1205+
teamId?: string,
1206+
teamPublicKeys?: {
1207+
accountId: string;
1208+
publicKey: string;
1209+
}[],
1210+
) {
11691211
// Generate symmetric key for ResourceGroup
11701212
const symmetricKey = await crypt.generateAES256Key();
11711213
const symmetricKeyStr = JSON.stringify(symmetricKey);
1172-
// Encrypt the symmetric key with Account public key
1173-
const encSymmetricKey = crypt.encryptRSAWithJWK(publicKey, symmetricKeyStr);
1214+
1215+
const teamKeys: any[] = [];
1216+
let encSymmetricKey: string | undefined;
1217+
1218+
if (teamId && teamPublicKeys) {
1219+
// Encrypt the symmetric key with the public keys of all the team members, ourselves included
1220+
for (const { accountId, publicKey } of teamPublicKeys) {
1221+
teamKeys.push({
1222+
accountId,
1223+
encSymmetricKey: crypt.encryptRSAWithJWK(JSON.parse(publicKey), symmetricKeyStr),
1224+
});
1225+
}
1226+
} else {
1227+
const { publicKey } = this._assertSession();
1228+
// Encrypt the symmetric key with the account public key
1229+
encSymmetricKey = crypt.encryptRSAWithJWK(publicKey, symmetricKeyStr);
1230+
}
1231+
11741232
const { projectCreate } = await this._runGraphQL(
11751233
`
1176-
mutation ($rootDocumentId: ID!, $name: String!, $id: ID!, $key: String!) {
1177-
projectCreate(name: $name, id: $id, rootDocumentId: $rootDocumentId, encSymmetricKey: $key) {
1234+
mutation (
1235+
$name: String!,
1236+
$id: ID!,
1237+
$rootDocumentId: ID!,
1238+
$encSymmetricKey: String,
1239+
$teamId: ID,
1240+
$teamKeys: [ProjectCreateKeyInput!],
1241+
) {
1242+
projectCreate(
1243+
name: $name,
1244+
id: $id,
1245+
rootDocumentId: $rootDocumentId,
1246+
encSymmetricKey: $encSymmetricKey,
1247+
teamId: $teamId,
1248+
teamKeys: $teamKeys,
1249+
) {
11781250
id
11791251
name
11801252
rootDocumentId
11811253
}
11821254
}
11831255
`,
11841256
{
1257+
name: workspaceName,
11851258
id: this._projectId(),
11861259
rootDocumentId: workspaceId,
1187-
name: workspaceName,
1188-
key: encSymmetricKey,
1260+
encSymmetricKey: encSymmetricKey,
1261+
teamId: teamId,
1262+
teamKeys: teamKeys,
11891263
},
1190-
'switchAndCreateProjectIfNotExist',
1264+
'createProject',
11911265
);
1266+
11921267
console.log(`[sync] Created remote project ${projectCreate.id} (${projectCreate.name})`);
11931268
return projectCreate as Project;
11941269
}

0 commit comments

Comments
 (0)