2020-07-21 13:20:51 +00:00
|
|
|
import * as crypto from 'crypto';
|
|
|
|
import * as express from 'express';
|
|
|
|
import {EntityManager} from 'typeorm';
|
2023-01-24 13:13:18 +00:00
|
|
|
import * as cookie from 'cookie';
|
|
|
|
import {Request} from 'express';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
import {ApiError} from 'app/common/ApiError';
|
|
|
|
import {FullUser} from 'app/common/LoginSessionAPI';
|
|
|
|
import {OrganizationProperties} from 'app/common/UserAPI';
|
|
|
|
import {getAuthorizedUserId, getUserId, getUserProfiles, RequestWithLogin} from 'app/server/lib/Authorizer';
|
|
|
|
import {getSessionUser, linkOrgWithEmail} from 'app/server/lib/BrowserSession';
|
|
|
|
import {expressWrap} from 'app/server/lib/expressWrap';
|
|
|
|
import {RequestWithOrg} from 'app/server/lib/extractOrg';
|
2022-07-04 14:14:55 +00:00
|
|
|
import log from 'app/server/lib/log';
|
(core) move more tests to grist-core
Summary:
* Tie build and run-time docker base images to a consistent version (buster)
* Extend the test login system activated by GRIST_TEST_LOGIN to ease porting tests that currently rely on cognito (many)
* Make org resets work in absence of billing endpoints
* When in-memory session caches are used, add missing invalidation steps
* Pass org information through sign-ups/sign-ins more carefully
* For CORS, explicitly trust GRIST_HOST origin when set
* Move some fixtures and tests to core, focussing on tests that cover existing failures or are in the set of tests run on deployments
* Retain regular `test` target to run the test suite directly, without docker
* Add a `test:smoke` target to run a single simple test without `GRIST_TEST_LOGIN` activated
* Add a `test:docker` target to run the tests against a grist-core docker image - since tests rely on certain fixture teams/docs, added `TEST_SUPPORT_API_KEY` and `TEST_ADD_SAMPLES` flags to ease porting
The tests ported were `nbrowser` tests: `ActionLog.ts` (the first test I tend to port to anything, out of habit), `Fork.ts` (exercises a lot of doc creation paths), `HomeIntro.ts` (a lot of DocMenu exercise), and `DuplicateDocument.ts` (covers a feature known to be failing prior to this diff, the CORS tweak resolves it).
Test Plan: Manually tested via `buildtools/build_core.sh`. In follow up, I want to add running the `test:docker` target in grist-core's workflows. In jenkins, only the smoke test is run. There'd be an argument for running all tests, but they include particularly slow tests, and are duplicates of tests already run (in different configuration admittedly), so I'd like to try first just using them in grist-core to gate updates to any packaged version of Grist (the docker image currently).
Reviewers: alexmojaki
Reviewed By: alexmojaki
Subscribers: alexmojaki
Differential Revision: https://phab.getgrist.com/D3176
2021-12-10 22:42:54 +00:00
|
|
|
import {addPermit, clearSessionCacheIfNeeded, getDocScope, getScope, integerParam,
|
2023-06-13 23:32:29 +00:00
|
|
|
isParameterOn, optStringParam, sendOkReply, sendReply, stringParam} from 'app/server/lib/requestUtils';
|
2021-11-26 10:43:55 +00:00
|
|
|
import {IWidgetRepository} from 'app/server/lib/WidgetRepository';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
import {User} from './entity/User';
|
2022-10-14 15:39:15 +00:00
|
|
|
import {HomeDBManager, QueryResult, Scope} from './lib/HomeDBManager';
|
2023-01-24 13:13:18 +00:00
|
|
|
import {getCookieDomain} from 'app/server/lib/gristSessions';
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
// Special public organization that contains examples and templates.
|
2022-07-27 20:20:14 +00:00
|
|
|
export const TEMPLATES_ORG_DOMAIN = process.env.GRIST_ID_PREFIX ?
|
2021-07-28 19:02:06 +00:00
|
|
|
`templates-${process.env.GRIST_ID_PREFIX}` :
|
|
|
|
'templates';
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
// exposed for testing purposes
|
|
|
|
export const Deps = {
|
|
|
|
apiKeyGenerator: () => crypto.randomBytes(20).toString('hex')
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fetch the org this request was made for, or null if it isn't tied to a particular org.
|
|
|
|
// Early middleware should have put the org in the request object for us.
|
|
|
|
export function getOrgFromRequest(req: Request): string|null {
|
|
|
|
return (req as RequestWithOrg).org || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the signature of the user's email address using HelpScout's secret key, to prove to
|
|
|
|
* HelpScout the user identity for identifying customer information and conversation history.
|
|
|
|
*/
|
|
|
|
function helpScoutSign(email: string): string|undefined {
|
2021-12-07 05:37:37 +00:00
|
|
|
const secretKey = process.env.HELP_SCOUT_SECRET_KEY_V2;
|
2020-07-21 13:20:51 +00:00
|
|
|
if (!secretKey) { return undefined; }
|
|
|
|
return crypto.createHmac('sha256', secretKey).update(email).digest('hex');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch an identifier for an organization from the "oid" parameter of the request.
|
|
|
|
* - Integers are accepted, and will be compared with values in orgs.id column
|
|
|
|
* - Strings are accepted, and will be compared with values in orgs.domain column
|
|
|
|
* (or, if they match the pattern docs-NNNN, will check orgs.owner_id)
|
|
|
|
* - The special string "current" is replaced with the current org domain embedded
|
|
|
|
* in the url
|
|
|
|
* - If there is no identifier available, a 400 error is thrown.
|
|
|
|
*/
|
|
|
|
export function getOrgKey(req: Request): string|number {
|
2021-11-29 20:12:45 +00:00
|
|
|
let orgKey: string|null = stringParam(req.params.oid, 'oid');
|
2020-07-21 13:20:51 +00:00
|
|
|
if (orgKey === 'current') {
|
|
|
|
orgKey = getOrgFromRequest(req);
|
|
|
|
}
|
|
|
|
if (!orgKey) {
|
|
|
|
throw new ApiError("No organization chosen", 400);
|
|
|
|
} else if (/^\d+$/.test(orgKey)) {
|
|
|
|
return parseInt(orgKey, 10);
|
|
|
|
}
|
|
|
|
return orgKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an non-personal org with a new billingAccout, with the given name and domain.
|
|
|
|
// Returns a QueryResult with the orgId on success.
|
|
|
|
export function addOrg(
|
|
|
|
dbManager: HomeDBManager,
|
|
|
|
userId: number,
|
|
|
|
props: Partial<OrganizationProperties>,
|
2022-02-02 19:30:50 +00:00
|
|
|
options?: {
|
2022-06-08 17:54:00 +00:00
|
|
|
planType?: string,
|
2022-02-02 19:30:50 +00:00
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
): Promise<number> {
|
|
|
|
return dbManager.connection.transaction(async manager => {
|
2022-08-31 16:30:16 +00:00
|
|
|
const user = await manager.findOne(User, {where: {id: userId}});
|
2020-07-21 13:20:51 +00:00
|
|
|
if (!user) { return handleDeletedUser(); }
|
2021-06-17 20:48:46 +00:00
|
|
|
const query = await dbManager.addOrg(user, props, {
|
2022-02-02 19:30:50 +00:00
|
|
|
...options,
|
2021-06-17 20:48:46 +00:00
|
|
|
setUserAsOwner: false,
|
|
|
|
useNewPlan: true
|
|
|
|
}, manager);
|
2020-07-21 13:20:51 +00:00
|
|
|
if (query.status !== 200) { throw new ApiError(query.errMessage!, query.status); }
|
|
|
|
return query.data!;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a REST API for the landing page, which returns user's workspaces, organizations and documents.
|
|
|
|
* Temporarily sqlite database is used. Later it will be changed to RDS Aurora or PostgreSQL.
|
|
|
|
*/
|
|
|
|
export class ApiServer {
|
|
|
|
/**
|
|
|
|
* Add API endpoints to the specified connection. An error handler is added to /api to make sure
|
|
|
|
* all error responses have a body in json format.
|
|
|
|
*
|
|
|
|
* Note that it expects bodyParser, userId, and jsonErrorHandler middleware to be set up outside
|
|
|
|
* to apply to these routes, and trustOrigin too for cross-domain requests.
|
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
private _app: express.Application,
|
2021-11-26 10:43:55 +00:00
|
|
|
private _dbManager: HomeDBManager,
|
|
|
|
private _widgetRepository: IWidgetRepository
|
2020-07-21 13:20:51 +00:00
|
|
|
) {
|
|
|
|
this._addEndpoints();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _addEndpoints(): void {
|
|
|
|
// GET /api/orgs
|
|
|
|
// Get all organizations user may have some access to.
|
|
|
|
this._app.get('/api/orgs', expressWrap(async (req, res) => {
|
|
|
|
const userId = getUserId(req);
|
|
|
|
const domain = getOrgFromRequest(req);
|
|
|
|
const merged = Boolean(req.query.merged);
|
|
|
|
const query = merged ?
|
|
|
|
await this._dbManager.getMergedOrgs(userId, userId, domain) :
|
|
|
|
await this._dbManager.getOrgs(userId, domain);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/workspace/:wid
|
|
|
|
// Get workspace by id, returning nested documents that user has access to.
|
|
|
|
this._app.get('/api/workspaces/:wid', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const query = await this._dbManager.getWorkspace(getScope(req), wsId);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/orgs/:oid
|
|
|
|
// Get organization by id
|
|
|
|
this._app.get('/api/orgs/:oid', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const query = await this._dbManager.getOrg(getScope(req), org);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/orgs/:oid/workspaces
|
|
|
|
// Get all workspaces and nested documents of organization that user has access to.
|
|
|
|
this._app.get('/api/orgs/:oid/workspaces', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const query = await this._dbManager.getOrgWorkspaces(getScope(req), org);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
// GET /api/orgs/:oid/usage
|
|
|
|
// Get usage summary of all un-deleted documents in the organization.
|
|
|
|
// Only accessible to org owners.
|
|
|
|
this._app.get('/api/orgs/:oid/usage', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const usage = await this._dbManager.getOrgUsageSummary(getScope(req), org);
|
|
|
|
return sendOkReply(req, res, usage);
|
|
|
|
}));
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
// POST /api/orgs
|
|
|
|
// Body params: name (required), domain
|
|
|
|
// Create a new org.
|
|
|
|
this._app.post('/api/orgs', expressWrap(async (req, res) => {
|
|
|
|
// Don't let anonymous users end up owning organizations, it will be confusing.
|
|
|
|
// Maybe if the user has presented credentials this would be ok - but addOrg
|
|
|
|
// doesn't have access to that information yet, so punting on this.
|
|
|
|
// TODO: figure out who should be allowed to create organizations
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
const orgId = await addOrg(this._dbManager, userId, req.body);
|
|
|
|
return sendOkReply(req, res, orgId);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/orgs/:oid
|
|
|
|
// Body params: name, domain
|
|
|
|
// Update the specified org.
|
|
|
|
this._app.patch('/api/orgs/:oid', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const query = await this._dbManager.updateOrg(getScope(req), org, req.body);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// // DELETE /api/orgs/:oid
|
|
|
|
// Delete the specified org and all included workspaces and docs.
|
|
|
|
this._app.delete('/api/orgs/:oid', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const query = await this._dbManager.deleteOrg(getScope(req), org);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/orgs/:oid/workspaces
|
|
|
|
// Body params: name
|
|
|
|
// Create a new workspace owned by the specific organization.
|
|
|
|
this._app.post('/api/orgs/:oid/workspaces', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const query = await this._dbManager.addWorkspace(getScope(req), org, req.body);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/workspaces/:wid
|
|
|
|
// Body params: name
|
|
|
|
// Update the specified workspace.
|
|
|
|
this._app.patch('/api/workspaces/:wid', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const query = await this._dbManager.updateWorkspace(getScope(req), wsId, req.body);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
2022-05-16 17:41:12 +00:00
|
|
|
// DELETE /api/workspaces/:wid
|
2020-07-21 13:20:51 +00:00
|
|
|
// Delete the specified workspace and all included docs.
|
|
|
|
this._app.delete('/api/workspaces/:wid', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const query = await this._dbManager.deleteWorkspace(getScope(req), wsId);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/workspaces/:wid/remove
|
|
|
|
// Soft-delete the specified workspace. If query parameter "permanent" is set,
|
|
|
|
// delete permanently.
|
|
|
|
this._app.post('/api/workspaces/:wid/remove', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
if (isParameterOn(req.query.permanent)) {
|
|
|
|
const query = await this._dbManager.deleteWorkspace(getScope(req), wsId);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
} else {
|
|
|
|
await this._dbManager.softDeleteWorkspace(getScope(req), wsId);
|
|
|
|
return sendOkReply(req, res);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/workspaces/:wid/unremove
|
|
|
|
// Recover the specified workspace if it was previously soft-deleted and is
|
|
|
|
// still available.
|
|
|
|
this._app.post('/api/workspaces/:wid/unremove', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
await this._dbManager.undeleteWorkspace(getScope(req), wsId);
|
|
|
|
return sendOkReply(req, res);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/workspaces/:wid/docs
|
|
|
|
// Create a new doc owned by the specific workspace.
|
|
|
|
this._app.post('/api/workspaces/:wid/docs', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const wsId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const query = await this._dbManager.addDocument(getScope(req), wsId, req.body);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
2021-07-28 19:02:06 +00:00
|
|
|
// GET /api/templates/
|
|
|
|
// Get all templates (or only featured templates if `onlyFeatured` is set).
|
|
|
|
this._app.get('/api/templates/', expressWrap(async (req, res) => {
|
|
|
|
const onlyFeatured = isParameterOn(req.query.onlyFeatured);
|
|
|
|
const query = await this._dbManager.getOrgWorkspaces(
|
|
|
|
{...getScope(req), showOnlyPinned: onlyFeatured},
|
|
|
|
TEMPLATES_ORG_DOMAIN
|
|
|
|
);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
2021-11-26 10:43:55 +00:00
|
|
|
// GET /api/widgets/
|
|
|
|
// Get all widget definitions from external source.
|
|
|
|
this._app.get('/api/widgets/', expressWrap(async (req, res) => {
|
|
|
|
const widgetList = await this._widgetRepository.getWidgets();
|
|
|
|
return sendOkReply(req, res, widgetList);
|
|
|
|
}));
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
// PATCH /api/docs/:did
|
|
|
|
// Update the specified doc.
|
|
|
|
this._app.patch('/api/docs/:did', expressWrap(async (req, res) => {
|
|
|
|
const query = await this._dbManager.updateDocument(getDocScope(req), req.body);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/docs/:did/unremove
|
|
|
|
// Recover the specified doc if it was previously soft-deleted and is
|
|
|
|
// still available.
|
|
|
|
this._app.post('/api/docs/:did/unremove', expressWrap(async (req, res) => {
|
|
|
|
await this._dbManager.undeleteDocument(getDocScope(req));
|
|
|
|
return sendOkReply(req, res);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/orgs/:oid/access
|
|
|
|
// Update the specified org acl rules.
|
|
|
|
this._app.patch('/api/orgs/:oid/access', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
|
|
|
const delta = req.body.delta;
|
|
|
|
const query = await this._dbManager.updateOrgPermissions(getScope(req), org, delta);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/workspaces/:wid/access
|
|
|
|
// Update the specified workspace acl rules.
|
|
|
|
this._app.patch('/api/workspaces/:wid/access', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const workspaceId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const delta = req.body.delta;
|
|
|
|
const query = await this._dbManager.updateWorkspacePermissions(getScope(req), workspaceId, delta);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/docs/:did
|
|
|
|
// Get information about a document.
|
|
|
|
this._app.get('/api/docs/:did', expressWrap(async (req, res) => {
|
2022-03-24 10:59:47 +00:00
|
|
|
const query = await this._dbManager.getDoc(req);
|
2020-07-21 13:20:51 +00:00
|
|
|
return sendOkReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/docs/:did/access
|
|
|
|
// Update the specified doc acl rules.
|
|
|
|
this._app.patch('/api/docs/:did/access', expressWrap(async (req, res) => {
|
|
|
|
const delta = req.body.delta;
|
|
|
|
const query = await this._dbManager.updateDocPermissions(getDocScope(req), delta);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// PATCH /api/docs/:did/move
|
|
|
|
// Move the doc to the workspace specified in the body.
|
|
|
|
this._app.patch('/api/docs/:did/move', expressWrap(async (req, res) => {
|
|
|
|
const workspaceId = req.body.workspace;
|
|
|
|
const query = await this._dbManager.moveDoc(getDocScope(req), workspaceId);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
this._app.patch('/api/docs/:did/pin', expressWrap(async (req, res) => {
|
|
|
|
const query = await this._dbManager.pinDoc(getDocScope(req), true);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
this._app.patch('/api/docs/:did/unpin', expressWrap(async (req, res) => {
|
|
|
|
const query = await this._dbManager.pinDoc(getDocScope(req), false);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/orgs/:oid/access
|
|
|
|
// Get user access information regarding an org
|
|
|
|
this._app.get('/api/orgs/:oid/access', expressWrap(async (req, res) => {
|
|
|
|
const org = getOrgKey(req);
|
2022-10-14 15:39:15 +00:00
|
|
|
const query = await this._withSupportUserAllowedToView(
|
|
|
|
org, req, (scope) => this._dbManager.getOrgAccess(scope, org)
|
|
|
|
);
|
2020-07-21 13:20:51 +00:00
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/workspaces/:wid/access
|
|
|
|
// Get user access information regarding a workspace
|
|
|
|
this._app.get('/api/workspaces/:wid/access', expressWrap(async (req, res) => {
|
2021-11-29 20:12:45 +00:00
|
|
|
const workspaceId = integerParam(req.params.wid, 'wid');
|
2020-07-21 13:20:51 +00:00
|
|
|
const query = await this._dbManager.getWorkspaceAccess(getScope(req), workspaceId);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/docs/:did/access
|
|
|
|
// Get user access information regarding a doc
|
|
|
|
this._app.get('/api/docs/:did/access', expressWrap(async (req, res) => {
|
|
|
|
const query = await this._dbManager.getDocAccess(getDocScope(req));
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/profile/user
|
|
|
|
// Get user's profile
|
|
|
|
this._app.get('/api/profile/user', expressWrap(async (req, res) => {
|
|
|
|
const fullUser = await this._getFullUser(req);
|
2022-02-14 21:26:21 +00:00
|
|
|
return sendOkReply(req, res, fullUser, {allowedFields: new Set(['allowGoogleLogin'])});
|
2020-07-21 13:20:51 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/profile/user/name
|
|
|
|
// Body params: string
|
|
|
|
// Update users profile.
|
|
|
|
this._app.post('/api/profile/user/name', expressWrap(async (req, res) => {
|
2020-08-04 19:35:05 +00:00
|
|
|
const userId = getAuthorizedUserId(req);
|
2020-07-21 13:20:51 +00:00
|
|
|
if (!(req.body && req.body.name)) {
|
|
|
|
throw new ApiError('Name expected in the body', 400);
|
|
|
|
}
|
|
|
|
const name = req.body.name;
|
|
|
|
await this._dbManager.updateUserName(userId, name);
|
|
|
|
res.sendStatus(200);
|
|
|
|
}));
|
|
|
|
|
2023-01-24 13:13:18 +00:00
|
|
|
// POST /api/profile/user/locale
|
|
|
|
// Body params: string
|
|
|
|
// Update users profile.
|
|
|
|
this._app.post('/api/profile/user/locale', expressWrap(async (req, res) => {
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
await this._dbManager.updateUserOptions(userId, {locale: req.body.locale || null});
|
|
|
|
res.append('Set-Cookie', cookie.serialize('grist_user_locale', req.body.locale || '', {
|
|
|
|
httpOnly: false, // make available to client-side scripts
|
|
|
|
domain: getCookieDomain(req),
|
|
|
|
path: '/',
|
|
|
|
secure: true,
|
|
|
|
maxAge: req.body.locale ? 31536000 : 0,
|
|
|
|
sameSite: 'None', // there is no security concern to expose this information.
|
|
|
|
}));
|
|
|
|
res.sendStatus(200);
|
|
|
|
}));
|
|
|
|
|
2022-02-14 21:26:21 +00:00
|
|
|
// POST /api/profile/allowGoogleLogin
|
|
|
|
// Update user's preference for allowing Google login.
|
|
|
|
this._app.post('/api/profile/allowGoogleLogin', expressWrap(async (req, res) => {
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
const fullUser = await this._getFullUser(req);
|
|
|
|
if (fullUser.loginMethod !== 'Email + Password') {
|
|
|
|
throw new ApiError('Only users signed in via email can enable/disable Google login', 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
const allowGoogleLogin: boolean | undefined = req.body.allowGoogleLogin;
|
|
|
|
if (allowGoogleLogin === undefined) {
|
|
|
|
throw new ApiError('Missing body param: allowGoogleLogin', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._dbManager.updateUserOptions(userId, {allowGoogleLogin});
|
|
|
|
res.sendStatus(200);
|
|
|
|
}));
|
|
|
|
|
2022-04-08 13:52:08 +00:00
|
|
|
this._app.post('/api/profile/isConsultant', expressWrap(async (req, res) => {
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
if (userId !== this._dbManager.getSupportUserId()) {
|
|
|
|
throw new ApiError('Only support user can enable/disable isConsultant', 401);
|
|
|
|
}
|
|
|
|
const isConsultant: boolean | undefined = req.body.isConsultant;
|
|
|
|
const targetUserId: number | undefined = req.body.userId;
|
|
|
|
if (isConsultant === undefined) {
|
|
|
|
throw new ApiError('Missing body param: isConsultant', 400);
|
|
|
|
}
|
|
|
|
if (targetUserId === undefined) {
|
|
|
|
throw new ApiError('Missing body param: targetUserId', 400);
|
|
|
|
}
|
|
|
|
await this._dbManager.updateUserOptions(targetUserId, {
|
|
|
|
isConsultant
|
|
|
|
});
|
|
|
|
res.sendStatus(200);
|
|
|
|
}));
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
// GET /api/profile/apikey
|
|
|
|
// Get user's apiKey
|
|
|
|
this._app.get('/api/profile/apikey', expressWrap(async (req, res) => {
|
|
|
|
const userId = getUserId(req);
|
2022-08-31 16:30:16 +00:00
|
|
|
const user = await User.findOne({where: {id: userId}});
|
2020-07-21 13:20:51 +00:00
|
|
|
if (user) {
|
|
|
|
// The null value is of no interest to the user, let's show empty string instead.
|
|
|
|
res.send(user.apiKey || '');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleDeletedUser();
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/profile/apikey
|
|
|
|
// Update user's apiKey
|
|
|
|
this._app.post('/api/profile/apikey', expressWrap(async (req, res) => {
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
const force = req.body ? req.body.force : false;
|
|
|
|
const manager = this._dbManager.connection.manager;
|
2022-08-31 16:30:16 +00:00
|
|
|
let user = await manager.findOne(User, {where: {id: userId}});
|
2020-07-21 13:20:51 +00:00
|
|
|
if (!user) { return handleDeletedUser(); }
|
|
|
|
if (!user.apiKey || force) {
|
|
|
|
user = await updateApiKeyWithRetry(manager, user);
|
|
|
|
res.status(200).send(user.apiKey);
|
|
|
|
} else {
|
|
|
|
res.status(400).send({error: "An apikey is already set, use `{force: true}` to override it."});
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// DELETE /api/profile/apiKey
|
|
|
|
// Delete apiKey
|
|
|
|
this._app.delete('/api/profile/apikey', expressWrap(async (req, res) => {
|
|
|
|
const userId = getAuthorizedUserId(req);
|
|
|
|
await this._dbManager.connection.transaction(async manager => {
|
2022-08-31 16:30:16 +00:00
|
|
|
const user = await manager.findOne(User, {where: {id: userId}});
|
2021-05-23 17:43:11 +00:00
|
|
|
if (!user) { return handleDeletedUser(); }
|
2020-07-21 13:20:51 +00:00
|
|
|
user.apiKey = null;
|
|
|
|
await manager.save(User, user);
|
|
|
|
});
|
|
|
|
res.sendStatus(200);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/session/access/active
|
|
|
|
// Returns active user and active org (if any)
|
|
|
|
this._app.get('/api/session/access/active', expressWrap(async (req, res) => {
|
2023-04-11 05:00:28 +00:00
|
|
|
const fullUser = await this._getFullUser(req, {includePrefs: true});
|
2020-07-21 13:20:51 +00:00
|
|
|
const domain = getOrgFromRequest(req);
|
2022-10-14 15:39:15 +00:00
|
|
|
const org = domain ? (await this._withSupportUserAllowedToView(
|
|
|
|
domain, req, (scope) => this._dbManager.getOrg(scope, domain)
|
|
|
|
)) : null;
|
2020-07-21 13:20:51 +00:00
|
|
|
const orgError = (org && org.errMessage) ? {error: org.errMessage, status: org.status} : undefined;
|
|
|
|
return sendOkReply(req, res, {
|
|
|
|
user: {...fullUser, helpScoutSignature: helpScoutSign(fullUser.email)},
|
|
|
|
org: (org && org.data) || null,
|
|
|
|
orgError
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// POST /api/session/access/active
|
|
|
|
// Body params: email (required)
|
2023-06-13 23:32:29 +00:00
|
|
|
// Body params: org (optional) - string subdomain or 'current', for which org's active user to modify.
|
2020-07-21 13:20:51 +00:00
|
|
|
// Sets active user for active org
|
|
|
|
this._app.post('/api/session/access/active', expressWrap(async (req, res) => {
|
|
|
|
const mreq = req as RequestWithLogin;
|
2023-06-13 23:32:29 +00:00
|
|
|
let domain = optStringParam(req.body.org);
|
|
|
|
if (!domain || domain === 'current') {
|
|
|
|
domain = getOrgFromRequest(mreq) || '';
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
const email = req.body.email;
|
|
|
|
if (!email) { throw new ApiError('email required', 400); }
|
|
|
|
try {
|
|
|
|
// Modify session copy in request. Will be saved to persistent storage before responding
|
|
|
|
// by express-session middleware.
|
2023-06-13 23:32:29 +00:00
|
|
|
linkOrgWithEmail(mreq.session, req.body.email, domain);
|
(core) move more tests to grist-core
Summary:
* Tie build and run-time docker base images to a consistent version (buster)
* Extend the test login system activated by GRIST_TEST_LOGIN to ease porting tests that currently rely on cognito (many)
* Make org resets work in absence of billing endpoints
* When in-memory session caches are used, add missing invalidation steps
* Pass org information through sign-ups/sign-ins more carefully
* For CORS, explicitly trust GRIST_HOST origin when set
* Move some fixtures and tests to core, focussing on tests that cover existing failures or are in the set of tests run on deployments
* Retain regular `test` target to run the test suite directly, without docker
* Add a `test:smoke` target to run a single simple test without `GRIST_TEST_LOGIN` activated
* Add a `test:docker` target to run the tests against a grist-core docker image - since tests rely on certain fixture teams/docs, added `TEST_SUPPORT_API_KEY` and `TEST_ADD_SAMPLES` flags to ease porting
The tests ported were `nbrowser` tests: `ActionLog.ts` (the first test I tend to port to anything, out of habit), `Fork.ts` (exercises a lot of doc creation paths), `HomeIntro.ts` (a lot of DocMenu exercise), and `DuplicateDocument.ts` (covers a feature known to be failing prior to this diff, the CORS tweak resolves it).
Test Plan: Manually tested via `buildtools/build_core.sh`. In follow up, I want to add running the `test:docker` target in grist-core's workflows. In jenkins, only the smoke test is run. There'd be an argument for running all tests, but they include particularly slow tests, and are duplicates of tests already run (in different configuration admittedly), so I'd like to try first just using them in grist-core to gate updates to any packaged version of Grist (the docker image currently).
Reviewers: alexmojaki
Reviewed By: alexmojaki
Subscribers: alexmojaki
Differential Revision: https://phab.getgrist.com/D3176
2021-12-10 22:42:54 +00:00
|
|
|
clearSessionCacheIfNeeded(req, {sessionID: mreq.sessionID});
|
2020-07-21 13:20:51 +00:00
|
|
|
return sendOkReply(req, res, {email});
|
|
|
|
} catch (e) {
|
|
|
|
throw new ApiError('email not available', 403);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// GET /api/session/access/all
|
|
|
|
// Returns all user profiles (with ids) and all orgs they can access.
|
|
|
|
// Flattens personal orgs into a single org.
|
|
|
|
this._app.get('/api/session/access/all', expressWrap(async (req, res) => {
|
|
|
|
const domain = getOrgFromRequest(req);
|
|
|
|
const users = getUserProfiles(req);
|
|
|
|
const userId = getUserId(req);
|
|
|
|
const orgs = await this._dbManager.getMergedOrgs(userId, users, domain);
|
|
|
|
if (orgs.errMessage) { throw new ApiError(orgs.errMessage, orgs.status); }
|
|
|
|
return sendOkReply(req, res, {
|
|
|
|
users: await this._dbManager.completeProfiles(users),
|
|
|
|
orgs: orgs.data
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// DELETE /users/:uid
|
|
|
|
// Delete the specified user, their personal organization, removing them from all groups.
|
|
|
|
// Not available to the anonymous user.
|
|
|
|
// TODO: should orphan orgs, inaccessible by anyone else, get deleted when last user
|
|
|
|
// leaves?
|
|
|
|
this._app.delete('/api/users/:uid', expressWrap(async (req, res) => {
|
|
|
|
const userIdToDelete = parseInt(req.params.uid, 10);
|
|
|
|
if (!(req.body && req.body.name !== undefined)) {
|
|
|
|
throw new ApiError('to confirm deletion of a user, provide their name', 400);
|
|
|
|
}
|
|
|
|
const query = await this._dbManager.deleteUser(getScope(req), userIdToDelete, req.body.name);
|
|
|
|
return sendReply(req, res, query);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-04-11 05:00:28 +00:00
|
|
|
private async _getFullUser(req: Request, options: {includePrefs?: boolean} = {}): Promise<FullUser> {
|
2020-07-21 13:20:51 +00:00
|
|
|
const mreq = req as RequestWithLogin;
|
|
|
|
const userId = getUserId(mreq);
|
2023-04-11 05:00:28 +00:00
|
|
|
const user = await this._dbManager.getUser(userId, options);
|
2022-02-14 21:26:21 +00:00
|
|
|
if (!user) { throw new ApiError("unable to find user", 400); }
|
|
|
|
|
|
|
|
const fullUser = this._dbManager.makeFullUser(user);
|
2020-07-21 13:20:51 +00:00
|
|
|
const domain = getOrgFromRequest(mreq);
|
2020-12-11 19:22:35 +00:00
|
|
|
const sessionUser = getSessionUser(mreq.session, domain || '', fullUser.email);
|
2020-07-21 13:20:51 +00:00
|
|
|
const loginMethod = sessionUser && sessionUser.profile ? sessionUser.profile.loginMethod : undefined;
|
2022-02-14 21:26:21 +00:00
|
|
|
const allowGoogleLogin = user.options?.allowGoogleLogin ?? true;
|
|
|
|
return {...fullUser, loginMethod, allowGoogleLogin};
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
2022-10-14 15:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a query, and, if it is denied and the user is the support
|
|
|
|
* user, rerun the query with permission to view the current
|
|
|
|
* org. This is a bit inefficient, but only affects the support
|
|
|
|
* user. We wait to add the special permission only if needed, since
|
|
|
|
* it will in fact override any other access the support user has
|
|
|
|
* been granted, which could reduce their apparent access if that is
|
|
|
|
* part of what is returned by the query.
|
|
|
|
*/
|
|
|
|
private async _withSupportUserAllowedToView<T>(
|
|
|
|
org: string|number, req: express.Request,
|
|
|
|
op: (scope: Scope) => Promise<QueryResult<T>>
|
|
|
|
): Promise<QueryResult<T>> {
|
|
|
|
const scope = getScope(req);
|
|
|
|
const userId = getUserId(req);
|
|
|
|
const result = await op(scope);
|
|
|
|
if (result.status === 200 || userId !== this._dbManager.getSupportUserId()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const extendedScope = addPermit(scope, this._dbManager.getSupportUserId(), {org});
|
|
|
|
return await op(extendedScope);
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw the error for when a user has been deleted since point of call (very unlikely to happen).
|
|
|
|
*/
|
|
|
|
function handleDeletedUser(): never {
|
|
|
|
throw new ApiError("user not known", 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to update a user's apiKey. Update might fail because of the DB uniqueness constraint on
|
|
|
|
* the apiKey (although it is very unlikely according to `crypto`), we retry until success. Fails
|
|
|
|
* after 5 unsuccessful attempts.
|
|
|
|
*/
|
|
|
|
async function updateApiKeyWithRetry(manager: EntityManager, user: User): Promise<User> {
|
|
|
|
const currentKey = user.apiKey;
|
|
|
|
for (let i = 0; i < 5; ++i) {
|
|
|
|
user.apiKey = Deps.apiKeyGenerator();
|
|
|
|
try {
|
|
|
|
// if new key is the same as the current, the db update won't fail so we check it here (very
|
|
|
|
// unlikely to happen but but still better to handle)
|
|
|
|
if (user.apiKey === currentKey) {
|
|
|
|
throw new Error('the new key is the same as the current key');
|
|
|
|
}
|
|
|
|
return await manager.save(User, user);
|
|
|
|
} catch (e) {
|
|
|
|
// swallow and retry
|
|
|
|
log.warn(`updateApiKeyWithRetry: failed attempt ${i}/5, %s`, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('Could not generate a valid api key.');
|
|
|
|
}
|