Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add support for oidc-provider@8 #23

Merged
merged 23 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b40ca5a
feat: add support for `oidc-provider@8`
adrianbrs Feb 19, 2024
ea4f896
chore: release v2.0.0-next.2
adrianbrs Feb 19, 2024
32daf5c
docs(example): update example to use @next release
adrianbrs Feb 19, 2024
d936a8e
fix: export only public constants
adrianbrs Feb 19, 2024
a3b1749
fix: add oidc-provider as a peer dependency
adrianbrs Feb 19, 2024
a653be3
docs(README): update docs to v2.0.0
adrianbrs Feb 19, 2024
04f185c
ci(test): update test workflow to use node >18 and pnpm
adrianbrs Feb 19, 2024
ee35454
ci(test): setup pnpm before setup node
adrianbrs Feb 19, 2024
10ec0d3
ci(test): fix pnpm install command
adrianbrs Feb 19, 2024
55d8d7c
ci(test): update nodejs version matrix
adrianbrs Feb 19, 2024
a86b2a1
chore: makes nodejs restriction more flexible
adrianbrs Feb 19, 2024
0862584
fix: use `eval` to skip dynamic import transpilation
adrianbrs Feb 19, 2024
01b52c3
test(e2e): use async/await and add --runInBand flag
adrianbrs Feb 19, 2024
757bf9e
chore: release v2.0.0-next.3
adrianbrs Feb 19, 2024
94a1f47
chore: fix reflect-metadata peer dependency version
adrianbrs Feb 19, 2024
90ac2a7
fix: remove duplicated oidc types
adrianbrs Feb 19, 2024
097c04c
build(example): update nest-oidc-provider to 2.0.0-next.3
adrianbrs Sep 30, 2024
7252228
docs(README): include a note about the `--experimental-require-module…
adrianbrs Oct 2, 2024
7a70e89
feat: add new `OidcSession` decorator
adrianbrs Oct 2, 2024
4532f12
build(deps): upgrade `oidc-provider` to version `8.5.1`
adrianbrs Oct 2, 2024
c710a49
test(e2e): fix `tsconfig.json` to compile to ESM
adrianbrs Oct 2, 2024
5b2e438
ci: upgrade pnpm version to `9.4.0`
adrianbrs Oct 2, 2024
52a0415
docs(README): add an example for the `OidcSession` decorator
adrianbrs Oct 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
root: true,
env: {
Expand Down
48 changes: 25 additions & 23 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,43 @@ name: Continuous Integration

on:
push:
branches: [ main ]
branches: [main]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
branches: [main]
paths-ignore:
- '**.md'

jobs:
build:

runs-on: ubuntu-latest

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

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- name: Install dependencies
run: yarn
- name: Build
run: yarn build
- name: Test
run: yarn test
- name: Coveralls GitHub Action
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: Integration

- name: Checkout
uses: actions/checkout@v2
- name: Setup pnpm
uses: pnpm/action-setup@v2
with:
version: 9.4.0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm build
- name: Test
run: pnpm test
- name: Coveralls GitHub Action
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
flag-name: Integration
4 changes: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"trailingComma": "all",
"singleQuote": true,
"arrowParens": "avoid"
"arrowParens": "avoid",
"quoteProps": "as-needed",
"tabWidth": 2
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"eslint.format.enable": true,
"typescript.tsdk": "node_modules/typescript/lib"
}
92 changes: 86 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,24 @@ OR
$ yarn add nest-oidc-provider oidc-provider
```

OR

```bash
$ pnpm add nest-oidc-provider oidc-provider
```

## Setup

> ⚠️ 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))!

### TypeScript

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

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

### Basic configuration

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

```ts
import type { OidcModuleOptionsFactory } from 'nest-oidc-provider';

@Injectable()
export class OidcConfigService implements OidcModuleOptionsFactory {
constructor(private readonly @InjectConnection() conn: Connection) {}
Expand Down Expand Up @@ -151,17 +170,63 @@ You can omit the `Adapter` option of oidc-provider configuration if you implemen
export class AppModule {}
```

## Custom injection decorators

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

```ts
import {
InjectOidcModule,
InjectOidcProvider,
type Provider,
type ProviderModule,
} from 'nest-oidc-provider';

@Controller('/some-controller')
export class SomeController {
constructor(
/** Returns exports from the `oidc-provider` module */
@InjectOidcModule() oidc: ProviderModule,
/** Returns the running `oidc-provider` instance */
@InjectOidcProvider() provider: Provider,
) {}
}
```

OR

```ts
import {
OIDC_PROVIDER,
OIDC_PROVIDER_MODULE,
type Provider,
type ProviderModule,
} from 'nest-oidc-provider';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);

const { Provider, errors, interactionPolicy } =
app.get<ProviderModule>(OIDC_PROVIDER_MODULE);
const provider = app.get<Provider>(OIDC_PROVIDER);

await app.listen(3000);
}
```

## Custom param decorators

### `@Oidc.Interaction()`
### `@OidcInteraction()`

Returns an instance of `InteractionHelper` class.

```ts
import { OidcInteraction, type InteractionHelper } from 'nest-oidc-provider';

@Get(':uid')
@Render('login')
async login(
@Oidc.Interaction() interaction: InteractionHelper
@OidcInteraction() interaction: InteractionHelper
) {
const { prompt, params, uid } = await interaction.details();

Expand Down Expand Up @@ -189,19 +254,34 @@ interface InteractionHelper {
}
```

### `@Oidc.Context()`
### `@OidcContext()`

Returns an instance of `KoaContextWithOIDC`.

```ts
import { OidcContext, type KoaContextWithOIDC } from 'nest-oidc-provider';

@Get()
async index(@Oidc.Context() ctx: KoaContextWithOIDC) {
async index(@OidcContext() ctx: KoaContextWithOIDC) {
const { oidc: { provider } } = ctx;
const session = await provider.Session.get(ctx);
//...
}
```

### `@OidcSession()`

Returns the user `Session` from the current context, equivalent to retrieving the session from `KoaContextWithOIDC`.

```ts
import { OidcSession, type Session } from 'nest-oidc-provider';

@Get()
async index(@OidcSession() session: Session) {
//...
}
```

## Examples

A complete example can be found in the [example](example) directory.
Expand Down
40 changes: 21 additions & 19 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,39 @@
"@nestjs/common": "^9.2.0",
"@nestjs/core": "^9.2.0",
"@nestjs/platform-express": "^9.2.0",
"axios": "^0.25.0",
"axios": "^1.6.7",
"ejs": "^3.1.9",
"lru-cache": "^6.0.0",
"nest-oidc-provider": "^1.1.1",
"oidc-provider": "^7.13.0",
"query-string": "^7.1.0",
"nest-oidc-provider": "2.0.0-next.3",
"query-string": "^7.1.3",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rimraf": "^5.0.5",
"rxjs": "^7.5.2"
},
"devDependencies": {
"@nestjs/cli": "^7.6.0",
"@nestjs/schematics": "^8.0.5",
"@nestjs/testing": "^9.2.0",
"@nestjs/schematics": "^9.2.0",
"@nestjs/testing": "^9.4.3",
"@types/eslint": "^8.56.2",
"@types/express": "^4.17.11",
"@types/jest": "^27.4.0",
"@types/jest": "^29.5.12",
"@types/lru-cache": "^5.1.1",
"@types/node": "^18.11.9",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^5.10.2",
"@typescript-eslint/parser": "^5.10.2",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.4.7",
"prettier": "^2.2.1",
"@types/node": "^20.11.19",
"@types/oidc-provider": "^8.4.4",
"@types/supertest": "^6.0.2",
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"supertest": "^6.1.3",
"ts-jest": "^27.1.3",
"ts-jest": "^29.1.2",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.8.4"
"typescript": "^5.3.3"
},
"jest": {
"moduleFileExtensions": [
Expand Down
Loading
Loading