Load oidc config by id

This commit is contained in:
Rory Powell 2021-07-13 17:07:48 +01:00
parent 3fa4b21a31
commit dd306d59c5
4 changed files with 27 additions and 11 deletions

View File

@ -134,6 +134,8 @@ exports.googleAuth = async (ctx, next) => {
}
async function oidcStrategyFactory(ctx) {
const { configId } = ctx.params
const db = new CouchDB(GLOBAL_DB)
const config = await authPkg.db.getScopedConfig(db, {
@ -141,9 +143,11 @@ async function oidcStrategyFactory(ctx) {
group: ctx.query.group,
})
const callbackUrl = `${ctx.protocol}://${ctx.host}/api/admin/auth/oidc/callback`
const chosenConfig = config.configs.filter(c => c.uuid === configId)[0]
return oidc.strategyFactory(config, callbackUrl)
const callbackUrl = `${ctx.protocol}://${ctx.host}/api/admin/auth/oidc/callback/${configId}`
return oidc.strategyFactory(chosenConfig, callbackUrl)
}
/**

View File

@ -39,7 +39,7 @@ router
.post("/api/admin/auth/logout", authController.logout)
.get("/api/admin/auth/google", authController.googlePreAuth)
.get("/api/admin/auth/google/callback", authController.googleAuth)
.get("/api/admin/auth/oidc", authController.oidcPreAuth)
.get("/api/admin/auth/oidc/callback", authController.oidcAuth)
.get("/api/admin/auth/oidc/:configId", authController.oidcPreAuth)
.get("/api/admin/auth/oidc/callback/:configId", authController.oidcAuth)
module.exports = router

View File

@ -62,21 +62,25 @@ describe("/api/admin/auth", () => {
const passportSpy = jest.spyOn(auth.passport, "authenticate")
let oidcConf
let chosenConfig
let configId
beforeEach(async () => {
oidcConf = await config.saveOIDCConfig()
chosenConfig = oidcConf.config.configs[0]
configId = chosenConfig.uuid
})
afterEach(() => {
expect(strategyFactory).toBeCalledWith(
oidcConf.config,
"http://127.0.0.1:4003/api/admin/auth/oidc/callback" // calculated url
chosenConfig,
`http://127.0.0.1:4003/api/admin/auth/oidc/callback/${configId}` // calculated url
)
})
describe("/api/admin/auth/oidc", () => {
it("should load strategy and delegate to passport", async () => {
await request.get(`/api/admin/auth/oidc`)
await request.get(`/api/admin/auth/oidc/${configId}`)
expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
scope: ["profile", "email"],
@ -87,7 +91,7 @@ describe("/api/admin/auth", () => {
describe("/api/admin/auth/oidc/callback", () => {
it("should load strategy and delegate to passport", async () => {
await request.get(`/api/admin/auth/oidc/callback`)
await request.get(`/api/admin/auth/oidc/callback/${configId}`)
expect(passportSpy).toBeCalledWith(mockStrategyReturn, {
successRedirect: "/", failureRedirect: "/error"

View File

@ -6,6 +6,7 @@ const { Cookies } = require("@budibase/auth").constants
const { Configs, LOGO_URL } = require("../../../../constants")
const { getGlobalUserByEmail } = require("@budibase/auth").utils
const { createASession } = require("@budibase/auth/sessions")
const { newid } = require("../../../../../../auth/src/hashing")
class TestConfiguration {
constructor(openServer = true) {
@ -160,9 +161,16 @@ class TestConfiguration {
const config = {
type: Configs.OIDC,
config: {
configUrl: "http://someconfigurl",
clientID: "clientId",
clientSecret: "clientSecret",
configs: [
{
configUrl: "http://someconfigurl",
clientID: "clientId",
clientSecret: "clientSecret",
logo: "Microsoft",
name: "Active Directory",
uuid: newid(),
},
],
},
}