@team-supercharge/oasg 14.1.0-feature-python-fastapi-e25c0a08.0 → 15.0.0-feature-nestjs-auth-guard-d8ac30be.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -821,6 +821,11 @@ describe('Auth', function () {
821
821
  | packageName | Name of the generated NPM package | Y | - |
822
822
  | repository | URL of the NPM package registry | Y | - |
823
823
 
824
+ ##### Authentication
825
+ The package generates an AuthGuard and applies to endpoints, where a security scheme is defined.
826
+ // TODO - rest of the description and how to use it
827
+
828
+
824
829
  **Known limitations:**
825
830
  - array of enums in query/header/path/form parameters are not validated (stay as string)
826
831
  - no multidimensional array validation in DTOs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@team-supercharge/oasg",
3
- "version": "14.1.0-feature-python-fastapi-e25c0a08.0",
3
+ "version": "15.0.0-feature-nestjs-auth-guard-d8ac30be.0",
4
4
  "description": "Node-based tool to lint OpenAPI documents and generate clients, servers and documentation from them",
5
5
  "author": "Supercharge",
6
6
  "license": "MIT",
@@ -15,6 +15,9 @@
15
15
  "pipes.ts": {
16
16
  "templateType": "SupportingFiles"
17
17
  },
18
+ "auth.guard.ts": {
19
+ "templateType": "SupportingFiles"
20
+ },
18
21
  "exceptions.ts": {
19
22
  "templateType": "SupportingFiles"
20
23
  },
@@ -5,11 +5,12 @@
5
5
  import { Get, Post, Put, Delete, Patch, Options, Head } from '@nestjs/common';
6
6
  import { Query, Param, Headers, Body } from '@nestjs/common';
7
7
  import { HttpCode, Request } from '@nestjs/common';
8
- import { UseInterceptors, UploadedFile, StreamableFile } from '@nestjs/common';
8
+ import { UseInterceptors, UseGuards, UploadedFile, StreamableFile } from '@nestjs/common';
9
9
  import { FileInterceptor } from '@nestjs/platform-express';
10
10
 
11
11
  import { OptionalParseIntPipe, OptionalParseFloatPipe, OptionalParseBoolPipe, OptionalParseEnumPipe, RequiredPipe } from '../pipes';
12
12
  import { ApiParseArrayPipe, ApiValidationPipe } from '../pipes';
13
+ import { AuthGuard, AuthSchemes } from '../auth.guard';
13
14
 
14
15
  {{#imports}}
15
16
  // @ts-ignore
@@ -31,19 +32,22 @@ export abstract class {{classname}} {
31
32
  // ||||||||||
32
33
  /**
33
34
  {{#summary}}
34
- * {{.}}
35
+ * {{.}}
35
36
  {{/summary}}
36
37
  {{#notes}}
37
- * {{.}}
38
+ * {{.}}
38
39
  {{/notes}}
39
-
40
40
  {{#allParams}}
41
- * @param {{paramName}} {{description}}
41
+ * @param {{paramName}} {{description}}
42
42
  {{/allParams}}
43
43
  {{#isDeprecated}}
44
- * @deprecated
44
+ * @deprecated
45
45
  {{/isDeprecated}}
46
- */
46
+ */
47
+
48
+ {{#authMethods}}{{#-first}}
49
+ @AuthSchemes([{{#authMethods}}'{{name}}'{{^-last}}, {{/-last}}{{/authMethods}}])
50
+ @UseGuards(AuthGuard) {{/-first}}{{/authMethods}}
47
51
  //// @{{httpMethod}}('{{path}}'){{#isMultipart}}
48
52
  @UseInterceptors(FileInterceptor({{#formParams}}{{#isFile}}'{{paramName}}'{{/isFile}}{{/formParams}})){{/isMultipart}}
49
53
  @HttpCode({{#responses.0}}{{code}}{{/responses.0}})
@@ -0,0 +1,38 @@
1
+ import {
2
+ CanActivate,
3
+ ExecutionContext,
4
+ Inject,
5
+ Injectable,
6
+ } from "@nestjs/common";
7
+ import { Reflector } from "@nestjs/core";
8
+ import { Observable } from "rxjs";
9
+
10
+ export const AuthSchemes = Reflector.createDecorator<string[]>();
11
+
12
+ export interface AuthServiceInterface {
13
+ validate: (
14
+ scheme: string,
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ request: any
17
+ ) => boolean | Promise<boolean> | Observable<boolean>;
18
+ }
19
+
20
+ export const AUTH_SERVICE_TOKEN = "OASG_AUTH_SERVICE";
21
+
22
+ @Injectable()
23
+ export class AuthGuard implements CanActivate {
24
+ constructor(
25
+ @Inject(AUTH_SERVICE_TOKEN)
26
+ private readonly authService: AuthServiceInterface,
27
+ private readonly reflector: Reflector
28
+ ) {}
29
+
30
+ canActivate(
31
+ context: ExecutionContext
32
+ ): boolean | Promise<boolean> | Observable<boolean> {
33
+ const schemes = this.reflector.get(AuthSchemes, context.getHandler());
34
+ const request = context.switchToHttp().getRequest();
35
+
36
+ return schemes.some((scheme) => this.authService.validate(scheme, request));
37
+ }
38
+ }
@@ -13,10 +13,9 @@
13
13
  },
14
14
  "main": "dist/index.js",
15
15
  "peerDependencies": {
16
- "@nestjs/common": "^9.0.0 || ^10.0.0",
17
- "@nestjs/core": "^9.0.0 || ^10.0.0",
18
- "@nestjs/platform-express": "^9.0.0 || ^10.0.0",
19
- "@types/validator": "13.11.7"
16
+ "@nestjs/common": "^10.0.0",
17
+ "@nestjs/core": "^10.0.0",
18
+ "@nestjs/platform-express": "^10.0.0",
20
19
  },
21
20
  "dependencies": {
22
21
  "class-transformer": "^0.5.1",