SYNC: dbgate cloud redirect workflow

This commit is contained in:
SPRINX0\prochazka
2025-06-24 09:22:16 +02:00
committed by Diflow
parent bd88b8411e
commit eba16cc15d
9 changed files with 64 additions and 13 deletions

View File

@@ -514,8 +514,8 @@ describe('Data browser data', () => {
cy.themeshot('query-result-chart');
});
// it.only('Private cloud', () => {
// cy.testid('WidgetIconPanel_cloudAccount').click();
// cy.contains('Sign in with Google').click();
// });
it.only('Private cloud', () => {
cy.testid('WidgetIconPanel_cloudAccount').click();
cy.contains('Sign in with Google').click();
});
});

View File

@@ -1,5 +1,6 @@
CONNECTIONS=mysql,postgres,mongo,redis
ALLOW_DBGATE_PRIVATE_CLOUD=1
REDIRECT_TO_DBGATE_CLOUD_LOGIN=1
LABEL_mysql=MySql-connection
SERVER_mysql=localhost

View File

@@ -2,9 +2,10 @@ DEVMODE=1
SHELL_SCRIPTING=1
ALLOW_DBGATE_PRIVATE_CLOUD=1
DEVWEB=1
# REDIRECT_TO_DBGATE_CLOUD_LOGIN=1
# PROD_DBGATE_CLOUD=1
# PROD_DBGATE_IDENTITY=1
LOCAL_DBGATE_CLOUD=1
# LOCAL_DBGATE_CLOUD=1
# LOCAL_DBGATE_IDENTITY=1
# CLOUD_UPGRADE_FILE=c:\test\upg\upgrade.zip

View File

@@ -13,7 +13,7 @@ const {
} = require('../auth/authProvider');
const storage = require('./storage');
const { decryptPasswordString } = require('../utility/crypting');
const { createDbGateIdentitySession, startCloudTokenChecking } = require('../utility/cloudIntf');
const { createDbGateIdentitySession, startCloudTokenChecking, readCloudTokenHolder } = require('../utility/cloudIntf');
const socket = require('../utility/socket');
const logger = getLogger('auth');
@@ -138,8 +138,8 @@ module.exports = {
},
createCloudLoginSession_meta: true,
async createCloudLoginSession({ client }) {
const res = await createDbGateIdentitySession(client);
async createCloudLoginSession({ client, redirectUri }) {
const res = await createDbGateIdentitySession(client, redirectUri);
startCloudTokenChecking(res.sid, tokenHolder => {
socket.emit('got-cloud-token', tokenHolder);
socket.emitChanged('cloud-content-changed');
@@ -148,5 +148,11 @@ module.exports = {
return res;
},
cloudLoginRedirected_meta: true,
async cloudLoginRedirected({ sid }) {
const tokenHolder = await readCloudTokenHolder(sid);
return tokenHolder;
},
authMiddleware,
};

View File

@@ -118,6 +118,7 @@ module.exports = {
supportCloudAutoUpgrade: !!process.env.CLOUD_UPGRADE_FILE,
allowPrivateCloud: platformInfo.isElectron || !!process.env.ALLOW_DBGATE_PRIVATE_CLOUD,
...currentVersion,
redirectToDbGateCloudLogin: !!process.env.REDIRECT_TO_DBGATE_CLOUD_LOGIN,
};
return configResult;

View File

@@ -34,11 +34,12 @@ const DBGATE_CLOUD_URL = process.env.LOCAL_DBGATE_CLOUD
? 'https://cloud.dbgate.udolni.net'
: 'https://cloud.dbgate.io';
async function createDbGateIdentitySession(client) {
async function createDbGateIdentitySession(client, redirectUri) {
const resp = await axios.default.post(
`${DBGATE_IDENTITY_URL}/api/create-session`,
{
client,
redirectUri,
},
{
headers: {
@@ -70,7 +71,7 @@ function startCloudTokenChecking(sid, callback) {
});
// console.log('CHECK RESP:', resp.data);
if (resp.data.email) {
if (resp.data?.email) {
clearInterval(interval);
callback(resp.data);
}
@@ -80,6 +81,18 @@ function startCloudTokenChecking(sid, callback) {
}, 500);
}
async function readCloudTokenHolder(sid) {
const resp = await axios.default.get(`${DBGATE_IDENTITY_URL}/api/get-token/${sid}`, {
headers: {
...getLicenseHttpHeaders(),
},
});
if (resp.data?.email) {
return resp.data;
}
return null;
}
async function loadCloudFiles() {
try {
const fileContent = await fs.readFile(path.join(datadir(), 'cloud-files.jsonl'), 'utf-8');
@@ -396,4 +409,5 @@ module.exports = {
loadCachedCloudConnection,
putCloudContent,
removeCloudCachedConnection,
readCloudTokenHolder,
};

View File

@@ -3,6 +3,7 @@ import { getConfig } from './utility/metadataLoaders';
import { isAdminPage } from './utility/pageDefs';
import getElectron from './utility/getElectron';
import { isProApp } from './utility/proTools';
import { cloudSigninTokenHolder, selectedWidget } from './stores';
export function isOauthCallback() {
const params = new URLSearchParams(location.search);
@@ -114,6 +115,12 @@ export function handleOauthCallback() {
return true;
}
const cloudSid = params.get('dbgate-cloud-sid');
if (cloudSid) {
sessionStorage.setItem('dbgate-cloud-sid', cloudSid);
internalRedirectTo(`/`);
}
return false;
}
@@ -187,6 +194,18 @@ export async function handleAuthOnStartup(config) {
}
}
async function checkDbGateCloudLogin() {
const sid = sessionStorage.getItem('dbgate-cloud-sid');
if (sid) {
const tokenHolder = await apiCall('auth/cloud-login-redirected', { sid });
if (tokenHolder) {
sessionStorage.removeItem('dbgate-cloud-sid');
cloudSigninTokenHolder.set(tokenHolder);
selectedWidget.set('cloud-private');
}
}
}
if (page == 'error') return;
if (checkConfigError()) return;
@@ -199,6 +218,7 @@ export async function handleAuthOnStartup(config) {
if (page == 'license' || page == 'admin-license') return;
if (checkTrialDaysLeft()) return;
if (checkInvalidLicense()) return;
checkDbGateCloudLogin();
// if (config.configurationError) {
// internalRedirectTo(`/error.html`);

View File

@@ -289,8 +289,8 @@ export function installNewVolatileConnectionListener() {
}
export function installNewCloudTokenListener() {
// console.log('HOLDER', tokenHolder);
apiOn('got-cloud-token', async tokenHolder => {
// console.log('HOLDER', tokenHolder);
cloudSigninTokenHolder.set(tokenHolder);
selectedWidget.set('cloud-private');
});

View File

@@ -135,8 +135,16 @@
}
async function handleOpenCloudLogin() {
const { url, sid } = await apiCall('auth/create-cloud-login-session', { client: getElectron() ? 'app' : 'web' });
openWebLink(url, true);
const useRedirect = getCurrentConfig()?.redirectToDbGateCloudLogin;
const { url, sid } = await apiCall('auth/create-cloud-login-session', {
client: getElectron() ? 'app' : 'web',
redirectUri: useRedirect ? location.origin + location.pathname : undefined,
});
if (useRedirect) {
location.href = url;
} else {
openWebLink(url, true);
}
}
</script>