Self API in worker conversion to typescript.
This commit is contained in:
parent
31208b1f8f
commit
c382b86fb2
|
@ -18,6 +18,7 @@ import * as logging from "./logging"
|
||||||
import pino from "./pino"
|
import pino from "./pino"
|
||||||
import * as middleware from "./middleware"
|
import * as middleware from "./middleware"
|
||||||
import plugins from "./plugin"
|
import plugins from "./plugin"
|
||||||
|
import encryption from "./security/encryption"
|
||||||
|
|
||||||
// mimic the outer package exports
|
// mimic the outer package exports
|
||||||
import * as db from "./pkg/db"
|
import * as db from "./pkg/db"
|
||||||
|
@ -60,6 +61,7 @@ const core = {
|
||||||
...pino,
|
...pino,
|
||||||
...errorClasses,
|
...errorClasses,
|
||||||
middleware,
|
middleware,
|
||||||
|
encryption,
|
||||||
}
|
}
|
||||||
|
|
||||||
export = core
|
export = core
|
||||||
|
|
|
@ -121,7 +121,7 @@ export const getTenantUser = async (
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isUserInAppTenant = (appId: string, user: any) => {
|
export const isUserInAppTenant = (appId: string, user?: any) => {
|
||||||
let userTenantId
|
let userTenantId
|
||||||
if (user) {
|
if (user) {
|
||||||
userTenantId = user.tenantId || DEFAULT_TENANT_ID
|
userTenantId = user.tenantId || DEFAULT_TENANT_ID
|
||||||
|
|
|
@ -1,39 +1,36 @@
|
||||||
const {
|
import { users } from "../../../sdk"
|
||||||
getGlobalDB,
|
import {
|
||||||
getTenantId,
|
events,
|
||||||
isUserInAppTenant,
|
featureFlags,
|
||||||
} = require("@budibase/backend-core/tenancy")
|
tenancy,
|
||||||
const { generateDevInfoID, SEPARATOR } = require("@budibase/backend-core/db")
|
constants,
|
||||||
const { user: userCache } = require("@budibase/backend-core/cache")
|
db as dbCore,
|
||||||
const {
|
utils,
|
||||||
hash,
|
cache,
|
||||||
platformLogout,
|
encryption,
|
||||||
getCookie,
|
} from "@budibase/backend-core"
|
||||||
clearCookie,
|
import env from "../../../environment"
|
||||||
} = require("@budibase/backend-core/utils")
|
const { hash, platformLogout, getCookie, clearCookie, newid } = utils
|
||||||
const { encrypt } = require("@budibase/backend-core/encryption")
|
const { user: userCache } = cache
|
||||||
const { newid } = require("@budibase/backend-core/utils")
|
|
||||||
const { users } = require("../../../sdk")
|
|
||||||
const { Cookies } = require("@budibase/backend-core/constants")
|
|
||||||
const { events, featureFlags } = require("@budibase/backend-core")
|
|
||||||
const env = require("../../../environment")
|
|
||||||
|
|
||||||
function newTestApiKey() {
|
function newTestApiKey() {
|
||||||
return env.ENCRYPTED_TEST_PUBLIC_API_KEY
|
return env.ENCRYPTED_TEST_PUBLIC_API_KEY
|
||||||
}
|
}
|
||||||
|
|
||||||
function newApiKey() {
|
function newApiKey() {
|
||||||
return encrypt(`${getTenantId()}${SEPARATOR}${newid()}`)
|
return encryption.encrypt(
|
||||||
|
`${tenancy.getTenantId()}${dbCore.SEPARATOR}${newid()}`
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanupDevInfo(info) {
|
function cleanupDevInfo(info: any) {
|
||||||
// user doesn't need to aware of dev doc info
|
// user doesn't need to aware of dev doc info
|
||||||
delete info._id
|
delete info._id
|
||||||
delete info._rev
|
delete info._rev
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.generateAPIKey = async ctx => {
|
exports.generateAPIKey = async (ctx: any) => {
|
||||||
let userId
|
let userId
|
||||||
let apiKey
|
let apiKey
|
||||||
if (env.isTest() && ctx.request.body.userId) {
|
if (env.isTest() && ctx.request.body.userId) {
|
||||||
|
@ -44,8 +41,8 @@ exports.generateAPIKey = async ctx => {
|
||||||
apiKey = newApiKey()
|
apiKey = newApiKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
const id = generateDevInfoID(userId)
|
const id = dbCore.generateDevInfoID(userId)
|
||||||
let devInfo
|
let devInfo
|
||||||
try {
|
try {
|
||||||
devInfo = await db.get(id)
|
devInfo = await db.get(id)
|
||||||
|
@ -57,9 +54,9 @@ exports.generateAPIKey = async ctx => {
|
||||||
ctx.body = cleanupDevInfo(devInfo)
|
ctx.body = cleanupDevInfo(devInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fetchAPIKey = async ctx => {
|
exports.fetchAPIKey = async (ctx: any) => {
|
||||||
const db = getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
const id = generateDevInfoID(ctx.user._id)
|
const id = dbCore.generateDevInfoID(ctx.user._id)
|
||||||
let devInfo
|
let devInfo
|
||||||
try {
|
try {
|
||||||
devInfo = await db.get(id)
|
devInfo = await db.get(id)
|
||||||
|
@ -74,20 +71,20 @@ exports.fetchAPIKey = async ctx => {
|
||||||
ctx.body = cleanupDevInfo(devInfo)
|
ctx.body = cleanupDevInfo(devInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkCurrentApp = ctx => {
|
const checkCurrentApp = (ctx: any) => {
|
||||||
const appCookie = getCookie(ctx, Cookies.CurrentApp)
|
const appCookie = getCookie(ctx, constants.Cookies.CurrentApp)
|
||||||
if (appCookie && !isUserInAppTenant(appCookie.appId)) {
|
if (appCookie && !tenancy.isUserInAppTenant(appCookie.appId)) {
|
||||||
// there is a currentapp cookie from another tenant
|
// there is a currentapp cookie from another tenant
|
||||||
// remove the cookie as this is incompatible with the builder
|
// remove the cookie as this is incompatible with the builder
|
||||||
// due to builder and admin permissions being removed
|
// due to builder and admin permissions being removed
|
||||||
clearCookie(ctx, Cookies.CurrentApp)
|
clearCookie(ctx, constants.Cookies.CurrentApp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the attributes that are session based to the current user.
|
* Add the attributes that are session based to the current user.
|
||||||
*/
|
*/
|
||||||
const addSessionAttributesToUser = ctx => {
|
const addSessionAttributesToUser = (ctx: any) => {
|
||||||
ctx.body.account = ctx.user.account
|
ctx.body.account = ctx.user.account
|
||||||
ctx.body.license = ctx.user.license
|
ctx.body.license = ctx.user.license
|
||||||
ctx.body.budibaseAccess = !!ctx.user.budibaseAccess
|
ctx.body.budibaseAccess = !!ctx.user.budibaseAccess
|
||||||
|
@ -95,9 +92,9 @@ const addSessionAttributesToUser = ctx => {
|
||||||
ctx.body.csrfToken = ctx.user.csrfToken
|
ctx.body.csrfToken = ctx.user.csrfToken
|
||||||
}
|
}
|
||||||
|
|
||||||
const sanitiseUserUpdate = ctx => {
|
const sanitiseUserUpdate = (ctx: any) => {
|
||||||
const allowed = ["firstName", "lastName", "password", "forceResetPassword"]
|
const allowed = ["firstName", "lastName", "password", "forceResetPassword"]
|
||||||
const resp = {}
|
const resp: { [key: string]: any } = {}
|
||||||
for (let [key, value] of Object.entries(ctx.request.body)) {
|
for (let [key, value] of Object.entries(ctx.request.body)) {
|
||||||
if (allowed.includes(key)) {
|
if (allowed.includes(key)) {
|
||||||
resp[key] = value
|
resp[key] = value
|
||||||
|
@ -106,7 +103,7 @@ const sanitiseUserUpdate = ctx => {
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.getSelf = async ctx => {
|
exports.getSelf = async (ctx: any) => {
|
||||||
if (!ctx.user) {
|
if (!ctx.user) {
|
||||||
ctx.throw(403, "User not logged in")
|
ctx.throw(403, "User not logged in")
|
||||||
}
|
}
|
||||||
|
@ -121,14 +118,14 @@ exports.getSelf = async ctx => {
|
||||||
ctx.body = await users.getUser(userId)
|
ctx.body = await users.getUser(userId)
|
||||||
|
|
||||||
// add the feature flags for this tenant
|
// add the feature flags for this tenant
|
||||||
const tenantId = getTenantId()
|
const tenantId = tenancy.getTenantId()
|
||||||
ctx.body.featureFlags = featureFlags.getTenantFeatureFlags(tenantId)
|
ctx.body.featureFlags = featureFlags.getTenantFeatureFlags(tenantId)
|
||||||
|
|
||||||
addSessionAttributesToUser(ctx)
|
addSessionAttributesToUser(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.updateSelf = async ctx => {
|
exports.updateSelf = async (ctx: any) => {
|
||||||
const db = getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
const user = await db.get(ctx.user._id)
|
const user = await db.get(ctx.user._id)
|
||||||
let passwordChange = false
|
let passwordChange = false
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
const Router = require("@koa/router")
|
import Router from "@koa/router"
|
||||||
const controller = require("../../controllers/global/templates")
|
import controller from "../../controllers/global/templates"
|
||||||
const { joiValidator } = require("@budibase/backend-core/auth")
|
import { TemplatePurpose, TemplateTypes } from "../../../constants"
|
||||||
const Joi = require("joi")
|
import { auth as authCore } from "@budibase/backend-core"
|
||||||
const { TemplatePurpose, TemplateTypes } = require("../../../constants")
|
import Joi from "joi"
|
||||||
const { adminOnly } = require("@budibase/backend-core/auth")
|
const { adminOnly, joiValidator } = authCore
|
||||||
|
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue