mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 10:46:00 +00:00
basic cloud signin workflow
This commit is contained in:
@@ -13,7 +13,8 @@ const {
|
||||
} = require('../auth/authProvider');
|
||||
const storage = require('./storage');
|
||||
const { decryptPasswordString } = require('../utility/crypting');
|
||||
const { createDbGateIdentitySession, getIdentitySigninUrl } = require('../utility/cloudIntf');
|
||||
const { createDbGateIdentitySession, startCloudTokenChecking } = require('../utility/cloudIntf');
|
||||
const socket = require('../utility/socket');
|
||||
|
||||
const logger = getLogger('auth');
|
||||
|
||||
@@ -138,10 +139,11 @@ module.exports = {
|
||||
|
||||
createCloudLoginSession_meta: true,
|
||||
async createCloudLoginSession({ client }) {
|
||||
const sid = await createDbGateIdentitySession(client);
|
||||
return {
|
||||
url: getIdentitySigninUrl(sid),
|
||||
};
|
||||
const res = await createDbGateIdentitySession(client);
|
||||
startCloudTokenChecking(res.sid, token => {
|
||||
socket.emit('got-cloud-token', { token });
|
||||
});
|
||||
return res;
|
||||
},
|
||||
|
||||
authMiddleware,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
const axios = require('axios');
|
||||
const { getExternalParamsWithLicense } = require('./authProxy');
|
||||
const { getLogger, extractErrorLogData } = require('dbgate-tools');
|
||||
|
||||
const logger = getLogger('cloudIntf');
|
||||
|
||||
const DBGATE_IDENTITY_URL = process.env.LOCAL_DBGATE_IDENTITY
|
||||
? 'http://localhost:3001'
|
||||
? 'http://localhost:3103'
|
||||
: process.env.DEVWEB || process.env.DEVMODE
|
||||
? 'https://identity.dbgate.udolni.net'
|
||||
: 'https://identity.dbgate.io';
|
||||
@@ -21,14 +24,37 @@ async function createDbGateIdentitySession(client) {
|
||||
},
|
||||
getExternalParamsWithLicense()
|
||||
);
|
||||
return resp.data.sid;
|
||||
return {
|
||||
sid: resp.data.sid,
|
||||
url: `${DBGATE_IDENTITY_URL}/api/signin/${resp.data.sid}`,
|
||||
};
|
||||
}
|
||||
|
||||
function getIdentitySigninUrl(sid) {
|
||||
return `${DBGATE_IDENTITY_URL}/signin/${sid}`;
|
||||
function startCloudTokenChecking(sid, callback) {
|
||||
const started = Date.now();
|
||||
const interval = setInterval(async () => {
|
||||
if (Date.now() - started > 60 * 1000) {
|
||||
clearInterval(interval);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const resp = await axios.default.get(
|
||||
`${DBGATE_IDENTITY_URL}/api/get-token/${sid}`,
|
||||
getExternalParamsWithLicense()
|
||||
);
|
||||
|
||||
if (resp.data.status == 'ok') {
|
||||
clearInterval(interval);
|
||||
callback(resp.data.token);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(extractErrorLogData(err), 'Error checking cloud token');
|
||||
}
|
||||
}, 500);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createDbGateIdentitySession,
|
||||
getIdentitySigninUrl,
|
||||
startCloudTokenChecking,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user