Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork19
oidc-provider module for Nest framework (node.js)
License
adrianbrs/nest-oidc-provider
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
oidc-provider module forNest framework (node.js)
$ npm i --save nest-oidc-provider oidc-provider
OR
$ yarn add nest-oidc-provider oidc-provider
OR
$ pnpm add nest-oidc-provider oidc-provider
⚠️ Version 8 ofoidc-provider
is 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)!
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
@Module({imports:[OidcModule.forRoot({issuer:'http://localhost:3000',path:'/oidc',oidc: ...// oidc-provider configuration})],})exportclassAppModule{}
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{}
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{}
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{}
@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{}
@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.
@Module({imports:[OidcModule.forRootAsync({imports:[OidcConfigModule],useExisting:OidcConfigService,}),],})exportclassAppModule{}
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);}
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>;}
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);//...}
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){//...}
A complete example can be found in theexample directory.
You are welcome to contribute to this project, just open a PR.
SeeCHANGELOG for more information.
This project isMIT licensed.
About
oidc-provider module for Nest framework (node.js)
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.