Skip to content

Mounting oidc-provider

The following snippets show how a Provider instance can be mounted to existing applications with a path prefix /oidc.

// assumes fastify ^4.0.0
const fastify = new Fastify();
await fastify.register(require("@fastify/middie"));
// or
// await app.register(require('@fastify/express'));
fastify.use("/oidc", provider.callback());
// assumes @hapi/hapi ^21.0.0
const callback = provider.callback();
hapiApp.route({
path: `/oidc/{any*}`,
method: "*",
config: { payload: { output: "stream", parse: false } },
async handler({ raw: { req, res } }, h) {
req.originalUrl = req.url;
req.url = req.url.replace("/oidc", "");
callback(req, res);
await once(res, "finish");
req.url = req.url.replace("/", "/oidc");
delete req.originalUrl;
return res.writableEnded ? h.abandon : h.continue;
},
});
// assumes NestJS ^7.0.0
import { Controller, All, Req, Res } from "@nestjs/common";
import { Request, Response } from "express";
const callback = provider.callback();
@Controller("oidc")
export class OidcController {
@All("/*")
public mountedOidc(@Req() req: Request, @Res() res: Response): void {
req.url = req.originalUrl.replace("/oidc", "");
return callback(req, res);
}
}
// assumes express ^4.0.0 || ^5.0.0
expressApp.use("/oidc", provider.callback());
// assumes koa ^2.0.0 || ^3.0.0
// assumes koa-mount ^4.0.0
import mount from "koa-mount";
koaApp.use(mount("/oidc", provider));