Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

oidc-provider module for Nest framework (node.js)

License

NotificationsYou must be signed in to change notification settings

adrianbrs/nest-oidc-provider

Repository files navigation

NPM VersionnpmNPM LicenseCoverage StatusContinuous Integration

Description

oidc-provider module forNest framework (node.js)

Installation

$ npm i --save nest-oidc-provider oidc-provider

OR

$ yarn add nest-oidc-provider oidc-provider

OR

$ pnpm add nest-oidc-provider oidc-provider

Setup

⚠️ Version 8 ofoidc-provideris now ESM-only, which is not yet supported by NestJS natively (nest#7021,nest#8736). This library enables the use of the ESM-only version ofoidc-provider for Node.js <= 20.17.x via dynamic imports. To avoid errors like [ERR_REQUIRE_ESM], all interfaces should be imported from this package, and the module should be accessed through dependency injection. Use@InjectOidcModule() to inject theoidc-provider module and@InjectOidcProvider() for the running instance.You must not import anything directly fromoidc-provider, unless you're using Node.js >= 20.17.x with the experimental--experimental-require-module flag (#54447)!

TypeScript

You need to install theoidc-provider @types package if you want to use the re-exported types from this library.

npm install @types/oidc-provider --save-dev

Basic configuration

@Module({imports:[OidcModule.forRoot({issuer:'http://localhost:3000',path:'/oidc',oidc: ...// oidc-provider configuration})],})exportclassAppModule{}

Custom factory function

You can pass afactory function to customize the provider instantiation.

@Module({imports:[OidcModule.forRoot({issuer:'http://localhost:3000',path:'/oidc',factory:({ issuer, config, module})=>{// `module` is the import from `oidc-provider`constprovider=newmodule.Provider(issuer,config);provider.on('server_error',(ctx,err)=>{...})returnprovider;},oidc: ...// oidc-provider configuration})],})exportclassAppModule{}

Trusting TLS offloading proxies

You can set theproxy option totrue to trust TLS offloading proxies.
For more info visit theoidc-provider documentation:Trusting TLS offloading proxies

@Module({imports:[OidcModule.forRoot({issuer:'http://localhost:3000',path:'/oidc',proxy:true,// <- trust TLS offloading proxiesoidc:{...}})],})exportclassAppModule{}

Dynamic host routing

You can set thehost option to require the HTTP host of incoming requests to match some specific value before they are processed by theoidc-provider callback.

For more info visit the NestJS documentation:Sub-domain routing.

@Module({imports:[OidcModule.forRoot({issuer:'https://oidc.example.com',path:'/oidc',host:'oidc.example.com',// <- Use a specific host/subdomainoidc:{...}})],})exportclassAppModule{}

Async configuration

useFactory

@Module({imports:[OidcModule.forRootAsync({imports:[ConfigModule],useFactory:async(configService:ConfigService)=>({issuer:configService.get<string>('ISSUER'),path:configService.get<string>('OIDC_PATH'),oidc: ...// oidc-provider configuration}),inject:[ConfigService],}),],})exportclassAppModule{}

useClass

@Module({imports:[OidcModule.forRootAsync({useClass:OidcConfigService,}),],})exportclassAppModule{}

Note that in this example, theOidcConfigService has to implement theOidcModuleOptionsFactory interface, as shown below.

importtype{OidcModuleOptionsFactory}from'nest-oidc-provider';@Injectable()exportclassOidcConfigServiceimplementsOidcModuleOptionsFactory{constructor(privatereadonly @InjectConnection()conn:Connection){}createModuleOptions():OidcModuleOptions{return{issuer:'http://localhost:3001',path:'/oidc',oidc: ...,// oidc-provider configuration};}createAdapterFactory?():AdapterFactory{return(modelName:string)=>newMyAdapter(modelName,this.conn);}}

You can omit theAdapter option of oidc-provider configuration if you implement thecreateAdapterFactory method.

useExisting

@Module({imports:[OidcModule.forRootAsync({imports:[OidcConfigModule],useExisting:OidcConfigService,}),],})exportclassAppModule{}

Custom injection decorators

To be able to access the exports of theoidc-provider module or the running instance, you need to use decorators or injection tokens:

import{InjectOidcModule,InjectOidcProvider,typeProvider,typeProviderModule,}from'nest-oidc-provider';@Controller('/some-controller')exportclassSomeController{constructor(/** Returns exports from the `oidc-provider` module */    @InjectOidcModule()oidc:ProviderModule,/** Returns the running `oidc-provider` instance */    @InjectOidcProvider()provider:Provider,){}}

OR

import{OIDC_PROVIDER,OIDC_PROVIDER_MODULE,typeProvider,typeProviderModule,}from'nest-oidc-provider';asyncfunctionbootstrap(){constapp=awaitNestFactory.create<NestExpressApplication>(AppModule);const{ Provider, errors, interactionPolicy}=app.get<ProviderModule>(OIDC_PROVIDER_MODULE);constprovider=app.get<Provider>(OIDC_PROVIDER);awaitapp.listen(3000);}

Custom param decorators

@OidcInteraction()

Returns an instance ofInteractionHelper class.

import{OidcInteraction,typeInteractionHelper}from'nest-oidc-provider';@Get(':uid')@Render('login')asynclogin(  @OidcInteraction()interaction:InteractionHelper){const{ prompt, params, uid}=awaitinteraction.details();constclient=awaitthis.provider.Client.find(params.client_idasstring);return{ prompt, client, params, uid, ...};}

TheInteractionHelper class is just a helper that omits thereq andres parameters from the existing interaction methods inoidc-provider.

interfaceInteractionHelper{details():Promise<InteractionDetails>;finished(result:InteractionResults,options?:{mergeWithLastSubmission?:boolean},):Promise<void>;result(result:InteractionResults,options?:{mergeWithLastSubmission?:boolean},):Promise<string>;}

@OidcContext()

Returns an instance ofKoaContextWithOIDC.

import{OidcContext,typeKoaContextWithOIDC}from'nest-oidc-provider';@Get()asyncindex(@OidcContext()ctx:KoaContextWithOIDC){const{oidc:{ provider}}=ctx;constsession=awaitprovider.Session.get(ctx);//...}

@OidcSession()

Returns the userSession from the current context, equivalent to retrieving the session fromKoaContextWithOIDC.

import{OidcSession,typeSession}from'nest-oidc-provider';@Get()asyncindex(@OidcSession()session:Session){//...}

Examples

A complete example can be found in theexample directory.

Contributing

You are welcome to contribute to this project, just open a PR.

CHANGELOG

SeeCHANGELOG for more information.

License

This project isMIT licensed.


[8]ページ先頭

©2009-2025 Movatter.jp