jr-common-micro 1.1.137 → 1.1.139
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/utils/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export * as entitlements from './entitlements';
|
|
|
4
4
|
export * as functions from './functions';
|
|
5
5
|
export * as logger_names from './logger_names';
|
|
6
6
|
export * as puppeteer_constants from './puppeteer_constants';
|
|
7
|
+
export * as session_revocation from './session_revocation';
|
|
7
8
|
export * as types from './types';
|
|
8
9
|
export * as walk_sync from './walk_sync';
|
package/dist/utils/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.walk_sync = exports.types = exports.puppeteer_constants = exports.logger_names = exports.functions = exports.entitlements = exports.constants = exports.badges = void 0;
|
|
3
|
+
exports.walk_sync = exports.types = exports.session_revocation = exports.puppeteer_constants = exports.logger_names = exports.functions = exports.entitlements = exports.constants = exports.badges = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
exports.badges = tslib_1.__importStar(require("./badges"));
|
|
6
6
|
exports.constants = tslib_1.__importStar(require("./constants"));
|
|
@@ -8,5 +8,6 @@ exports.entitlements = tslib_1.__importStar(require("./entitlements"));
|
|
|
8
8
|
exports.functions = tslib_1.__importStar(require("./functions"));
|
|
9
9
|
exports.logger_names = tslib_1.__importStar(require("./logger_names"));
|
|
10
10
|
exports.puppeteer_constants = tslib_1.__importStar(require("./puppeteer_constants"));
|
|
11
|
+
exports.session_revocation = tslib_1.__importStar(require("./session_revocation"));
|
|
11
12
|
exports.types = tslib_1.__importStar(require("./types"));
|
|
12
13
|
exports.walk_sync = tslib_1.__importStar(require("./walk_sync"));
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Redis, RedisOptions } from 'ioredis';
|
|
2
|
+
export declare const SESSION_REVOKED_CHANNEL = "session:revoked";
|
|
3
|
+
export declare const SESSION_CUTOFF_TTL_S: number;
|
|
4
|
+
export declare const REVOKED_SESSION_AUTHORIZATION = "Bearer revoked-session";
|
|
5
|
+
export type SessionCutoff = {
|
|
6
|
+
at: number;
|
|
7
|
+
keep: string | null;
|
|
8
|
+
};
|
|
9
|
+
export type SessionVerdict = 'ok' | 'revoked' | 'cutoff';
|
|
10
|
+
export type SessionRevokedEvent = {
|
|
11
|
+
user_id: string;
|
|
12
|
+
hash: string;
|
|
13
|
+
} | {
|
|
14
|
+
user_id: string;
|
|
15
|
+
at: number;
|
|
16
|
+
keep: string | null;
|
|
17
|
+
};
|
|
18
|
+
export type SessionClaims = {
|
|
19
|
+
sub: string | number;
|
|
20
|
+
iat?: number;
|
|
21
|
+
exp?: number;
|
|
22
|
+
};
|
|
23
|
+
export declare const sessionTokenHash: (token: string) => string;
|
|
24
|
+
export declare const sessionRevokedKey: (hash: string) => string;
|
|
25
|
+
export declare const sessionCutoffKey: (userId: string | number) => string;
|
|
26
|
+
export declare const bearerToken: (authorization: unknown) => string | null;
|
|
27
|
+
export declare const parseSessionCutoff: (raw: string | null | undefined) => SessionCutoff | null;
|
|
28
|
+
export declare const sessionVerdict: (hash: string, iat: number | undefined, revoked: boolean, cutoff: SessionCutoff | null) => SessionVerdict;
|
|
29
|
+
export declare const sessionEndedBy: (event: SessionRevokedEvent, hash: string, iat: number | undefined) => boolean;
|
|
30
|
+
export declare const parseSessionRevokedEvent: (raw: string) => SessionRevokedEvent | null;
|
|
31
|
+
export declare const checkSession: (redis: Redis, token: string, claims: SessionClaims) => Promise<SessionVerdict>;
|
|
32
|
+
export declare const revokeSession: (redis: Redis, token: string, claims: SessionClaims) => Promise<boolean>;
|
|
33
|
+
export declare const cutOffSessions: (redis: Redis, userId: string | number, keepToken?: string | null) => Promise<void>;
|
|
34
|
+
export declare const sessionRedisDb: () => number;
|
|
35
|
+
export declare const createSessionRedis: (options: Pick<RedisOptions, "host" | "port" | "username" | "password">, onError?: (error: Error) => void) => Redis;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSessionRedis = exports.sessionRedisDb = exports.cutOffSessions = exports.revokeSession = exports.checkSession = exports.parseSessionRevokedEvent = exports.sessionEndedBy = exports.sessionVerdict = exports.parseSessionCutoff = exports.bearerToken = exports.sessionCutoffKey = exports.sessionRevokedKey = exports.sessionTokenHash = exports.REVOKED_SESSION_AUTHORIZATION = exports.SESSION_CUTOFF_TTL_S = exports.SESSION_REVOKED_CHANNEL = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const ioredis_1 = require("ioredis");
|
|
6
|
+
exports.SESSION_REVOKED_CHANNEL = 'session:revoked';
|
|
7
|
+
exports.SESSION_CUTOFF_TTL_S = 91 * 24 * 60 * 60;
|
|
8
|
+
exports.REVOKED_SESSION_AUTHORIZATION = 'Bearer revoked-session';
|
|
9
|
+
const sessionTokenHash = (token) => (0, crypto_1.createHash)('sha256').update(token).digest('hex');
|
|
10
|
+
exports.sessionTokenHash = sessionTokenHash;
|
|
11
|
+
const sessionRevokedKey = (hash) => `session:revoked:${hash}`;
|
|
12
|
+
exports.sessionRevokedKey = sessionRevokedKey;
|
|
13
|
+
const sessionCutoffKey = (userId) => `session:cutoff:${userId}`;
|
|
14
|
+
exports.sessionCutoffKey = sessionCutoffKey;
|
|
15
|
+
const nowSeconds = () => Math.floor(Date.now() / 1000);
|
|
16
|
+
const bearerToken = (authorization) => {
|
|
17
|
+
if (typeof authorization !== 'string')
|
|
18
|
+
return null;
|
|
19
|
+
if (!authorization.startsWith('Bearer '))
|
|
20
|
+
return null;
|
|
21
|
+
const token = authorization.slice(7).trim();
|
|
22
|
+
return token || null;
|
|
23
|
+
};
|
|
24
|
+
exports.bearerToken = bearerToken;
|
|
25
|
+
const parseSessionCutoff = (raw) => {
|
|
26
|
+
if (!raw)
|
|
27
|
+
return null;
|
|
28
|
+
try {
|
|
29
|
+
const value = JSON.parse(raw);
|
|
30
|
+
const at = Number(value?.at);
|
|
31
|
+
if (!Number.isFinite(at) || at <= 0)
|
|
32
|
+
return null;
|
|
33
|
+
const keep = typeof value?.keep === 'string' ? value.keep : null;
|
|
34
|
+
return { at, keep };
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.parseSessionCutoff = parseSessionCutoff;
|
|
41
|
+
const sessionVerdict = (hash, iat, revoked, cutoff) => {
|
|
42
|
+
if (revoked)
|
|
43
|
+
return 'revoked';
|
|
44
|
+
if (cutoff && (iat ?? 0) <= cutoff.at && cutoff.keep !== hash) {
|
|
45
|
+
return 'cutoff';
|
|
46
|
+
}
|
|
47
|
+
return 'ok';
|
|
48
|
+
};
|
|
49
|
+
exports.sessionVerdict = sessionVerdict;
|
|
50
|
+
const sessionEndedBy = (event, hash, iat) => 'hash' in event
|
|
51
|
+
? event.hash === hash
|
|
52
|
+
: (0, exports.sessionVerdict)(hash, iat, false, { at: event.at, keep: event.keep }) !==
|
|
53
|
+
'ok';
|
|
54
|
+
exports.sessionEndedBy = sessionEndedBy;
|
|
55
|
+
const parseSessionRevokedEvent = (raw) => {
|
|
56
|
+
try {
|
|
57
|
+
const value = JSON.parse(raw);
|
|
58
|
+
const userId = String(value?.user_id ?? '');
|
|
59
|
+
if (!/^[1-9]\d{0,19}$/.test(userId))
|
|
60
|
+
return null;
|
|
61
|
+
if (typeof value.hash === 'string' && /^[0-9a-f]{64}$/.test(value.hash)) {
|
|
62
|
+
return { user_id: userId, hash: value.hash };
|
|
63
|
+
}
|
|
64
|
+
const cutoff = (0, exports.parseSessionCutoff)(raw);
|
|
65
|
+
return cutoff ? { user_id: userId, ...cutoff } : null;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
exports.parseSessionRevokedEvent = parseSessionRevokedEvent;
|
|
72
|
+
const checkSession = async (redis, token, claims) => {
|
|
73
|
+
const hash = (0, exports.sessionTokenHash)(token);
|
|
74
|
+
const [revoked, cutoff] = await redis.mget((0, exports.sessionRevokedKey)(hash), (0, exports.sessionCutoffKey)(claims.sub));
|
|
75
|
+
return (0, exports.sessionVerdict)(hash, claims.iat, revoked !== null, (0, exports.parseSessionCutoff)(cutoff));
|
|
76
|
+
};
|
|
77
|
+
exports.checkSession = checkSession;
|
|
78
|
+
const revokeSession = async (redis, token, claims) => {
|
|
79
|
+
const ttl = Math.floor(Number(claims.exp ?? 0)) - nowSeconds();
|
|
80
|
+
if (!(ttl > 0))
|
|
81
|
+
return false;
|
|
82
|
+
const hash = (0, exports.sessionTokenHash)(token);
|
|
83
|
+
await redis.set((0, exports.sessionRevokedKey)(hash), '1', 'EX', ttl);
|
|
84
|
+
const event = { user_id: String(claims.sub), hash };
|
|
85
|
+
await redis.publish(exports.SESSION_REVOKED_CHANNEL, JSON.stringify(event));
|
|
86
|
+
return true;
|
|
87
|
+
};
|
|
88
|
+
exports.revokeSession = revokeSession;
|
|
89
|
+
const cutOffSessions = async (redis, userId, keepToken) => {
|
|
90
|
+
const cutoff = {
|
|
91
|
+
at: nowSeconds(),
|
|
92
|
+
keep: keepToken ? (0, exports.sessionTokenHash)(keepToken) : null,
|
|
93
|
+
};
|
|
94
|
+
await redis.set((0, exports.sessionCutoffKey)(userId), JSON.stringify(cutoff), 'EX', exports.SESSION_CUTOFF_TTL_S);
|
|
95
|
+
const event = { user_id: String(userId), ...cutoff };
|
|
96
|
+
await redis.publish(exports.SESSION_REVOKED_CHANNEL, JSON.stringify(event));
|
|
97
|
+
};
|
|
98
|
+
exports.cutOffSessions = cutOffSessions;
|
|
99
|
+
const sessionRedisDb = () => {
|
|
100
|
+
const db = parseInt(process.env.SESSION_REDIS_DB ?? '', 10);
|
|
101
|
+
return Number.isInteger(db) && db >= 0 ? db : 0;
|
|
102
|
+
};
|
|
103
|
+
exports.sessionRedisDb = sessionRedisDb;
|
|
104
|
+
const createSessionRedis = (options, onError) => {
|
|
105
|
+
const redis = new ioredis_1.Redis({
|
|
106
|
+
host: options.host,
|
|
107
|
+
port: options.port,
|
|
108
|
+
username: options.username || undefined,
|
|
109
|
+
password: options.password || undefined,
|
|
110
|
+
db: (0, exports.sessionRedisDb)(),
|
|
111
|
+
maxRetriesPerRequest: 1,
|
|
112
|
+
commandTimeout: 250,
|
|
113
|
+
connectTimeout: 5000,
|
|
114
|
+
});
|
|
115
|
+
redis.on('error', (error) => onError?.(error));
|
|
116
|
+
return redis;
|
|
117
|
+
};
|
|
118
|
+
exports.createSessionRedis = createSessionRedis;
|