From 83ce5710ae08613d5a817f7efb60afd426a54830 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Wed, 14 Aug 2024 14:23:00 +0200 Subject: [PATCH] electron auth proxy WIP --- packages/api/src/controllers/auth.js | 3 ++- packages/api/src/controllers/connections.js | 21 ++++++++++++++++--- packages/web/src/utility/api.ts | 18 +++++++++++----- .../dbgate-plugin-mssql/src/backend/driver.js | 1 - 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/api/src/controllers/auth.js b/packages/api/src/controllers/auth.js index c7845a9f..b87b8097 100644 --- a/packages/api/src/controllers/auth.js +++ b/packages/api/src/controllers/auth.js @@ -37,7 +37,8 @@ function authMiddleware(req, res, next) { '/stream', 'storage/get-connections-for-login-page', 'auth/get-providers', - '/connections/dblogin', + '/connections/dblogin-web', + '/connections/dblogin-app', '/connections/dblogin-auth', '/connections/dblogin-auth-token', ]; diff --git a/packages/api/src/controllers/connections.js b/packages/api/src/controllers/connections.js index 1dd0d6f5..75d13cf9 100644 --- a/packages/api/src/controllers/connections.js +++ b/packages/api/src/controllers/connections.js @@ -386,18 +386,33 @@ module.exports = { return res; }, - dblogin_meta: { + dbloginWeb_meta: { raw: true, method: 'get', }, - async dblogin(req, res) { + async dbloginWeb(req, res) { const { conid, state, redirectUri } = req.query; const connection = await this.getCore({ conid }); const driver = requireEngineDriver(connection); - const authUrl = await driver.getRedirectAuthUrl(connection, { redirectUri, state }); + const authUrl = await driver.getRedirectAuthUrl(connection, { + redirectUri, + state, + client: 'web', + }); res.redirect(authUrl); }, + dbloginApp_meta: true, + async dbloginApp({ conid, state }) { + const connection = await this.getCore({ conid }); + const driver = requireEngineDriver(connection); + const url = await driver.getRedirectAuthUrl(connection, { + state, + client: 'app', + }); + return { url }; + }, + dbloginToken_meta: true, async dbloginToken({ code, conid, strmid, redirectUri, sid }) { try { diff --git a/packages/web/src/utility/api.ts b/packages/web/src/utility/api.ts index 0e2ed5b8..e7fc527d 100644 --- a/packages/web/src/utility/api.ts +++ b/packages/web/src/utility/api.ts @@ -97,11 +97,19 @@ async function processApiResponse(route, args, resp) { const state = `dbg-dblogin:${strmid}:${resp.detail.conid}`; localStorage.setItem('dbloginState', state); - openWebLink( - `connections/dblogin?conid=${resp.detail.conid}&state=${encodeURIComponent(state)}&redirectUri=${ - location.origin + location.pathname - }` - ); + if (getElectron()) { + const dbloginApp = await apiCall('connections/dblogin-app', { + conid: resp.detail.conid, + state, + }); + openWebLink(dbloginApp.url); + } else { + openWebLink( + `connections/dblogin-web?conid=${resp.detail.conid}&state=${encodeURIComponent(state)}&redirectUri=${ + location.origin + location.pathname + }` + ); + } } else if (!isDatabaseLoginVisible()) { showModal(DatabaseLoginModal, resp.detail); } diff --git a/plugins/dbgate-plugin-mssql/src/backend/driver.js b/plugins/dbgate-plugin-mssql/src/backend/driver.js index 3b8fdfe9..fc385ad2 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/driver.js +++ b/plugins/dbgate-plugin-mssql/src/backend/driver.js @@ -141,7 +141,6 @@ const driver = { if (connection.authType != 'msentra') return null; return authProxy.authProxyGetRedirectUrl({ ...options, - client: platformInfo.isElectron ? 'app' : 'web', type: 'msentra', }); },