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/delete-api-key.ts
Views: 687
1
/*
2
Delete the api key of the given compute server, if one is set.
3
*/
4
5
import getAccountId from "lib/account/get-account";
6
import { getServer } from "@cocalc/server/compute/get-servers";
7
import getParams from "lib/api/get-params";
8
import { deleteProjectApiKey } from "@cocalc/server/compute/project-api-key";
9
10
import { apiRoute, apiRouteOperation } from "lib/api";
11
import { OkStatus } from "lib/api/status";
12
import {
13
DeleteComputeServerAPIKeyInputSchema,
14
DeleteComputeServerAPIKeyOutputSchema,
15
} from "lib/api/schema/compute/delete-api-key";
16
17
async function handle(req, res) {
18
try {
19
res.json(await get(req));
20
} catch (err) {
21
res.json({ error: `${err.message}` });
22
return;
23
}
24
}
25
26
async function get(req) {
27
const account_id = await getAccountId(req);
28
if (!account_id) {
29
throw Error("must be signed in");
30
}
31
const { id } = getParams(req); // security: definitely needs to be a POST request
32
const server = await getServer({ id, account_id });
33
if (server.account_id != account_id) {
34
throw Error("you must be the owner of the compute server");
35
}
36
await deleteProjectApiKey({ account_id, server });
37
return OkStatus;
38
}
39
40
export default apiRoute({
41
deleteServerAPIKey: apiRouteOperation({
42
method: "POST",
43
openApiOperation: {
44
tags: ["Compute"],
45
},
46
})
47
.input({
48
contentType: "application/json",
49
body: DeleteComputeServerAPIKeyInputSchema,
50
})
51
.outputs([
52
{
53
status: 200,
54
contentType: "application/json",
55
body: DeleteComputeServerAPIKeyOutputSchema,
56
},
57
])
58
.handler(handle),
59
});
60
61