@ntalmagor/3drise-common 1.0.17 → 1.0.21
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/build/auth/tokens.d.ts +12 -0
- package/build/auth/tokens.js +35 -0
- package/build/database.js +29 -10
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/middlewares/current-user.d.ts +2 -8
- package/build/middlewares/current-user.js +2 -6
- package/package.json +1 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface TokenClaims {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
userName?: string;
|
|
5
|
+
role?: 'user' | 'admin';
|
|
6
|
+
}
|
|
7
|
+
export interface VerifiedClaims extends TokenClaims {
|
|
8
|
+
iat: number;
|
|
9
|
+
exp: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function signAccessToken(claims: TokenClaims): string;
|
|
12
|
+
export declare function verifyAccessToken(token: string): VerifiedClaims;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.signAccessToken = signAccessToken;
|
|
7
|
+
exports.verifyAccessToken = verifyAccessToken;
|
|
8
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
9
|
+
// Access-token TTL stays at 7d during 2B (no behavior change vs current).
|
|
10
|
+
// 2C will shorten this to '15m' once refresh-token rotation is wired up.
|
|
11
|
+
const ACCESS_TTL = '7d';
|
|
12
|
+
function signAccessToken(claims) {
|
|
13
|
+
if (process.env.JWT_PRIVATE_KEY) {
|
|
14
|
+
return jsonwebtoken_1.default.sign(claims, process.env.JWT_PRIVATE_KEY, {
|
|
15
|
+
algorithm: 'RS256',
|
|
16
|
+
expiresIn: ACCESS_TTL,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return jsonwebtoken_1.default.sign(claims, process.env.JWT_KEY, { expiresIn: ACCESS_TTL });
|
|
20
|
+
}
|
|
21
|
+
function verifyAccessToken(token) {
|
|
22
|
+
if (process.env.JWT_PUBLIC_KEY) {
|
|
23
|
+
try {
|
|
24
|
+
return jsonwebtoken_1.default.verify(token, process.env.JWT_PUBLIC_KEY, {
|
|
25
|
+
algorithms: ['RS256'],
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
catch (_a) {
|
|
29
|
+
// Fall through to HS256 — supports old cookies issued before the RS256 rollout.
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return jsonwebtoken_1.default.verify(token, process.env.JWT_KEY, {
|
|
33
|
+
algorithms: ['HS256'],
|
|
34
|
+
});
|
|
35
|
+
}
|
package/build/database.js
CHANGED
|
@@ -6,27 +6,46 @@ let _sequelize = null;
|
|
|
6
6
|
function getSequelize() {
|
|
7
7
|
if (!_sequelize) {
|
|
8
8
|
_sequelize = new sequelize_typescript_1.Sequelize({
|
|
9
|
-
database: process.env.RISE_MYSQL_DATABASE,
|
|
10
9
|
dialect: 'mysql',
|
|
10
|
+
database: process.env.RISE_MYSQL_DATABASE,
|
|
11
11
|
host: process.env.MYSQL_HOST,
|
|
12
12
|
username: process.env.RISE_MYSQL_USER,
|
|
13
13
|
password: process.env.RISE_MYSQL_PASSWORD,
|
|
14
14
|
port: parseInt(process.env.MYSQL_PORT || '3306'),
|
|
15
15
|
models: [],
|
|
16
16
|
logging: false,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
min: 0,
|
|
21
|
-
acquire: 30000,
|
|
22
|
-
idle: 10000,
|
|
17
|
+
define: {
|
|
18
|
+
timestamps: true,
|
|
19
|
+
underscored: false,
|
|
23
20
|
},
|
|
24
21
|
dialectOptions: {
|
|
25
|
-
connectTimeout:
|
|
22
|
+
connectTimeout: 120000, // 2 min for the initial TCP connect
|
|
23
|
+
},
|
|
24
|
+
pool: {
|
|
25
|
+
max: 15,
|
|
26
|
+
min: 0,
|
|
27
|
+
acquire: 120000, // 2 min to acquire a connection from the pool
|
|
28
|
+
idle: 30000, // close idle conns after 30s
|
|
29
|
+
evict: 5000, // sweep the pool every 5s
|
|
26
30
|
},
|
|
27
31
|
retry: {
|
|
28
|
-
max:
|
|
29
|
-
|
|
32
|
+
max: 5,
|
|
33
|
+
backoffBase: 1000, // 1s, doubles each retry (effectively)
|
|
34
|
+
backoffExponent: 1.5,
|
|
35
|
+
match: [
|
|
36
|
+
/ETIMEDOUT/,
|
|
37
|
+
/ECONNREFUSED/,
|
|
38
|
+
/ECONNRESET/,
|
|
39
|
+
/EAI_AGAIN/,
|
|
40
|
+
/PROTOCOL_CONNECTION_LOST/,
|
|
41
|
+
/SequelizeConnectionError/,
|
|
42
|
+
/SequelizeConnectionRefusedError/,
|
|
43
|
+
/SequelizeHostNotFoundError/,
|
|
44
|
+
/SequelizeHostNotReachableError/,
|
|
45
|
+
/SequelizeConnectionTimedOutError/,
|
|
46
|
+
/SequelizeConnectionAcquireTimeoutError/,
|
|
47
|
+
],
|
|
48
|
+
},
|
|
30
49
|
});
|
|
31
50
|
}
|
|
32
51
|
return _sequelize;
|
package/build/index.d.ts
CHANGED
|
@@ -30,6 +30,7 @@ export * from './middlewares/error-handler';
|
|
|
30
30
|
export * from './middlewares/require-auth';
|
|
31
31
|
export * from './middlewares/require-admin';
|
|
32
32
|
export * from './middlewares/validate-request';
|
|
33
|
+
export * from './auth/tokens';
|
|
33
34
|
export * from './events/base-listener';
|
|
34
35
|
export * from './events/base-publisher';
|
|
35
36
|
export * from './events/subjects';
|
package/build/index.js
CHANGED
|
@@ -70,6 +70,7 @@ __exportStar(require("./middlewares/error-handler"), exports);
|
|
|
70
70
|
__exportStar(require("./middlewares/require-auth"), exports);
|
|
71
71
|
__exportStar(require("./middlewares/require-admin"), exports);
|
|
72
72
|
__exportStar(require("./middlewares/validate-request"), exports);
|
|
73
|
+
__exportStar(require("./auth/tokens"), exports);
|
|
73
74
|
__exportStar(require("./events/base-listener"), exports);
|
|
74
75
|
__exportStar(require("./events/base-publisher"), exports);
|
|
75
76
|
__exportStar(require("./events/subjects"), exports);
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
email: string;
|
|
5
|
-
userName?: string;
|
|
6
|
-
role?: 'user' | 'admin';
|
|
7
|
-
iat: number;
|
|
8
|
-
exp: number;
|
|
9
|
-
}
|
|
2
|
+
import { VerifiedClaims } from '../auth/tokens';
|
|
3
|
+
export type UserPayload = VerifiedClaims;
|
|
10
4
|
declare module 'express-serve-static-core' {
|
|
11
5
|
interface Request {
|
|
12
6
|
currentUser?: UserPayload;
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.currentUser = void 0;
|
|
7
|
-
const
|
|
4
|
+
const tokens_1 = require("../auth/tokens");
|
|
8
5
|
const currentUser = (req, res, next) => {
|
|
9
6
|
var _a;
|
|
10
7
|
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
11
8
|
return next();
|
|
12
9
|
}
|
|
13
10
|
try {
|
|
14
|
-
|
|
15
|
-
req.currentUser = payload;
|
|
11
|
+
req.currentUser = (0, tokens_1.verifyAccessToken)(req.session.jwt);
|
|
16
12
|
}
|
|
17
13
|
catch (err) {
|
|
18
14
|
// Invalid or expired token — continue unauthenticated
|