Load oidc config by id

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

View File

@ -134,6 +134,8 @@ exports.googleAuth = async (ctx, next) => {
} }
async function oidcStrategyFactory(ctx) { async function oidcStrategyFactory(ctx) {
const { configId } = ctx.params
const db = new CouchDB(GLOBAL_DB) const db = new CouchDB(GLOBAL_DB)
const config = await authPkg.db.getScopedConfig(db, { const config = await authPkg.db.getScopedConfig(db, {
@ -141,9 +143,11 @@ async function oidcStrategyFactory(ctx) {
group: ctx.query.group, 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) .post("/api/admin/auth/logout", authController.logout)
.get("/api/admin/auth/google", authController.googlePreAuth) .get("/api/admin/auth/google", authController.googlePreAuth)
.get("/api/admin/auth/google/callback", authController.googleAuth) .get("/api/admin/auth/google/callback", authController.googleAuth)
.get("/api/admin/auth/oidc", authController.oidcPreAuth) .get("/api/admin/auth/oidc/:configId", authController.oidcPreAuth)
.get("/api/admin/auth/oidc/callback", authController.oidcAuth) .get("/api/admin/auth/oidc/callback/:configId", authController.oidcAuth)
module.exports = router module.exports = router

View File

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

View File

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