tsledge 0.1.15 → 0.1.17
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/dist/index.js +24 -13
- package/dist/middleware/authentication/session.d.ts +4 -18
- package/dist/middleware/authentication/session.d.ts.map +1 -1
- package/dist/middleware/authentication/validation.d.ts +14 -12
- package/dist/middleware/authentication/validation.d.ts.map +1 -1
- package/dist/src/index.js +1028 -0
- package/dist/tests/main.js +1038 -0
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -200,10 +200,10 @@ var AuthTokenBlocklistModel = mongoose3.model(
|
|
|
200
200
|
var FORBIDDEN = 403;
|
|
201
201
|
var UNAUTHORIZED = 401;
|
|
202
202
|
async function jwtRequired(req, res, next) {
|
|
203
|
-
|
|
203
|
+
await validateJwt(req, res, next, JwtSecret);
|
|
204
204
|
}
|
|
205
205
|
async function jwtRefreshRequired(req, res, next) {
|
|
206
|
-
|
|
206
|
+
await validateJwt(req, res, next, JwtRefreshSecret);
|
|
207
207
|
}
|
|
208
208
|
async function verifyToken(token, jwtSecret) {
|
|
209
209
|
try {
|
|
@@ -341,12 +341,14 @@ async function generateCredentials(auth) {
|
|
|
341
341
|
async function authRegister(req, res, next) {
|
|
342
342
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
343
343
|
if (!identifier || !secret) {
|
|
344
|
-
|
|
344
|
+
res.sendStatus(FORBIDDEN2);
|
|
345
|
+
return;
|
|
345
346
|
}
|
|
346
347
|
identifier = identifier.toLowerCase();
|
|
347
348
|
let user = await AuthUserModel.findOne({ identifier });
|
|
348
349
|
if (user) {
|
|
349
|
-
|
|
350
|
+
res.sendStatus(BAD_REQUEST);
|
|
351
|
+
return;
|
|
350
352
|
}
|
|
351
353
|
res.locals.authUser = new AuthUserModel({
|
|
352
354
|
identifier,
|
|
@@ -357,23 +359,28 @@ async function authRegister(req, res, next) {
|
|
|
357
359
|
async function authLogin(req, res, next) {
|
|
358
360
|
let { identifier = void 0, secret = void 0 } = req.body || {};
|
|
359
361
|
if (!identifier || !secret) {
|
|
360
|
-
|
|
362
|
+
res.sendStatus(FORBIDDEN2);
|
|
363
|
+
return;
|
|
361
364
|
}
|
|
362
365
|
identifier = identifier.toLowerCase();
|
|
363
366
|
let user = await AuthUserModel.findOne({ identifier }).select("+secretHash");
|
|
364
367
|
if (!user || !user.secretHash) {
|
|
365
|
-
|
|
368
|
+
res.sendStatus(BAD_REQUEST);
|
|
369
|
+
return;
|
|
366
370
|
}
|
|
367
371
|
if (user.blockedSince) {
|
|
368
|
-
|
|
372
|
+
res.sendStatus(FORBIDDEN2);
|
|
373
|
+
return;
|
|
369
374
|
}
|
|
370
375
|
let isMatch = await bcrypt.compare(secret, user.secretHash);
|
|
371
376
|
if (!isMatch) {
|
|
372
|
-
|
|
377
|
+
res.sendStatus(BAD_REQUEST);
|
|
378
|
+
return;
|
|
373
379
|
}
|
|
374
380
|
let credentials = await generateCredentials(user);
|
|
375
381
|
if (!credentials) {
|
|
376
|
-
|
|
382
|
+
res.sendStatus(BAD_REQUEST);
|
|
383
|
+
return;
|
|
377
384
|
}
|
|
378
385
|
res.locals.credentials = credentials;
|
|
379
386
|
next();
|
|
@@ -382,7 +389,8 @@ async function authLogout(req, res, next) {
|
|
|
382
389
|
await jwtRefreshRequired(req, res, async () => {
|
|
383
390
|
const refreshToken = res.locals.token;
|
|
384
391
|
if (!refreshToken) {
|
|
385
|
-
|
|
392
|
+
res.sendStatus(BAD_REQUEST);
|
|
393
|
+
return;
|
|
386
394
|
}
|
|
387
395
|
const decoded = jwt2.decode(refreshToken);
|
|
388
396
|
const jti = decoded?.jti;
|
|
@@ -411,7 +419,8 @@ async function authRefresh(req, res, next) {
|
|
|
411
419
|
});
|
|
412
420
|
const refreshToken = res.locals.token;
|
|
413
421
|
if (!refreshToken) {
|
|
414
|
-
|
|
422
|
+
res.sendStatus(BAD_REQUEST);
|
|
423
|
+
return;
|
|
415
424
|
}
|
|
416
425
|
try {
|
|
417
426
|
const decoded = jwt2.decode(refreshToken);
|
|
@@ -438,13 +447,15 @@ async function authRefresh(req, res, next) {
|
|
|
438
447
|
const payload = jwt2.verify(refreshToken, JwtRefreshSecret);
|
|
439
448
|
let credentials = await generateCredentials(payload);
|
|
440
449
|
if (!credentials) {
|
|
441
|
-
|
|
450
|
+
res.sendStatus(BAD_REQUEST);
|
|
451
|
+
return;
|
|
442
452
|
}
|
|
443
453
|
res.locals.credentials = credentials;
|
|
444
454
|
next();
|
|
445
455
|
} catch (err) {
|
|
446
456
|
console.log("[WARN] refreshing JWT:", err);
|
|
447
|
-
|
|
457
|
+
res.sendStatus(BAD_REQUEST);
|
|
458
|
+
return;
|
|
448
459
|
}
|
|
449
460
|
}
|
|
450
461
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Request, Response } from 'express';
|
|
2
2
|
import { JWTCredentials, AuthUserPayload } from './types';
|
|
3
3
|
import { AuthUserDocument } from '../../models';
|
|
4
4
|
declare const router: import("express-serve-static-core").Router;
|
|
@@ -14,11 +14,7 @@ export declare function authRegister(req: Request, res: Response & {
|
|
|
14
14
|
locals: {
|
|
15
15
|
authUser: AuthUserDocument;
|
|
16
16
|
};
|
|
17
|
-
}, next: any): Promise<
|
|
18
|
-
locals: {
|
|
19
|
-
authUser: AuthUserDocument;
|
|
20
|
-
};
|
|
21
|
-
}) | undefined>;
|
|
17
|
+
}, next: any): Promise<void>;
|
|
22
18
|
/**
|
|
23
19
|
* Handles user login by validating credentials and generating JWT tokens.
|
|
24
20
|
* Passes data in ``res.locals.credentials`` for the next middleware to use.
|
|
@@ -31,11 +27,7 @@ export declare function authLogin(req: Request, res: Response & {
|
|
|
31
27
|
locals: {
|
|
32
28
|
credentials: JWTCredentials;
|
|
33
29
|
};
|
|
34
|
-
}, next: any): Promise<
|
|
35
|
-
locals: {
|
|
36
|
-
credentials: JWTCredentials;
|
|
37
|
-
};
|
|
38
|
-
}) | undefined>;
|
|
30
|
+
}, next: any): Promise<void>;
|
|
39
31
|
/**
|
|
40
32
|
* Handles user logout by invalidating the provided refresh token and optionally the access token.
|
|
41
33
|
* JWTRefresh Token is required
|
|
@@ -63,12 +55,6 @@ export declare function authRefresh(req: Request, res: Response & {
|
|
|
63
55
|
token: string;
|
|
64
56
|
credentials: JWTCredentials;
|
|
65
57
|
};
|
|
66
|
-
}, next: any): Promise<
|
|
67
|
-
locals: {
|
|
68
|
-
user: AuthUserPayload;
|
|
69
|
-
token: string;
|
|
70
|
-
credentials: JWTCredentials;
|
|
71
|
-
};
|
|
72
|
-
}) | undefined>;
|
|
58
|
+
}, next: any): Promise<void>;
|
|
73
59
|
export default router;
|
|
74
60
|
//# sourceMappingURL=session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/middleware/authentication/session.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../../src/middleware/authentication/session.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI1D,OAAO,EAAE,gBAAgB,EAA0C,MAAM,cAAc,CAAC;AAExF,QAAA,MAAM,MAAM,4CAAmB,CAAC;AA2ChC;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,GAAG;IAAE,MAAM,EAAE;QAAE,QAAQ,EAAE,gBAAgB,CAAA;KAAE,CAAA;CAAE,EAC1D,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,IAAI,CAAC,CAkBf;AAED;;;;;;;GAOG;AACH,wBAAsB,SAAS,CAC7B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,GAAG;IAAE,MAAM,EAAE;QAAE,WAAW,EAAE,cAAc,CAAA;KAAE,CAAA;CAAE,EAC3D,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,IAAI,CAAC,CA4Bf;AAED;;;;;;GAMG;AACH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,GAAG;IAAE,MAAM,EAAE;QAAE,IAAI,EAAE,eAAe,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,EACpE,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,IAAI,CAAC,CA4Bf;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,GAAG;IAAE,MAAM,EAAE;QAAE,IAAI,EAAE,eAAe,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,cAAc,CAAA;KAAE,CAAA;CAAE,EACjG,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,IAAI,CAAC,CA0Cf;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
2
|
import { AuthUserPayload } from './types';
|
|
3
3
|
export interface TokenVerificationResult {
|
|
4
|
+
/**
|
|
5
|
+
* Indicates if the token is valid (signature is correct, not blocked, and user is not blocked).
|
|
6
|
+
*/
|
|
4
7
|
isTokenValid: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Indicates if the token is expired.
|
|
10
|
+
*/
|
|
5
11
|
isTokenExpired: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Indicates if the user associated with the token is blocked.
|
|
14
|
+
*/
|
|
6
15
|
isUserBlocked: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The decoded payload from the JWT token, which should contain user information.
|
|
18
|
+
*/
|
|
7
19
|
payload: AuthUserPayload | any;
|
|
8
20
|
}
|
|
9
21
|
/**
|
|
@@ -19,12 +31,7 @@ export declare function jwtRequired(req: Request, res: Response & {
|
|
|
19
31
|
user: AuthUserPayload;
|
|
20
32
|
token: string;
|
|
21
33
|
};
|
|
22
|
-
}, next: any): Promise<
|
|
23
|
-
locals: {
|
|
24
|
-
user: AuthUserPayload;
|
|
25
|
-
token: string;
|
|
26
|
-
};
|
|
27
|
-
}) | undefined>;
|
|
34
|
+
}, next: any): Promise<void>;
|
|
28
35
|
/**
|
|
29
36
|
* Express middleware to require a valid refresh JWT token for access. Checks the token against the blocklist and user status.
|
|
30
37
|
* Adding user and access token to ``res.locals.user`` and ``res.locals.token``
|
|
@@ -38,12 +45,7 @@ export declare function jwtRefreshRequired(req: Request, res: Response & {
|
|
|
38
45
|
user: AuthUserPayload;
|
|
39
46
|
token: string;
|
|
40
47
|
};
|
|
41
|
-
}, next: any): Promise<
|
|
42
|
-
locals: {
|
|
43
|
-
user: AuthUserPayload;
|
|
44
|
-
token: string;
|
|
45
|
-
};
|
|
46
|
-
}) | undefined>;
|
|
48
|
+
}, next: any): Promise<void>;
|
|
47
49
|
/**
|
|
48
50
|
* Verifies a JWT token and checks for blocklist and user status.
|
|
49
51
|
* @param token
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../../src/middleware/authentication/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI1C,MAAM,WAAW,uBAAuB;IACtC,YAAY,EAAE,OAAO,CAAC;IACtB,cAAc,EAAE,OAAO,CAAC;IACxB,aAAa,EAAE,OAAO,CAAC;IACvB,OAAO,EAAE,eAAe,GAAG,GAAG,CAAC;CAChC;AAKD;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,GAAG;IAAE,MAAM,EAAE;QAAE,IAAI,EAAE,eAAe,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA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