2022-11-16 19:13:34 +01:00
|
|
|
import { redis, utils } from "@budibase/backend-core"
|
2021-05-05 13:11:06 +02:00
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
function getExpirySecondsForDB(db: string) {
|
2021-05-05 16:10:28 +02:00
|
|
|
switch (db) {
|
2022-11-16 19:13:34 +01:00
|
|
|
case redis.utils.Databases.PW_RESETS:
|
2021-05-05 16:10:28 +02:00
|
|
|
// a hour
|
|
|
|
return 3600
|
2022-11-16 19:13:34 +01:00
|
|
|
case redis.utils.Databases.INVITATIONS:
|
2021-05-05 16:10:28 +02:00
|
|
|
// a day
|
|
|
|
return 86400
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 13:11:06 +02:00
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
let pwResetClient: any, invitationClient: any
|
2021-05-24 19:45:43 +02:00
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
function getClient(db: string) {
|
2021-05-24 19:45:43 +02:00
|
|
|
switch (db) {
|
2022-11-16 19:13:34 +01:00
|
|
|
case redis.utils.Databases.PW_RESETS:
|
2021-05-24 19:45:43 +02:00
|
|
|
return pwResetClient
|
2022-11-16 19:13:34 +01:00
|
|
|
case redis.utils.Databases.INVITATIONS:
|
2021-05-24 19:45:43 +02:00
|
|
|
return invitationClient
|
|
|
|
}
|
2021-05-05 13:11:06 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
async function writeACode(db: string, value: any) {
|
2021-05-05 13:11:06 +02:00
|
|
|
const client = await getClient(db)
|
2022-11-16 19:13:34 +01:00
|
|
|
const code = utils.newid()
|
2021-05-05 16:10:28 +02:00
|
|
|
await client.store(code, value, getExpirySecondsForDB(db))
|
2021-05-05 13:11:06 +02:00
|
|
|
return code
|
|
|
|
}
|
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
async function getACode(db: string, code: string, deleteCode = true) {
|
2021-05-05 16:10:28 +02:00
|
|
|
const client = await getClient(db)
|
|
|
|
const value = await client.get(code)
|
|
|
|
if (!value) {
|
|
|
|
throw "Invalid code."
|
|
|
|
}
|
|
|
|
if (deleteCode) {
|
|
|
|
await client.delete(code)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function init() {
|
|
|
|
pwResetClient = new redis.Client(redis.utils.Databases.PW_RESETS)
|
|
|
|
invitationClient = new redis.Client(redis.utils.Databases.INVITATIONS)
|
2021-08-05 10:59:08 +02:00
|
|
|
await pwResetClient.init()
|
|
|
|
await invitationClient.init()
|
2021-05-24 19:45:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* make sure redis connection is closed.
|
|
|
|
*/
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function shutdown() {
|
2021-05-28 11:09:32 +02:00
|
|
|
if (pwResetClient) await pwResetClient.finish()
|
|
|
|
if (invitationClient) await invitationClient.finish()
|
2022-08-25 20:41:47 +02:00
|
|
|
console.log("Redis shutdown")
|
2021-05-24 19:45:43 +02:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:11:06 +02:00
|
|
|
/**
|
|
|
|
* Given a user ID this will store a code (that is returned) for an hour in redis.
|
|
|
|
* The user can then return this code for resetting their password (through their reset link).
|
|
|
|
* @param {string} userId the ID of the user which is to be reset.
|
2022-05-23 16:03:52 +02:00
|
|
|
* @param {object} info Info about the user/the reset process.
|
2021-05-05 13:11:06 +02:00
|
|
|
* @return {Promise<string>} returns the code that was stored to redis.
|
|
|
|
*/
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function getResetPasswordCode(userId: string, info: any) {
|
|
|
|
return writeACode(redis.utils.Databases.PW_RESETS, { userId, info })
|
2021-05-05 13:11:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a reset code this will lookup to redis, check if the code is valid and delete if required.
|
|
|
|
* @param {string} resetCode The code provided via the email link.
|
2021-05-05 16:10:28 +02:00
|
|
|
* @param {boolean} deleteCode If the code is used/finished with this will delete it - defaults to true.
|
2021-05-05 13:11:06 +02:00
|
|
|
* @return {Promise<string>} returns the user ID if it is found
|
|
|
|
*/
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function checkResetPasswordCode(
|
|
|
|
resetCode: string,
|
|
|
|
deleteCode = true
|
|
|
|
) {
|
2021-05-05 16:10:28 +02:00
|
|
|
try {
|
2022-11-16 19:13:34 +01:00
|
|
|
return getACode(redis.utils.Databases.PW_RESETS, resetCode, deleteCode)
|
2021-05-05 16:10:28 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw "Provided information is not valid, cannot reset password - please try again."
|
2021-05-05 13:11:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an invitation code and writes it to redis - which can later be checked for user creation.
|
|
|
|
* @param {string} email the email address which the code is being sent to (for use later).
|
2021-05-24 19:45:43 +02:00
|
|
|
* @param {object|null} info Information to be carried along with the invitation.
|
2021-05-05 13:11:06 +02:00
|
|
|
* @return {Promise<string>} returns the code that was stored to redis.
|
|
|
|
*/
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function getInviteCode(email: string, info: any) {
|
|
|
|
return writeACode(redis.utils.Databases.INVITATIONS, { email, info })
|
2021-05-05 13:11:31 +02:00
|
|
|
}
|
2021-05-05 16:10:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the provided invite code is valid - will return the email address of user that was invited.
|
|
|
|
* @param {string} inviteCode the invite code that was provided as part of the link.
|
|
|
|
* @param {boolean} deleteCode whether or not the code should be deleted after retrieval - defaults to true.
|
2021-05-24 19:45:43 +02:00
|
|
|
* @return {Promise<object>} If the code is valid then an email address will be returned.
|
2021-05-05 16:10:28 +02:00
|
|
|
*/
|
2022-11-16 19:13:34 +01:00
|
|
|
export async function checkInviteCode(
|
|
|
|
inviteCode: string,
|
|
|
|
deleteCode: boolean = true
|
|
|
|
) {
|
2021-05-05 16:10:28 +02:00
|
|
|
try {
|
2022-11-16 19:13:34 +01:00
|
|
|
return getACode(redis.utils.Databases.INVITATIONS, inviteCode, deleteCode)
|
2021-05-05 16:10:28 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw "Invitation is not valid or has expired, please request a new one."
|
|
|
|
}
|
|
|
|
}
|