@flowfuse/flowfuse
Version:
An open source low-code development platform
179 lines (168 loc) • 5.74 kB
JavaScript
const { generateToken, compareHash } = require('../utils')
async function createClient (app, { username, ownerId, ownerType, prefix, where, usePublicUrl = true }) {
const existingClient = await app.db.models.BrokerClient.findOne({
where: where ?? { ownerId, ownerType }
})
if (existingClient) {
await existingClient.destroy()
}
const password = generateToken(32, prefix)
await app.db.models.BrokerClient.create({ username, password, ownerId, ownerType })
const url = usePublicUrl
? (app.config.broker.public_url || app.config.broker.url || null)
: (app.config.broker.url || null)
return { url, username, password }
}
module.exports = {
/**
* Validate the username/password
*/
authenticateCredentials: async function (app, username, password) {
const user = await app.db.models.BrokerClient.findOne({
where: { username },
attributes: ['username', 'password']
})
if (compareHash(password || '', user ? user.password : '')) {
if (username.startsWith('frontend:') || username.startsWith('expert-client:') || username.startsWith('fe-team:')) {
await user.destroy()
}
return true
}
return false
},
ensurePlatformClient: async function (app, project) {
const existingClient = await app.db.models.BrokerClient.findOne({
where: {
username: 'forge_platform'
}
})
if (existingClient) {
return
}
const username = 'forge_platform'
const password = generateToken(32, 'ffbpl')
await app.db.models.BrokerClient.create({
username,
password,
ownerId: '',
ownerType: 'platform'
})
await app.settings.set('commsToken', password)
return {
username,
password
}
},
/**
*
*/
createClientForProject: async function (app, project) {
if (!app.comms) {
return null
}
if (!project.Team) {
// When restarting the platform, the container drivers get a minimal list
// of projects to restart. They don't necessarily include the Team in their
// query - so we need to ensure its available.
await project.reload({
include: [{
model: app.db.models.Team,
attributes: ['hashid', 'id', 'name', 'slug', 'links']
}]
})
}
return createClient(app, {
username: `project:${project.Team.hashid}:${project.id}`,
ownerId: project.id,
ownerType: 'project',
prefix: 'ffbp',
usePublicUrl: false
})
},
createClientForDevice: async function (app, device) {
if (!app.comms) {
return null
}
return createClient(app, {
username: `device:${device.Team.hashid}:${device.hashid}`,
ownerId: '' + device.id,
ownerType: 'device',
prefix: 'ffbd'
})
},
createClientForFrontend: async function (app, device) {
if (!app.comms) {
return null
}
return createClient(app, {
username: `frontend:${device.Team.hashid}:${device.hashid}`,
ownerId: '' + device.id,
ownerType: 'frontend',
prefix: 'ffbf'
})
},
createClientForExpertAgent: async function (app) {
if (app.comms) {
const username = 'expert-agent:api:v1'
const password = generateToken(32, 'ffbea') // ff broker expert agent
const [client, created] = await app.db.models.BrokerClient.findOrCreate({
where: {
username
},
defaults: {
password,
ownerId: '',
ownerType: 'platform'
}
})
// if it was created, the password is already set. If not, we need to update it with a new one.
if (!created) {
client.password = password
await client.save()
}
await app.settings.set('platform:expert-agent:creds', true)
return {
username,
password
}
}
return null
},
removeClientForExpertAgent: async function (app) {
if (app.comms) {
await app.db.models.BrokerClient.destroy({
where: {
username: 'expert-agent:api:v1'
}
})
await app.settings.set('platform:expert-agent:creds', false)
}
return null
},
createClientForTeamFrontend: async function (app, user, team, sessionId) {
if (!app.comms) {
return null
}
const username = `fe-team:${user.hashid}:${team.hashid}:${sessionId}`
return createClient(app, {
where: { username },
username,
ownerId: '' + user.id,
ownerType: 'fe-team',
prefix: 'ffbtf'
})
},
createClientForExpertClient: async function (app, user, sessionId) {
if (!app.comms) {
return null
}
const username = `expert-client:${user.hashid}:${sessionId}`
return createClient(app, {
where: { ownerId: '' + user.id, ownerType: 'expert-user', username },
username,
ownerId: '' + user.id,
ownerType: 'expert-user',
prefix: 'ffbec'
})
}
}