Mounting oidc-provider
This content is for v8.x. Switch to the latest version for up-to-date documentation.
The following snippets show how a Provider instance can be mounted to existing applications with a
path prefix /oidc.
Note: if you mount oidc-provider to a path it’s likely you will have to also update the
interactions.url configuration to reflect the new path.
to a connect application
Section titled “to a connect application”// assumes connect ^3.0.0connectApp.use('/oidc', oidc.callback())to a fastify application
Section titled “to a fastify application”// assumes fastify ^4.0.0const fastify = new Fastify()await fastify.register(require('@fastify/middie'))// or// await app.register(require('@fastify/express'));fastify.use('/oidc', oidc.callback())to a hapi application
Section titled “to a hapi application”// assumes @hapi/hapi ^21.0.0const callback = oidc.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 },})to a nest application
Section titled “to a nest application”// assumes NestJS ^7.0.0import { Controller, All, Req, Res } from '@nestjs/common'import { Request, Response } from 'express'const callback = oidc.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) }}to an express application
Section titled “to an express application”// assumes express ^4.0.0expressApp.use('/oidc', oidc.callback())to a koa application
Section titled “to a koa application”// assumes koa ^2.0.0// assumes koa-mount ^4.0.0import mount from 'koa-mount'koaApp.use(mount('/oidc', oidc.app))Note: when the issuer identifier does not include the path prefix you should take care of rewriting
your ${root}/.well-known/openid-configuration to ${root}${prefix}/.well-known/openid-configuration
so that your deployment remains conform to the
Discovery 1.0
specification.