|
|
|
@ -21,8 +21,9 @@ const { |
|
|
|
isMultiTenant, |
|
|
|
} = require("@budibase/backend-core/tenancy") |
|
|
|
const env = require("../../../environment") |
|
|
|
import { users } from "@budibase/pro" |
|
|
|
|
|
|
|
const ssoCallbackUrl = async (config, type) => { |
|
|
|
const ssoCallbackUrl = async (config: any, type: any) => { |
|
|
|
// incase there is a callback URL from before
|
|
|
|
if (config && config.callbackURL) { |
|
|
|
return config.callbackURL |
|
|
|
@ -42,15 +43,15 @@ const ssoCallbackUrl = async (config, type) => { |
|
|
|
return `${publicConfig.platformUrl}${callbackUrl}` |
|
|
|
} |
|
|
|
|
|
|
|
exports.googleCallbackUrl = async config => { |
|
|
|
export const googleCallbackUrl = async (config: any) => { |
|
|
|
return ssoCallbackUrl(config, "google") |
|
|
|
} |
|
|
|
|
|
|
|
exports.oidcCallbackUrl = async config => { |
|
|
|
export const oidcCallbackUrl = async (config: any) => { |
|
|
|
return ssoCallbackUrl(config, "oidc") |
|
|
|
} |
|
|
|
|
|
|
|
async function authInternal(ctx, user, err = null, info = null) { |
|
|
|
async function authInternal(ctx: any, user: any, err = null, info = null) { |
|
|
|
if (err) { |
|
|
|
console.error("Authentication error", err) |
|
|
|
return ctx.throw(403, info ? info : "Unauthorized") |
|
|
|
@ -71,27 +72,30 @@ async function authInternal(ctx, user, err = null, info = null) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.authenticate = async (ctx, next) => { |
|
|
|
return passport.authenticate("local", async (err, user, info) => { |
|
|
|
await authInternal(ctx, user, err, info) |
|
|
|
ctx.status = 200 |
|
|
|
})(ctx, next) |
|
|
|
export const authenticate = async (ctx: any, next: any) => { |
|
|
|
return passport.authenticate( |
|
|
|
"local", |
|
|
|
async (err: any, user: any, info: any) => { |
|
|
|
await authInternal(ctx, user, err, info) |
|
|
|
ctx.status = 200 |
|
|
|
} |
|
|
|
)(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
exports.setInitInfo = ctx => { |
|
|
|
export const setInitInfo = (ctx: any) => { |
|
|
|
const initInfo = ctx.request.body |
|
|
|
setCookie(ctx, initInfo, Cookies.Init) |
|
|
|
ctx.status = 200 |
|
|
|
} |
|
|
|
|
|
|
|
exports.getInitInfo = ctx => { |
|
|
|
export const getInitInfo = (ctx: any) => { |
|
|
|
ctx.body = getCookie(ctx, Cookies.Init) || {} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Reset the user password, used as part of a forgotten password flow. |
|
|
|
*/ |
|
|
|
exports.reset = async ctx => { |
|
|
|
export const reset = async (ctx: any) => { |
|
|
|
const { email } = ctx.request.body |
|
|
|
const configured = await isEmailConfigured() |
|
|
|
if (!configured) { |
|
|
|
@ -121,7 +125,7 @@ exports.reset = async ctx => { |
|
|
|
/** |
|
|
|
* Perform the user password update if the provided reset code is valid. |
|
|
|
*/ |
|
|
|
exports.resetUpdate = async ctx => { |
|
|
|
export const resetUpdate = async (ctx: any) => { |
|
|
|
const { resetCode, password } = ctx.request.body |
|
|
|
try { |
|
|
|
const { userId } = await checkResetPasswordCode(resetCode) |
|
|
|
@ -137,14 +141,14 @@ exports.resetUpdate = async ctx => { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.logout = async ctx => { |
|
|
|
export const logout = async (ctx: any) => { |
|
|
|
if (ctx.user && ctx.user._id) { |
|
|
|
await platformLogout({ ctx, userId: ctx.user._id }) |
|
|
|
} |
|
|
|
ctx.body = { message: "User logged out." } |
|
|
|
} |
|
|
|
|
|
|
|
exports.datasourcePreAuth = async (ctx, next) => { |
|
|
|
export const datasourcePreAuth = async (ctx: any, next: any) => { |
|
|
|
const provider = ctx.params.provider |
|
|
|
const middleware = require(`@budibase/backend-core/middleware`) |
|
|
|
const handler = middleware.datasource[provider] |
|
|
|
@ -162,7 +166,7 @@ exports.datasourcePreAuth = async (ctx, next) => { |
|
|
|
return handler.preAuth(passport, ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
exports.datasourceAuth = async (ctx, next) => { |
|
|
|
export const datasourceAuth = async (ctx: any, next: any) => { |
|
|
|
const authStateCookie = getCookie(ctx, Cookies.DatasourceAuth) |
|
|
|
const provider = authStateCookie.provider |
|
|
|
const middleware = require(`@budibase/backend-core/middleware`) |
|
|
|
@ -174,7 +178,7 @@ exports.datasourceAuth = async (ctx, next) => { |
|
|
|
* 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. |
|
|
|
*/ |
|
|
|
exports.googlePreAuth = async (ctx, next) => { |
|
|
|
export const googlePreAuth = async (ctx: any, next: any) => { |
|
|
|
const db = getGlobalDB() |
|
|
|
|
|
|
|
const config = await core.db.getScopedConfig(db, { |
|
|
|
@ -182,14 +186,14 @@ exports.googlePreAuth = async (ctx, next) => { |
|
|
|
workspace: ctx.query.workspace, |
|
|
|
}) |
|
|
|
let callbackUrl = await exports.googleCallbackUrl(config) |
|
|
|
const strategy = await google.strategyFactory(config, callbackUrl) |
|
|
|
const strategy = await google.strategyFactory(config, callbackUrl, users.save) |
|
|
|
|
|
|
|
return passport.authenticate(strategy, { |
|
|
|
scope: ["profile", "email"], |
|
|
|
})(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
exports.googleAuth = async (ctx, next) => { |
|
|
|
export const googleAuth = async (ctx: any, next: any) => { |
|
|
|
const db = getGlobalDB() |
|
|
|
|
|
|
|
const config = await core.db.getScopedConfig(db, { |
|
|
|
@ -197,12 +201,12 @@ exports.googleAuth = async (ctx, next) => { |
|
|
|
workspace: ctx.query.workspace, |
|
|
|
}) |
|
|
|
const callbackUrl = await exports.googleCallbackUrl(config) |
|
|
|
const strategy = await google.strategyFactory(config, callbackUrl) |
|
|
|
const strategy = await google.strategyFactory(config, callbackUrl, users.save) |
|
|
|
|
|
|
|
return passport.authenticate( |
|
|
|
strategy, |
|
|
|
{ successRedirect: "/", failureRedirect: "/error" }, |
|
|
|
async (err, user, info) => { |
|
|
|
async (err: any, user: any, info: any) => { |
|
|
|
await authInternal(ctx, user, err, info) |
|
|
|
|
|
|
|
ctx.redirect("/") |
|
|
|
@ -210,14 +214,14 @@ exports.googleAuth = async (ctx, next) => { |
|
|
|
)(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
async function oidcStrategyFactory(ctx, configId) { |
|
|
|
async function oidcStrategyFactory(ctx: any, configId: any) { |
|
|
|
const db = getGlobalDB() |
|
|
|
const config = await core.db.getScopedConfig(db, { |
|
|
|
type: Configs.OIDC, |
|
|
|
group: ctx.query.group, |
|
|
|
}) |
|
|
|
|
|
|
|
const chosenConfig = config.configs.filter(c => c.uuid === configId)[0] |
|
|
|
const chosenConfig = config.configs.filter((c: any) => c.uuid === configId)[0] |
|
|
|
let callbackUrl = await exports.oidcCallbackUrl(chosenConfig) |
|
|
|
|
|
|
|
return oidc.strategyFactory(chosenConfig, callbackUrl) |
|
|
|
@ -227,7 +231,7 @@ async function oidcStrategyFactory(ctx, configId) { |
|
|
|
* The initial call that OIDC authentication makes to take you to the configured OIDC login screen. |
|
|
|
* On a successful login, you will be redirected to the oidcAuth callback route. |
|
|
|
*/ |
|
|
|
exports.oidcPreAuth = async (ctx, next) => { |
|
|
|
export const oidcPreAuth = async (ctx: any, next: any) => { |
|
|
|
const { configId } = ctx.params |
|
|
|
const strategy = await oidcStrategyFactory(ctx, configId) |
|
|
|
|
|
|
|
@ -239,14 +243,14 @@ exports.oidcPreAuth = async (ctx, next) => { |
|
|
|
})(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
exports.oidcAuth = async (ctx, next) => { |
|
|
|
export const oidcAuth = async (ctx: any, next: any) => { |
|
|
|
const configId = getCookie(ctx, Cookies.OIDC_CONFIG) |
|
|
|
const strategy = await oidcStrategyFactory(ctx, configId) |
|
|
|
|
|
|
|
return passport.authenticate( |
|
|
|
strategy, |
|
|
|
{ successRedirect: "/", failureRedirect: "/error" }, |
|
|
|
async (err, user, info) => { |
|
|
|
async (err: any, user: any, info: any) => { |
|
|
|
await authInternal(ctx, user, err, info) |
|
|
|
|
|
|
|
ctx.redirect("/") |