From efe53bb217ecfccc34d3da3311e4d5aeea19b029 Mon Sep 17 00:00:00 2001 From: Rory Powell Date: Thu, 6 Jul 2023 20:46:25 +0100 Subject: [PATCH] Update license endpoints to provide consistent pattern for offline license and license key (create, read, delete) --- packages/types/src/api/web/global/index.ts | 1 + packages/types/src/api/web/global/license.ts | 19 +++++ .../src/api/controllers/global/license.ts | 69 ++++++++++++++----- .../worker/src/api/routes/global/license.ts | 21 +++++- 4 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 packages/types/src/api/web/global/license.ts diff --git a/packages/types/src/api/web/global/index.ts b/packages/types/src/api/web/global/index.ts index bf4b43f0ba..efcb6dc39c 100644 --- a/packages/types/src/api/web/global/index.ts +++ b/packages/types/src/api/web/global/index.ts @@ -3,3 +3,4 @@ export * from "./auditLogs" export * from "./events" export * from "./configs" export * from "./scim" +export * from "./license" diff --git a/packages/types/src/api/web/global/license.ts b/packages/types/src/api/web/global/license.ts new file mode 100644 index 0000000000..34de2e4361 --- /dev/null +++ b/packages/types/src/api/web/global/license.ts @@ -0,0 +1,19 @@ +// LICENSE KEY + +export interface ActivateLicenseKeyRequest { + licenseKey: string +} + +export interface GetLicenseKeyResponse { + licenseKey: string, +} + +// OFFLINE LICENSE + +export interface ActivateOfflineLicenseRequest { + offlineLicense: string +} + +export interface GetOfflineLicenseResponse { + offlineLicense: string +} diff --git a/packages/worker/src/api/controllers/global/license.ts b/packages/worker/src/api/controllers/global/license.ts index 2bd173010f..b4670e0dbb 100644 --- a/packages/worker/src/api/controllers/global/license.ts +++ b/packages/worker/src/api/controllers/global/license.ts @@ -1,33 +1,66 @@ import { licensing, quotas } from "@budibase/pro" +import { + ActivateLicenseKeyRequest, + ActivateOfflineLicenseRequest, + GetLicenseKeyResponse, + GetOfflineLicenseResponse, + UserCtx, +} from "@budibase/types" -export const activate = async (ctx: any) => { +// LICENSE KEY + +export async function activateLicenseKey(ctx: UserCtx) { const { licenseKey } = ctx.request.body - if (!licenseKey) { - ctx.throw(400, "licenseKey is required") - } - await licensing.activateLicenseKey(licenseKey) ctx.status = 200 } +export async function getLicenseKey(ctx: UserCtx) { + const licenseKey = await licensing.getLicenseKey() + if (licenseKey) { + ctx.body = { licenseKey: "*" } + ctx.status = 200 + } else { + ctx.status = 404 + } +} + +export async function deleteLicenseKey(ctx: UserCtx) { + await licensing.deleteLicenseKey() + ctx.status = 204 +} + +// OFFLINE LICENSE + +export async function activateOfflineLicense(ctx: UserCtx) { + const { offlineLicense } = ctx.request.body + await licensing.activateOfflineLicense(offlineLicense) + ctx.status = 200 +} + +export async function getOfflineLicense(ctx: UserCtx) { + const offlineLicense = await licensing.getOfflineLicense() + if (offlineLicense) { + ctx.body = { offlineLicense: "*" } + ctx.status = 200 + } else { + ctx.status = 404 + } +} + +export async function deleteOfflineLicense(ctx: UserCtx) { + await licensing.deleteOfflineLicense() + ctx.status = 204 +} + +// LICENSES + export const refresh = async (ctx: any) => { await licensing.cache.refresh() ctx.status = 200 } -export const getInfo = async (ctx: any) => { - const licenseInfo = await licensing.getLicenseInfo() - if (licenseInfo) { - licenseInfo.licenseKey = "*" - ctx.body = licenseInfo - } - ctx.status = 200 -} - -export const deleteInfo = async (ctx: any) => { - await licensing.deleteLicenseInfo() - ctx.status = 200 -} +// USAGE export const getQuotaUsage = async (ctx: any) => { ctx.body = await quotas.getQuotaUsage() diff --git a/packages/worker/src/api/routes/global/license.ts b/packages/worker/src/api/routes/global/license.ts index 0fb2a6e8bd..1d6185a402 100644 --- a/packages/worker/src/api/routes/global/license.ts +++ b/packages/worker/src/api/routes/global/license.ts @@ -1,13 +1,28 @@ import Router from "@koa/router" import * as controller from "../../controllers/global/license" +import { middleware } from "@budibase/backend-core" +import Joi from "joi" + +const activateLicenseKeyValidator = middleware.joiValidator.body(Joi.object({ + licenseKey: Joi.string().required(), + }).required()) + +const activateOfflineLicenseValidator = middleware.joiValidator.body(Joi.object({ + offlineLicense: Joi.string().required(), + }).required()) const router: Router = new Router() router - .post("/api/global/license/activate", controller.activate) .post("/api/global/license/refresh", controller.refresh) - .get("/api/global/license/info", controller.getInfo) - .delete("/api/global/license/info", controller.deleteInfo) .get("/api/global/license/usage", controller.getQuotaUsage) + // LICENSE KEY + .post("/api/global/license/key", activateLicenseKeyValidator, controller.activateLicenseKey) + .get("/api/global/license/key", controller.getLicenseKey) + .delete("/api/global/license/key", controller.deleteLicenseKey) + // OFFLINE LICENSE + .post("/api/global/license/offline", activateOfflineLicenseValidator, controller.activateOfflineLicense) + .get("/api/global/license/offline", controller.getOfflineLicense) + .delete("/api/global/license/offline", controller.deleteOfflineLicense) export default router