Getting api key/dev info docs setup and in use.

This commit is contained in:
mike12345567 2022-02-10 19:06:49 +00:00
parent 3fb3a5e47d
commit 594cb77b1e
5 changed files with 49 additions and 11 deletions

View File

@ -14,6 +14,7 @@ exports.DocumentTypes = {
APP_METADATA: `${PRE_APP}${exports.SEPARATOR}metadata`, APP_METADATA: `${PRE_APP}${exports.SEPARATOR}metadata`,
ROLE: "role", ROLE: "role",
MIGRATIONS: "migrations", MIGRATIONS: "migrations",
DEV_INFO: "devinfo",
} }
exports.StaticDatabases = { exports.StaticDatabases = {

View File

@ -336,6 +336,14 @@ const getConfigParams = ({ type, workspace, user }, otherProps = {}) => {
} }
} }
/**
* Generates a new dev info document ID - this is scoped to a user.
* @returns {string} The new dev info ID which info for dev (like api key) can be stored under.
*/
const generateDevInfoID = userId => {
return `${DocumentTypes.DEV_INFO}${SEPARATOR}${userId}`
}
/** /**
* Returns the most granular configuration document from the DB based on the type, workspace and userID passed. * Returns the most granular configuration document from the DB based on the type, workspace and userID passed.
* @param {Object} db - db instance to query * @param {Object} db - db instance to query
@ -451,3 +459,4 @@ exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams exports.getConfigParams = getConfigParams
exports.getScopedFullConfig = getScopedFullConfig exports.getScopedFullConfig = getScopedFullConfig
exports.generateNewUsageQuotaDoc = generateNewUsageQuotaDoc exports.generateNewUsageQuotaDoc = generateNewUsageQuotaDoc
exports.generateDevInfoID = generateDevInfoID

View File

@ -7,11 +7,13 @@
async function generateAPIKey() { async function generateAPIKey() {
try { try {
await auth.generateAPIKey() apiKey = await auth.generateAPIKey()
notifications.success("New API key generated.") notifications.success("New API key generated.")
} catch (err) { } catch (err) {
notifications.error("Unable to generate new API key") notifications.error("Unable to generate new API key")
} }
// need to return false to keep modal open
return false
} }
onMount(async () => { onMount(async () => {

View File

@ -173,15 +173,12 @@ export function createAuthStore() {
}) })
}, },
generateAPIKey: async () => { generateAPIKey: async () => {
return API.generateAPIKey() const info = await API.generateAPIKey()
return info?.apiKey ? info.apiKey : null
}, },
fetchAPIKey: async () => { fetchAPIKey: async () => {
const info = await API.fetchDeveloperInfo() const info = await API.fetchDeveloperInfo()
if (info?.apiKey) { return info?.apiKey ? info.apiKey : null
return info.apiKey
} else {
return null
}
}, },
} }

View File

@ -1,11 +1,40 @@
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { generateDevInfoID } = require("@budibase/backend-core/db")
const { newid } = require("@budibase/backend-core/utils")
function cleanupDevInfo(info) {
// user doesn't need to aware of dev doc info
delete info._id
delete info._rev
return info
}
exports.generateAPIKey = async ctx => { exports.generateAPIKey = async ctx => {
ctx.body = { const db = getGlobalDB()
apiKey: "a175402a-89fc-11ec-a8a3-0242ac120002", const id = generateDevInfoID(ctx.user._id)
let devInfo
try {
devInfo = await db.get(id)
} catch (err) {
devInfo = { _id: id }
} }
devInfo.apiKey = newid()
await db.put(devInfo)
ctx.body = cleanupDevInfo(devInfo)
} }
exports.fetchAPIKey = async ctx => { exports.fetchAPIKey = async ctx => {
ctx.body = { const db = getGlobalDB()
apiKey: "a175402a-89fc-11ec-a8a3-0242ac120002", const id = generateDevInfoID(ctx.user._id)
let devInfo
try {
devInfo = await db.get(id)
} catch (err) {
devInfo = {
_id: id,
apiKey: newid(),
}
await db.put(devInfo)
} }
ctx.body = cleanupDevInfo(devInfo)
} }