Skip to content

Commit b455438

Browse files
authored
Merge pull request #23 from adrianbrs/feature/oidc-provider-v8
feature: add support for `oidc-provider@8`
2 parents 35b1fbe + 52a0415 commit b455438

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+15733
-12955
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module.exports = {
88
extends: [
99
'plugin:@typescript-eslint/eslint-recommended',
1010
'plugin:@typescript-eslint/recommended',
11-
'prettier',
1211
],
1312
root: true,
1413
env: {

.github/workflows/test.yml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,43 @@ name: Continuous Integration
55

66
on:
77
push:
8-
branches: [ main ]
8+
branches: [main]
99
paths-ignore:
1010
- '**.md'
1111
pull_request:
12-
branches: [ main ]
12+
branches: [main]
1313
paths-ignore:
1414
- '**.md'
1515

1616
jobs:
1717
build:
18-
1918
runs-on: ubuntu-latest
2019

2120
strategy:
2221
matrix:
23-
node-version: [12.x, 14.x, 16.x, 18.x]
22+
node-version: [18.19.1, 20.11.1]
2423
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
2524

2625
steps:
27-
- name: Checkout
28-
uses: actions/checkout@v2
29-
- name: Use Node.js ${{ matrix.node-version }}
30-
uses: actions/setup-node@v2
31-
with:
32-
node-version: ${{ matrix.node-version }}
33-
cache: 'yarn'
34-
- name: Install dependencies
35-
run: yarn
36-
- name: Build
37-
run: yarn build
38-
- name: Test
39-
run: yarn test
40-
- name: Coveralls GitHub Action
41-
uses: coverallsapp/[email protected]
42-
with:
43-
github-token: ${{ secrets.GITHUB_TOKEN }}
44-
flag-name: Integration
45-
26+
- name: Checkout
27+
uses: actions/checkout@v2
28+
- name: Setup pnpm
29+
uses: pnpm/action-setup@v2
30+
with:
31+
version: 9.4.0
32+
- name: Use Node.js ${{ matrix.node-version }}
33+
uses: actions/setup-node@v2
34+
with:
35+
node-version: ${{ matrix.node-version }}
36+
cache: 'pnpm'
37+
- name: Install dependencies
38+
run: pnpm install
39+
- name: Build
40+
run: pnpm build
41+
- name: Test
42+
run: pnpm test
43+
- name: Coveralls GitHub Action
44+
uses: coverallsapp/[email protected]
45+
with:
46+
github-token: ${{ secrets.GITHUB_TOKEN }}
47+
flag-name: Integration

.prettierrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"trailingComma": "all",
33
"singleQuote": true,
4-
"arrowParens": "avoid"
4+
"arrowParens": "avoid",
5+
"quoteProps": "as-needed",
6+
"tabWidth": 2
57
}

.vscode/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
3+
"editor.formatOnSave": true,
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll": "explicit",
6+
"source.organizeImports": "explicit"
7+
},
8+
"eslint.format.enable": true,
9+
"typescript.tsdk": "node_modules/typescript/lib"
10+
}

README.md

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,24 @@ OR
2222
$ yarn add nest-oidc-provider oidc-provider
2323
```
2424

25+
OR
26+
27+
```bash
28+
$ pnpm add nest-oidc-provider oidc-provider
29+
```
30+
2531
## Setup
2632

33+
> ⚠️ Version 8 of `oidc-provider` [is now ESM-only](<https://github.com/panva/node-oidc-provider/releases/tag/v8.0.0#:~:text=tokens%20(cb67083)-,oidc%2Dprovider%20is%20now%20an%20ESM%2Donly%20module,-(3c5ebe1)>), which is not yet supported by NestJS natively ([nest#7021](https://github.com/nestjs/nest/issues/7021), [nest#8736](https://github.com/nestjs/nest/pull/8736)). This library enables the use of the ESM-only version of `oidc-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 the `oidc-provider` module and `@InjectOidcProvider()` for the running instance. **You must not import anything directly from** `oidc-provider`, unless you're using Node.js >= 20.17.x with the experimental `--experimental-require-module` flag ([#54447](https://github.com/nodejs/node/pull/54447))!
34+
35+
### TypeScript
36+
37+
You need to install the `oidc-provider` @types package if you want to use the re-exported types from this library.
38+
39+
```bash
40+
npm install @types/oidc-provider --save-dev
41+
```
42+
2743
### Basic configuration
2844

2945
```ts
@@ -49,8 +65,9 @@ You can pass a `factory` function to customize the provider instantiation.
4965
OidcModule.forRoot({
5066
issuer: 'http://localhost:3000',
5167
path: '/oidc',
52-
factory: (issuer, config) => {
53-
const provider = new oidc.Provider(issuer, config);
68+
factory: ({ issuer, config, module }) => {
69+
// `module` is the import from `oidc-provider`
70+
const provider = new module.Provider(issuer, config);
5471
provider.on('server_error', (ctx, err) => {...})
5572
return provider;
5673
},
@@ -117,6 +134,8 @@ export class AppModule {}
117134
Note that in this example, the `OidcConfigService` has to implement the `OidcModuleOptionsFactory` interface, as shown below.
118135

119136
```ts
137+
import type { OidcModuleOptionsFactory } from 'nest-oidc-provider';
138+
120139
@Injectable()
121140
export class OidcConfigService implements OidcModuleOptionsFactory {
122141
constructor(private readonly @InjectConnection() conn: Connection) {}
@@ -151,17 +170,63 @@ You can omit the `Adapter` option of oidc-provider configuration if you implemen
151170
export class AppModule {}
152171
```
153172

173+
## Custom injection decorators
174+
175+
To be able to access the exports of the `oidc-provider` module or the running instance, you need to use decorators or injection tokens:
176+
177+
```ts
178+
import {
179+
InjectOidcModule,
180+
InjectOidcProvider,
181+
type Provider,
182+
type ProviderModule,
183+
} from 'nest-oidc-provider';
184+
185+
@Controller('/some-controller')
186+
export class SomeController {
187+
constructor(
188+
/** Returns exports from the `oidc-provider` module */
189+
@InjectOidcModule() oidc: ProviderModule,
190+
/** Returns the running `oidc-provider` instance */
191+
@InjectOidcProvider() provider: Provider,
192+
) {}
193+
}
194+
```
195+
196+
OR
197+
198+
```ts
199+
import {
200+
OIDC_PROVIDER,
201+
OIDC_PROVIDER_MODULE,
202+
type Provider,
203+
type ProviderModule,
204+
} from 'nest-oidc-provider';
205+
206+
async function bootstrap() {
207+
const app = await NestFactory.create<NestExpressApplication>(AppModule);
208+
209+
const { Provider, errors, interactionPolicy } =
210+
app.get<ProviderModule>(OIDC_PROVIDER_MODULE);
211+
const provider = app.get<Provider>(OIDC_PROVIDER);
212+
213+
await app.listen(3000);
214+
}
215+
```
216+
154217
## Custom param decorators
155218

156-
### `@Oidc.Interaction()`
219+
### `@OidcInteraction()`
157220

158221
Returns an instance of `InteractionHelper` class.
159222

160223
```ts
224+
import { OidcInteraction, type InteractionHelper } from 'nest-oidc-provider';
225+
161226
@Get(':uid')
162227
@Render('login')
163228
async login(
164-
@Oidc.Interaction() interaction: InteractionHelper
229+
@OidcInteraction() interaction: InteractionHelper
165230
) {
166231
const { prompt, params, uid } = await interaction.details();
167232

@@ -189,19 +254,34 @@ interface InteractionHelper {
189254
}
190255
```
191256

192-
### `@Oidc.Context()`
257+
### `@OidcContext()`
193258

194259
Returns an instance of `KoaContextWithOIDC`.
195260

196261
```ts
262+
import { OidcContext, type KoaContextWithOIDC } from 'nest-oidc-provider';
263+
197264
@Get()
198-
async index(@Oidc.Context() ctx: KoaContextWithOIDC) {
265+
async index(@OidcContext() ctx: KoaContextWithOIDC) {
199266
const { oidc: { provider } } = ctx;
200267
const session = await provider.Session.get(ctx);
201268
//...
202269
}
203270
```
204271

272+
### `@OidcSession()`
273+
274+
Returns the user `Session` from the current context, equivalent to retrieving the session from `KoaContextWithOIDC`.
275+
276+
```ts
277+
import { OidcSession, type Session } from 'nest-oidc-provider';
278+
279+
@Get()
280+
async index(@OidcSession() session: Session) {
281+
//...
282+
}
283+
```
284+
205285
## Examples
206286

207287
A complete example can be found in the [example](example) directory.

example/package.json

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,39 @@
2424
"@nestjs/common": "^9.2.0",
2525
"@nestjs/core": "^9.2.0",
2626
"@nestjs/platform-express": "^9.2.0",
27-
"axios": "^0.25.0",
27+
"axios": "^1.6.7",
28+
"ejs": "^3.1.9",
2829
"lru-cache": "^6.0.0",
29-
"nest-oidc-provider": "^1.1.1",
30-
"oidc-provider": "^7.13.0",
31-
"query-string": "^7.1.0",
30+
"nest-oidc-provider": "2.0.0-next.3",
31+
"query-string": "^7.1.3",
3232
"reflect-metadata": "^0.1.13",
33-
"rimraf": "^3.0.2",
33+
"rimraf": "^5.0.5",
3434
"rxjs": "^7.5.2"
3535
},
3636
"devDependencies": {
3737
"@nestjs/cli": "^7.6.0",
38-
"@nestjs/schematics": "^8.0.5",
39-
"@nestjs/testing": "^9.2.0",
38+
"@nestjs/schematics": "^9.2.0",
39+
"@nestjs/testing": "^9.4.3",
40+
"@types/eslint": "^8.56.2",
4041
"@types/express": "^4.17.11",
41-
"@types/jest": "^27.4.0",
42+
"@types/jest": "^29.5.12",
4243
"@types/lru-cache": "^5.1.1",
43-
"@types/node": "^18.11.9",
44-
"@types/supertest": "^2.0.10",
45-
"@typescript-eslint/eslint-plugin": "^5.10.2",
46-
"@typescript-eslint/parser": "^5.10.2",
47-
"eslint": "^8.8.0",
48-
"eslint-config-prettier": "^8.1.0",
49-
"eslint-plugin-prettier": "^4.0.0",
50-
"jest": "^27.4.7",
51-
"prettier": "^2.2.1",
44+
"@types/node": "^20.11.19",
45+
"@types/oidc-provider": "^8.4.4",
46+
"@types/supertest": "^6.0.2",
47+
"@typescript-eslint/eslint-plugin": "^7.0.1",
48+
"@typescript-eslint/parser": "^7.0.1",
49+
"eslint": "^8.56.0",
50+
"eslint-config-prettier": "^9.1.0",
51+
"eslint-plugin-prettier": "^5.1.3",
52+
"jest": "^29.7.0",
53+
"prettier": "^3.2.5",
5254
"supertest": "^6.1.3",
53-
"ts-jest": "^27.1.3",
55+
"ts-jest": "^29.1.2",
5456
"ts-loader": "^9.2.6",
5557
"ts-node": "^10.4.0",
5658
"tsconfig-paths": "^3.9.0",
57-
"typescript": "^4.8.4"
59+
"typescript": "^5.3.3"
5860
},
5961
"jest": {
6062
"moduleFileExtensions": [

0 commit comments

Comments
 (0)