Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/conat/project.ts
1698 views
1
/*
2
This is a bridge to call the Conat rpc api that is offered by projects.
3
This is meant to be called by either a user account or a project, so API
4
keys that resolves to either work.
5
6
For security reasons this is ONLY usable via an API key -- using an account
7
is not allowed, since that opens us to XSS attacks.
8
9
Here is an example of how this would be used:
10
11
key=sk-...02
12
13
curl -sk -u $key: -H 'Content-Type: application/json' \
14
-d '{TODO}' \
15
http://localhost:9000/api/conat/project
16
17
The api is defined in packages/conat/project/api/
18
*/
19
20
import projectBridge from "@cocalc/server/api/project-bridge";
21
import getParams from "lib/api/get-params";
22
import { getAccountFromApiKey } from "@cocalc/server/auth/api";
23
import isCollaborator from "@cocalc/server/projects/is-collaborator";
24
25
export default async function handle(req, res) {
26
try {
27
const { account_id, project_id: project_id0 } =
28
(await getAccountFromApiKey(req)) ?? {};
29
if (!account_id && !project_id0) {
30
throw Error("must sign in as project or account");
31
}
32
const {
33
project_id = project_id0,
34
compute_server_id,
35
name,
36
args,
37
timeout,
38
} = getParams(req);
39
if (!project_id) {
40
throw Error("must specify project_id or use project-specific api key");
41
}
42
if (project_id0) {
43
// auth via project_id
44
if (project_id0 != project_id) {
45
throw Error("project specific api key must match requested project");
46
}
47
}
48
if (account_id) {
49
// account_id based auth
50
if (!(await isCollaborator({ account_id, project_id }))) {
51
throw Error("user must be a collaborator on the project");
52
}
53
}
54
const resp = await projectBridge({
55
project_id,
56
compute_server_id,
57
name,
58
args,
59
timeout,
60
});
61
res.json(resp);
62
} catch (err) {
63
res.json({ error: err.message });
64
}
65
}
66
67