Skip to content

Commit

Permalink
handle V3 and V2
Browse files Browse the repository at this point in the history
  • Loading branch information
lmauromb authored and ggazzo committed Sep 22, 2023
1 parent b5e1a43 commit ac3f4bc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions ee/packages/license/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"dependencies": {
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/jwt": "workspace:^",
"@rocket.chat/logger": "workspace:^",
"@rocket.chat/models": "workspace:^"
}
Expand Down
15 changes: 15 additions & 0 deletions ee/packages/license/src/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import crypto from 'crypto';

import { verify } from '@rocket.chat/jwt';

const publicKey =
'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQ0lqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FnOEFNSUlDQ2dLQ0FnRUFxV1Nza2Q5LzZ6Ung4a3lQY2ljcwpiMzJ3Mnd4VnV3N3lCVDk2clEvOEQreU1lQ01POXdTU3BIYS85bkZ5d293RXRpZ3B0L3dyb1BOK1ZHU3didHdQCkZYQmVxRWxCbmRHRkFsODZlNStFbGlIOEt6L2hHbkNtSk5tWHB4RUsyUkUwM1g0SXhzWVg3RERCN010eC9pcXMKY2pCL091dlNCa2ppU2xlUzdibE5JVC9kQTdLNC9DSjNvaXUwMmJMNEV4Y2xDSGVwenFOTWVQM3dVWmdweE9uZgpOT3VkOElYWUs3M3pTY3VFOEUxNTdZd3B6Q0twVmFIWDdaSmY4UXVOc09PNVcvYUlqS2wzTDYyNjkrZUlPRXJHCndPTm1hSG56Zmc5RkxwSmh6Z3BPMzhhVm43NnZENUtLakJhaldza1krNGEyZ1NRbUtOZUZxYXFPb3p5RUZNMGUKY0ZXWlZWWjNMZWg0dkVNb1lWUHlJeng5Nng4ZjIveW1QbmhJdXZRdjV3TjRmeWVwYTdFWTVVQ2NwNzF6OGtmUAo0RmNVelBBMElEV3lNaWhYUi9HNlhnUVFaNEdiL3FCQmh2cnZpSkNGemZZRGNKZ0w3RmVnRllIUDNQR0wwN1FnCnZMZXZNSytpUVpQcnhyYnh5U3FkUE9rZ3VyS2pWclhUVXI0QTlUZ2lMeUlYNVVsSnEzRS9SVjdtZk9xWm5MVGEKU0NWWEhCaHVQbG5DR1pSMDFUb1RDZktoTUcxdTBDRm5MMisxNWhDOWZxT21XdjlRa2U0M3FsSjBQZ0YzVkovWAp1eC9tVHBuazlnbmJHOUpIK21mSDM5Um9GdlROaW5Zd1NNdll6dXRWT242OXNPemR3aERsYTkwbDNBQ2g0eENWCks3Sk9YK3VIa29OdTNnMmlWeGlaVU0wQ0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo=';

// #TODO: use async/await
export default function decrypt(encrypted: string): string {
// handle V3
if (encrypted.startsWith('RCV3_')) {
let decrypted = '';
const jwt = encrypted.substring(5);

verify(jwt, publicKey).then(([payload, _header]) => {
decrypted = JSON.stringify(payload);
});

return decrypted;
}

const decrypted = crypto.publicDecrypt(Buffer.from(publicKey, 'base64').toString('utf-8'), Buffer.from(encrypted, 'base64'));

return decrypted.toString('utf-8');
Expand Down
5 changes: 3 additions & 2 deletions ee/packages/license/src/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export const setLicense = async (encryptedLicense: string, forceSet = false): Pr
logger.debug({ msg: 'license', decrypted });
}

// #TODO: Check license version and call setLicenseV2 or setLicenseV3
await setLicenseV2(JSON.parse(decrypted), encryptedLicense);
encryptedLicense.startsWith('RCV3_')
? await setLicenseV3(JSON.parse(decrypted), encryptedLicense)
: await setLicenseV2(JSON.parse(decrypted), encryptedLicense);

return true;
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions packages/jwt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.base.server.json",
"compilerOptions": {
"declaration": true,
"rootDir": "./src",
"outDir": "./dist"
},
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8395,6 +8395,7 @@ __metadata:
resolution: "@rocket.chat/license@workspace:ee/packages/license"
dependencies:
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/jwt": "workspace:^"
"@rocket.chat/logger": "workspace:^"
"@rocket.chat/models": "workspace:^"
"@types/jest": ~29.5.3
Expand Down

0 comments on commit ac3f4bc

Please sign in to comment.