mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 10:13:57 +00:00
auth db login workflow
This commit is contained in:
@@ -26,6 +26,10 @@ class AuthProviderBase {
|
|||||||
return login;
|
return login;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isUserLoggedIn(req) {
|
||||||
|
return !!req.user || !!req.auth;
|
||||||
|
}
|
||||||
|
|
||||||
getCurrentPermissions(req) {
|
getCurrentPermissions(req) {
|
||||||
const login = this.getCurrentLogin(req);
|
const login = this.getCurrentLogin(req);
|
||||||
const permissions = process.env[`LOGIN_PERMISSIONS_${login}`];
|
const permissions = process.env[`LOGIN_PERMISSIONS_${login}`];
|
||||||
|
|||||||
@@ -23,12 +23,14 @@ function unauthorizedResponse(req, res, text) {
|
|||||||
function authMiddleware(req, res, next) {
|
function authMiddleware(req, res, next) {
|
||||||
const SKIP_AUTH_PATHS = [
|
const SKIP_AUTH_PATHS = [
|
||||||
'/config/get',
|
'/config/get',
|
||||||
|
'/config/get-settings',
|
||||||
'/auth/oauth-token',
|
'/auth/oauth-token',
|
||||||
'/auth/login',
|
'/auth/login',
|
||||||
'/stream',
|
'/stream',
|
||||||
'storage/get-connections-for-login-page',
|
'storage/get-connections-for-login-page',
|
||||||
'/connections/dblogin',
|
'/connections/dblogin',
|
||||||
'/connections/dblogin-auth',
|
'/connections/dblogin-auth',
|
||||||
|
'/connections/dblogin-auth-token',
|
||||||
];
|
];
|
||||||
|
|
||||||
// console.log('********************* getAuthProvider()', getAuthProvider());
|
// console.log('********************* getAuthProvider()', getAuthProvider());
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ module.exports = {
|
|||||||
const permissions = authProvider.getCurrentPermissions(req);
|
const permissions = authProvider.getCurrentPermissions(req);
|
||||||
const isLoginForm = authProvider.isLoginForm();
|
const isLoginForm = authProvider.isLoginForm();
|
||||||
const additionalConfigProps = authProvider.getAdditionalConfigProps();
|
const additionalConfigProps = authProvider.getAdditionalConfigProps();
|
||||||
|
const isUserLoggedIn = authProvider.isUserLoggedIn(req);
|
||||||
|
|
||||||
const singleConid = authProvider.getSingleConnectionId(req);
|
const singleConid = authProvider.getSingleConnectionId(req);
|
||||||
|
|
||||||
@@ -44,6 +45,7 @@ module.exports = {
|
|||||||
runAsPortal: !!connections.portalConnections,
|
runAsPortal: !!connections.portalConnections,
|
||||||
singleDbConnection: connections.singleDbConnection,
|
singleDbConnection: connections.singleDbConnection,
|
||||||
singleConnection: singleConnection,
|
singleConnection: singleConnection,
|
||||||
|
isUserLoggedIn,
|
||||||
// hideAppEditor: !!process.env.HIDE_APP_EDITOR,
|
// hideAppEditor: !!process.env.HIDE_APP_EDITOR,
|
||||||
allowShellConnection: platformInfo.allowShellConnection,
|
allowShellConnection: platformInfo.allowShellConnection,
|
||||||
allowShellScripting: platformInfo.allowShellScripting,
|
allowShellScripting: platformInfo.allowShellScripting,
|
||||||
|
|||||||
@@ -412,6 +412,22 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
dbloginAuthToken_meta: true,
|
||||||
|
async dbloginAuthToken({ code, conid, redirectUri }) {
|
||||||
|
try {
|
||||||
|
const connection = await this.getCore({ conid });
|
||||||
|
const driver = requireEngineDriver(connection);
|
||||||
|
const accessToken = await driver.getAuthTokenFromCode(connection, { code, redirectUri });
|
||||||
|
const volatile = await this.saveVolatile({ conid, accessToken });
|
||||||
|
const authProvider = getAuthProvider();
|
||||||
|
const resp = await authProvider.login(null, null, { conid: volatile._id });
|
||||||
|
return resp;
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ err }, 'Error getting DB token');
|
||||||
|
return { error: err.message };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
dbloginAuth_meta: true,
|
dbloginAuth_meta: true,
|
||||||
async dbloginAuth({ conid, user, password }) {
|
async dbloginAuth({ conid, user, password }) {
|
||||||
if (user || password) {
|
if (user || password) {
|
||||||
|
|||||||
@@ -99,17 +99,17 @@
|
|||||||
value="Open database login page"
|
value="Open database login page"
|
||||||
on:click={async e => {
|
on:click={async e => {
|
||||||
const state = `dbg-dblogin:${strmid}:${selectedConnection?.conid}`;
|
const state = `dbg-dblogin:${strmid}:${selectedConnection?.conid}`;
|
||||||
localStorage.setItem('dbloginState', state);
|
sessionStorage.setItem('dbloginAuthState', state);
|
||||||
openWebLink(
|
// openWebLink(
|
||||||
`connections/dblogin?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
|
|
||||||
location.origin + location.pathname
|
|
||||||
}`
|
|
||||||
);
|
|
||||||
// internalRedirectTo(
|
|
||||||
// `connections/dblogin?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
|
// `connections/dblogin?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
|
||||||
// location.origin + location.pathname
|
// location.origin + location.pathname
|
||||||
// }`
|
// }`
|
||||||
// );
|
// );
|
||||||
|
internalRedirectTo(
|
||||||
|
`connections/dblogin?conid=${selectedConnection?.conid}&state=${encodeURIComponent(state)}&redirectUri=${
|
||||||
|
location.origin + location.pathname
|
||||||
|
}`
|
||||||
|
);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
{:else if selectedConnection}
|
{:else if selectedConnection}
|
||||||
|
|||||||
@@ -22,6 +22,19 @@ export function isDbLoginCallback() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isDbLoginAuthCallback() {
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
const sentCode = params.get('code');
|
||||||
|
const sentState = params.get('state');
|
||||||
|
|
||||||
|
return (
|
||||||
|
sentCode &&
|
||||||
|
sentState &&
|
||||||
|
sentState.startsWith('dbg-dblogin:') &&
|
||||||
|
sentState == sessionStorage.getItem('dbloginAuthState')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function handleOauthCallback() {
|
export function handleOauthCallback() {
|
||||||
const params = new URLSearchParams(location.search);
|
const params = new URLSearchParams(location.search);
|
||||||
const sentCode = params.get('code');
|
const sentCode = params.get('code');
|
||||||
@@ -37,7 +50,7 @@ export function handleOauthCallback() {
|
|||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
console.log('Settings access token from OAUTH');
|
console.log('Settings access token from OAUTH');
|
||||||
localStorage.setItem('accessToken', accessToken);
|
localStorage.setItem('accessToken', accessToken);
|
||||||
internalRedirectTo('/');
|
internalRedirectTo('?');
|
||||||
} else {
|
} else {
|
||||||
console.log('Error when processing OAUTH callback', error || errorMessage);
|
console.log('Error when processing OAUTH callback', error || errorMessage);
|
||||||
internalRedirectTo(`?page=not-logged&error=${error || errorMessage}`);
|
internalRedirectTo(`?page=not-logged&error=${error || errorMessage}`);
|
||||||
@@ -60,7 +73,29 @@ export function handleOauthCallback() {
|
|||||||
if (authResp.success) {
|
if (authResp.success) {
|
||||||
window.close();
|
window.close();
|
||||||
} else if (authResp.error) {
|
} else if (authResp.error) {
|
||||||
internalRedirectTo(`?page=error&error=${encodeURIComponent(authResp)}`);
|
internalRedirectTo(`?page=error&error=${encodeURIComponent(authResp.error)}`);
|
||||||
|
} else {
|
||||||
|
internalRedirectTo(`?page=error`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isDbLoginAuthCallback()) {
|
||||||
|
const [_prefix, strmid, conid] = sessionStorage.getItem('dbloginAuthState').split(':');
|
||||||
|
sessionStorage.removeItem('dbloginAuthState');
|
||||||
|
|
||||||
|
apiCall('connections/dblogin-auth-token', {
|
||||||
|
code: sentCode,
|
||||||
|
conid,
|
||||||
|
redirectUri: location.origin + location.pathname,
|
||||||
|
}).then(authResp => {
|
||||||
|
if (authResp.accessToken) {
|
||||||
|
localStorage.setItem('accessToken', authResp.accessToken);
|
||||||
|
internalRedirectTo('?');
|
||||||
|
} else if (authResp.error) {
|
||||||
|
internalRedirectTo(`?page=error&error=${encodeURIComponent(authResp.error)}`);
|
||||||
} else {
|
} else {
|
||||||
internalRedirectTo(`?page=error`);
|
internalRedirectTo(`?page=error`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -551,7 +551,7 @@ registerCommand({
|
|||||||
id: 'app.logout',
|
id: 'app.logout',
|
||||||
category: 'App',
|
category: 'App',
|
||||||
name: 'Logout',
|
name: 'Logout',
|
||||||
testEnabled: () => getCurrentConfig()?.login != null,
|
testEnabled: () => getCurrentConfig()?.isUserLoggedIn,
|
||||||
onClick: doLogout,
|
onClick: doLogout,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -559,7 +559,7 @@ registerCommand({
|
|||||||
id: 'app.disconnect',
|
id: 'app.disconnect',
|
||||||
category: 'App',
|
category: 'App',
|
||||||
name: 'Disconnect',
|
name: 'Disconnect',
|
||||||
testEnabled: () => getCurrentConfig()?.singleConnection != null,
|
testEnabled: () => getCurrentConfig()?.singleConnection != null && !getCurrentConfig()?.isUserLoggedIn,
|
||||||
onClick: () => disconnectServerConnection(getCurrentConfig()?.singleConnection?._id),
|
onClick: () => disconnectServerConnection(getCurrentConfig()?.singleConnection?._id),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -873,7 +873,6 @@ registerCommand({
|
|||||||
onClick: () => showModal(UploadErrorModal),
|
onClick: () => showModal(UploadErrorModal),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const electron = getElectron();
|
const electron = getElectron();
|
||||||
if (electron) {
|
if (electron) {
|
||||||
electron.addEventListener('run-command', (e, commandId) => runCommand(commandId));
|
electron.addEventListener('run-command', (e, commandId) => runCommand(commandId));
|
||||||
|
|||||||
Reference in New Issue
Block a user