1
0
Fork 0
mirror of synced 2024-06-01 18:20:18 +12:00
budibase/packages/worker/src/api/controllers/admin/auth.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

const authPkg = require("@budibase/auth")
2021-04-22 08:08:04 +12:00
const { google } = require("@budibase/auth/src/middleware")
const { Configs } = require("../../../constants")
const CouchDB = require("../../../db")
const { sendEmail, isEmailConfigured } = require("../../../utilities/email")
const { clearCookie, getGlobalUserByEmail } = authPkg.utils
2021-04-23 02:27:09 +12:00
const { Cookies } = authPkg.constants
const { passport } = authPkg.auth
2021-04-23 00:46:54 +12:00
const GLOBAL_DB = authPkg.StaticDatabases.GLOBAL.name
function authInternal(ctx, user, err = null) {
if (err) {
return ctx.throw(403, "Unauthorized")
}
2021-04-07 22:33:16 +12:00
const expires = new Date()
expires.setDate(expires.getDate() + 1)
2021-04-08 02:15:05 +12:00
if (!user) {
return ctx.throw(403, "Unauthorized")
}
2021-04-12 21:47:48 +12:00
ctx.cookies.set(Cookies.Auth, user.token, {
expires,
path: "/",
httpOnly: false,
overwrite: true,
})
}
exports.authenticate = async (ctx, next) => {
return passport.authenticate("local", async (err, user) => {
2021-04-29 01:28:25 +12:00
authInternal(ctx, user, err)
2021-04-08 02:15:05 +12:00
2021-04-12 21:47:48 +12:00
delete user.token
ctx.body = { user }
})(ctx, next)
}
2021-04-11 22:35:55 +12:00
/**
* Reset the user password, used as part of a forgotten password flow.
*/
exports.reset = async ctx => {
const { email } = ctx.request.body
const configured = await isEmailConfigured()
if (!configured) {
throw "Please contact your platform administrator, SMTP is not configured."
}
try {
const user = await getGlobalUserByEmail(email)
sendEmail()
} catch (err) {
// don't throw any kind of error to the user, this might give away something
}
ctx.body = {
2021-05-05 23:11:31 +12:00
message: "If user exists an email has been sent.",
}
}
2021-04-14 00:56:28 +12:00
exports.logout = async ctx => {
clearCookie(ctx, Cookies.Auth)
2021-04-16 03:49:35 +12:00
ctx.body = { message: "User logged out" }
2021-04-14 00:56:28 +12:00
}
2021-04-22 22:45:22 +12:00
/**
* The initial call that google authentication makes to take you to the google login screen.
* On a successful login, you will be redirected to the googleAuth callback route.
*/
2021-04-22 08:08:04 +12:00
exports.googlePreAuth = async (ctx, next) => {
2021-04-23 00:46:54 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-04-23 02:27:09 +12:00
const config = await authPkg.db.determineScopedConfig(db, {
2021-04-22 22:45:22 +12:00
type: Configs.GOOGLE,
group: ctx.query.group,
})
2021-04-23 00:46:54 +12:00
const strategy = await google.strategyFactory(config)
2021-04-22 08:08:04 +12:00
return passport.authenticate(strategy, {
scope: ["profile", "email"],
})(ctx, next)
}
2021-04-22 05:40:32 +12:00
2021-04-21 23:12:22 +12:00
exports.googleAuth = async (ctx, next) => {
2021-04-23 00:46:54 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-04-23 02:27:09 +12:00
const config = await authPkg.db.determineScopedConfig(db, {
2021-04-22 22:48:37 +12:00
type: Configs.GOOGLE,
group: ctx.query.group,
})
2021-04-23 00:46:54 +12:00
const strategy = await google.strategyFactory(config)
2021-04-22 08:08:04 +12:00
2021-04-21 23:12:22 +12:00
return passport.authenticate(
2021-04-22 08:08:04 +12:00
strategy,
{ successRedirect: "/", failureRedirect: "/error" },
2021-04-21 23:12:22 +12:00
async (err, user) => {
authInternal(ctx, user, err)
2021-04-21 23:12:22 +12:00
ctx.redirect("/")
}
)(ctx, next)
2021-04-11 22:35:55 +12:00
}