Skip to content

Commit

Permalink
Merge pull request #23 from adrianbrs/feature/oidc-provider-v8
Browse files Browse the repository at this point in the history
feature: add support for `oidc-provider@8`
  • Loading branch information
adrianbrs authored Oct 2, 2024
2 parents 35b1fbe + 52a0415 commit b455438
Show file tree
Hide file tree
Showing 54 changed files with 15,733 additions and 12,955 deletions.
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

0 comments on commit b455438

Please sign in to comment.