dms-middleware-auth 1.0.10 → 1.1.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.
@@ -60,14 +60,12 @@ let AuthMiddleware = AuthMiddleware_1 = class AuthMiddleware {
60
60
  async onModuleInit() {
61
61
  const { publicKey, realm } = this.options;
62
62
  const isValid = await this.checkLicenceAndValidate(realm, publicKey);
63
- if (!isValid) {
64
- this.stopServer();
65
- }
66
63
  }
67
64
  async use(req, res, next) {
68
65
  const { publicKey, clientId, realm, bypassURL } = this.options;
69
66
  try {
70
67
  if (AuthMiddleware_1.licenceExpired) {
68
+ this.stopServer();
71
69
  return res.status(axios_1.HttpStatusCode.Forbidden).json({ message: AuthMiddleware_1.licenceExpiredMessage });
72
70
  }
73
71
  if (!AuthMiddleware_1.licenceValidatedUntilMs) {
@@ -267,7 +265,7 @@ let AuthMiddleware = AuthMiddleware_1 = class AuthMiddleware {
267
265
  if (!AuthMiddleware_1.licenceExpired) {
268
266
  AuthMiddleware_1.licenceExpired = true;
269
267
  }
270
- this.stopServer();
268
+ // this.stopServer();
271
269
  }
272
270
  scheduleLicenceShutdown(licEndMs) {
273
271
  if (!licEndMs) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dms-middleware-auth",
3
- "version": "1.0.10",
3
+ "version": "1.1.0",
4
4
  "description": "Reusable middleware for authentication and authorization in NestJS applications.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,4 +1,4 @@
1
- import { Inject, Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common';
1
+ import { Inject, Injectable, NestMiddleware, OnModuleInit } from '@nestjs/common';
2
2
  import { Response, NextFunction } from 'express';
3
3
  import * as jwt from 'jsonwebtoken';
4
4
  import axios, { HttpStatusCode } from 'axios';
@@ -16,44 +16,45 @@ interface AuthMiddlewareOptions {
16
16
  }
17
17
 
18
18
  @Injectable()
19
- export class AuthMiddleware implements NestMiddleware, OnModuleInit {
20
- private static licenceExpired = false;
21
- private static shutdownTimer: NodeJS.Timeout | null = null;
22
- private static licenceExpiryTimer: NodeJS.Timeout | null = null;
23
- private static licenceValidatedUntilMs: number | null = null;
24
- private static licenceValidationPromise: Promise<boolean> | null = null;
25
- private static shutdownInitiated = false;
26
- private static readonly licenceExpiredMessage = "server Licence is expired!. please renew";
27
-
28
- constructor(
29
- @Inject(CACHE_MANAGER) private cacheManager: Cache,
30
- @Inject('AUTH_MIDDLEWARE_OPTIONS') private readonly options: AuthMiddlewareOptions
31
- ) {}
32
-
33
- async onModuleInit() {
34
- const { publicKey, realm } = this.options;
35
- const isValid = await this.checkLicenceAndValidate(realm, publicKey);
36
- if (!isValid) {
37
- this.stopServer();
38
- }
39
- }
40
-
41
- async use(req: any, res: Response, next: NextFunction) {
19
+ export class AuthMiddleware implements NestMiddleware, OnModuleInit {
20
+ private static licenceExpired = false;
21
+ private static shutdownTimer: NodeJS.Timeout | null = null;
22
+ private static licenceExpiryTimer: NodeJS.Timeout | null = null;
23
+ private static licenceValidatedUntilMs: number | null = null;
24
+ private static licenceValidationPromise: Promise<boolean> | null = null;
25
+ private static shutdownInitiated = false;
26
+ private static readonly licenceExpiredMessage = "server Licence is expired!. please renew";
27
+
28
+ constructor(
29
+ @Inject(CACHE_MANAGER) private cacheManager: Cache,
30
+ @Inject('AUTH_MIDDLEWARE_OPTIONS') private readonly options: AuthMiddlewareOptions
31
+ ) {}
32
+
33
+ async onModuleInit() {
34
+ const { publicKey, realm } = this.options;
35
+ const isValid = await this.checkLicenceAndValidate(realm, publicKey);
36
+
37
+ }
38
+
39
+ async use(req: any, res: Response, next: NextFunction) {
42
40
 
43
41
 
44
42
  const { publicKey, clientId, realm, bypassURL } = this.options;
45
43
 
46
44
  try {
47
45
  if (AuthMiddleware.licenceExpired) {
46
+
47
+ this.stopServer();
48
+
48
49
  return res.status(HttpStatusCode.Forbidden).json({ message: AuthMiddleware.licenceExpiredMessage });
49
50
  }
50
51
 
51
- if (!AuthMiddleware.licenceValidatedUntilMs) {
52
- return res.status(HttpStatusCode.Forbidden).json({ message: AuthMiddleware.licenceExpiredMessage });
53
- }
54
- if (req.originalUrl == bypassURL) {
55
- return next();
56
- }
52
+ if (!AuthMiddleware.licenceValidatedUntilMs) {
53
+ return res.status(HttpStatusCode.Forbidden).json({ message: AuthMiddleware.licenceExpiredMessage });
54
+ }
55
+ if (req.originalUrl == bypassURL) {
56
+ return next();
57
+ }
57
58
  const authHeader = req.headers['authorization'];
58
59
 
59
60
  if (!authHeader || !authHeader.startsWith('Bearer ')) {
@@ -173,36 +174,36 @@ export class AuthMiddleware implements NestMiddleware, OnModuleInit {
173
174
  }
174
175
  }
175
176
 
176
- private async checkLicenceAndValidate(realm: string, publicKey:string) {
177
- try {
178
- if (AuthMiddleware.licenceExpired) {
179
- return false;
180
- }
181
- if (AuthMiddleware.licenceValidatedUntilMs && AuthMiddleware.licenceValidatedUntilMs > Date.now()) {
182
- return true;
183
- }
184
- if (AuthMiddleware.licenceValidationPromise) {
185
- return await AuthMiddleware.licenceValidationPromise;
186
- }
187
- AuthMiddleware.licenceValidationPromise = (async () => {
188
- const response:any = await this.getLicencingDetails(realm);
189
- if (response.code === HttpStatusCode.InternalServerError) {
190
- return false
191
- }
192
- else{
193
- const validate = await this.validateLicence(response.data, publicKey);
194
- return validate.status;
195
- }
196
- })();
197
- try {
198
- return await AuthMiddleware.licenceValidationPromise;
199
- } finally {
200
- AuthMiddleware.licenceValidationPromise = null;
201
- }
202
- }
203
- catch(error:any){
204
- return false;
205
-
177
+ private async checkLicenceAndValidate(realm: string, publicKey:string) {
178
+ try {
179
+ if (AuthMiddleware.licenceExpired) {
180
+ return false;
181
+ }
182
+ if (AuthMiddleware.licenceValidatedUntilMs && AuthMiddleware.licenceValidatedUntilMs > Date.now()) {
183
+ return true;
184
+ }
185
+ if (AuthMiddleware.licenceValidationPromise) {
186
+ return await AuthMiddleware.licenceValidationPromise;
187
+ }
188
+ AuthMiddleware.licenceValidationPromise = (async () => {
189
+ const response:any = await this.getLicencingDetails(realm);
190
+ if (response.code === HttpStatusCode.InternalServerError) {
191
+ return false
192
+ }
193
+ else{
194
+ const validate = await this.validateLicence(response.data, publicKey);
195
+ return validate.status;
196
+ }
197
+ })();
198
+ try {
199
+ return await AuthMiddleware.licenceValidationPromise;
200
+ } finally {
201
+ AuthMiddleware.licenceValidationPromise = null;
202
+ }
203
+ }
204
+ catch(error:any){
205
+ return false;
206
+
206
207
  }
207
208
  }
208
209
  private async getLicencingDetails(realm: string) {
@@ -234,81 +235,81 @@ export class AuthMiddleware implements NestMiddleware, OnModuleInit {
234
235
  };
235
236
  }
236
237
  }
237
- private async validateLicence(lic_data:any, publicKey:any){
238
- try{
239
- const token:any = await this.verifyJwt(lic_data, publicKey);
240
- const licEndMs = this.normalizeEpochMs(token?.lic_end);
241
- // Reject when licence end (epoch) is missing or already in the past.
242
- if (!licEndMs) {
243
- this.markLicenceExpired();
244
- return{
245
- status: false
246
- }
247
- }
248
- if (licEndMs <= Date.now()) {
249
- this.markLicenceExpired();
250
- return {
251
- status: false
252
- }
253
- } else {
254
- AuthMiddleware.licenceValidatedUntilMs = licEndMs;
255
- this.scheduleLicenceShutdown(licEndMs);
256
- return {
257
- status: true,
258
- ttl: (licEndMs - Date.now()) > 0 ? (licEndMs - Date.now()) : null
259
- }
260
- }
261
- }
262
- catch(error:any){
263
- this.markLicenceExpired();
264
- return {
265
- status: false
266
- };
267
- }
268
- }
269
-
270
- private markLicenceExpired() {
271
- if (!AuthMiddleware.licenceExpired) {
272
- AuthMiddleware.licenceExpired = true;
273
- }
274
- this.stopServer();
275
- }
276
-
277
- private scheduleLicenceShutdown(licEndMs: number) {
278
- if (!licEndMs) {
279
- return;
280
- }
281
- const delay = licEndMs - Date.now();
282
- if (delay <= 0) {
283
- this.markLicenceExpired();
284
- return;
285
- }
286
- if (AuthMiddleware.licenceExpiryTimer) {
287
- return;
288
- }
289
- AuthMiddleware.licenceExpiryTimer = setTimeout(() => {
290
- this.markLicenceExpired();
291
- }, delay);
292
- }
293
-
294
- private stopServer() {
295
- if (AuthMiddleware.shutdownInitiated) {
296
- return;
297
- }
298
- AuthMiddleware.shutdownInitiated = true;
299
- setTimeout(() => {
300
- try {
301
- process.kill(process.pid, 'SIGTERM');
302
- } catch {
303
- process.exit(1);
304
- }
305
- if (!AuthMiddleware.shutdownTimer) {
306
- AuthMiddleware.shutdownTimer = setTimeout(() => {
307
- process.exit(1);
308
- }, 5000);
309
- }
310
- }, 0);
311
- }
238
+ private async validateLicence(lic_data:any, publicKey:any){
239
+ try{
240
+ const token:any = await this.verifyJwt(lic_data, publicKey);
241
+ const licEndMs = this.normalizeEpochMs(token?.lic_end);
242
+ // Reject when licence end (epoch) is missing or already in the past.
243
+ if (!licEndMs) {
244
+ this.markLicenceExpired();
245
+ return{
246
+ status: false
247
+ }
248
+ }
249
+ if (licEndMs <= Date.now()) {
250
+ this.markLicenceExpired();
251
+ return {
252
+ status: false
253
+ }
254
+ } else {
255
+ AuthMiddleware.licenceValidatedUntilMs = licEndMs;
256
+ this.scheduleLicenceShutdown(licEndMs);
257
+ return {
258
+ status: true,
259
+ ttl: (licEndMs - Date.now()) > 0 ? (licEndMs - Date.now()) : null
260
+ }
261
+ }
262
+ }
263
+ catch(error:any){
264
+ this.markLicenceExpired();
265
+ return {
266
+ status: false
267
+ };
268
+ }
269
+ }
270
+
271
+ private markLicenceExpired() {
272
+ if (!AuthMiddleware.licenceExpired) {
273
+ AuthMiddleware.licenceExpired = true;
274
+ }
275
+ // this.stopServer();
276
+ }
277
+
278
+ private scheduleLicenceShutdown(licEndMs: number) {
279
+ if (!licEndMs) {
280
+ return;
281
+ }
282
+ const delay = licEndMs - Date.now();
283
+ if (delay <= 0) {
284
+ this.markLicenceExpired();
285
+ return;
286
+ }
287
+ if (AuthMiddleware.licenceExpiryTimer) {
288
+ return;
289
+ }
290
+ AuthMiddleware.licenceExpiryTimer = setTimeout(() => {
291
+ this.markLicenceExpired();
292
+ }, delay);
293
+ }
294
+
295
+ private stopServer() {
296
+ if (AuthMiddleware.shutdownInitiated) {
297
+ return;
298
+ }
299
+ AuthMiddleware.shutdownInitiated = true;
300
+ setTimeout(() => {
301
+ try {
302
+ process.kill(process.pid, 'SIGTERM');
303
+ } catch {
304
+ process.exit(1);
305
+ }
306
+ if (!AuthMiddleware.shutdownTimer) {
307
+ AuthMiddleware.shutdownTimer = setTimeout(() => {
308
+ process.exit(1);
309
+ }, 5000);
310
+ }
311
+ }, 0);
312
+ }
312
313
 
313
314
  private normalizeEpochMs(epoch: number): number | null {
314
315
  if (!epoch || Number.isNaN(epoch)) {