Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/compute/get-api-key.ts
Views: 687
/*1Gets the api key of the compute server.23Calling this always invalidates any existing key for4this server and creates a new one.56This is only allowed right now for on prem servers.7*/89import getAccountId from "lib/account/get-account";10import { getServer } from "@cocalc/server/compute/get-servers";11import getParams from "lib/api/get-params";12import {13setProjectApiKey,14deleteProjectApiKey,15} from "@cocalc/server/compute/project-api-key";1617import { apiRoute, apiRouteOperation } from "lib/api";18import {19GetComputeServerAPIKeyInputSchema,20GetComputeServerAPIKeyOutputSchema,21} from "lib/api/schema/compute/get-api-key";222324async function handle(req, res) {25try {26res.json(await get(req));27} catch (err) {28res.json({ error: `${err.message}` });29return;30}31}3233async function get(req) {34const account_id = await getAccountId(req);35if (!account_id) {36throw Error("must be signed in");37}38const { id } = getParams(req); // security: definitely needs to be a POST request39const server = await getServer({ id, account_id });40if (server.cloud != "onprem") {41throw Error("getting api key is only supported for onprem compute servers");42}43if (server.account_id != account_id) {44throw Error("you must be the owner of the compute server");45}46await deleteProjectApiKey({ account_id, server });47return await setProjectApiKey({ account_id, server });48}4950export default apiRoute({51getServerAPIKey: apiRouteOperation({52method: "POST",53openApiOperation: {54tags: ["Compute"]55},56})57.input({58contentType: "application/json",59body: GetComputeServerAPIKeyInputSchema,60})61.outputs([62{63status: 200,64contentType: "application/json",65body: GetComputeServerAPIKeyOutputSchema,66},67])68.handler(handle),69});707172