2019-06-14 11:05:46 +02:00
|
|
|
const {getApisWithFullAccess, getApisForSession} = require("./budibaseApi");
|
|
|
|
const getDatastore = require("./datastore");
|
|
|
|
const getDatabaseManager = require("./databaseManager");
|
|
|
|
const {$} = require("budibase-core").common;
|
|
|
|
const {keyBy} = require("lodash/fp");
|
|
|
|
const {unauthorized} = require("./exceptions");
|
|
|
|
|
|
|
|
const isMaster = appname => appname === "_master";
|
|
|
|
|
|
|
|
module.exports = async (config) => {
|
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
const datastoreModule = getDatastore(config);
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
const databaseManager = getDatabaseManager(
|
2019-06-14 18:01:01 +02:00
|
|
|
datastoreModule,
|
2019-06-14 11:05:46 +02:00
|
|
|
config.datastoreConfig);
|
|
|
|
|
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
const masterDatastore = datastoreModule.getDatastore(
|
|
|
|
databaseManager.masterDatastoreConfig);
|
|
|
|
|
|
|
|
const bb = await getApisWithFullAccess(masterDatastore);
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
let applications;
|
|
|
|
const loadApplications = async () =>
|
|
|
|
applications = $(await bb.indexApi.listItems("/all_applications"), [
|
|
|
|
keyBy("name")
|
|
|
|
]);
|
|
|
|
await loadApplications();
|
|
|
|
|
2019-06-14 18:01:01 +02:00
|
|
|
const getInstanceDatastore = (instanceDatastoreConfig) =>
|
|
|
|
datastoreModule.getDatastore(instanceDatastoreConfig);
|
|
|
|
|
2019-06-14 11:05:46 +02:00
|
|
|
const getCustomSessionId = (appname, sessionId) =>
|
|
|
|
isMaster(appname)
|
|
|
|
? bb.recordApi.customId("mastersession", sessionId)
|
|
|
|
: bb.recordApi.customId("session", sessionId);
|
|
|
|
|
|
|
|
|
|
|
|
const getApplication = async (name) => {
|
|
|
|
if(applications[name])
|
|
|
|
return applications[name];
|
|
|
|
|
|
|
|
await loadApplications();
|
|
|
|
|
|
|
|
return applications[name];
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSession = async (sessionId, appname) => {
|
|
|
|
const customSessionId = getCustomSessionId(appname, sessionId);
|
|
|
|
if(isMaster(appname)) {
|
|
|
|
return await bb.recordApi.load(`/sessions/${customSessionId}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const app = await getApplication(appname);
|
|
|
|
return await bb.recordApi.load(`/applications/${app.id}/sessions/${customSessionId}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteSession = async (sessionId, appname) => {
|
|
|
|
const customSessionId = getCustomSessionId(appname, sessionId);
|
|
|
|
if(isMaster(appname)) {
|
|
|
|
return await bb.recordApi.delete(`/sessions/${customSessionId}`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const app = await getApplication(appname);
|
|
|
|
return await bb.recordApi.delete(`/applications/${app.id}/sessions/${customSessionId}`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const authenticate = async (sessionId, appname, username, password, instanceName="default") => {
|
|
|
|
|
|
|
|
if(isMaster(appname)) {
|
|
|
|
const authUser = await bb.authApi.authenticate(username, password);
|
|
|
|
if(!authUser) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const session = bb.recordApi.getNew("/sessions", "mastersession");
|
|
|
|
bb.recordApi.setCustomId(session, sessionId);
|
|
|
|
session.user_json = JSON.stringify(authUser);
|
|
|
|
await bb.recordApi.save(session);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
const app = await getApplication(appname);
|
|
|
|
|
|
|
|
const userInMaster = await bb.indexApi.listItems(
|
|
|
|
`/applications/${app.id}/users_by_name`,
|
|
|
|
{name:username}
|
|
|
|
).find(u => u.name === username);
|
|
|
|
|
|
|
|
const instance = await bb.recordApi.load(
|
|
|
|
userInMaster.instance.key);
|
|
|
|
|
|
|
|
const bbInstance = await getApisWithFullAccess(
|
2019-06-14 18:01:01 +02:00
|
|
|
datastoreModule.getDatastore(instance.datastoreconfig));
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
const authUser = await bbInstance.authApi.authenticate(username, password);
|
|
|
|
|
|
|
|
if(!authUser) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const session = bb.recordApi.getNew(`/applications/${app.id}/sessions`, "session");
|
|
|
|
bb.recordApi.setCustomId(session, sessionId);
|
|
|
|
session.user_json = JSON.stringify(authUser);
|
|
|
|
session.instanceDatastoreConfig = instance.datastoreconfig;
|
|
|
|
await bb.recordApi.save(session);
|
|
|
|
return session;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getInstanceApiForSession = async (appname, sessionId) => {
|
|
|
|
if(isMaster(appname)) {
|
2019-06-14 18:01:01 +02:00
|
|
|
const customId = bb.recordApi.customId("mastersession", sessionId);
|
2019-06-14 11:05:46 +02:00
|
|
|
const session = await bb.recordApi.load(`/sessions/${customId}`);
|
2019-06-14 18:01:01 +02:00
|
|
|
return await getApisForSession(masterDatastore, session);
|
2019-06-14 11:05:46 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const app = await getApplication(appname);
|
|
|
|
const customId = bb.recordApi.customId("session", sessionId);
|
|
|
|
const session = await bb.recordApi.load(`/applications/${app.id}/sessions/${customId}`);
|
2019-06-14 18:01:01 +02:00
|
|
|
const instanceDatastore = getInstanceDatastore(session.instanceDatastoreConfig)
|
|
|
|
return await getApisForSession(instanceDatastore, session);
|
2019-06-14 11:05:46 +02:00
|
|
|
}
|
2019-06-16 23:17:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const getFullAccessInstanceApiForUsername = async (appname, username) => {
|
|
|
|
|
|
|
|
if(isMaster(appname)) {
|
|
|
|
const user = bb.authApi.getUsers()
|
|
|
|
.find(u => u.name === username);
|
|
|
|
if(!user) return;
|
|
|
|
if(!user.enabled) return;
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const app = await getApplication(appname);
|
|
|
|
const matches = bb.indexApi.listItems(
|
|
|
|
`/applications/${app.id}/user_name_lookup`,
|
|
|
|
{
|
|
|
|
rangeStartParams:{name:username},
|
|
|
|
rangeEndParams:{name:username},
|
|
|
|
searchPhrase:`name:${username}`
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if(matches.length !== 1) return;
|
|
|
|
|
|
|
|
const instanceDatastore = getInstanceDatastore(
|
|
|
|
matches[0].instanceDatastoreConfig);
|
|
|
|
|
|
|
|
return await getApisWithFullAccess(instanceDatastore);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2019-06-14 11:05:46 +02:00
|
|
|
|
|
|
|
return ({
|
|
|
|
getApplication,
|
|
|
|
getSession,
|
|
|
|
deleteSession,
|
|
|
|
authenticate,
|
2019-06-16 23:17:22 +02:00
|
|
|
getInstanceApiForSession,
|
|
|
|
getFullAccessInstanceApiForUsername,
|
|
|
|
createTemporaryAccessCode
|
2019-06-14 11:05:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|