@betterinternship/core 2.28.0 → 2.30.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.
- package/README.md +26 -26
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/auth/db.d.ts +2 -0
- package/dist/lib/auth/db.js +2 -0
- package/dist/lib/auth/db.js.map +1 -0
- package/dist/lib/auth/events.d.ts +21 -0
- package/dist/lib/auth/events.js +21 -0
- package/dist/lib/auth/events.js.map +1 -0
- package/dist/lib/auth/god-otp.d.ts +8 -0
- package/dist/lib/auth/god-otp.js +84 -0
- package/dist/lib/auth/god-otp.js.map +1 -0
- package/dist/lib/auth/index.d.ts +4 -0
- package/dist/lib/auth/index.js +5 -0
- package/dist/lib/auth/index.js.map +1 -0
- package/dist/lib/auth/limits.d.ts +10 -0
- package/dist/lib/auth/limits.js +15 -0
- package/dist/lib/auth/limits.js.map +1 -0
- package/dist/lib/auth/sessions.d.ts +28 -0
- package/dist/lib/auth/sessions.js +233 -0
- package/dist/lib/auth/sessions.js.map +1 -0
- package/dist/lib/email/action.js +3 -3
- package/dist/lib/email/preview.js +20 -20
- package/dist/lib/email/shell.js +22 -22
- package/dist/lib/forms/field-preset-templates.js +8 -8
- package/dist/lib/forms/form-metadata.d.ts +1 -0
- package/dist/lib/forms/form-metadata.js +9 -4
- package/dist/lib/forms/form-metadata.js.map +1 -1
- package/dist/lib/forms/index.d.ts +1 -0
- package/dist/lib/forms/index.js +1 -0
- package/dist/lib/forms/index.js.map +1 -1
- package/dist/lib/forms/recipient-email.d.ts +7 -0
- package/dist/lib/forms/recipient-email.js +37 -0
- package/dist/lib/forms/recipient-email.js.map +1 -0
- package/dist/lib/pdf-viewer/form-previewer-rendering.js +7 -7
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +91 -86
package/README.md
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
# Package.Core
|
|
2
|
-
|
|
3
|
-
Contains dependencies shared between the different services of the site.
|
|
4
|
-
|
|
5
|
-
## How to update
|
|
6
|
-
|
|
7
|
-
1. Build the package:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm run build
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
2. Commit the build:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
git add .
|
|
17
|
-
git commit -m "<follow conventional commits, k thanks>"
|
|
18
|
-
git push
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
3. Update the version and publish the package to NPM:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm version <patch|minor|major>
|
|
25
|
-
npm publish
|
|
26
|
-
```
|
|
1
|
+
# Package.Core
|
|
2
|
+
|
|
3
|
+
Contains dependencies shared between the different services of the site.
|
|
4
|
+
|
|
5
|
+
## How to update
|
|
6
|
+
|
|
7
|
+
1. Build the package:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm run build
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
2. Commit the build:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git add .
|
|
17
|
+
git commit -m "<follow conventional commits, k thanks>"
|
|
18
|
+
git push
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
3. Update the version and publish the package to NPM:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm version <patch|minor|major>
|
|
25
|
+
npm publish
|
|
26
|
+
```
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db.js","sourceRoot":"","sources":["../../../lib/auth/db.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,IAA+B,EAAE,CACjE,CAAC,MAAM,MAAM,CAAC,0BAA0B,CAAC,CAAC,CAAC,UAAU,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AuthEventType, AuthRealm } from './limits.js';
|
|
2
|
+
import { InternalExecutor } from './db.js';
|
|
3
|
+
export interface IAuthEvent {
|
|
4
|
+
event: AuthEventType;
|
|
5
|
+
realm: AuthRealm;
|
|
6
|
+
subjectId: string;
|
|
7
|
+
email?: string | null;
|
|
8
|
+
sessionId?: string | null;
|
|
9
|
+
godId?: string | null;
|
|
10
|
+
reason?: string | null;
|
|
11
|
+
}
|
|
12
|
+
export declare const toAuthEventRow: (entry: IAuthEvent) => {
|
|
13
|
+
event: import("@betterinternship/schema/dist/db.internal.types.js").InternalAuthEventType;
|
|
14
|
+
realm: import("@betterinternship/schema/dist/db.internal.types.js").InternalAuthRealm;
|
|
15
|
+
subject_id: string;
|
|
16
|
+
email: string | null;
|
|
17
|
+
session_id: string | null;
|
|
18
|
+
god_id: string | null;
|
|
19
|
+
reason: string | null;
|
|
20
|
+
};
|
|
21
|
+
export declare const logAuthEvent: (entry: IAuthEvent | IAuthEvent[], executor?: InternalExecutor) => Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { getInternalDb } from './db.js';
|
|
2
|
+
export const toAuthEventRow = (entry) => ({
|
|
3
|
+
event: entry.event,
|
|
4
|
+
realm: entry.realm,
|
|
5
|
+
subject_id: entry.subjectId,
|
|
6
|
+
email: entry.email ?? null,
|
|
7
|
+
session_id: entry.sessionId ?? null,
|
|
8
|
+
god_id: entry.godId ?? null,
|
|
9
|
+
reason: entry.reason ?? null,
|
|
10
|
+
});
|
|
11
|
+
export const logAuthEvent = async (entry, executor) => {
|
|
12
|
+
const entries = Array.isArray(entry) ? entry : [entry];
|
|
13
|
+
if (!entries.length)
|
|
14
|
+
return;
|
|
15
|
+
const db = executor ?? (await getInternalDb());
|
|
16
|
+
await db
|
|
17
|
+
.insertInto('internal.auth_event_log')
|
|
18
|
+
.values(entries.map(toAuthEventRow))
|
|
19
|
+
.execute();
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../../lib/auth/events.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAoB,MAAM,SAAS,CAAC;AAe1D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAiB,EAAE,EAAE,CAAC,CAAC;IACpD,KAAK,EAAE,KAAK,CAAC,KAAK;IAClB,KAAK,EAAE,KAAK,CAAC,KAAK;IAClB,UAAU,EAAE,KAAK,CAAC,SAAS;IAC3B,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,IAAI;IAC1B,UAAU,EAAE,KAAK,CAAC,SAAS,IAAI,IAAI;IACnC,MAAM,EAAE,KAAK,CAAC,KAAK,IAAI,IAAI;IAC3B,MAAM,EAAE,KAAK,CAAC,MAAM,IAAI,IAAI;CAC7B,CAAC,CAAC;AAQH,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC/B,KAAgC,EAChC,QAA2B,EACZ,EAAE;IACjB,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IACvD,IAAI,CAAC,OAAO,CAAC,MAAM;QAAE,OAAO;IAE5B,MAAM,EAAE,GAAG,QAAQ,IAAI,CAAC,MAAM,aAAa,EAAE,CAAC,CAAC;IAC/C,MAAM,EAAE;SACL,UAAU,CAAC,yBAAyB,CAAC;SACrC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;SACnC,OAAO,EAAE,CAAC;AACf,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface IGodAccount {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
displayName: string;
|
|
5
|
+
}
|
|
6
|
+
export type GodOtpRequestStatus = 'sent' | 'cooldown' | 'unknown_email' | 'send_failed';
|
|
7
|
+
export declare const requestGodOtp: (rawEmail: string) => Promise<GodOtpRequestStatus>;
|
|
8
|
+
export declare const verifyGodOtp: (rawEmail: string, code: string) => Promise<IGodAccount | null>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { randomInt, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { sendSingleEmail } from '../email/email.js';
|
|
3
|
+
import { getInternalDb } from './db.js';
|
|
4
|
+
const OTP_TTL_MS = 10 * 60 * 1000;
|
|
5
|
+
const COOLDOWN_MS = 60 * 1000;
|
|
6
|
+
const MAX_ATTEMPTS = 10;
|
|
7
|
+
const pending = new Map();
|
|
8
|
+
const cooldowns = new Map();
|
|
9
|
+
const normalise = (email) => email.trim().toLowerCase();
|
|
10
|
+
const prune = (now) => {
|
|
11
|
+
for (const [email, otp] of pending)
|
|
12
|
+
if (otp.expiresAt < now)
|
|
13
|
+
pending.delete(email);
|
|
14
|
+
for (const [email, until] of cooldowns)
|
|
15
|
+
if (until < now)
|
|
16
|
+
cooldowns.delete(email);
|
|
17
|
+
};
|
|
18
|
+
const generateCode = () => randomInt(0, 1_000_000).toString().padStart(6, '0');
|
|
19
|
+
const findActiveGod = async (email) => {
|
|
20
|
+
const db = await getInternalDb();
|
|
21
|
+
const god = await db
|
|
22
|
+
.selectFrom('internal.gods')
|
|
23
|
+
.select(['id', 'email', 'display_name'])
|
|
24
|
+
.where((eb) => eb(eb.fn('lower', ['email']), '=', email))
|
|
25
|
+
.where('is_deactivated', '=', false)
|
|
26
|
+
.executeTakeFirst();
|
|
27
|
+
return god
|
|
28
|
+
? { id: god.id, email: god.email, displayName: god.display_name }
|
|
29
|
+
: null;
|
|
30
|
+
};
|
|
31
|
+
const otpEmailHtml = (name, code) => `
|
|
32
|
+
<div style="font-family: sans-serif; max-width: 480px; margin: 0 auto;">
|
|
33
|
+
<p>Hi ${name.replace(/[<>&"]/g, '')},</p>
|
|
34
|
+
<p>Your BetterInternship admin sign-in code is:</p>
|
|
35
|
+
<p style="font-size: 32px; letter-spacing: 6px; font-weight: bold;">${code}</p>
|
|
36
|
+
<p>It expires in ${Math.floor(OTP_TTL_MS / 60000)} minutes. If you did not ask for it, you can ignore this email.</p>
|
|
37
|
+
</div>`;
|
|
38
|
+
export const requestGodOtp = async (rawEmail) => {
|
|
39
|
+
const email = normalise(rawEmail);
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
prune(now);
|
|
42
|
+
if (cooldowns.has(email))
|
|
43
|
+
return 'cooldown';
|
|
44
|
+
cooldowns.set(email, now + COOLDOWN_MS);
|
|
45
|
+
const god = await findActiveGod(email);
|
|
46
|
+
if (!god)
|
|
47
|
+
return 'unknown_email';
|
|
48
|
+
const code = generateCode();
|
|
49
|
+
pending.set(email, { code, expiresAt: now + OTP_TTL_MS, attempts: 0 });
|
|
50
|
+
const sent = await sendSingleEmail({
|
|
51
|
+
sender: 'BetterInternship',
|
|
52
|
+
subject: `Your admin sign-in code: ${code}`,
|
|
53
|
+
recipient: god.email,
|
|
54
|
+
content: otpEmailHtml(god.displayName, code),
|
|
55
|
+
alias: 'otp',
|
|
56
|
+
});
|
|
57
|
+
if (!sent) {
|
|
58
|
+
pending.delete(email);
|
|
59
|
+
return 'send_failed';
|
|
60
|
+
}
|
|
61
|
+
return 'sent';
|
|
62
|
+
};
|
|
63
|
+
export const verifyGodOtp = async (rawEmail, code) => {
|
|
64
|
+
const email = normalise(rawEmail);
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
prune(now);
|
|
67
|
+
const otp = pending.get(email);
|
|
68
|
+
if (!otp)
|
|
69
|
+
return null;
|
|
70
|
+
const given = Buffer.from(code.trim());
|
|
71
|
+
const expected = Buffer.from(otp.code);
|
|
72
|
+
const matches = given.length === expected.length && timingSafeEqual(given, expected);
|
|
73
|
+
if (!matches) {
|
|
74
|
+
if (++otp.attempts >= MAX_ATTEMPTS) {
|
|
75
|
+
pending.delete(email);
|
|
76
|
+
cooldowns.set(email, now + COOLDOWN_MS);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
pending.delete(email);
|
|
81
|
+
cooldowns.delete(email);
|
|
82
|
+
return findActiveGod(email);
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=god-otp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"god-otp.js","sourceRoot":"","sources":["../../../lib/auth/god-otp.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAClC,MAAM,WAAW,GAAG,EAAE,GAAG,IAAI,CAAC;AAC9B,MAAM,YAAY,GAAG,EAAE,CAAC;AAQxB,MAAM,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAC;AAE/C,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;AAc5C,MAAM,SAAS,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAExE,MAAM,KA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,10 @@
|
|
|
1
|
+
import type { Internal } from '@betterinternship/schema';
|
|
2
|
+
export type AuthRealm = Internal.InternalAuthRealm;
|
|
3
|
+
export type AuthEventType = Internal.InternalAuthEventType;
|
|
4
|
+
export interface ISessionLimits {
|
|
5
|
+
idleMs: number;
|
|
6
|
+
maxMs: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const SESSION_LIMITS: Readonly<Record<AuthRealm, ISessionLimits>>;
|
|
9
|
+
export declare const limitsFor: (realm: AuthRealm, impersonated: boolean) => ISessionLimits;
|
|
10
|
+
export declare const LAST_SEEN_WRITE_INTERVAL_MS: number;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const HOUR = 60 * 60 * 1000;
|
|
2
|
+
const DAY = 24 * HOUR;
|
|
3
|
+
const REGULAR = { idleMs: 7 * DAY, maxMs: 30 * DAY };
|
|
4
|
+
const GOD = { idleMs: 12 * HOUR, maxMs: 7 * DAY };
|
|
5
|
+
export const SESSION_LIMITS = {
|
|
6
|
+
student: REGULAR,
|
|
7
|
+
employer: REGULAR,
|
|
8
|
+
signatory: REGULAR,
|
|
9
|
+
company: REGULAR,
|
|
10
|
+
university: REGULAR,
|
|
11
|
+
god: GOD,
|
|
12
|
+
};
|
|
13
|
+
export const limitsFor = (realm, impersonated) => impersonated ? SESSION_LIMITS.god : SESSION_LIMITS[realm];
|
|
14
|
+
export const LAST_SEEN_WRITE_INTERVAL_MS = HOUR;
|
|
15
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.js","sourceRoot":"","sources":["../../../lib/auth/limits.ts"],"names":[],"mappings":"AAKA,MAAM,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAC5B,MAAM,GAAG,GAAG,EAAE,GAAG,IAAI,CAAC;AAStB,MAAM,OAAO,GAAmB,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,KAAK,EAAE,EAAE,GAAG,GAAG,EAAE,CAAC;AACrE,MAAM,GAAG,GAAmB,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC;AAGlE,MAAM,CAAC,MAAM,cAAc,GAAgD;IACzE,OAAO,EAAE,OAAO;IAChB,QAAQ,EAAE,OAAO;IACjB,SAAS,EAAE,OAAO;IAClB,OAAO,EAAE,OAAO;IAChB,UAAU,EAAE,OAAO;IACnB,GAAG,EAAE,GAAG;CACT,CAAC;AAOF,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,KAAgB,EAChB,YAAqB,EACL,EAAE,CAClB,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;AAG5D,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AuthRealm } from './limits.js';
|
|
2
|
+
export declare const hashSessionId: (rawId: string) => string;
|
|
3
|
+
export interface ICreateSessionParams {
|
|
4
|
+
realm: AuthRealm;
|
|
5
|
+
subjectId: string;
|
|
6
|
+
email?: string | null;
|
|
7
|
+
impersonatedBySessionId?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ICreatedSession {
|
|
10
|
+
sessionId: string;
|
|
11
|
+
sessionRowId: string;
|
|
12
|
+
maxAgeMs: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const createSession: (params: ICreateSessionParams) => Promise<ICreatedSession>;
|
|
15
|
+
export interface IResolvedSession {
|
|
16
|
+
sessionRowId: string;
|
|
17
|
+
realm: AuthRealm;
|
|
18
|
+
subjectId: string;
|
|
19
|
+
email: string | null;
|
|
20
|
+
impersonator?: {
|
|
21
|
+
sessionRowId: string;
|
|
22
|
+
godId: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export declare const resolveSession: (realm: AuthRealm, rawId: string) => Promise<IResolvedSession | null>;
|
|
26
|
+
export declare const endSession: (rawId: string, event?: "logout" | "expired") => Promise<boolean>;
|
|
27
|
+
export declare const endAllSessions: (realm: AuthRealm, subjectId: string, reason: string) => Promise<number>;
|
|
28
|
+
export declare const sweepExpiredSessions: () => Promise<number>;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
+
import { getInternalDb } from './db.js';
|
|
3
|
+
import { logAuthEvent, toAuthEventRow } from './events.js';
|
|
4
|
+
import { LAST_SEEN_WRITE_INTERVAL_MS, limitsFor, SESSION_LIMITS, } from './limits.js';
|
|
5
|
+
const SWEEP_BATCH_SIZE = 500;
|
|
6
|
+
export const hashSessionId = (rawId) => createHash('sha256').update(rawId).digest('hex');
|
|
7
|
+
const generateSessionId = () => randomBytes(32).toString('base64url');
|
|
8
|
+
export const createSession = async (params) => {
|
|
9
|
+
const db = await getInternalDb();
|
|
10
|
+
const limits = limitsFor(params.realm, !!params.impersonatedBySessionId);
|
|
11
|
+
const sessionId = generateSessionId();
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
return db.transaction().execute(async (trx) => {
|
|
14
|
+
let expiresAtMs = now + limits.maxMs;
|
|
15
|
+
let godId = null;
|
|
16
|
+
if (params.impersonatedBySessionId) {
|
|
17
|
+
const god = await trx
|
|
18
|
+
.selectFrom('internal.auth_sessions')
|
|
19
|
+
.select(['subject_id', 'realm', 'expires_at'])
|
|
20
|
+
.where('id', '=', params.impersonatedBySessionId)
|
|
21
|
+
.executeTakeFirst();
|
|
22
|
+
if (!god || god.realm !== 'god')
|
|
23
|
+
throw new Error('[AUTH] Impersonation needs a live god session.');
|
|
24
|
+
godId = god.subject_id;
|
|
25
|
+
expiresAtMs = Math.min(expiresAtMs, new Date(god.expires_at).getTime());
|
|
26
|
+
}
|
|
27
|
+
const row = await trx
|
|
28
|
+
.insertInto('internal.auth_sessions')
|
|
29
|
+
.values({
|
|
30
|
+
token_hash: hashSessionId(sessionId),
|
|
31
|
+
realm: params.realm,
|
|
32
|
+
subject_id: params.subjectId,
|
|
33
|
+
email: params.email ?? null,
|
|
34
|
+
expires_at: new Date(expiresAtMs),
|
|
35
|
+
impersonated_by_session_id: params.impersonatedBySessionId ?? null,
|
|
36
|
+
})
|
|
37
|
+
.returning('id')
|
|
38
|
+
.executeTakeFirstOrThrow();
|
|
39
|
+
await logAuthEvent({
|
|
40
|
+
event: godId ? 'impersonation_started' : 'login',
|
|
41
|
+
realm: params.realm,
|
|
42
|
+
subjectId: params.subjectId,
|
|
43
|
+
email: params.email,
|
|
44
|
+
sessionId: row.id,
|
|
45
|
+
godId,
|
|
46
|
+
}, trx);
|
|
47
|
+
return {
|
|
48
|
+
sessionId,
|
|
49
|
+
sessionRowId: row.id,
|
|
50
|
+
maxAgeMs: Math.max(0, expiresAtMs - now),
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
const isDead = (row, limits, now) => now > new Date(row.expires_at).getTime() ||
|
|
55
|
+
now - new Date(row.last_seen_at).getTime() > limits.idleMs;
|
|
56
|
+
export const resolveSession = async (realm, rawId) => {
|
|
57
|
+
if (!rawId)
|
|
58
|
+
return null;
|
|
59
|
+
const db = await getInternalDb();
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
const row = await db
|
|
62
|
+
.selectFrom('internal.auth_sessions')
|
|
63
|
+
.select([
|
|
64
|
+
'id',
|
|
65
|
+
'subject_id',
|
|
66
|
+
'email',
|
|
67
|
+
'last_seen_at',
|
|
68
|
+
'expires_at',
|
|
69
|
+
'impersonated_by_session_id',
|
|
70
|
+
])
|
|
71
|
+
.where('token_hash', '=', hashSessionId(rawId))
|
|
72
|
+
.where('realm', '=', realm)
|
|
73
|
+
.executeTakeFirst();
|
|
74
|
+
if (!row)
|
|
75
|
+
return null;
|
|
76
|
+
if (isDead(row, limitsFor(realm, !!row.impersonated_by_session_id), now)) {
|
|
77
|
+
await removeSessions([row.id], 'expired');
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const touchIds = [];
|
|
81
|
+
let impersonator;
|
|
82
|
+
if (row.impersonated_by_session_id) {
|
|
83
|
+
const god = await db
|
|
84
|
+
.selectFrom('internal.auth_sessions as s')
|
|
85
|
+
.innerJoin('internal.gods as g', 'g.id', 's.subject_id')
|
|
86
|
+
.select([
|
|
87
|
+
's.id',
|
|
88
|
+
's.subject_id',
|
|
89
|
+
's.last_seen_at',
|
|
90
|
+
's.expires_at',
|
|
91
|
+
'g.is_deactivated',
|
|
92
|
+
])
|
|
93
|
+
.where('s.id', '=', row.impersonated_by_session_id)
|
|
94
|
+
.where('s.realm', '=', 'god')
|
|
95
|
+
.executeTakeFirst();
|
|
96
|
+
if (!god || god.is_deactivated)
|
|
97
|
+
return null;
|
|
98
|
+
if (isDead(god, SESSION_LIMITS.god, now)) {
|
|
99
|
+
await removeSessions([god.id], 'expired');
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
impersonator = { sessionRowId: god.id, godId: god.subject_id };
|
|
103
|
+
if (now - new Date(god.last_seen_at).getTime() >
|
|
104
|
+
LAST_SEEN_WRITE_INTERVAL_MS)
|
|
105
|
+
touchIds.push(god.id);
|
|
106
|
+
}
|
|
107
|
+
if (now - new Date(row.last_seen_at).getTime() > LAST_SEEN_WRITE_INTERVAL_MS)
|
|
108
|
+
touchIds.push(row.id);
|
|
109
|
+
if (touchIds.length)
|
|
110
|
+
await db
|
|
111
|
+
.updateTable('internal.auth_sessions')
|
|
112
|
+
.set({ last_seen_at: new Date(now) })
|
|
113
|
+
.where('id', 'in', touchIds)
|
|
114
|
+
.execute();
|
|
115
|
+
return {
|
|
116
|
+
sessionRowId: row.id,
|
|
117
|
+
realm,
|
|
118
|
+
subjectId: row.subject_id,
|
|
119
|
+
email: row.email,
|
|
120
|
+
impersonator,
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
const removeSessions = async (ids, event, reason) => {
|
|
124
|
+
if (!ids.length)
|
|
125
|
+
return 0;
|
|
126
|
+
const db = await getInternalDb();
|
|
127
|
+
return db.transaction().execute(async (trx) => {
|
|
128
|
+
const selectSessions = () => trx
|
|
129
|
+
.selectFrom('internal.auth_sessions as s')
|
|
130
|
+
.leftJoin('internal.auth_sessions as p', 'p.id', 's.impersonated_by_session_id')
|
|
131
|
+
.select([
|
|
132
|
+
's.id',
|
|
133
|
+
's.realm',
|
|
134
|
+
's.subject_id',
|
|
135
|
+
's.email',
|
|
136
|
+
's.impersonated_by_session_id',
|
|
137
|
+
'p.subject_id as god_id',
|
|
138
|
+
]);
|
|
139
|
+
const rows = await selectSessions().where('s.id', 'in', ids).execute();
|
|
140
|
+
if (!rows.length)
|
|
141
|
+
return 0;
|
|
142
|
+
const children = (await selectSessions()
|
|
143
|
+
.where('s.impersonated_by_session_id', 'in', ids)
|
|
144
|
+
.execute()).filter((child) => !ids.includes(child.id));
|
|
145
|
+
await trx
|
|
146
|
+
.deleteFrom('internal.auth_sessions')
|
|
147
|
+
.where('id', 'in', ids)
|
|
148
|
+
.execute();
|
|
149
|
+
const events = [
|
|
150
|
+
...rows.map((row) => ({
|
|
151
|
+
event: row.impersonated_by_session_id && event === 'logout'
|
|
152
|
+
? 'impersonation_ended'
|
|
153
|
+
: event,
|
|
154
|
+
realm: row.realm,
|
|
155
|
+
subjectId: row.subject_id,
|
|
156
|
+
email: row.email,
|
|
157
|
+
sessionId: row.id,
|
|
158
|
+
godId: row.god_id,
|
|
159
|
+
reason,
|
|
160
|
+
})),
|
|
161
|
+
...children.map((child) => ({
|
|
162
|
+
event: 'impersonation_ended',
|
|
163
|
+
realm: child.realm,
|
|
164
|
+
subjectId: child.subject_id,
|
|
165
|
+
email: child.email,
|
|
166
|
+
sessionId: child.id,
|
|
167
|
+
godId: child.god_id,
|
|
168
|
+
reason: reason ?? `god_session_${event}`,
|
|
169
|
+
})),
|
|
170
|
+
];
|
|
171
|
+
await trx
|
|
172
|
+
.insertInto('internal.auth_event_log')
|
|
173
|
+
.values(events.map(toAuthEventRow))
|
|
174
|
+
.execute();
|
|
175
|
+
return rows.length;
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
export const endSession = async (rawId, event = 'logout') => {
|
|
179
|
+
if (!rawId)
|
|
180
|
+
return false;
|
|
181
|
+
const db = await getInternalDb();
|
|
182
|
+
const row = await db
|
|
183
|
+
.selectFrom('internal.auth_sessions')
|
|
184
|
+
.select('id')
|
|
185
|
+
.where('token_hash', '=', hashSessionId(rawId))
|
|
186
|
+
.executeTakeFirst();
|
|
187
|
+
if (!row)
|
|
188
|
+
return false;
|
|
189
|
+
return (await removeSessions([row.id], event)) > 0;
|
|
190
|
+
};
|
|
191
|
+
export const endAllSessions = async (realm, subjectId, reason) => {
|
|
192
|
+
const db = await getInternalDb();
|
|
193
|
+
const rows = await db
|
|
194
|
+
.selectFrom('internal.auth_sessions')
|
|
195
|
+
.select('id')
|
|
196
|
+
.where('realm', '=', realm)
|
|
197
|
+
.where('subject_id', '=', subjectId)
|
|
198
|
+
.execute();
|
|
199
|
+
return removeSessions(rows.map((row) => row.id), 'revoked', reason);
|
|
200
|
+
};
|
|
201
|
+
export const sweepExpiredSessions = async () => {
|
|
202
|
+
const db = await getInternalDb();
|
|
203
|
+
const now = new Date();
|
|
204
|
+
const cutoff = (idleMs) => new Date(now.getTime() - idleMs);
|
|
205
|
+
const realms = Object.keys(SESSION_LIMITS);
|
|
206
|
+
let total = 0;
|
|
207
|
+
for (;;) {
|
|
208
|
+
const batch = await db
|
|
209
|
+
.selectFrom('internal.auth_sessions')
|
|
210
|
+
.select('id')
|
|
211
|
+
.where((eb) => eb.or([
|
|
212
|
+
eb('expires_at', '<', now),
|
|
213
|
+
eb.and([
|
|
214
|
+
eb('impersonated_by_session_id', 'is not', null),
|
|
215
|
+
eb('last_seen_at', '<', cutoff(SESSION_LIMITS.god.idleMs)),
|
|
216
|
+
]),
|
|
217
|
+
eb.and([
|
|
218
|
+
eb('impersonated_by_session_id', 'is', null),
|
|
219
|
+
eb.or(realms.map((realm) => eb.and([
|
|
220
|
+
eb('realm', '=', realm),
|
|
221
|
+
eb('last_seen_at', '<', cutoff(SESSION_LIMITS[realm].idleMs)),
|
|
222
|
+
]))),
|
|
223
|
+
]),
|
|
224
|
+
]))
|
|
225
|
+
.limit(SWEEP_BATCH_SIZE)
|
|
226
|
+
.execute();
|
|
227
|
+
const removed = await removeSessions(batch.map((row) => row.id), 'expired');
|
|
228
|
+
total += removed;
|
|
229
|
+
if (batch.length < SWEEP_BATCH_SIZE || removed === 0)
|
|
230
|
+
return total;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
//# sourceMappingURL=sessions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sessions.js","sourceRoot":"","sources":["../../../lib/auth/sessions.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAc,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAGL,2BAA2B,EAC3B,SAAS,EACT,cAAc,GACf,MAAM,aAAa,CAAC;AAErB,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAE7B,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAa,EAAU,EAAE,CACrD,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAEnD,MAAM,iBAAiB,GAAG,GAAW,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AA4B9E,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,MAA4B,EACF,EAAE;IAC5B,MAAM,EAAE,GAAG,MAAM,aAAa,EAAE,CAAC;IACjC,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,MAA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,KAAK,EAAE,EAAE,CACnB,EAAE,CAAC,GAAG,CAAC;oBACL,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC;oBACvB,EAAE,CAAC,cAAc,EAAE,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC;iBAC9D,CAAC,CACH,CACF;aACF,CAAC;SACH,CAAC,CACH;aACA,KAAK,CAAC,gBAAgB,CAAC;aACvB,OAAO,EAAE,CAAC;QAEb,MAAM,OAAO,GAAG,MAAM,cAAc,CAClC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAC1B,SAAS,CACV,CAAC;QACF,KAAK,IAAI,OAAO,CAAC;QAEjB,IAAI,KAAK,CAAC,MAAM,GAAG,gBAAgB,IAAI,OAAO,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;IACrE,CAAC;AACH,CAAC,CAAC"}
|
package/dist/lib/email/action.js
CHANGED
|
@@ -6,9 +6,9 @@ const escapeHtml = (value) => value
|
|
|
6
6
|
.replaceAll("'", ''');
|
|
7
7
|
export const renderEmailAction = ({ href, label }) => {
|
|
8
8
|
const escapedHref = escapeHtml(href);
|
|
9
|
-
return `<div style="margin:20px 0 0">
|
|
10
|
-
<a href="${escapedHref}" style="display:inline-block;padding:12px 22px;background:#155eef;color:#ffffff;border-radius:8px;text-decoration:none;font-size:14px;font-weight:700">${escapeHtml(label)} →</a>
|
|
11
|
-
<p style="margin:8px 0 0;font-size:11px;line-height:1.45;color:#8a94a8;word-break:break-all">Or copy this link: <a href="${escapedHref}" style="color:#64748b;text-decoration:underline;word-break:break-all">${escapedHref}</a></p>
|
|
9
|
+
return `<div style="margin:20px 0 0">
|
|
10
|
+
<a href="${escapedHref}" style="display:inline-block;padding:12px 22px;background:#155eef;color:#ffffff;border-radius:8px;text-decoration:none;font-size:14px;font-weight:700">${escapeHtml(label)} →</a>
|
|
11
|
+
<p style="margin:8px 0 0;font-size:11px;line-height:1.45;color:#8a94a8;word-break:break-all">Or copy this link: <a href="${escapedHref}" style="color:#64748b;text-decoration:underline;word-break:break-all">${escapedHref}</a></p>
|
|
12
12
|
</div>`;
|
|
13
13
|
};
|
|
14
14
|
//# sourceMappingURL=action.js.map
|
|
@@ -26,26 +26,26 @@ export const writeEmailPreviewCatalog = ({ title, previews, outputDirectory, })
|
|
|
26
26
|
});
|
|
27
27
|
const escapedTitle = escapeHtml(title);
|
|
28
28
|
const catalogPath = join(outputDirectory, 'index.html');
|
|
29
|
-
writeFileSync(catalogPath, `<!doctype html>
|
|
30
|
-
<html lang="en">
|
|
31
|
-
<head>
|
|
32
|
-
<meta charset="utf-8" />
|
|
33
|
-
<title>${escapedTitle}</title>
|
|
34
|
-
<style>
|
|
35
|
-
body { margin: 0; padding: 32px; background: #eef2f7; color: #17213d; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
|
|
36
|
-
h1 { margin: 0 0 8px; }
|
|
37
|
-
p { margin: 0 0 32px; color: #5c6880; }
|
|
38
|
-
section { max-width: 680px; margin: 0 auto 36px; }
|
|
39
|
-
h2 { display: flex; align-items: center; gap: 8px; font-size: 16px; margin: 0 0 10px; }
|
|
40
|
-
h2 span { padding: 3px 7px; border-radius: 999px; background: #dbeafe; color: #1d4ed8; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
|
|
41
|
-
iframe { width: 100%; height: 760px; border: 1px solid #d7deea; background: #fff; border-radius: 8px; }
|
|
42
|
-
</style>
|
|
43
|
-
</head>
|
|
44
|
-
<body>
|
|
45
|
-
<h1>${escapedTitle}</h1>
|
|
46
|
-
<p>Generated from the production email factories and their templates.</p>
|
|
47
|
-
${catalogItems.join('\n ')}
|
|
48
|
-
</body>
|
|
29
|
+
writeFileSync(catalogPath, `<!doctype html>
|
|
30
|
+
<html lang="en">
|
|
31
|
+
<head>
|
|
32
|
+
<meta charset="utf-8" />
|
|
33
|
+
<title>${escapedTitle}</title>
|
|
34
|
+
<style>
|
|
35
|
+
body { margin: 0; padding: 32px; background: #eef2f7; color: #17213d; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
|
|
36
|
+
h1 { margin: 0 0 8px; }
|
|
37
|
+
p { margin: 0 0 32px; color: #5c6880; }
|
|
38
|
+
section { max-width: 680px; margin: 0 auto 36px; }
|
|
39
|
+
h2 { display: flex; align-items: center; gap: 8px; font-size: 16px; margin: 0 0 10px; }
|
|
40
|
+
h2 span { padding: 3px 7px; border-radius: 999px; background: #dbeafe; color: #1d4ed8; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
|
|
41
|
+
iframe { width: 100%; height: 760px; border: 1px solid #d7deea; background: #fff; border-radius: 8px; }
|
|
42
|
+
</style>
|
|
43
|
+
</head>
|
|
44
|
+
<body>
|
|
45
|
+
<h1>${escapedTitle}</h1>
|
|
46
|
+
<p>Generated from the production email factories and their templates.</p>
|
|
47
|
+
${catalogItems.join('\n ')}
|
|
48
|
+
</body>
|
|
49
49
|
</html>`);
|
|
50
50
|
return catalogPath;
|
|
51
51
|
};
|