@nathapp/nestjs-auth 1.0.0-rc.6 → 1.0.0-rc.8

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.
@@ -1,2 +1,2 @@
1
- import { JwtModule } from "./jwt-module";
2
- export { JwtModule, };
1
+ import { DefaultJwtModuleOptionsFactory, JwtModule, JwtModuleOptions, JwtModuleOptionsFactory, JwtModuleOptionsWithoutProvider } from "./jwt-module";
2
+ export { JwtModule, JwtModuleOptions, JwtModuleOptionsFactory, JwtModuleOptionsWithoutProvider, DefaultJwtModuleOptionsFactory, };
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JwtModule = void 0;
3
+ exports.DefaultJwtModuleOptionsFactory = exports.JwtModule = void 0;
4
4
  const jwt_module_1 = require("./jwt-module");
5
+ Object.defineProperty(exports, "DefaultJwtModuleOptionsFactory", { enumerable: true, get: function () { return jwt_module_1.DefaultJwtModuleOptionsFactory; } });
5
6
  Object.defineProperty(exports, "JwtModule", { enumerable: true, get: function () { return jwt_module_1.JwtModule; } });
@@ -1,11 +1,13 @@
1
1
  import { DynamicModule, ModuleMetadata, Type } from '@nestjs/common';
2
2
  import { AuthProvider } from '../auth-provider';
3
3
  import { JwtOptions, JwtStrategyProvider } from '../strategy';
4
+ import { ConfigService } from '@nestjs/config';
4
5
  export declare class JwtModule {
5
6
  static forRoot(options: JwtModuleOptions): DynamicModule;
6
7
  static forRootAsync(options: JwtModuleAsyncOptions): DynamicModule;
7
8
  private static sanitizeJwtOptions;
8
9
  private static createStrategyProvider;
10
+ private static createAsyncOptionsProviders;
9
11
  private static createAsyncOptionsProvider;
10
12
  }
11
13
  export type JwtModuleOptionsWithoutProvider = Omit<JwtModuleOptions, 'authProvider' | 'strategyProvider'>;
@@ -27,3 +29,10 @@ export interface JwtModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
27
29
  strategyProvider?: Type<JwtStrategyProvider>;
28
30
  inject?: any[];
29
31
  }
32
+ export declare class DefaultJwtModuleOptionsFactory implements JwtModuleOptionsFactory {
33
+ private readonly configService;
34
+ private readonly extraOptions;
35
+ constructor(configService: ConfigService);
36
+ private parseOption;
37
+ createOptions(): JwtModuleOptionsWithoutProvider | Promise<JwtModuleOptionsWithoutProvider>;
38
+ }
@@ -5,13 +5,18 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
5
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
7
7
  };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
8
11
  var JwtModule_1;
9
12
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.JwtModule = void 0;
11
- const common_1 = require("@nestjs/common");
13
+ exports.DefaultJwtModuleOptionsFactory = exports.JwtModule = void 0;
14
+ const common_1 = require("@nathapp/common");
15
+ const common_2 = require("@nestjs/common");
12
16
  const auth_provider_1 = require("../auth-provider");
13
17
  const module_constant_1 = require("../constant/module-constant");
14
18
  const strategy_1 = require("../strategy");
19
+ const config_1 = require("@nestjs/config");
15
20
  const defaultOptions = {
16
21
  logging: true,
17
22
  authProvider: auth_provider_1.SimpleAuthProvider,
@@ -37,7 +42,7 @@ let JwtModule = JwtModule_1 = class JwtModule {
37
42
  }
38
43
  static forRootAsync(options) {
39
44
  options = this.sanitizeJwtOptions(options);
40
- const asyncOptionsProvider = this.createAsyncOptionsProvider(options);
45
+ const asyncOptionsProviders = this.createAsyncOptionsProviders(options);
41
46
  const authProvider = {
42
47
  provide: module_constant_1.AUTH_PROVIDER,
43
48
  useClass: options.authProvider,
@@ -46,7 +51,7 @@ let JwtModule = JwtModule_1 = class JwtModule {
46
51
  return {
47
52
  module: JwtModule_1,
48
53
  imports: options.imports || [],
49
- providers: [asyncOptionsProvider, authProvider, strategyProvider],
54
+ providers: [...asyncOptionsProviders, authProvider, strategyProvider],
50
55
  exports: [module_constant_1.JWT_OPTIONS, strategyProvider],
51
56
  };
52
57
  }
@@ -71,6 +76,18 @@ let JwtModule = JwtModule_1 = class JwtModule {
71
76
  };
72
77
  }
73
78
  }
79
+ static createAsyncOptionsProviders(options) {
80
+ if (options.useExisting || options.useFactory) {
81
+ return [this.createAsyncOptionsProvider(options)];
82
+ }
83
+ return [
84
+ this.createAsyncOptionsProvider(options),
85
+ {
86
+ provide: options.useClass,
87
+ useClass: options.useClass,
88
+ },
89
+ ];
90
+ }
74
91
  static createAsyncOptionsProvider(options) {
75
92
  if (options.useFactory) {
76
93
  return {
@@ -89,6 +106,50 @@ let JwtModule = JwtModule_1 = class JwtModule {
89
106
  }
90
107
  };
91
108
  JwtModule = JwtModule_1 = __decorate([
92
- (0, common_1.Module)({})
109
+ (0, common_2.Module)({})
93
110
  ], JwtModule);
94
111
  exports.JwtModule = JwtModule;
112
+ let DefaultJwtModuleOptionsFactory = class DefaultJwtModuleOptionsFactory {
113
+ constructor(configService) {
114
+ this.configService = configService;
115
+ this.extraOptions = {
116
+ bindIp: false,
117
+ checkBlacklisted: false,
118
+ allowDuplicateLogin: true,
119
+ ignoreExpiration: false,
120
+ passReqToCallback: true,
121
+ };
122
+ this.extraOptions.bindIp = this.parseOption('JWT_ACCESS_BINDIP', false);
123
+ this.extraOptions.checkBlacklisted = this.parseOption('JWT_ACCESS_CHECK_BLACKLISTED', false);
124
+ this.extraOptions.allowDuplicateLogin = this.parseOption('JWT_ACCESS_ALLOW_DUPLICATE_LOGIN', true);
125
+ this.extraOptions.ignoreExpiration = this.parseOption('JWT_ACCESS_IGNORE_EXPIRATION', false);
126
+ this.extraOptions.passReqToCallback = this.parseOption('JWT_ACCESS_PASS_REQ_TO_CALLBACK', true);
127
+ }
128
+ parseOption(key, defaultValue) {
129
+ const str = this.configService.get(key);
130
+ if ((0, common_1.isUndefinedOrNullOrEmpty)(str)) {
131
+ return defaultValue;
132
+ }
133
+ return str.toLowerCase() == 'true' || str == '1';
134
+ }
135
+ createOptions() {
136
+ return {
137
+ jwtOptions: {
138
+ secret: process.env.JWT_ACCESS_SECERT,
139
+ publicKey: process.env.JWT_ACCESS_PUBLICKEY,
140
+ privateKey: process.env.JWT_ACCESS_PRIVATEKEY,
141
+ passphrase: process.env.JWT_ACCESS_PASSPHRASE,
142
+ signOption: {
143
+ expiresIn: process.env.JWT_ACCESS_EXPIRE,
144
+ algorithm: process.env.JWT_ACCESS_ALGORITHM,
145
+ },
146
+ ...this.extraOptions,
147
+ },
148
+ };
149
+ }
150
+ };
151
+ DefaultJwtModuleOptionsFactory = __decorate([
152
+ (0, common_2.Injectable)(),
153
+ __metadata("design:paramtypes", [config_1.ConfigService])
154
+ ], DefaultJwtModuleOptionsFactory);
155
+ exports.DefaultJwtModuleOptionsFactory = DefaultJwtModuleOptionsFactory;
@@ -4,7 +4,7 @@ import { JwtFromRequestFunction } from "passport-jwt";
4
4
  import { Request } from "express";
5
5
  import { AuthProvider, IPrincipal } from "../auth-provider";
6
6
  import * as jwt from 'jsonwebtoken';
7
- export interface JwtOptions extends jOption {
7
+ export interface JwtNestJsOptions {
8
8
  bindIp?: boolean;
9
9
  allowDuplicateLogin?: boolean;
10
10
  checkBlacklisted?: boolean;
@@ -12,6 +12,7 @@ export interface JwtOptions extends jOption {
12
12
  passReqToCallback?: boolean;
13
13
  jwtFromRequestFunction?: JwtFromRequestFunction;
14
14
  }
15
+ export declare type JwtOptions = JwtNestJsOptions & jOption;
15
16
  export interface StrategyProvider {
16
17
  validate(req: Request, payload: any): IPrincipal | Promise<IPrincipal>;
17
18
  sign(payload: string | object | Buffer): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-auth",
3
- "version": "1.0.0-rc.6",
3
+ "version": "1.0.0-rc.8",
4
4
  "description": "nestjs-auth",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && tsc -p tsconfig.json",
@@ -38,6 +38,7 @@
38
38
  },
39
39
  "peerDependencies": {
40
40
  "@nestjs/common": "*",
41
+ "@nestjs/config": "*",
41
42
  "@nestjs/core": "*",
42
43
  "@nestjs/passport": "*",
43
44
  "class-transformer": "*",