|
|
|
@ -1,49 +1,22 @@ |
|
|
|
const core = require("@budibase/backend-core") |
|
|
|
const { getScopedConfig } = require("@budibase/backend-core/db") |
|
|
|
const { google } = require("@budibase/backend-core/middleware") |
|
|
|
const { oidc } = require("@budibase/backend-core/middleware") |
|
|
|
const { Configs, EmailTemplatePurpose } = require("../../../constants") |
|
|
|
const { sendEmail, isEmailConfigured } = require("../../../utilities/email") |
|
|
|
const { setCookie, getCookie, clearCookie, hash, platformLogout } = core.utils |
|
|
|
const { Cookies, Headers } = core.constants |
|
|
|
const { passport } = core.auth |
|
|
|
const { passport, ssoCallbackUrl, google, oidc } = core.auth |
|
|
|
const { checkResetPasswordCode } = require("../../../utilities/redis") |
|
|
|
const { |
|
|
|
getGlobalDB, |
|
|
|
getTenantId, |
|
|
|
isMultiTenant, |
|
|
|
} = require("@budibase/backend-core/tenancy") |
|
|
|
const { getGlobalDB } = require("@budibase/backend-core/tenancy") |
|
|
|
const env = require("../../../environment") |
|
|
|
import { events, users as usersCore, context } from "@budibase/backend-core" |
|
|
|
import { users } from "../../../sdk" |
|
|
|
import { User } from "@budibase/types" |
|
|
|
|
|
|
|
const ssoCallbackUrl = async (config: any, type: any) => { |
|
|
|
// incase there is a callback URL from before
|
|
|
|
if (config && config.callbackURL) { |
|
|
|
return config.callbackURL |
|
|
|
} |
|
|
|
|
|
|
|
const db = getGlobalDB() |
|
|
|
const publicConfig = await getScopedConfig(db, { |
|
|
|
type: Configs.SETTINGS, |
|
|
|
}) |
|
|
|
|
|
|
|
let callbackUrl = `/api/global/auth` |
|
|
|
if (isMultiTenant()) { |
|
|
|
callbackUrl += `/${getTenantId()}` |
|
|
|
} |
|
|
|
callbackUrl += `/${type}/callback` |
|
|
|
|
|
|
|
return `${publicConfig.platformUrl}${callbackUrl}` |
|
|
|
} |
|
|
|
|
|
|
|
export const googleCallbackUrl = async (config: any) => { |
|
|
|
return ssoCallbackUrl(config, "google") |
|
|
|
return ssoCallbackUrl(getGlobalDB(), config, "google") |
|
|
|
} |
|
|
|
|
|
|
|
export const oidcCallbackUrl = async (config: any) => { |
|
|
|
return ssoCallbackUrl(config, "oidc") |
|
|
|
return ssoCallbackUrl(getGlobalDB(), config, "oidc") |
|
|
|
} |
|
|
|
|
|
|
|
async function authInternal(ctx: any, user: any, err = null, info = null) { |
|
|
|
@ -198,6 +171,8 @@ export const googlePreAuth = async (ctx: any, next: any) => { |
|
|
|
|
|
|
|
return passport.authenticate(strategy, { |
|
|
|
scope: ["profile", "email"], |
|
|
|
accessType: "offline", |
|
|
|
prompt: "consent", |
|
|
|
})(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
@ -224,7 +199,7 @@ export const googleAuth = async (ctx: any, next: any) => { |
|
|
|
)(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
async function oidcStrategyFactory(ctx: any, configId: any) { |
|
|
|
export const oidcStrategyFactory = async (ctx: any, configId: any) => { |
|
|
|
const db = getGlobalDB() |
|
|
|
const config = await core.db.getScopedConfig(db, { |
|
|
|
type: Configs.OIDC, |
|
|
|
@ -234,7 +209,12 @@ async function oidcStrategyFactory(ctx: any, configId: any) { |
|
|
|
const chosenConfig = config.configs.filter((c: any) => c.uuid === configId)[0] |
|
|
|
let callbackUrl = await exports.oidcCallbackUrl(chosenConfig) |
|
|
|
|
|
|
|
return oidc.strategyFactory(chosenConfig, callbackUrl, users.save) |
|
|
|
//Remote Config
|
|
|
|
const enrichedConfig = await oidc.fetchStrategyConfig( |
|
|
|
chosenConfig, |
|
|
|
callbackUrl |
|
|
|
) |
|
|
|
return oidc.strategyFactory(enrichedConfig, users.save) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -249,7 +229,7 @@ export const oidcPreAuth = async (ctx: any, next: any) => { |
|
|
|
|
|
|
|
return passport.authenticate(strategy, { |
|
|
|
// required 'openid' scope is added by oidc strategy factory
|
|
|
|
scope: ["profile", "email"], |
|
|
|
scope: ["profile", "email", "offline_access"], //auth0 offline_access scope required for the refresh token behaviour.
|
|
|
|
})(ctx, next) |
|
|
|
} |
|
|
|
|
|
|
|
|