From 3cc2abf8b99f8c67e58f7375bee680e693305527 Mon Sep 17 00:00:00 2001 From: "SPRINX0\\prochazka" Date: Mon, 14 Jul 2025 12:21:29 +0200 Subject: [PATCH] SYNC: better handling of expired license in electron app --- packages/api/src/controllers/config.js | 45 ++++++++++++------- .../web/src/settings/SettingsModal.svelte | 22 ++++++++- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/packages/api/src/controllers/config.js b/packages/api/src/controllers/config.js index 6c92b164a..a9862ffeb 100644 --- a/packages/api/src/controllers/config.js +++ b/packages/api/src/controllers/config.js @@ -16,7 +16,7 @@ const connections = require('../controllers/connections'); const { getAuthProviderFromReq } = require('../auth/authProvider'); const { checkLicense, checkLicenseKey } = require('../utility/checkLicense'); const storage = require('./storage'); -const { getAuthProxyUrl } = require('../utility/authProxy'); +const { getAuthProxyUrl, tryToGetRefreshedLicense } = require('../utility/authProxy'); const { getPublicHardwareFingerprint } = require('../utility/hardwareFingerprint'); const { extractErrorMessage } = require('dbgate-tools'); const { @@ -191,6 +191,7 @@ module.exports = { return { ...this.fillMissingSettings(JSON.parse(settingsText)), 'other.licenseKey': platformInfo.isElectron ? await this.loadLicenseKey() : undefined, + // 'other.licenseKey': await this.loadLicenseKey(), }; } } catch (err) { @@ -208,21 +209,23 @@ module.exports = { }, saveLicenseKey_meta: true, - async saveLicenseKey({ licenseKey }) { - const decoded = jwt.decode(licenseKey?.trim()); - if (!decoded) { - return { - status: 'error', - errorMessage: 'Invalid license key', - }; - } + async saveLicenseKey({ licenseKey, forceSave = false }) { + if (!forceSave) { + const decoded = jwt.decode(licenseKey?.trim()); + if (!decoded) { + return { + status: 'error', + errorMessage: 'Invalid license key', + }; + } - const { exp } = decoded; - if (exp * 1000 < Date.now()) { - return { - status: 'error', - errorMessage: 'License key is expired', - }; + const { exp } = decoded; + if (exp * 1000 < Date.now()) { + return { + status: 'error', + errorMessage: 'License key is expired', + }; + } } try { @@ -297,7 +300,7 @@ module.exports = { // this.settingsValue = updated; if (currentValue['other.licenseKey'] != values['other.licenseKey']) { - await this.saveLicenseKey({ licenseKey: values['other.licenseKey'] }); + await this.saveLicenseKey({ licenseKey: values['other.licenseKey'], forceSave: true }); socket.emitChanged(`config-changed`); } } @@ -327,6 +330,16 @@ module.exports = { return resp; }, + getNewLicense_meta: true, + async getNewLicense({ oldLicenseKey }) { + const newLicenseKey = await tryToGetRefreshedLicense(oldLicenseKey); + const res = await checkLicenseKey(newLicenseKey.token); + if (res.status == 'ok') { + res.licenseKey = newLicenseKey.token; + } + return res; + }, + recryptDatabaseForExport(db) { const encryptionKey = generateTransportEncryptionKey(); const transportEncryptor = createTransportEncryptor(encryptionKey); diff --git a/packages/web/src/settings/SettingsModal.svelte b/packages/web/src/settings/SettingsModal.svelte index 73ce8aa66..f60166f32 100644 --- a/packages/web/src/settings/SettingsModal.svelte +++ b/packages/web/src/settings/SettingsModal.svelte @@ -528,7 +528,27 @@ ORDER BY
License key expiration: {safeFormatDate(licenseKeyCheckResult.expiration)}
{/if} {:else if licenseKeyCheckResult.status == 'error'} - License key is invalid +
+ + {licenseKeyCheckResult.errorMessage ?? 'License key is invalid'} + {#if licenseKeyCheckResult.expiration} +
License key expiration: {safeFormatDate(licenseKeyCheckResult.expiration)}
+ {/if} +
+ {#if licenseKeyCheckResult.isExpired} +
+ { + licenseKeyCheckResult = await apiCall('config/get-new-license', { oldLicenseKey: licenseKey }); + if (licenseKeyCheckResult.licenseKey) { + apiCall('config/update-settings', { 'other.licenseKey': licenseKeyCheckResult.licenseKey }); + } + }} + /> +
+ {/if} {/if} {/if}