CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/compute/get-api-key.ts
Views: 687
1
/*
2
Gets the api key of the compute server.
3
4
Calling this always invalidates any existing key for
5
this server and creates a new one.
6
7
This is only allowed right now for on prem servers.
8
*/
9
10
import getAccountId from "lib/account/get-account";
11
import { getServer } from "@cocalc/server/compute/get-servers";
12
import getParams from "lib/api/get-params";
13
import {
14
setProjectApiKey,
15
deleteProjectApiKey,
16
} from "@cocalc/server/compute/project-api-key";
17
18
import { apiRoute, apiRouteOperation } from "lib/api";
19
import {
20
GetComputeServerAPIKeyInputSchema,
21
GetComputeServerAPIKeyOutputSchema,
22
} from "lib/api/schema/compute/get-api-key";
23
24
25
async function handle(req, res) {
26
try {
27
res.json(await get(req));
28
} catch (err) {
29
res.json({ error: `${err.message}` });
30
return;
31
}
32
}
33
34
async function get(req) {
35
const account_id = await getAccountId(req);
36
if (!account_id) {
37
throw Error("must be signed in");
38
}
39
const { id } = getParams(req); // security: definitely needs to be a POST request
40
const server = await getServer({ id, account_id });
41
if (server.cloud != "onprem") {
42
throw Error("getting api key is only supported for onprem compute servers");
43
}
44
if (server.account_id != account_id) {
45
throw Error("you must be the owner of the compute server");
46
}
47
await deleteProjectApiKey({ account_id, server });
48
return await setProjectApiKey({ account_id, server });
49
}
50
51
export default apiRoute({
52
getServerAPIKey: apiRouteOperation({
53
method: "POST",
54
openApiOperation: {
55
tags: ["Compute"]
56
},
57
})
58
.input({
59
contentType: "application/json",
60
body: GetComputeServerAPIKeyInputSchema,
61
})
62
.outputs([
63
{
64
status: 200,
65
contentType: "application/json",
66
body: GetComputeServerAPIKeyOutputSchema,
67
},
68
])
69
.handler(handle),
70
});
71
72