@feasibleone/blong-login 1.12.0 → 1.13.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/CHANGELOG.md +7 -0
- package/browser/orchestrator/subject/init.ts +19 -0
- package/browser.ts +31 -0
- package/config/heft.json +23 -0
- package/error/error.ts +25 -0
- package/eslint.config.mjs +2 -0
- package/gateway/login/login.oidc.getConfiguration.ts +13 -0
- package/gateway/login/login.oidc.getKeys.ts +13 -0
- package/gateway/login/login.token.create.ts +20 -0
- package/gateway/login/login.token.exchange.ts +24 -0
- package/gateway/login/login.token.refresh.ts +14 -0
- package/gateway/login/login.token.restore.ts +13 -0
- package/gateway/login/login.token.revoke.ts +16 -0
- package/orchestrator/login/loginOidcGetConfiguration.ts +31 -0
- package/orchestrator/login/loginOidcGetKeys.ts +9 -0
- package/orchestrator/login/loginTokenCreate.ts +245 -0
- package/orchestrator/login/loginTokenExchange.ts +66 -0
- package/orchestrator/login/loginTokenRefresh.ts +252 -0
- package/orchestrator/login/loginTokenRestore.ts +205 -0
- package/orchestrator/login/loginTokenRevoke.ts +69 -0
- package/orchestrator/login/refresh.ts +22 -0
- package/orchestrator/login/sessionLib.ts +227 -0
- package/orchestrator/login/token.ts +112 -0
- package/orchestrator/loginDispatch.ts +17 -0
- package/package.json +2 -2
- package/server.ts +124 -0
- package/test/test/testUserAdminLogin.ts +13 -0
- package/tsconfig.json +27 -0
- package/types/ut-function.cbc/index.d.ts +6 -0
- package/types/ut-function.cbc/package.json +4 -0
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
import {type ITokenResult, type SessionConfig, sessionCleanupParams} from './sessionLib.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* OAuth refresh-grant endpoint (JSON-RPC `login.token.refresh`).
|
|
7
|
+
*
|
|
8
|
+
* Redeems the CBC-encrypted refresh token minted at login:
|
|
9
|
+
* 1. `readRefresh` decrypts + checks its own expiry (→ `login.refreshTokenExpired`).
|
|
10
|
+
* 2. `access.session.verify` checks the DB session is live (not revoked /
|
|
11
|
+
* expired / inactive) and TOUCHES `lastActivityAt` — renewing updates the
|
|
12
|
+
* inactivity timer.
|
|
13
|
+
* 3. `access.permission.list` re-resolves the user's effective role bits and
|
|
14
|
+
* actions from the materialized `core_path` — so a renewed token carries
|
|
15
|
+
* the CURRENT permission set (role/capability/action changes take effect
|
|
16
|
+
* on the next refresh without a re-login).
|
|
17
|
+
* 4. `token()` mints a fresh access token + a rotated refresh token, and
|
|
18
|
+
* `access.session.rotate` stores the new refresh-token hash.
|
|
19
|
+
*
|
|
20
|
+
* A revoked / inactive / expired session refuses renewal — the still-valid
|
|
21
|
+
* access token keeps working until it expires, but cannot be renewed, so the
|
|
22
|
+
* client is effectively logged out within one access-token lifetime.
|
|
23
|
+
*
|
|
24
|
+
* Wire: `login.token.refresh` (`auth: 'login'`).
|
|
25
|
+
*/
|
|
26
|
+
export default handler(({errors, config, lib: {methods = {}, readRefresh, token, sha256Hex}}) => {
|
|
27
|
+
const inactivityTimeout = (config as SessionConfig).expire?.inactivity;
|
|
28
|
+
return async function loginTokenRefresh({refreshToken}: {refreshToken: string}, $meta: IMeta) {
|
|
29
|
+
const ipAddress = ($meta as {ipAddress?: string}).ipAddress;
|
|
30
|
+
const recordAudit = (entry: {
|
|
31
|
+
actorId?: string;
|
|
32
|
+
sessionId?: string;
|
|
33
|
+
isSuccess: boolean;
|
|
34
|
+
failureReason?: string;
|
|
35
|
+
}) =>
|
|
36
|
+
methods.auditRecord
|
|
37
|
+
? (
|
|
38
|
+
methods.auditRecord(
|
|
39
|
+
{
|
|
40
|
+
audit: [
|
|
41
|
+
{
|
|
42
|
+
...entry,
|
|
43
|
+
actionName: 'login.refresh',
|
|
44
|
+
ipAddress,
|
|
45
|
+
statusCode: entry.isSuccess ? 200 : 401,
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
$meta,
|
|
50
|
+
) as Promise<{inserted: number}>
|
|
51
|
+
).catch(() => undefined)
|
|
52
|
+
: Promise.resolve(undefined);
|
|
53
|
+
|
|
54
|
+
let payload: {
|
|
55
|
+
actorId: string;
|
|
56
|
+
sessionId: string;
|
|
57
|
+
clientId?: string;
|
|
58
|
+
mlsk?: object | 'header';
|
|
59
|
+
mlek?: object | 'header';
|
|
60
|
+
refresh?: number;
|
|
61
|
+
expire?: number;
|
|
62
|
+
actions?: string[];
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
payload = readRefresh(refreshToken) as typeof payload;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
await recordAudit({isSuccess: false, failureReason: 'login.refreshTokenExpired'});
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
if (!payload?.sessionId) {
|
|
71
|
+
await recordAudit({
|
|
72
|
+
isSuccess: false,
|
|
73
|
+
failureReason: 'login.invalidRefreshToken',
|
|
74
|
+
});
|
|
75
|
+
throw errors['login.invalidRefreshToken']();
|
|
76
|
+
}
|
|
77
|
+
const {sessionId, actorId, clientId, mlsk, mlek} = payload;
|
|
78
|
+
|
|
79
|
+
// Session verification is required for renewal — without it a
|
|
80
|
+
// refresh cannot validate the session (lightweight suites disable
|
|
81
|
+
// it and don't expose renewal).
|
|
82
|
+
if (!methods.sessionVerify) {
|
|
83
|
+
await recordAudit({
|
|
84
|
+
actorId,
|
|
85
|
+
sessionId,
|
|
86
|
+
isSuccess: false,
|
|
87
|
+
failureReason: 'login.configurationError',
|
|
88
|
+
});
|
|
89
|
+
throw errors['login.configurationError']({params: {method: 'sessionVerify'}});
|
|
90
|
+
}
|
|
91
|
+
let check: {sessionId: string; userId: string; tokenHash?: string};
|
|
92
|
+
try {
|
|
93
|
+
check = await (methods.sessionVerify(
|
|
94
|
+
{
|
|
95
|
+
sessionId,
|
|
96
|
+
touch: true,
|
|
97
|
+
inactivityTimeout,
|
|
98
|
+
},
|
|
99
|
+
$meta,
|
|
100
|
+
) as Promise<{sessionId: string; userId: string; tokenHash?: string}>);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
// `access.session.verify` throws the specific `access.session.*`
|
|
103
|
+
// error with the reason on `error.params.reason`; surface it as
|
|
104
|
+
// the matching `login.*` error (the endpoint's public contract).
|
|
105
|
+
const reason =
|
|
106
|
+
(
|
|
107
|
+
error as {
|
|
108
|
+
params?: {
|
|
109
|
+
reason?:
|
|
110
|
+
| 'notFound'
|
|
111
|
+
| 'revoked'
|
|
112
|
+
| 'expired'
|
|
113
|
+
| 'inactive'
|
|
114
|
+
| 'userInactive'
|
|
115
|
+
| 'loginNotAllowed';
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
).params?.reason ?? 'notFound';
|
|
119
|
+
const errorType =
|
|
120
|
+
reason === 'revoked'
|
|
121
|
+
? 'login.sessionRevoked'
|
|
122
|
+
: reason === 'inactive'
|
|
123
|
+
? 'login.sessionInactive'
|
|
124
|
+
: reason === 'expired'
|
|
125
|
+
? 'login.sessionExpired'
|
|
126
|
+
: reason === 'userInactive'
|
|
127
|
+
? 'login.userInactive'
|
|
128
|
+
: reason === 'loginNotAllowed'
|
|
129
|
+
? 'login.loginNotAllowed'
|
|
130
|
+
: 'login.sessionNotFound';
|
|
131
|
+
await recordAudit({actorId, sessionId, isSuccess: false, failureReason: errorType});
|
|
132
|
+
throw errors[errorType]();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Refresh-token reuse detection: the presented refresh token must
|
|
136
|
+
// still be the CURRENT one. A mismatch means the token was rotated
|
|
137
|
+
// already (stolen/replayed) — revoke the session as a precaution.
|
|
138
|
+
if (check.tokenHash && check.tokenHash !== sha256Hex<string>(refreshToken)) {
|
|
139
|
+
if (methods.sessionClose) {
|
|
140
|
+
// The session bound to the presented refresh token is the caller's
|
|
141
|
+
// own — mark it as such so closing it (reuse protection) does not
|
|
142
|
+
// require the `access.session.close` permission.
|
|
143
|
+
await (methods.sessionClose(
|
|
144
|
+
{sessionId},
|
|
145
|
+
{...$meta, auth: {...$meta.auth, sessionId}},
|
|
146
|
+
) as Promise<{success: boolean}>);
|
|
147
|
+
}
|
|
148
|
+
await recordAudit({
|
|
149
|
+
actorId,
|
|
150
|
+
sessionId,
|
|
151
|
+
isSuccess: false,
|
|
152
|
+
failureReason: 'login.invalidRefreshToken',
|
|
153
|
+
});
|
|
154
|
+
throw errors['login.invalidRefreshToken']();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Fresh permission set — role/capability/action changes apply on renewal.
|
|
158
|
+
// Optional: without a configured `permissionList` (lightweight suite)
|
|
159
|
+
// the renewed token simply carries no permissions.
|
|
160
|
+
let permissionMap = '';
|
|
161
|
+
let actions: string[] = [];
|
|
162
|
+
let isActive = true;
|
|
163
|
+
if (methods.permissionList) {
|
|
164
|
+
const resolved = (await methods.permissionList({userId: check.userId}, $meta)) as {
|
|
165
|
+
permissionMap: string;
|
|
166
|
+
actions: string[];
|
|
167
|
+
isActive: boolean;
|
|
168
|
+
};
|
|
169
|
+
permissionMap = resolved.permissionMap;
|
|
170
|
+
actions = resolved.actions;
|
|
171
|
+
isActive = resolved.isActive;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Login-eligibility gates on renewal: a user who has since been
|
|
175
|
+
// deactivated (`user.isActive = false`) or lost the `accessLogin`
|
|
176
|
+
// action can no longer renew. Their session dies within one
|
|
177
|
+
// access-token lifetime instead of living on.
|
|
178
|
+
if (methods.permissionList) {
|
|
179
|
+
if (!isActive) {
|
|
180
|
+
await recordAudit({
|
|
181
|
+
actorId,
|
|
182
|
+
sessionId,
|
|
183
|
+
isSuccess: false,
|
|
184
|
+
failureReason: 'login.userInactive',
|
|
185
|
+
});
|
|
186
|
+
throw errors['login.userInactive']();
|
|
187
|
+
}
|
|
188
|
+
if (!actions.includes('accessLogin')) {
|
|
189
|
+
await recordAudit({
|
|
190
|
+
actorId,
|
|
191
|
+
sessionId,
|
|
192
|
+
isSuccess: false,
|
|
193
|
+
failureReason: 'login.loginNotAllowed',
|
|
194
|
+
});
|
|
195
|
+
throw errors['login.loginNotAllowed']();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Keep the session lifetime FIXED (absolute expiry from login): the
|
|
200
|
+
// new refresh token expires at the same moment as the session.
|
|
201
|
+
const remaining = payload.expire
|
|
202
|
+
? Math.max(0, Math.round((payload.expire - Date.now()) / 1000))
|
|
203
|
+
: undefined;
|
|
204
|
+
|
|
205
|
+
// Best-effort profile resolution so the UI can apply the user's
|
|
206
|
+
// preferred language after renewal. Optional — yields 'en'.
|
|
207
|
+
let profile: {actorId?: string; language?: string} | undefined;
|
|
208
|
+
let language = 'en';
|
|
209
|
+
if (methods.profileGet) {
|
|
210
|
+
try {
|
|
211
|
+
const profileData = (await methods.profileGet(
|
|
212
|
+
{},
|
|
213
|
+
{...$meta, auth: {...$meta.auth, actorId}},
|
|
214
|
+
)) as {preferredLanguage?: string | null} | undefined;
|
|
215
|
+
language = profileData?.preferredLanguage ?? 'en';
|
|
216
|
+
profile = {actorId, language};
|
|
217
|
+
} catch {
|
|
218
|
+
// Profile is optional refresh metadata — never fails the refresh.
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const tokenResult = (await token({
|
|
223
|
+
clientId: clientId ?? '',
|
|
224
|
+
actorId,
|
|
225
|
+
sessionId,
|
|
226
|
+
language,
|
|
227
|
+
refresh: remaining ?? payload.refresh ?? 0,
|
|
228
|
+
permissionMap,
|
|
229
|
+
mlek,
|
|
230
|
+
mlsk,
|
|
231
|
+
actions,
|
|
232
|
+
profile,
|
|
233
|
+
})) as ITokenResult;
|
|
234
|
+
|
|
235
|
+
if (methods.sessionRotate) {
|
|
236
|
+
await (methods.sessionRotate(
|
|
237
|
+
{sessionId, tokenHash: sha256Hex(tokenResult.refresh_token)},
|
|
238
|
+
$meta,
|
|
239
|
+
) as Promise<{success: boolean}>);
|
|
240
|
+
}
|
|
241
|
+
if (methods.sessionCleanup) {
|
|
242
|
+
await (
|
|
243
|
+
methods.sessionCleanup(
|
|
244
|
+
sessionCleanupParams(config as SessionConfig),
|
|
245
|
+
$meta,
|
|
246
|
+
) as Promise<{deleted: number}>
|
|
247
|
+
).catch(() => undefined);
|
|
248
|
+
}
|
|
249
|
+
await recordAudit({actorId, sessionId, isSuccess: true});
|
|
250
|
+
return tokenResult;
|
|
251
|
+
};
|
|
252
|
+
});
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type ITokenResult,
|
|
5
|
+
type SessionConfig,
|
|
6
|
+
SESSION_COOKIE_DEFAULT,
|
|
7
|
+
sessionCleanupParams,
|
|
8
|
+
} from './sessionLib.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Session-restore endpoint (JSON-RPC `login.token.restore`, `auth: 'login'`).
|
|
12
|
+
*
|
|
13
|
+
* Lets the UI skip the login screen on reload when a live session exists.
|
|
14
|
+
* The restore cookie is HttpOnly + Secure + SameSite=Lax and Path-scoped to
|
|
15
|
+
* this URL ONLY — it is never sent to other endpoints and cannot be read by
|
|
16
|
+
* JavaScript. It carries an opaque random handle (only its SHA-256 digest is
|
|
17
|
+
* stored on the session row), which is exchanged here for fresh tokens:
|
|
18
|
+
* 1. read the cookie → `access.session.restore` validates the session
|
|
19
|
+
* (not revoked / not expired / not inactive, user active + `accessLogin`
|
|
20
|
+
* eligibility) and touches activity,
|
|
21
|
+
* 2. `access.permission.list` re-resolves the CURRENT permission set,
|
|
22
|
+
* 3. fresh access + refresh tokens are minted, a NEW cookie handle is
|
|
23
|
+
* issued (rotation — a captured cookie cannot be reused) and the token
|
|
24
|
+
* hash is rotated via `access.session.restore`.
|
|
25
|
+
*
|
|
26
|
+
* Wire: `login.token.restore`.
|
|
27
|
+
*/
|
|
28
|
+
export default handler(
|
|
29
|
+
({
|
|
30
|
+
errors,
|
|
31
|
+
config,
|
|
32
|
+
lib: {
|
|
33
|
+
methods = {},
|
|
34
|
+
token,
|
|
35
|
+
crockfordEncode,
|
|
36
|
+
sessionCookieOptions,
|
|
37
|
+
newCookieHandle,
|
|
38
|
+
readSessionCookie,
|
|
39
|
+
sha256Hex,
|
|
40
|
+
},
|
|
41
|
+
}) => {
|
|
42
|
+
const sessionConfig = config as SessionConfig;
|
|
43
|
+
const SESSION_COOKIE = sessionConfig.session?.cookieName ?? SESSION_COOKIE_DEFAULT;
|
|
44
|
+
const inactivityTimeout = sessionConfig.expire?.inactivity;
|
|
45
|
+
return async function loginTokenRestore(params: Record<string, never>, $meta: IMeta) {
|
|
46
|
+
const ipAddress = ($meta as {ipAddress?: string}).ipAddress;
|
|
47
|
+
const cookieHandle = readSessionCookie($meta, SESSION_COOKIE);
|
|
48
|
+
const cookieOptions = sessionCookieOptions(sessionConfig);
|
|
49
|
+
const recordAudit = (entry: {
|
|
50
|
+
actorId?: string;
|
|
51
|
+
sessionId?: string;
|
|
52
|
+
isSuccess: boolean;
|
|
53
|
+
failureReason?: string;
|
|
54
|
+
}) =>
|
|
55
|
+
methods.auditRecord
|
|
56
|
+
? (
|
|
57
|
+
methods.auditRecord(
|
|
58
|
+
{
|
|
59
|
+
audit: [
|
|
60
|
+
{
|
|
61
|
+
...entry,
|
|
62
|
+
actionName: 'login.restore',
|
|
63
|
+
ipAddress,
|
|
64
|
+
statusCode: entry.isSuccess ? 200 : 401,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
$meta,
|
|
69
|
+
) as Promise<{inserted: number}>
|
|
70
|
+
).catch(() => undefined)
|
|
71
|
+
: Promise.resolve(undefined);
|
|
72
|
+
|
|
73
|
+
if (!cookieHandle) {
|
|
74
|
+
await recordAudit({isSuccess: false, failureReason: 'login.invalidCookie'});
|
|
75
|
+
throw errors['login.invalidCookie']();
|
|
76
|
+
}
|
|
77
|
+
if (!methods.sessionRestore) {
|
|
78
|
+
await recordAudit({
|
|
79
|
+
isSuccess: false,
|
|
80
|
+
failureReason: 'login.configurationError',
|
|
81
|
+
});
|
|
82
|
+
throw errors['login.configurationError']({params: {method: 'sessionRestore'}});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const newHandle = newCookieHandle();
|
|
86
|
+
const check = (await methods.sessionRestore(
|
|
87
|
+
{
|
|
88
|
+
cookieHash: sha256Hex(cookieHandle),
|
|
89
|
+
newCookieHash: sha256Hex(newHandle),
|
|
90
|
+
touch: true,
|
|
91
|
+
inactivityTimeout,
|
|
92
|
+
},
|
|
93
|
+
$meta,
|
|
94
|
+
)) as {
|
|
95
|
+
valid: boolean;
|
|
96
|
+
reason?:
|
|
97
|
+
| 'notFound'
|
|
98
|
+
| 'revoked'
|
|
99
|
+
| 'expired'
|
|
100
|
+
| 'inactive'
|
|
101
|
+
| 'userInactive'
|
|
102
|
+
| 'loginNotAllowed';
|
|
103
|
+
sessionId?: string;
|
|
104
|
+
userId?: string;
|
|
105
|
+
credentialId?: number;
|
|
106
|
+
};
|
|
107
|
+
if (!check.valid) {
|
|
108
|
+
const reason = check.reason ?? 'notFound';
|
|
109
|
+
const errorType =
|
|
110
|
+
reason === 'revoked'
|
|
111
|
+
? 'login.sessionRevoked'
|
|
112
|
+
: reason === 'inactive'
|
|
113
|
+
? 'login.sessionInactive'
|
|
114
|
+
: reason === 'expired'
|
|
115
|
+
? 'login.sessionExpired'
|
|
116
|
+
: reason === 'userInactive'
|
|
117
|
+
? 'login.userInactive'
|
|
118
|
+
: reason === 'loginNotAllowed'
|
|
119
|
+
? 'login.loginNotAllowed'
|
|
120
|
+
: 'login.sessionNotFound';
|
|
121
|
+
await recordAudit({isSuccess: false, failureReason: errorType});
|
|
122
|
+
throw errors[errorType]();
|
|
123
|
+
}
|
|
124
|
+
const sessionId = check.sessionId!;
|
|
125
|
+
// Same actorId format as login — crockford-encoded user key (JWT `sub`).
|
|
126
|
+
const actorId = check.userId
|
|
127
|
+
? crockfordEncode(Buffer.from(check.userId.replace(/-/g, ''), 'hex'))
|
|
128
|
+
: '';
|
|
129
|
+
|
|
130
|
+
// Current permission set — no stale claims on a restored session.
|
|
131
|
+
// Optional: without a configured `permissionList` (lightweight suite)
|
|
132
|
+
// the restored token simply carries no permissions.
|
|
133
|
+
let permissionMap = '';
|
|
134
|
+
let actions: string[] = [];
|
|
135
|
+
let isActive = true;
|
|
136
|
+
if (methods.permissionList) {
|
|
137
|
+
const resolved = (await methods.permissionList({userId: check.userId!}, $meta)) as {
|
|
138
|
+
permissionMap: string;
|
|
139
|
+
actions: string[];
|
|
140
|
+
isActive: boolean;
|
|
141
|
+
};
|
|
142
|
+
permissionMap = resolved.permissionMap;
|
|
143
|
+
actions = resolved.actions;
|
|
144
|
+
isActive = resolved.isActive;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Login-eligibility gates on restore: a deactivated user — or one
|
|
148
|
+
// who no longer holds the `accessLogin` action — cannot resume a
|
|
149
|
+
// session via the cookie.
|
|
150
|
+
if (methods.permissionList) {
|
|
151
|
+
if (!isActive) {
|
|
152
|
+
await recordAudit({isSuccess: false, failureReason: 'login.userInactive'});
|
|
153
|
+
throw errors['login.userInactive']();
|
|
154
|
+
}
|
|
155
|
+
if (!actions.includes('accessLogin')) {
|
|
156
|
+
await recordAudit({isSuccess: false, failureReason: 'login.loginNotAllowed'});
|
|
157
|
+
throw errors['login.loginNotAllowed']();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Best-effort profile resolution so the UI can apply the user's
|
|
162
|
+
// preferred language right after restore. Optional — yields 'en'.
|
|
163
|
+
let profile: {actorId?: string; language?: string} | undefined;
|
|
164
|
+
let language = 'en';
|
|
165
|
+
if (methods.profileGet) {
|
|
166
|
+
try {
|
|
167
|
+
const profileData = (await methods.profileGet(
|
|
168
|
+
{},
|
|
169
|
+
{...$meta, auth: {...$meta.auth, actorId}},
|
|
170
|
+
)) as {preferredLanguage?: string | null} | undefined;
|
|
171
|
+
language = profileData?.preferredLanguage ?? 'en';
|
|
172
|
+
profile = {actorId, language};
|
|
173
|
+
} catch {
|
|
174
|
+
// Profile is optional restore metadata — never fails the restore.
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const tokenResult = (await token({
|
|
179
|
+
clientId: '',
|
|
180
|
+
actorId,
|
|
181
|
+
sessionId,
|
|
182
|
+
language,
|
|
183
|
+
refresh: '',
|
|
184
|
+
permissionMap,
|
|
185
|
+
mlek: $meta?.auth?.mlek,
|
|
186
|
+
mlsk: $meta?.auth?.mlsk,
|
|
187
|
+
actions,
|
|
188
|
+
profile,
|
|
189
|
+
})) as ITokenResult;
|
|
190
|
+
if (methods.sessionCleanup) {
|
|
191
|
+
await (
|
|
192
|
+
methods.sessionCleanup(sessionCleanupParams(sessionConfig), $meta) as Promise<{
|
|
193
|
+
deleted: number;
|
|
194
|
+
}>
|
|
195
|
+
).catch(() => undefined);
|
|
196
|
+
}
|
|
197
|
+
await recordAudit({actorId, sessionId, isSuccess: true});
|
|
198
|
+
// Rotate the restore cookie (one-time use) via $meta.httpResponse.
|
|
199
|
+
$meta.httpResponse = {
|
|
200
|
+
state: [[SESSION_COOKIE, newHandle, cookieOptions]],
|
|
201
|
+
};
|
|
202
|
+
return tokenResult;
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {type IMeta, handler} from '@feasibleone/blong';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type SessionConfig,
|
|
5
|
+
SESSION_COOKIE_DEFAULT,
|
|
6
|
+
SESSION_COOKIE_PATH_DEFAULT,
|
|
7
|
+
sessionCleanupParams,
|
|
8
|
+
} from './sessionLib.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Logout endpoint (JSON-RPC `login.token.revoke`).
|
|
12
|
+
*
|
|
13
|
+
* Revokes the caller's session (`access.session.close`) and clears the
|
|
14
|
+
* restore cookie. Already-issued access tokens remain valid until they
|
|
15
|
+
* expire — renewal is what refuses revoked sessions — so this is the
|
|
16
|
+
* "closing a session leaves an active token for up to the refresh interval"
|
|
17
|
+
* behaviour. Critical operations that must not run on a closed session call
|
|
18
|
+
* `access.session.verify` explicitly.
|
|
19
|
+
*
|
|
20
|
+
* Authenticated (bearer) route — the session id comes from `$meta.auth.sessionId`
|
|
21
|
+
* unless explicitly passed.
|
|
22
|
+
*
|
|
23
|
+
* Wire: `login.token.revoke`.
|
|
24
|
+
*/
|
|
25
|
+
export default handler(({config, lib: {methods = {}}}) => {
|
|
26
|
+
const sessionConfig = config as SessionConfig;
|
|
27
|
+
const SESSION_COOKIE = sessionConfig.session?.cookieName ?? SESSION_COOKIE_DEFAULT;
|
|
28
|
+
const SESSION_COOKIE_PATH = sessionConfig.session?.cookiePath ?? SESSION_COOKIE_PATH_DEFAULT;
|
|
29
|
+
return async function loginTokenRevoke({sessionId}: {sessionId?: string}, $meta: IMeta) {
|
|
30
|
+
const auth = ($meta as {auth?: {sessionId?: string; actorId?: string}}).auth;
|
|
31
|
+
const targetSessionId = sessionId ?? auth?.sessionId;
|
|
32
|
+
if (targetSessionId && methods.sessionClose) {
|
|
33
|
+
await (methods.sessionClose({sessionId: targetSessionId}, $meta) as Promise<{
|
|
34
|
+
success: boolean;
|
|
35
|
+
}>);
|
|
36
|
+
}
|
|
37
|
+
if (methods.sessionCleanup) {
|
|
38
|
+
await (
|
|
39
|
+
methods.sessionCleanup(sessionCleanupParams(sessionConfig), $meta) as Promise<{
|
|
40
|
+
deleted: number;
|
|
41
|
+
}>
|
|
42
|
+
).catch(() => undefined);
|
|
43
|
+
}
|
|
44
|
+
if (methods.auditRecord) {
|
|
45
|
+
await (
|
|
46
|
+
methods.auditRecord(
|
|
47
|
+
{
|
|
48
|
+
audit: [
|
|
49
|
+
{
|
|
50
|
+
actorId: auth?.actorId,
|
|
51
|
+
sessionId: targetSessionId,
|
|
52
|
+
actionName: 'logout',
|
|
53
|
+
ipAddress: ($meta as {ipAddress?: string}).ipAddress,
|
|
54
|
+
isSuccess: true,
|
|
55
|
+
statusCode: 200,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
$meta,
|
|
60
|
+
) as Promise<{inserted: number}>
|
|
61
|
+
).catch(() => undefined);
|
|
62
|
+
}
|
|
63
|
+
// Clear the path-scoped restore cookie via $meta.httpResponse.
|
|
64
|
+
$meta.httpResponse = {
|
|
65
|
+
unstate: [[SESSION_COOKIE, {path: SESSION_COOKIE_PATH}]],
|
|
66
|
+
};
|
|
67
|
+
return {success: true};
|
|
68
|
+
};
|
|
69
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {library} from '@feasibleone/blong';
|
|
2
|
+
import cbc from 'ut-function.cbc';
|
|
3
|
+
|
|
4
|
+
export default library<{keys: {refresh: string; refreshCbc: ReturnType<typeof cbc>}}>(
|
|
5
|
+
({
|
|
6
|
+
config: {
|
|
7
|
+
keys: {refresh, refreshCbc = cbc(refresh)},
|
|
8
|
+
},
|
|
9
|
+
handler: {errorLoginRefreshTokenExpired},
|
|
10
|
+
}) => ({
|
|
11
|
+
writeRefresh(params: {refresh: number}) {
|
|
12
|
+
return refreshCbc
|
|
13
|
+
.encrypt(JSON.stringify({...params, expire: Date.now() + params.refresh * 1000}))
|
|
14
|
+
.toString('base64');
|
|
15
|
+
},
|
|
16
|
+
readRefresh(token: string) {
|
|
17
|
+
const result = JSON.parse(refreshCbc.decrypt(Buffer.from(token, 'base64')));
|
|
18
|
+
if (result.expire <= Date.now()) throw errorLoginRefreshTokenExpired();
|
|
19
|
+
return result;
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
);
|