strapi-plugin-magic-sessionmanager 4.22.1 → 4.23.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 +31 -0
- package/README.md +116 -82
- package/dist/admin/ActiveImpersonationsCard-CbyOAlrL.mjs +396 -0
- package/dist/admin/ActiveImpersonationsCard-D1CVmV-w.js +395 -0
- package/dist/admin/{Analytics-CIHgaURb.mjs → Analytics-DGAHD19E.mjs} +1 -1
- package/dist/admin/{Analytics-B7BAjApz.js → Analytics-_WA68oHb.js} +1 -1
- package/dist/admin/{App-DJkJC06E.js → App-CfhAQ17e.js} +304 -104
- package/dist/admin/{App-CPE-CQGz.mjs → App-VB66H73A.mjs} +276 -76
- package/dist/admin/ImpersonationTabBanner-B6Ps5-oe.mjs +244 -0
- package/dist/admin/ImpersonationTabBanner-DdwXNmh4.js +244 -0
- package/dist/admin/{OnlineUsersWidget-Dou2hxGY.mjs → OnlineUsersWidget-2u-fYMb7.mjs} +1 -1
- package/dist/admin/{OnlineUsersWidget-CcjNfzzf.js → OnlineUsersWidget-CmBqFLAA.js} +1 -1
- package/dist/admin/{Settings--eEiHv_a.js → Settings-BBs2FDp7.js} +27 -229
- package/dist/admin/{Settings-h_exWUiI.mjs → Settings-CKDNGJhl.mjs} +19 -221
- package/dist/admin/{de-Wz5Emtrb.mjs → de-CK-HuY41.mjs} +42 -4
- package/dist/admin/{de-oBmcAcXj.js → de-CSRQPn6F.js} +42 -4
- package/dist/admin/{en-B1TN5F62.js → en-DCtAgGEa.js} +42 -4
- package/dist/admin/{en-B_1Ka64U.mjs → en-KZ5ajaAR.mjs} +42 -4
- package/dist/admin/{es-CVgw4zsj.mjs → es-B7k8taWV.mjs} +42 -4
- package/dist/admin/{es-Bb515J0p.js → es-CKT00l4K.js} +42 -4
- package/dist/admin/{fr-yDdwunmg.js → fr-B2EdBFSb.js} +42 -4
- package/dist/admin/{fr-0AgMxTFc.mjs → fr-Q2gmYqCn.mjs} +42 -4
- package/dist/admin/{index-DVQ7tdTC.mjs → index-BPbs1GyP.mjs} +365 -57
- package/dist/admin/{index-qtN2dOm4.js → index-Cho0XGZ1.js} +319 -11
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +2 -2
- package/dist/admin/{pt-BAB07LY-.js → pt-CcMUcqDy.js} +42 -4
- package/dist/admin/{pt-DtM74ffN.mjs → pt-CmxZTaf2.mjs} +42 -4
- package/dist/admin/useBreakpoint-CoBfu0Ov.js +62 -0
- package/dist/admin/useBreakpoint-CuyIc2qy.mjs +63 -0
- package/dist/server/index.js +867 -10
- package/dist/server/index.mjs +867 -10
- package/package.json +6 -2
- package/dist/admin/dashboardModel-DNT_l-Bk.js +0 -169
- package/dist/admin/dashboardModel-fApbKGay.mjs +0 -170
package/dist/server/index.mjs
CHANGED
|
@@ -2536,7 +2536,11 @@ var require_security_log_model = __commonJS({
|
|
|
2536
2536
|
IMPERSONATION_STARTED: "impersonation.started",
|
|
2537
2537
|
IMPERSONATION_ENDED: "impersonation.ended",
|
|
2538
2538
|
IMPERSONATION_EXPIRED: "impersonation.expired",
|
|
2539
|
-
IMPERSONATION_REFUSED: "impersonation.refused"
|
|
2539
|
+
IMPERSONATION_REFUSED: "impersonation.refused",
|
|
2540
|
+
// 4.23: a change made while signed in as another admin. Actor = the real
|
|
2541
|
+
// admin, target and `meta.impersonating` = the admin signed in as, plus
|
|
2542
|
+
// the method and path (no query, no body).
|
|
2543
|
+
IMPERSONATION_WRITE: "impersonation.write"
|
|
2540
2544
|
});
|
|
2541
2545
|
var KEEP_WITHOUT_ACTOR_EVENTS = Object.freeze([
|
|
2542
2546
|
SECURITY_EVENTS.ADMIN_SESSION_REVOKED,
|
|
@@ -2545,7 +2549,8 @@ var require_security_log_model = __commonJS({
|
|
|
2545
2549
|
SECURITY_EVENTS.IMPERSONATION_STARTED,
|
|
2546
2550
|
SECURITY_EVENTS.IMPERSONATION_ENDED,
|
|
2547
2551
|
SECURITY_EVENTS.IMPERSONATION_EXPIRED,
|
|
2548
|
-
SECURITY_EVENTS.IMPERSONATION_REFUSED
|
|
2552
|
+
SECURITY_EVENTS.IMPERSONATION_REFUSED,
|
|
2553
|
+
SECURITY_EVENTS.IMPERSONATION_WRITE
|
|
2549
2554
|
]);
|
|
2550
2555
|
function dropRankOf(event) {
|
|
2551
2556
|
if (event === SECURITY_EVENTS.ADMIN_SIGN_IN_FAILED || event === SECURITY_EVENTS.ADMIN_LOCKOUT || USER_SIGN_IN_EVENTS.includes(event)) {
|
|
@@ -2585,12 +2590,20 @@ var require_security_log_model = __commonJS({
|
|
|
2585
2590
|
SECURITY_EVENTS.LOG_RETENTION_PURGED,
|
|
2586
2591
|
SECURITY_EVENTS.CONFIG_WEAK_KEY
|
|
2587
2592
|
]);
|
|
2593
|
+
var ADMIN_IMPERSONATION_EVENTS = Object.freeze([
|
|
2594
|
+
SECURITY_EVENTS.IMPERSONATION_STARTED,
|
|
2595
|
+
SECURITY_EVENTS.IMPERSONATION_ENDED,
|
|
2596
|
+
SECURITY_EVENTS.IMPERSONATION_EXPIRED,
|
|
2597
|
+
SECURITY_EVENTS.IMPERSONATION_REFUSED,
|
|
2598
|
+
SECURITY_EVENTS.IMPERSONATION_WRITE
|
|
2599
|
+
]);
|
|
2588
2600
|
var SIGN_IN_FILTERS = Object.freeze({
|
|
2589
2601
|
all: [...ADMIN_SIGN_IN_EVENTS, ...ADMIN_LOG_EVENTS],
|
|
2590
2602
|
succeeded: [SECURITY_EVENTS.ADMIN_SIGN_IN],
|
|
2591
2603
|
failed: [SECURITY_EVENTS.ADMIN_SIGN_IN_FAILED, SECURITY_EVENTS.ADMIN_LOCKOUT],
|
|
2592
2604
|
"signed-out": [SECURITY_EVENTS.ADMIN_SIGN_OUT, SECURITY_EVENTS.ADMIN_SESSION_REVOKED],
|
|
2593
|
-
changes: ADMIN_LOG_EVENTS
|
|
2605
|
+
changes: ADMIN_LOG_EVENTS,
|
|
2606
|
+
impersonation: ADMIN_IMPERSONATION_EVENTS
|
|
2594
2607
|
});
|
|
2595
2608
|
var CONTENT_ACTIONS = Object.freeze([
|
|
2596
2609
|
"view",
|
|
@@ -2792,6 +2805,7 @@ var require_security_log_model = __commonJS({
|
|
|
2792
2805
|
USER_SIGN_IN_EVENTS,
|
|
2793
2806
|
ACTOR_TYPES,
|
|
2794
2807
|
ADMIN_CONTENT_EVENTS,
|
|
2808
|
+
ADMIN_IMPERSONATION_EVENTS,
|
|
2795
2809
|
ADMIN_LOG_EVENTS,
|
|
2796
2810
|
ADMIN_SIGN_IN_EVENTS,
|
|
2797
2811
|
CONTENT_ACTIONS,
|
|
@@ -3264,10 +3278,20 @@ var require_register = __commonJS({
|
|
|
3264
3278
|
// their own.
|
|
3265
3279
|
{
|
|
3266
3280
|
section: "plugins",
|
|
3267
|
-
displayName: "View
|
|
3281
|
+
displayName: "View admin permissions (read-only preview)",
|
|
3268
3282
|
uid: "impersonate.admins",
|
|
3269
3283
|
subCategory: "Impersonation",
|
|
3270
3284
|
pluginName: "magic-sessionmanager"
|
|
3285
|
+
},
|
|
3286
|
+
// "Sign in as this admin" (4.23): a real admin session of another admin
|
|
3287
|
+
// in a separate tab, read-only unless changes are allowed. The same
|
|
3288
|
+
// anti-escalation rules as the preview; only on tested Strapi releases.
|
|
3289
|
+
{
|
|
3290
|
+
section: "plugins",
|
|
3291
|
+
displayName: "Sign in as another admin",
|
|
3292
|
+
uid: "impersonate.admins.sign-in",
|
|
3293
|
+
subCategory: "Impersonation",
|
|
3294
|
+
pluginName: "magic-sessionmanager"
|
|
3271
3295
|
}
|
|
3272
3296
|
]);
|
|
3273
3297
|
disableGraphqlShadowCrud(strapi2, log);
|
|
@@ -5946,6 +5970,237 @@ var require_impersonation_guard = __commonJS({
|
|
|
5946
5970
|
}
|
|
5947
5971
|
});
|
|
5948
5972
|
|
|
5973
|
+
// server/src/utils/admin-sign-in-rules.js
|
|
5974
|
+
var require_admin_sign_in_rules = __commonJS({
|
|
5975
|
+
"server/src/utils/admin-sign-in-rules.js"(exports, module) {
|
|
5976
|
+
"use strict";
|
|
5977
|
+
var { REASON_MAX, REASON_MIN, cleanReason, normalizePath } = require_impersonation_rules();
|
|
5978
|
+
var ADMIN_SIGN_IN_ACTION = "plugin::magic-sessionmanager.impersonate.admins.sign-in";
|
|
5979
|
+
var TESTED_STRAPI = Object.freeze({ major: 5, minMinor: 50, maxMinor: 64 });
|
|
5980
|
+
var ADMIN_SIGN_IN_DURATIONS = Object.freeze([15, 30]);
|
|
5981
|
+
var ADMIN_DEVICE_PREFIX = "msm-admin-impersonation-";
|
|
5982
|
+
var LANDING_PATH = "/plugins/magic-sessionmanager/impersonate/land";
|
|
5983
|
+
function adminSignInSupport(version) {
|
|
5984
|
+
const match = typeof version === "string" ? /^(\d+)\.(\d+)\.(\d+)/.exec(version.trim()) : null;
|
|
5985
|
+
if (!match) return { supported: false, version: null, reason: "unknown-version" };
|
|
5986
|
+
const [major, minor] = [Number(match[1]), Number(match[2])];
|
|
5987
|
+
const clean = `${match[1]}.${match[2]}.${match[3]}`;
|
|
5988
|
+
const supported = major === TESTED_STRAPI.major && minor >= TESTED_STRAPI.minMinor && minor <= TESTED_STRAPI.maxMinor;
|
|
5989
|
+
return { supported, version: clean, reason: supported ? null : "untested-version" };
|
|
5990
|
+
}
|
|
5991
|
+
function allowedSignInDurations(accessTokenLifespanSeconds) {
|
|
5992
|
+
const seconds = Number(accessTokenLifespanSeconds);
|
|
5993
|
+
const lifespan = Number.isFinite(seconds) && seconds > 0 ? seconds : 30 * 60;
|
|
5994
|
+
return ADMIN_SIGN_IN_DURATIONS.filter((minutes) => minutes * 60 <= lifespan);
|
|
5995
|
+
}
|
|
5996
|
+
function validateSignInBody(body, durations) {
|
|
5997
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
5998
|
+
return { value: null, errors: [{ path: [], message: "Body must be an object" }] };
|
|
5999
|
+
}
|
|
6000
|
+
const errors = [];
|
|
6001
|
+
const source = (
|
|
6002
|
+
/** @type {Record<string, unknown>} */
|
|
6003
|
+
body
|
|
6004
|
+
);
|
|
6005
|
+
for (const key of Object.keys(source)) {
|
|
6006
|
+
if (!["reason", "durationMinutes", "allowChanges"].includes(key)) {
|
|
6007
|
+
errors.push({ path: [key], message: "Unknown field" });
|
|
6008
|
+
}
|
|
6009
|
+
}
|
|
6010
|
+
const reason = cleanReason(source.reason);
|
|
6011
|
+
if (reason === null) {
|
|
6012
|
+
errors.push({
|
|
6013
|
+
path: ["reason"],
|
|
6014
|
+
message: `reason must be ${REASON_MIN} to ${REASON_MAX} characters`
|
|
6015
|
+
});
|
|
6016
|
+
}
|
|
6017
|
+
const duration = source.durationMinutes;
|
|
6018
|
+
if (typeof duration !== "number" || !durations.includes(duration)) {
|
|
6019
|
+
errors.push({
|
|
6020
|
+
path: ["durationMinutes"],
|
|
6021
|
+
message: `durationMinutes must be one of: ${durations.join(", ")}`
|
|
6022
|
+
});
|
|
6023
|
+
}
|
|
6024
|
+
if (source.allowChanges !== void 0 && typeof source.allowChanges !== "boolean") {
|
|
6025
|
+
errors.push({ path: ["allowChanges"], message: "allowChanges must be a boolean" });
|
|
6026
|
+
}
|
|
6027
|
+
if (errors.length) return { value: null, errors };
|
|
6028
|
+
return {
|
|
6029
|
+
value: {
|
|
6030
|
+
reason: (
|
|
6031
|
+
/** @type {string} */
|
|
6032
|
+
reason
|
|
6033
|
+
),
|
|
6034
|
+
durationMinutes: (
|
|
6035
|
+
/** @type {number} */
|
|
6036
|
+
duration
|
|
6037
|
+
),
|
|
6038
|
+
allowChanges: source.allowChanges === true
|
|
6039
|
+
},
|
|
6040
|
+
errors
|
|
6041
|
+
};
|
|
6042
|
+
}
|
|
6043
|
+
var SAFE_METHODS = Object.freeze(["GET", "HEAD", "OPTIONS"]);
|
|
6044
|
+
var READ_ONLY_POSTS = Object.freeze(["/admin/permissions/check"]);
|
|
6045
|
+
var IDENTITY_PREFIXES = Object.freeze([
|
|
6046
|
+
"/admin/users",
|
|
6047
|
+
"/admin/roles",
|
|
6048
|
+
"/admin/api-tokens",
|
|
6049
|
+
"/admin/transfer",
|
|
6050
|
+
"/admin/providers",
|
|
6051
|
+
"/admin/register",
|
|
6052
|
+
"/admin/register-admin",
|
|
6053
|
+
"/admin/registration-info",
|
|
6054
|
+
"/admin/forgot-password",
|
|
6055
|
+
"/admin/reset-password",
|
|
6056
|
+
"/admin/renew-token",
|
|
6057
|
+
"/admin/login",
|
|
6058
|
+
"/admin/sessions",
|
|
6059
|
+
"/magic-sessionmanager"
|
|
6060
|
+
]);
|
|
6061
|
+
var SELF_PATHS = Object.freeze({
|
|
6062
|
+
state: "/magic-sessionmanager/admin-impersonation/self",
|
|
6063
|
+
end: "/magic-sessionmanager/admin-impersonation/self/end"
|
|
6064
|
+
});
|
|
6065
|
+
function under(path, prefix) {
|
|
6066
|
+
return path === prefix || path.startsWith(`${prefix}/`);
|
|
6067
|
+
}
|
|
6068
|
+
function classifyAdminImpersonationRequest(method, rawPath) {
|
|
6069
|
+
const verb = String(method || "").toUpperCase();
|
|
6070
|
+
const path = normalizePath(rawPath);
|
|
6071
|
+
if (path === SELF_PATHS.state || path === SELF_PATHS.end) return "self";
|
|
6072
|
+
if (verb === "POST" && path === "/admin/logout") return "logout";
|
|
6073
|
+
if (SAFE_METHODS.includes(verb)) return "read";
|
|
6074
|
+
if (verb === "POST" && READ_ONLY_POSTS.includes(path)) return "read";
|
|
6075
|
+
if (IDENTITY_PREFIXES.some((prefix) => under(path, prefix))) return "identity";
|
|
6076
|
+
return "write";
|
|
6077
|
+
}
|
|
6078
|
+
function recordedPath(rawPath) {
|
|
6079
|
+
return String(rawPath || "").split("?")[0].slice(0, 200);
|
|
6080
|
+
}
|
|
6081
|
+
module.exports = {
|
|
6082
|
+
ADMIN_DEVICE_PREFIX,
|
|
6083
|
+
ADMIN_SIGN_IN_ACTION,
|
|
6084
|
+
ADMIN_SIGN_IN_DURATIONS,
|
|
6085
|
+
IDENTITY_PREFIXES,
|
|
6086
|
+
LANDING_PATH,
|
|
6087
|
+
READ_ONLY_POSTS,
|
|
6088
|
+
SELF_PATHS,
|
|
6089
|
+
TESTED_STRAPI,
|
|
6090
|
+
adminSignInSupport,
|
|
6091
|
+
allowedSignInDurations,
|
|
6092
|
+
classifyAdminImpersonationRequest,
|
|
6093
|
+
recordedPath,
|
|
6094
|
+
validateSignInBody
|
|
6095
|
+
};
|
|
6096
|
+
}
|
|
6097
|
+
});
|
|
6098
|
+
|
|
6099
|
+
// server/src/middlewares/admin-impersonation-guard.js
|
|
6100
|
+
var require_admin_impersonation_guard = __commonJS({
|
|
6101
|
+
"server/src/middlewares/admin-impersonation-guard.js"(exports, module) {
|
|
6102
|
+
"use strict";
|
|
6103
|
+
var { extractBearerToken } = require_extract_token();
|
|
6104
|
+
var { IMPERSONATION_HEADER, normalizePath } = require_impersonation_rules();
|
|
6105
|
+
var { classifyAdminImpersonationRequest, recordedPath } = require_admin_sign_in_rules();
|
|
6106
|
+
function reply(ctx, status, name, message, reason) {
|
|
6107
|
+
ctx.status = status;
|
|
6108
|
+
ctx.body = { data: null, error: { status, name, message, details: { reason } } };
|
|
6109
|
+
}
|
|
6110
|
+
module.exports = (_config, { strapi: strapi2 }) => {
|
|
6111
|
+
const service = () => strapi2.plugin("magic-sessionmanager").service("impersonation");
|
|
6112
|
+
return async function adminImpersonationGuard(ctx, next) {
|
|
6113
|
+
const prefix = normalizePath(strapi2.config.get("api.rest.prefix", "/api") || "/api");
|
|
6114
|
+
const path = normalizePath(ctx.path);
|
|
6115
|
+
const base = prefix === "/" ? "" : prefix;
|
|
6116
|
+
if (base && (path === base || path.startsWith(`${base}/`))) return next();
|
|
6117
|
+
const bearer = extractBearerToken(ctx);
|
|
6118
|
+
if (!bearer) return next();
|
|
6119
|
+
let impersonationId;
|
|
6120
|
+
let impersonation;
|
|
6121
|
+
try {
|
|
6122
|
+
impersonationId = await service().adminImpersonationOfToken(bearer);
|
|
6123
|
+
if (!impersonationId) return next();
|
|
6124
|
+
impersonation = impersonationId === "unknown" ? null : await service().loadAdminForGuard(impersonationId);
|
|
6125
|
+
} catch {
|
|
6126
|
+
return reply(
|
|
6127
|
+
ctx,
|
|
6128
|
+
503,
|
|
6129
|
+
"ServiceUnavailableError",
|
|
6130
|
+
"Session check unavailable",
|
|
6131
|
+
"impersonation-check"
|
|
6132
|
+
);
|
|
6133
|
+
}
|
|
6134
|
+
ctx.set(IMPERSONATION_HEADER, "true");
|
|
6135
|
+
const kind = classifyAdminImpersonationRequest(ctx.method, path);
|
|
6136
|
+
if (!impersonation || impersonation.state !== "active") {
|
|
6137
|
+
if (kind === "logout") {
|
|
6138
|
+
ctx.body = { data: {} };
|
|
6139
|
+
return void 0;
|
|
6140
|
+
}
|
|
6141
|
+
return reply(ctx, 401, "UnauthorizedError", "Impersonation ended", "impersonation-ended");
|
|
6142
|
+
}
|
|
6143
|
+
if (Date.now() >= impersonation.expiresAt) {
|
|
6144
|
+
try {
|
|
6145
|
+
await service().end(impersonation.id, { reason: "expired" });
|
|
6146
|
+
} catch {
|
|
6147
|
+
}
|
|
6148
|
+
if (kind === "logout") {
|
|
6149
|
+
ctx.body = { data: {} };
|
|
6150
|
+
return void 0;
|
|
6151
|
+
}
|
|
6152
|
+
return reply(ctx, 401, "UnauthorizedError", "Impersonation ended", "impersonation-expired");
|
|
6153
|
+
}
|
|
6154
|
+
if (kind === "logout") {
|
|
6155
|
+
await service().end(impersonation.id, { reason: "ended", endedBy: impersonation.actorId });
|
|
6156
|
+
ctx.body = { data: {} };
|
|
6157
|
+
return void 0;
|
|
6158
|
+
}
|
|
6159
|
+
const method = String(ctx.method).toUpperCase();
|
|
6160
|
+
const recorded = recordedPath(ctx.path);
|
|
6161
|
+
if (kind === "identity" || kind === "write" && !impersonation.allowChanges) {
|
|
6162
|
+
const reason = kind === "identity" ? "identity" : "read-only";
|
|
6163
|
+
service().recordRefusedAdminCall(impersonation, reason, { method, path: recorded });
|
|
6164
|
+
return reply(
|
|
6165
|
+
ctx,
|
|
6166
|
+
403,
|
|
6167
|
+
"ForbiddenError",
|
|
6168
|
+
kind === "identity" ? "Not allowed while signed in as another admin" : "Read-only: changes were not allowed for this sign-in",
|
|
6169
|
+
`impersonation-${reason}`
|
|
6170
|
+
);
|
|
6171
|
+
}
|
|
6172
|
+
ctx.state.magicAdminImpersonation = {
|
|
6173
|
+
id: impersonation.id,
|
|
6174
|
+
actorId: impersonation.actorId,
|
|
6175
|
+
targetId: impersonation.targetId,
|
|
6176
|
+
allowChanges: impersonation.allowChanges,
|
|
6177
|
+
expiresAt: impersonation.expiresAt
|
|
6178
|
+
};
|
|
6179
|
+
let failedStatus = 0;
|
|
6180
|
+
try {
|
|
6181
|
+
await next();
|
|
6182
|
+
} catch (err) {
|
|
6183
|
+
failedStatus = Number(
|
|
6184
|
+
/** @type {any} */
|
|
6185
|
+
err?.status
|
|
6186
|
+
) || 500;
|
|
6187
|
+
throw err;
|
|
6188
|
+
} finally {
|
|
6189
|
+
ctx.set(IMPERSONATION_HEADER, "true");
|
|
6190
|
+
if (kind === "write") {
|
|
6191
|
+
service().recordAdminWrite(impersonation, {
|
|
6192
|
+
method,
|
|
6193
|
+
path: recorded,
|
|
6194
|
+
status: failedStatus || Number(ctx.status) || 0
|
|
6195
|
+
});
|
|
6196
|
+
}
|
|
6197
|
+
}
|
|
6198
|
+
return void 0;
|
|
6199
|
+
};
|
|
6200
|
+
};
|
|
6201
|
+
}
|
|
6202
|
+
});
|
|
6203
|
+
|
|
5949
6204
|
// server/src/middlewares/session-rejection-headers.js
|
|
5950
6205
|
var require_session_rejection_headers = __commonJS({
|
|
5951
6206
|
"server/src/middlewares/session-rejection-headers.js"(exports, module) {
|
|
@@ -10269,6 +10524,7 @@ var require_bootstrap = __commonJS({
|
|
|
10269
10524
|
);
|
|
10270
10525
|
log.info("[TIME] Dynamic cleanup + retention scheduled");
|
|
10271
10526
|
strapi2.server.use(require_impersonation_guard()({}, { strapi: strapi2 }));
|
|
10527
|
+
strapi2.server.use(require_admin_impersonation_guard()({}, { strapi: strapi2 }));
|
|
10272
10528
|
const impersonationSweep = setInterval(() => {
|
|
10273
10529
|
strapi2.plugin("magic-sessionmanager").service("impersonation").sweep().catch((err) => log.warn("[IMPERSONATION] Sweep failed:", err?.name || "Error"));
|
|
10274
10530
|
}, 30 * 1e3);
|
|
@@ -12483,7 +12739,7 @@ var require_schema4 = __commonJS({
|
|
|
12483
12739
|
singularName: "impersonation",
|
|
12484
12740
|
pluralName: "impersonations",
|
|
12485
12741
|
displayName: "Impersonation",
|
|
12486
|
-
description: "Time-limited impersonations of app users
|
|
12742
|
+
description: "Time-limited impersonations of app users, permission previews of admins and sign-ins as another admin, started by an admin with a reason"
|
|
12487
12743
|
},
|
|
12488
12744
|
options: {
|
|
12489
12745
|
draftAndPublish: false,
|
|
@@ -12546,13 +12802,17 @@ var require_schema4 = __commonJS({
|
|
|
12546
12802
|
},
|
|
12547
12803
|
handoff: {
|
|
12548
12804
|
type: "enumeration",
|
|
12549
|
-
enum: ["frontend", "token", "preview"],
|
|
12805
|
+
enum: ["frontend", "token", "preview", "admin-session"],
|
|
12550
12806
|
required: true
|
|
12551
12807
|
},
|
|
12552
12808
|
notifyUser: {
|
|
12553
12809
|
type: "boolean",
|
|
12554
12810
|
default: false
|
|
12555
12811
|
},
|
|
12812
|
+
allowChanges: {
|
|
12813
|
+
type: "boolean",
|
|
12814
|
+
default: false
|
|
12815
|
+
},
|
|
12556
12816
|
startedAt: {
|
|
12557
12817
|
type: "datetime",
|
|
12558
12818
|
required: true
|
|
@@ -12832,6 +13092,7 @@ var require_admin = __commonJS({
|
|
|
12832
13092
|
var SETTINGS_UPDATE_ACTION = "plugin::magic-sessionmanager.settings.update";
|
|
12833
13093
|
var IMPERSONATE_USERS_ACTION = "plugin::magic-sessionmanager.impersonate.users";
|
|
12834
13094
|
var IMPERSONATE_ADMINS_ACTION = "plugin::magic-sessionmanager.impersonate.admins";
|
|
13095
|
+
var ADMIN_SIGN_IN_ACTION = "plugin::magic-sessionmanager.impersonate.admins.sign-in";
|
|
12835
13096
|
var impersonationListPolicy = () => [
|
|
12836
13097
|
...adminPolicy(),
|
|
12837
13098
|
"plugin::magic-sessionmanager.can-impersonate"
|
|
@@ -13236,6 +13497,59 @@ var require_admin = __commonJS({
|
|
|
13236
13497
|
]
|
|
13237
13498
|
}
|
|
13238
13499
|
},
|
|
13500
|
+
// Sign in as this admin (4.23).
|
|
13501
|
+
{
|
|
13502
|
+
method: "POST",
|
|
13503
|
+
path: "/impersonation/admins/:adminId/sign-in",
|
|
13504
|
+
handler: "impersonation.signInAdmin",
|
|
13505
|
+
config: {
|
|
13506
|
+
policies: adminSessionsPolicy([ADMIN_SIGN_IN_ACTION]),
|
|
13507
|
+
middlewares: [
|
|
13508
|
+
{
|
|
13509
|
+
name: "plugin::magic-sessionmanager.rate-limit",
|
|
13510
|
+
config: { max: 10, window: 10 * 60 * 1e3 }
|
|
13511
|
+
}
|
|
13512
|
+
]
|
|
13513
|
+
}
|
|
13514
|
+
},
|
|
13515
|
+
{
|
|
13516
|
+
method: "POST",
|
|
13517
|
+
path: "/admin-impersonation/exchange",
|
|
13518
|
+
handler: "impersonation.exchangeAdmin",
|
|
13519
|
+
config: {
|
|
13520
|
+
// The code is bound to the admin who started the sign-in, who must
|
|
13521
|
+
// still hold the action.
|
|
13522
|
+
policies: adminSessionsPolicy([ADMIN_SIGN_IN_ACTION]),
|
|
13523
|
+
middlewares: [
|
|
13524
|
+
{
|
|
13525
|
+
name: "plugin::magic-sessionmanager.rate-limit",
|
|
13526
|
+
config: { max: 10, window: 6e4 }
|
|
13527
|
+
}
|
|
13528
|
+
]
|
|
13529
|
+
}
|
|
13530
|
+
},
|
|
13531
|
+
{
|
|
13532
|
+
method: "GET",
|
|
13533
|
+
path: "/admin-impersonation/self",
|
|
13534
|
+
handler: "impersonation.adminSelf",
|
|
13535
|
+
config: {
|
|
13536
|
+
// The impersonation tab holds the target's token, and the target may
|
|
13537
|
+
// not have plugin access. The admin impersonation guard recognises
|
|
13538
|
+
// the session (signed token + session marker) before routing; the
|
|
13539
|
+
// handler answers 401 to everybody else.
|
|
13540
|
+
auth: false,
|
|
13541
|
+
middlewares: adminReadRateLimit()
|
|
13542
|
+
}
|
|
13543
|
+
},
|
|
13544
|
+
{
|
|
13545
|
+
method: "POST",
|
|
13546
|
+
path: "/admin-impersonation/self/end",
|
|
13547
|
+
handler: "impersonation.endAdminSelf",
|
|
13548
|
+
config: {
|
|
13549
|
+
auth: false,
|
|
13550
|
+
middlewares: adminSignOutRateLimit()
|
|
13551
|
+
}
|
|
13552
|
+
},
|
|
13239
13553
|
{
|
|
13240
13554
|
method: "GET",
|
|
13241
13555
|
path: "/impersonation/previews/:id",
|
|
@@ -18456,6 +18770,14 @@ var require_impersonation = __commonJS({
|
|
|
18456
18770
|
resolveFrontendHandoff,
|
|
18457
18771
|
validateStartBody
|
|
18458
18772
|
} = require_impersonation_rules();
|
|
18773
|
+
var {
|
|
18774
|
+
ADMIN_DEVICE_PREFIX,
|
|
18775
|
+
ADMIN_SIGN_IN_ACTION,
|
|
18776
|
+
LANDING_PATH,
|
|
18777
|
+
adminSignInSupport,
|
|
18778
|
+
allowedSignInDurations,
|
|
18779
|
+
validateSignInBody
|
|
18780
|
+
} = require_admin_sign_in_rules();
|
|
18459
18781
|
var UID = "plugin::magic-sessionmanager.impersonation";
|
|
18460
18782
|
var USER_UID = "plugin::users-permissions.user";
|
|
18461
18783
|
var NATIVE_SESSION_UID = "admin::session";
|
|
@@ -18483,7 +18805,8 @@ var require_impersonation = __commonJS({
|
|
|
18483
18805
|
STARTED: "impersonation.started",
|
|
18484
18806
|
ENDED: "impersonation.ended",
|
|
18485
18807
|
EXPIRED: "impersonation.expired",
|
|
18486
|
-
REFUSED: "impersonation.refused"
|
|
18808
|
+
REFUSED: "impersonation.refused",
|
|
18809
|
+
WRITE: "impersonation.write"
|
|
18487
18810
|
});
|
|
18488
18811
|
function remember(map, key, value) {
|
|
18489
18812
|
map.delete(key);
|
|
@@ -18579,9 +18902,27 @@ var require_impersonation = __commonJS({
|
|
|
18579
18902
|
frontendOrigin: frontend ? frontend.origin : null,
|
|
18580
18903
|
durations: [...USER_DURATIONS],
|
|
18581
18904
|
adminDurations: [...ADMIN_DURATIONS],
|
|
18582
|
-
mode: getNativeSessionManager(strapi2) ? "refresh" : "legacy"
|
|
18905
|
+
mode: getNativeSessionManager(strapi2) ? "refresh" : "legacy",
|
|
18906
|
+
adminSignIn: service.adminSignInStatus()
|
|
18583
18907
|
};
|
|
18584
18908
|
},
|
|
18909
|
+
/**
|
|
18910
|
+
* Whether "Sign in as this admin" can run on this Strapi: a tested
|
|
18911
|
+
* release, Strapi's session manager present, and at least one duration
|
|
18912
|
+
* that fits into one admin access token.
|
|
18913
|
+
*
|
|
18914
|
+
* @returns {{supported: boolean, version: string|null, reason: string|null, durations: number[]}}
|
|
18915
|
+
*/
|
|
18916
|
+
adminSignInStatus() {
|
|
18917
|
+
const support = adminSignInSupport(strapi2.config.get("info.strapi"));
|
|
18918
|
+
const durations = allowedSignInDurations(
|
|
18919
|
+
strapi2.config.get("admin.auth.sessions.accessTokenLifespan", 30 * 60)
|
|
18920
|
+
);
|
|
18921
|
+
let reason = support.reason;
|
|
18922
|
+
if (!reason && typeof strapi2.sessionManager !== "function") reason = "no-session-manager";
|
|
18923
|
+
if (!reason && durations.length === 0) reason = "token-lifespan";
|
|
18924
|
+
return { supported: reason === null, version: support.version, reason, durations };
|
|
18925
|
+
},
|
|
18585
18926
|
/**
|
|
18586
18927
|
* Records a refused start.
|
|
18587
18928
|
*
|
|
@@ -18845,6 +19186,416 @@ var require_impersonation = __commonJS({
|
|
|
18845
19186
|
preview
|
|
18846
19187
|
};
|
|
18847
19188
|
},
|
|
19189
|
+
/**
|
|
19190
|
+
* Starts "Sign in as this admin" (4.23): checks every anti-escalation
|
|
19191
|
+
* rule, stores the impersonation with a one-time code (60 seconds,
|
|
19192
|
+
* stored hashed) and returns the landing path for a new tab. The Strapi
|
|
19193
|
+
* admin session is created only when that tab exchanges the code.
|
|
19194
|
+
*
|
|
19195
|
+
* @param {{actor: any, adminId: unknown, body: unknown, canReadAdmins: boolean,
|
|
19196
|
+
* nested?: boolean}} input Caller (with roles), target id, body; `nested` when the
|
|
19197
|
+
* request itself comes from an impersonation session.
|
|
19198
|
+
* @returns {Promise<{impersonation: object, handoff: {type: 'admin-tab', path: string,
|
|
19199
|
+
* code: string, codeExpiresAt: string}}>}
|
|
19200
|
+
* @throws {ImpersonationError} 400 invalid body; 403 off, unsupported Strapi, nested,
|
|
19201
|
+
* self, Super Admin, inactive, not a subset or already active; 404 unknown admin.
|
|
19202
|
+
* @sideeffect Creates an impersonation row; queues `impersonation.started` or `.refused`.
|
|
19203
|
+
*/
|
|
19204
|
+
async startAdminSignIn({ actor, adminId, body, canReadAdmins, nested = false }) {
|
|
19205
|
+
const actorId = String(actor.id);
|
|
19206
|
+
const refuse = (reason, status, message, extra = {}) => {
|
|
19207
|
+
service.recordRefused({
|
|
19208
|
+
actorId,
|
|
19209
|
+
targetType: "admin",
|
|
19210
|
+
targetId: adminId,
|
|
19211
|
+
reason,
|
|
19212
|
+
via: "sign-in"
|
|
19213
|
+
});
|
|
19214
|
+
return new ImpersonationError(status, message, { reason, ...extra });
|
|
19215
|
+
};
|
|
19216
|
+
if (!await service.isEnabled()) {
|
|
19217
|
+
throw refuse("disabled", 403, "Impersonation is turned off");
|
|
19218
|
+
}
|
|
19219
|
+
const support = service.adminSignInStatus();
|
|
19220
|
+
if (!support.supported) {
|
|
19221
|
+
throw refuse("unsupported", 403, "Not available on this Strapi version", {
|
|
19222
|
+
cause: support.reason
|
|
19223
|
+
});
|
|
19224
|
+
}
|
|
19225
|
+
if (nested)
|
|
19226
|
+
throw refuse("nested", 403, "You cannot start an impersonation while impersonating");
|
|
19227
|
+
if (typeof adminId !== "string" || !ADMIN_ID_PATTERN.test(adminId)) {
|
|
19228
|
+
throw new ImpersonationError(404, "Admin not found");
|
|
19229
|
+
}
|
|
19230
|
+
const { value, errors: fieldErrors } = validateSignInBody(body, support.durations);
|
|
19231
|
+
if (!value) {
|
|
19232
|
+
throw new ImpersonationError(400, "Invalid sign-in request", {
|
|
19233
|
+
errors: fieldErrors.map((error) => ({ ...error, name: "ValidationError" }))
|
|
19234
|
+
});
|
|
19235
|
+
}
|
|
19236
|
+
const [caller, target] = await Promise.all([
|
|
19237
|
+
loadAdmin(strapi2, actor.id),
|
|
19238
|
+
loadAdmin(strapi2, Number(adminId))
|
|
19239
|
+
]);
|
|
19240
|
+
if (!target) throw new ImpersonationError(404, "Admin not found");
|
|
19241
|
+
const refusal = await previewRefusal(strapi2, caller || actor, target);
|
|
19242
|
+
if (refusal?.reason === "super-admin" && !canReadAdmins) {
|
|
19243
|
+
service.recordRefused({
|
|
19244
|
+
actorId,
|
|
19245
|
+
targetType: "admin",
|
|
19246
|
+
targetId: adminId,
|
|
19247
|
+
reason: "super-admin",
|
|
19248
|
+
via: "sign-in"
|
|
19249
|
+
});
|
|
19250
|
+
throw new ImpersonationError(404, "Admin not found");
|
|
19251
|
+
}
|
|
19252
|
+
if (refusal) {
|
|
19253
|
+
const messages = {
|
|
19254
|
+
self: "You cannot sign in as yourself",
|
|
19255
|
+
"super-admin": "You cannot sign in as a Super Admin",
|
|
19256
|
+
"not-subset": "This admin has permissions that you do not have"
|
|
19257
|
+
};
|
|
19258
|
+
throw refuse(
|
|
19259
|
+
refusal.reason,
|
|
19260
|
+
403,
|
|
19261
|
+
messages[refusal.reason],
|
|
19262
|
+
refusal.uncovered ? { uncovered: refusal.uncovered } : {}
|
|
19263
|
+
);
|
|
19264
|
+
}
|
|
19265
|
+
if (target.isActive !== true) {
|
|
19266
|
+
throw refuse("inactive", 403, "This admin account is not active");
|
|
19267
|
+
}
|
|
19268
|
+
await service.sweep();
|
|
19269
|
+
const running = await query().findOne({
|
|
19270
|
+
where: {
|
|
19271
|
+
targetType: "admin",
|
|
19272
|
+
targetId: String(target.id),
|
|
19273
|
+
handoff: "admin-session",
|
|
19274
|
+
state: "active"
|
|
19275
|
+
},
|
|
19276
|
+
select: ["id"]
|
|
19277
|
+
});
|
|
19278
|
+
if (running)
|
|
19279
|
+
throw refuse("already-active", 403, "Someone is already signed in as this admin");
|
|
19280
|
+
const now = /* @__PURE__ */ new Date();
|
|
19281
|
+
const expiresAt = new Date(now.getTime() + value.durationMinutes * 60 * 1e3);
|
|
19282
|
+
const code = crypto.randomBytes(32).toString("base64url");
|
|
19283
|
+
const codeExpiresAt = new Date(now.getTime() + CODE_TTL_MS);
|
|
19284
|
+
const row = await query().create({
|
|
19285
|
+
data: {
|
|
19286
|
+
targetType: "admin",
|
|
19287
|
+
targetId: String(target.id),
|
|
19288
|
+
actorId,
|
|
19289
|
+
state: "active",
|
|
19290
|
+
reason: value.reason,
|
|
19291
|
+
durationMinutes: value.durationMinutes,
|
|
19292
|
+
handoff: "admin-session",
|
|
19293
|
+
notifyUser: false,
|
|
19294
|
+
allowChanges: value.allowChanges,
|
|
19295
|
+
startedAt: now,
|
|
19296
|
+
expiresAt,
|
|
19297
|
+
codeHash: hashToken(code),
|
|
19298
|
+
codeExpiresAt
|
|
19299
|
+
}
|
|
19300
|
+
});
|
|
19301
|
+
record({
|
|
19302
|
+
event: EVENTS.STARTED,
|
|
19303
|
+
actorType: "admin",
|
|
19304
|
+
actorId,
|
|
19305
|
+
targetType: "admin",
|
|
19306
|
+
targetId: String(target.id),
|
|
19307
|
+
meta: {
|
|
19308
|
+
reason: value.reason,
|
|
19309
|
+
durationMinutes: value.durationMinutes,
|
|
19310
|
+
handoff: "admin-session",
|
|
19311
|
+
allowChanges: value.allowChanges
|
|
19312
|
+
}
|
|
19313
|
+
});
|
|
19314
|
+
log.info(
|
|
19315
|
+
`[IMPERSONATION] Admin sign-in started (${value.durationMinutes} min, ${value.allowChanges ? "changes allowed" : "read-only"})`
|
|
19316
|
+
);
|
|
19317
|
+
return {
|
|
19318
|
+
impersonation: service.toDto(row, {
|
|
19319
|
+
admins: /* @__PURE__ */ new Map([
|
|
19320
|
+
[String(target.id), canReadAdmins ? target : null],
|
|
19321
|
+
[actorId, caller || actor]
|
|
19322
|
+
]),
|
|
19323
|
+
viewer: actor
|
|
19324
|
+
}),
|
|
19325
|
+
handoff: {
|
|
19326
|
+
type: "admin-tab",
|
|
19327
|
+
path: LANDING_PATH,
|
|
19328
|
+
cookieName: service.accessCookieName(),
|
|
19329
|
+
code,
|
|
19330
|
+
codeExpiresAt: codeExpiresAt.toISOString()
|
|
19331
|
+
}
|
|
19332
|
+
};
|
|
19333
|
+
},
|
|
19334
|
+
/**
|
|
19335
|
+
* Exchanges the one-time code of an admin sign-in for an admin access
|
|
19336
|
+
* token of the target, in the new tab. The request carries the starting
|
|
19337
|
+
* admin's own token, and only that admin can redeem the code. The Strapi
|
|
19338
|
+
* admin session is created here on a dedicated device id, marked in its
|
|
19339
|
+
* metadata, and expires with the impersonation; its refresh token never
|
|
19340
|
+
* leaves the server, so the tab cannot renew it.
|
|
19341
|
+
*
|
|
19342
|
+
* @param {{code: unknown, actor: any, ability: any}} input Code and the calling admin.
|
|
19343
|
+
* @returns {Promise<object|null>} `{ token, expiresAt, cookieName, impersonation }`, or null
|
|
19344
|
+
* for any unusable code.
|
|
19345
|
+
* @sideeffect Consumes the code; creates one Strapi admin session for the target.
|
|
19346
|
+
*/
|
|
19347
|
+
async exchangeAdminCode({ code, actor, ability }) {
|
|
19348
|
+
if (typeof code !== "string" || !CODE_PATTERN.test(code)) return null;
|
|
19349
|
+
if (!await service.isEnabled() || !service.adminSignInStatus().supported) return null;
|
|
19350
|
+
const codeHash = hashToken(code);
|
|
19351
|
+
const now = /* @__PURE__ */ new Date();
|
|
19352
|
+
const row = await query().findOne({
|
|
19353
|
+
where: { codeHash, state: "active", handoff: "admin-session" }
|
|
19354
|
+
});
|
|
19355
|
+
if (!row || String(row.actorId) !== String(actor?.id)) return null;
|
|
19356
|
+
let stillAllowed;
|
|
19357
|
+
try {
|
|
19358
|
+
stillAllowed = ability?.can?.(ADMIN_SIGN_IN_ACTION) === true;
|
|
19359
|
+
} catch {
|
|
19360
|
+
stillAllowed = false;
|
|
19361
|
+
}
|
|
19362
|
+
if (!stillAllowed) return null;
|
|
19363
|
+
const claimed = await query().updateMany({
|
|
19364
|
+
where: {
|
|
19365
|
+
id: row.id,
|
|
19366
|
+
codeHash,
|
|
19367
|
+
state: "active",
|
|
19368
|
+
credentialIssuedAt: { $null: true },
|
|
19369
|
+
codeExpiresAt: { $gt: now },
|
|
19370
|
+
expiresAt: { $gt: now }
|
|
19371
|
+
},
|
|
19372
|
+
data: { codeHash: null, codeExpiresAt: null, credentialIssuedAt: now }
|
|
19373
|
+
});
|
|
19374
|
+
if (Number(claimed?.count) !== 1) return null;
|
|
19375
|
+
const target = await loadAdmin(strapi2, Number(row.targetId));
|
|
19376
|
+
if (!target || target.isActive !== true || await previewRefusal(strapi2, actor, target)) {
|
|
19377
|
+
await service.end(row.documentId, { reason: "failed", silent: true });
|
|
19378
|
+
return null;
|
|
19379
|
+
}
|
|
19380
|
+
try {
|
|
19381
|
+
const token = await service.issueAdminSession(row, target);
|
|
19382
|
+
return {
|
|
19383
|
+
token,
|
|
19384
|
+
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
19385
|
+
cookieName: service.accessCookieName(),
|
|
19386
|
+
impersonation: {
|
|
19387
|
+
id: row.documentId,
|
|
19388
|
+
allowChanges: row.allowChanges === true,
|
|
19389
|
+
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
19390
|
+
targetName: adminLabel(target),
|
|
19391
|
+
actorName: adminLabel(actor)
|
|
19392
|
+
}
|
|
19393
|
+
};
|
|
19394
|
+
} catch (err) {
|
|
19395
|
+
await service.end(row.documentId, { reason: "failed", silent: true });
|
|
19396
|
+
throw err;
|
|
19397
|
+
}
|
|
19398
|
+
},
|
|
19399
|
+
/**
|
|
19400
|
+
* Name of Strapi's admin access cookie (`admin.auth.cookie.name`,
|
|
19401
|
+
* default `jwtToken`), so the impersonation tab can hide it.
|
|
19402
|
+
*
|
|
19403
|
+
* @returns {string}
|
|
19404
|
+
*/
|
|
19405
|
+
accessCookieName() {
|
|
19406
|
+
const configured = strapi2.config.get("admin.auth.cookie.name");
|
|
19407
|
+
return typeof configured === "string" && /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(configured.trim()) ? configured.trim() : "jwtToken";
|
|
19408
|
+
},
|
|
19409
|
+
/**
|
|
19410
|
+
* Creates the Strapi admin session of an admin sign-in and returns its
|
|
19411
|
+
* access token. The device id is recorded before the session exists, so
|
|
19412
|
+
* a failure below still revokes exactly this session.
|
|
19413
|
+
*
|
|
19414
|
+
* @param {any} row Impersonation row.
|
|
19415
|
+
* @param {any} target Target admin.
|
|
19416
|
+
* @returns {Promise<string>} Access token.
|
|
19417
|
+
* @throws {Error} When the session cannot be created.
|
|
19418
|
+
* @sideeffect Creates one `admin::session` row and shortens its expiry.
|
|
19419
|
+
*/
|
|
19420
|
+
async issueAdminSession(row, target) {
|
|
19421
|
+
const expiresAt = new Date(row.expiresAt);
|
|
19422
|
+
if (expiresAt.getTime() - Date.now() < 5e3) {
|
|
19423
|
+
throw new ImpersonationError(403, "Impersonation ended");
|
|
19424
|
+
}
|
|
19425
|
+
const deviceId = `${ADMIN_DEVICE_PREFIX}${crypto.randomBytes(12).toString("hex")}`;
|
|
19426
|
+
await query().update({
|
|
19427
|
+
where: { id: row.id },
|
|
19428
|
+
data: { nativeUserId: String(target.id), nativeDeviceId: deviceId }
|
|
19429
|
+
});
|
|
19430
|
+
const manager = strapi2.sessionManager("admin");
|
|
19431
|
+
const refresh = await manager.generateRefreshToken(String(target.id), deviceId, {
|
|
19432
|
+
type: "session",
|
|
19433
|
+
metadata: {
|
|
19434
|
+
loginAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
19435
|
+
deviceName: "Impersonation",
|
|
19436
|
+
msmImpersonation: row.documentId
|
|
19437
|
+
}
|
|
19438
|
+
});
|
|
19439
|
+
await strapi2.db.query(NATIVE_SESSION_UID).update({
|
|
19440
|
+
where: { sessionId: refresh.sessionId },
|
|
19441
|
+
data: { expiresAt, absoluteExpiresAt: expiresAt }
|
|
19442
|
+
});
|
|
19443
|
+
const access = await manager.generateAccessToken(refresh.token);
|
|
19444
|
+
if (!access || "error" in access) throw new Error("Access token could not be minted");
|
|
19445
|
+
remember(nativeSessionCache, `admin:${refresh.sessionId}`, row.documentId);
|
|
19446
|
+
recordCache.delete(row.documentId);
|
|
19447
|
+
return access.token;
|
|
19448
|
+
},
|
|
19449
|
+
/**
|
|
19450
|
+
* Deletes the Strapi admin session of an admin sign-in, and only that
|
|
19451
|
+
* one: by its user and its own device id. Without a stored device id
|
|
19452
|
+
* nothing is deleted, because Strapi's `invalidateRefreshToken` without
|
|
19453
|
+
* a device id would delete every session of the target.
|
|
19454
|
+
*
|
|
19455
|
+
* @param {any} row Impersonation row.
|
|
19456
|
+
* @returns {Promise<void>}
|
|
19457
|
+
* @sideeffect Deletes `admin::session` rows of the impersonation device.
|
|
19458
|
+
*/
|
|
19459
|
+
async revokeAdminSession(row) {
|
|
19460
|
+
const userId = row.nativeUserId ? String(row.nativeUserId) : "";
|
|
19461
|
+
const deviceId = row.nativeDeviceId ? String(row.nativeDeviceId) : "";
|
|
19462
|
+
if (!userId || !deviceId.startsWith(ADMIN_DEVICE_PREFIX)) return;
|
|
19463
|
+
try {
|
|
19464
|
+
await strapi2.sessionManager("admin").invalidateRefreshToken(userId, deviceId);
|
|
19465
|
+
} catch (err) {
|
|
19466
|
+
log.error("[IMPERSONATION] Admin session revocation failed:", err?.name || "Error");
|
|
19467
|
+
}
|
|
19468
|
+
},
|
|
19469
|
+
/**
|
|
19470
|
+
* The admin sign-in an admin access token belongs to, or null. The
|
|
19471
|
+
* signature is verified with Strapi's session manager; the session row
|
|
19472
|
+
* names the impersonation in its metadata (or by its device id, which
|
|
19473
|
+
* fails closed). Results are cached per session id (immutable).
|
|
19474
|
+
*
|
|
19475
|
+
* @param {string} token Bearer token.
|
|
19476
|
+
* @returns {Promise<string|null>} Impersonation id (`'unknown'` for a marked device
|
|
19477
|
+
* without readable metadata).
|
|
19478
|
+
* @throws {Error} When the session lookup fails.
|
|
19479
|
+
*/
|
|
19480
|
+
async adminImpersonationOfToken(token) {
|
|
19481
|
+
const payload = decodeJwtPayload(token);
|
|
19482
|
+
if (!payload || payload.type !== "access") return null;
|
|
19483
|
+
const sessionId = payload.sessionId;
|
|
19484
|
+
if (typeof sessionId !== "string" || !/^[a-f0-9]{16,64}$/i.test(sessionId)) return null;
|
|
19485
|
+
const key = `admin:${sessionId}`;
|
|
19486
|
+
if (nativeSessionCache.has(key)) {
|
|
19487
|
+
return service.verifyAdminToken(token) ? nativeSessionCache.get(key) : null;
|
|
19488
|
+
}
|
|
19489
|
+
if (!service.verifyAdminToken(token)) return null;
|
|
19490
|
+
const session = await strapi2.db.query(NATIVE_SESSION_UID).findOne({
|
|
19491
|
+
where: { sessionId, origin: "admin" },
|
|
19492
|
+
select: ["metadata", "deviceId"]
|
|
19493
|
+
});
|
|
19494
|
+
let id = null;
|
|
19495
|
+
const mark = session?.metadata?.msmImpersonation;
|
|
19496
|
+
if (typeof mark === "string" && DOCUMENT_ID_PATTERN.test(mark)) id = mark;
|
|
19497
|
+
else if (typeof session?.deviceId === "string" && session.deviceId.startsWith(ADMIN_DEVICE_PREFIX)) {
|
|
19498
|
+
id = "unknown";
|
|
19499
|
+
}
|
|
19500
|
+
if (session) remember(nativeSessionCache, key, id);
|
|
19501
|
+
return id;
|
|
19502
|
+
},
|
|
19503
|
+
/**
|
|
19504
|
+
* Whether a token is a validly signed Strapi admin access token.
|
|
19505
|
+
*
|
|
19506
|
+
* @param {string} token Bearer token.
|
|
19507
|
+
* @returns {boolean}
|
|
19508
|
+
*/
|
|
19509
|
+
verifyAdminToken(token) {
|
|
19510
|
+
try {
|
|
19511
|
+
return strapi2.sessionManager("admin").validateAccessToken(token)?.isValid === true;
|
|
19512
|
+
} catch {
|
|
19513
|
+
return false;
|
|
19514
|
+
}
|
|
19515
|
+
},
|
|
19516
|
+
/**
|
|
19517
|
+
* Loads an admin sign-in for the request guard (active rows are read on
|
|
19518
|
+
* every request, like `loadForGuard`).
|
|
19519
|
+
*
|
|
19520
|
+
* @param {string} documentId Impersonation id.
|
|
19521
|
+
* @returns {Promise<{id: string, actorId: string, targetId: string, state: string,
|
|
19522
|
+
* expiresAt: number, allowChanges: boolean}|null>}
|
|
19523
|
+
*/
|
|
19524
|
+
async loadAdminForGuard(documentId) {
|
|
19525
|
+
const cached = recordCache.get(`admin:${documentId}`);
|
|
19526
|
+
if (cached && Date.now() - cached.at < RECORD_CACHE_MS) return cached.value;
|
|
19527
|
+
const row = await query().findOne({
|
|
19528
|
+
where: { documentId, targetType: "admin", handoff: "admin-session" },
|
|
19529
|
+
select: ["documentId", "actorId", "targetId", "state", "expiresAt", "allowChanges"]
|
|
19530
|
+
});
|
|
19531
|
+
const value = row ? {
|
|
19532
|
+
id: row.documentId,
|
|
19533
|
+
actorId: String(row.actorId),
|
|
19534
|
+
targetId: String(row.targetId),
|
|
19535
|
+
state: row.state,
|
|
19536
|
+
expiresAt: new Date(row.expiresAt).getTime(),
|
|
19537
|
+
allowChanges: row.allowChanges === true
|
|
19538
|
+
} : null;
|
|
19539
|
+
if (!value || value.state !== "active") {
|
|
19540
|
+
remember(recordCache, `admin:${documentId}`, { value, at: Date.now() });
|
|
19541
|
+
}
|
|
19542
|
+
return value;
|
|
19543
|
+
},
|
|
19544
|
+
/**
|
|
19545
|
+
* Records a change made while signed in as another admin: the real
|
|
19546
|
+
* admin is the actor, the target admin in `meta.impersonating`.
|
|
19547
|
+
*
|
|
19548
|
+
* @param {{id: string, actorId: string, targetId: string}} impersonation Impersonation.
|
|
19549
|
+
* @param {{method: string, path: string, status: number}} request What was done.
|
|
19550
|
+
* @returns {void}
|
|
19551
|
+
* @sideeffect Queues one always-on `impersonation.write` event.
|
|
19552
|
+
*/
|
|
19553
|
+
recordAdminWrite(impersonation, { method, path, status }) {
|
|
19554
|
+
record({
|
|
19555
|
+
event: EVENTS.WRITE,
|
|
19556
|
+
actorType: "admin",
|
|
19557
|
+
actorId: impersonation.actorId,
|
|
19558
|
+
targetType: "admin",
|
|
19559
|
+
targetId: impersonation.targetId,
|
|
19560
|
+
meta: {
|
|
19561
|
+
impersonation: impersonation.id,
|
|
19562
|
+
impersonating: impersonation.targetId,
|
|
19563
|
+
method,
|
|
19564
|
+
route: path,
|
|
19565
|
+
status
|
|
19566
|
+
}
|
|
19567
|
+
});
|
|
19568
|
+
},
|
|
19569
|
+
/**
|
|
19570
|
+
* Records a refused admin API call of an admin sign-in (one event per
|
|
19571
|
+
* impersonation and reason per minute).
|
|
19572
|
+
*
|
|
19573
|
+
* @param {{id: string, actorId: string, targetId: string}} impersonation Impersonation.
|
|
19574
|
+
* @param {string} reason `read-only` or `identity`.
|
|
19575
|
+
* @param {{method: string, path: string}} request Refused request.
|
|
19576
|
+
* @returns {void}
|
|
19577
|
+
*/
|
|
19578
|
+
recordRefusedAdminCall(impersonation, reason, { method, path }) {
|
|
19579
|
+
const key = `${impersonation.id}|admin-${reason}`;
|
|
19580
|
+
const now = Date.now();
|
|
19581
|
+
const last = refusals.get(key);
|
|
19582
|
+
if (last !== void 0 && now - last < REFUSAL_DEDUP_MS) return;
|
|
19583
|
+
remember(refusals, key, now);
|
|
19584
|
+
record({
|
|
19585
|
+
event: EVENTS.REFUSED,
|
|
19586
|
+
actorType: "admin",
|
|
19587
|
+
actorId: impersonation.actorId,
|
|
19588
|
+
targetType: "admin",
|
|
19589
|
+
targetId: impersonation.targetId,
|
|
19590
|
+
meta: {
|
|
19591
|
+
refusal: `call-${reason}`,
|
|
19592
|
+
impersonation: impersonation.id,
|
|
19593
|
+
impersonating: impersonation.targetId,
|
|
19594
|
+
method,
|
|
19595
|
+
route: path
|
|
19596
|
+
}
|
|
19597
|
+
});
|
|
19598
|
+
},
|
|
18848
19599
|
/**
|
|
18849
19600
|
* Mints the short-lived credential of an impersonation and links its
|
|
18850
19601
|
* Magic session row.
|
|
@@ -19014,6 +19765,10 @@ var require_impersonation = __commonJS({
|
|
|
19014
19765
|
* @sideeffect Deletes Strapi session rows, writes a revocation record, ends the Magic session.
|
|
19015
19766
|
*/
|
|
19016
19767
|
async revoke(row, sessionReason) {
|
|
19768
|
+
if (row.handoff === "admin-session") {
|
|
19769
|
+
await service.revokeAdminSession(row);
|
|
19770
|
+
return;
|
|
19771
|
+
}
|
|
19017
19772
|
if (row.nativeUserId && row.nativeDeviceId) {
|
|
19018
19773
|
try {
|
|
19019
19774
|
await strapi2.sessionManager(NATIVE_ORIGIN).invalidateRefreshToken(String(row.nativeUserId), String(row.nativeDeviceId));
|
|
@@ -19155,6 +19910,7 @@ var require_impersonation = __commonJS({
|
|
|
19155
19910
|
reason: row.reason,
|
|
19156
19911
|
durationMinutes: row.durationMinutes,
|
|
19157
19912
|
handoff: row.handoff,
|
|
19913
|
+
allowChanges: row.allowChanges === true,
|
|
19158
19914
|
credentialIssued: Boolean(row.credentialIssuedAt),
|
|
19159
19915
|
startedAt: new Date(row.startedAt).toISOString(),
|
|
19160
19916
|
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
@@ -19303,6 +20059,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19303
20059
|
var { sendServerError } = require_http_errors();
|
|
19304
20060
|
var IMPERSONATE_USERS_ACTION = "plugin::magic-sessionmanager.impersonate.users";
|
|
19305
20061
|
var IMPERSONATE_ADMINS_ACTION = "plugin::magic-sessionmanager.impersonate.admins";
|
|
20062
|
+
var { ADMIN_SIGN_IN_ACTION } = require_admin_sign_in_rules();
|
|
19306
20063
|
function callerCan(ctx, action) {
|
|
19307
20064
|
try {
|
|
19308
20065
|
return ctx?.state?.userAbility?.can?.(action) === true;
|
|
@@ -19313,7 +20070,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19313
20070
|
function visibleTargetTypes(ctx) {
|
|
19314
20071
|
return [
|
|
19315
20072
|
...callerCan(ctx, IMPERSONATE_USERS_ACTION) ? ["user"] : [],
|
|
19316
|
-
...callerCan(ctx, IMPERSONATE_ADMINS_ACTION) ? ["admin"] : []
|
|
20073
|
+
...callerCan(ctx, IMPERSONATE_ADMINS_ACTION) || callerCan(ctx, ADMIN_SIGN_IN_ACTION) ? ["admin"] : []
|
|
19317
20074
|
];
|
|
19318
20075
|
}
|
|
19319
20076
|
var REPLY = Object.freeze({ 400: "badRequest", 403: "forbidden", 404: "notFound" });
|
|
@@ -19476,6 +20233,104 @@ var require_impersonation2 = __commonJS({
|
|
|
19476
20233
|
};
|
|
19477
20234
|
});
|
|
19478
20235
|
},
|
|
20236
|
+
/**
|
|
20237
|
+
* Starts "Sign in as this admin" (4.23). Returns the landing path of the
|
|
20238
|
+
* new tab and a one-time code for it; no session exists yet.
|
|
20239
|
+
*
|
|
20240
|
+
* @route POST /magic-sessionmanager/impersonation/admins/:adminId/sign-in
|
|
20241
|
+
* @auth Admin JWT only (admin API tokens and impersonation sessions are refused);
|
|
20242
|
+
* `access` + `admin-sessions.read` + `impersonate.admins.sign-in`.
|
|
20243
|
+
* @param {any} ctx Body `{ reason: string(3..255), durationMinutes: 15|30,
|
|
20244
|
+
* allowChanges?: boolean }`.
|
|
20245
|
+
* @returns {Promise<void>} 201 `{ data: { impersonation, handoff: { type: 'admin-tab', path,
|
|
20246
|
+
* code, codeExpiresAt } } }`. Answers 403 with `details.reason` `disabled`, `unsupported`,
|
|
20247
|
+
* `api-token`, `nested`, `self`, `super-admin`, `not-subset`, `inactive` or
|
|
20248
|
+
* `already-active`, 400 for an invalid body and 404 for an unknown admin.
|
|
20249
|
+
* @sideeffect Creates an impersonation row; logs `impersonation.started` or `.refused`.
|
|
20250
|
+
*/
|
|
20251
|
+
async signInAdmin(ctx) {
|
|
20252
|
+
ctx.set("Cache-Control", "no-store");
|
|
20253
|
+
return respond(ctx, "sign-in", async () => {
|
|
20254
|
+
const { adminId } = ctx.params;
|
|
20255
|
+
requirePerson(ctx, "admin", adminId);
|
|
20256
|
+
const result = await service().startAdminSignIn({
|
|
20257
|
+
actor: ctx.state.user,
|
|
20258
|
+
adminId,
|
|
20259
|
+
body: ctx.request.body,
|
|
20260
|
+
canReadAdmins: callerCan(ctx, "admin::users.read"),
|
|
20261
|
+
nested: Boolean(ctx.state.magicAdminImpersonation)
|
|
20262
|
+
});
|
|
20263
|
+
ctx.status = 201;
|
|
20264
|
+
ctx.body = { data: result };
|
|
20265
|
+
});
|
|
20266
|
+
},
|
|
20267
|
+
/**
|
|
20268
|
+
* Exchanges the one-time code of an admin sign-in for the target's
|
|
20269
|
+
* access token. Called by the new tab with the starting admin's own
|
|
20270
|
+
* token; only that admin can redeem the code. Every unusable code gets
|
|
20271
|
+
* the same 400.
|
|
20272
|
+
*
|
|
20273
|
+
* @route POST /magic-sessionmanager/admin-impersonation/exchange
|
|
20274
|
+
* @auth Admin JWT of the admin who started it (still holding `impersonate.admins.sign-in`).
|
|
20275
|
+
* @param {any} ctx Body `{ code: string }`.
|
|
20276
|
+
* @returns {Promise<void>} `{ data: { token, expiresAt, cookieName, impersonation } }`.
|
|
20277
|
+
* @sideeffect Consumes the code; creates one Strapi admin session for the target.
|
|
20278
|
+
*/
|
|
20279
|
+
async exchangeAdmin(ctx) {
|
|
20280
|
+
ctx.set("Cache-Control", "no-store");
|
|
20281
|
+
const body = ctx.request.body;
|
|
20282
|
+
const valid = body && typeof body === "object" && !Array.isArray(body) && Object.keys(body).length === 1 && typeof body.code === "string" && ctx.state?.auth?.strategy?.name === "admin" && !ctx.state.magicAdminImpersonation;
|
|
20283
|
+
return respond(ctx, "exchange", async () => {
|
|
20284
|
+
const result = valid ? await service().exchangeAdminCode({
|
|
20285
|
+
code: body.code,
|
|
20286
|
+
actor: ctx.state.user,
|
|
20287
|
+
ability: ctx.state.userAbility
|
|
20288
|
+
}) : null;
|
|
20289
|
+
if (!result) throw new ImpersonationError(400, "Invalid or expired code");
|
|
20290
|
+
ctx.body = { data: result };
|
|
20291
|
+
});
|
|
20292
|
+
},
|
|
20293
|
+
/**
|
|
20294
|
+
* State of the admin sign-in of the calling tab, for its banner.
|
|
20295
|
+
*
|
|
20296
|
+
* @route GET /magic-sessionmanager/admin-impersonation/self
|
|
20297
|
+
* @auth The impersonation session's own token (checked by the admin impersonation guard);
|
|
20298
|
+
* 401 for any other caller.
|
|
20299
|
+
* @returns {Promise<void>} `{ data: { id, expiresAt, allowChanges } }`.
|
|
20300
|
+
*/
|
|
20301
|
+
async adminSelf(ctx) {
|
|
20302
|
+
ctx.set("Cache-Control", "no-store");
|
|
20303
|
+
const current = ctx.state.magicAdminImpersonation;
|
|
20304
|
+
if (!current) return ctx.unauthorized("Not an impersonation session");
|
|
20305
|
+
ctx.body = {
|
|
20306
|
+
data: {
|
|
20307
|
+
id: current.id,
|
|
20308
|
+
expiresAt: new Date(current.expiresAt).toISOString(),
|
|
20309
|
+
allowChanges: current.allowChanges === true
|
|
20310
|
+
}
|
|
20311
|
+
};
|
|
20312
|
+
return void 0;
|
|
20313
|
+
},
|
|
20314
|
+
/**
|
|
20315
|
+
* Ends the admin sign-in of the calling tab: deletes its own Strapi
|
|
20316
|
+
* session and nothing else.
|
|
20317
|
+
*
|
|
20318
|
+
* @route POST /magic-sessionmanager/admin-impersonation/self/end
|
|
20319
|
+
* @auth The impersonation session's own token; 401 for any other caller.
|
|
20320
|
+
* @returns {Promise<void>} `{ data: { ended: boolean } }`.
|
|
20321
|
+
* @sideeffect Ends the impersonation; logs `impersonation.ended`.
|
|
20322
|
+
*/
|
|
20323
|
+
async endAdminSelf(ctx) {
|
|
20324
|
+
const current = ctx.state.magicAdminImpersonation;
|
|
20325
|
+
if (!current) return ctx.unauthorized("Not an impersonation session");
|
|
20326
|
+
return respond(ctx, "end", async () => {
|
|
20327
|
+
const ended = await service().end(current.id, {
|
|
20328
|
+
reason: "ended",
|
|
20329
|
+
endedBy: current.actorId
|
|
20330
|
+
});
|
|
20331
|
+
ctx.body = { data: { ended } };
|
|
20332
|
+
});
|
|
20333
|
+
},
|
|
19479
20334
|
/**
|
|
19480
20335
|
* Exchanges a one-time hand-off code for the impersonation credential.
|
|
19481
20336
|
* Public on purpose: the code is the credential (32 random bytes, stored
|
|
@@ -19503,6 +20358,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19503
20358
|
};
|
|
19504
20359
|
module.exports.IMPERSONATE_USERS_ACTION = IMPERSONATE_USERS_ACTION;
|
|
19505
20360
|
module.exports.IMPERSONATE_ADMINS_ACTION = IMPERSONATE_ADMINS_ACTION;
|
|
20361
|
+
module.exports.ADMIN_SIGN_IN_ACTION = ADMIN_SIGN_IN_ACTION;
|
|
19506
20362
|
}
|
|
19507
20363
|
});
|
|
19508
20364
|
|
|
@@ -23377,6 +24233,7 @@ var require_security_log = __commonJS({
|
|
|
23377
24233
|
function signInWhere(query) {
|
|
23378
24234
|
const conditions = [{ event: { $in: [...SIGN_IN_FILTERS[query.event || "all"]] } }];
|
|
23379
24235
|
if (query.adminId) conditions.push({ targetType: "admin", targetId: String(query.adminId) });
|
|
24236
|
+
else if (query.event === "impersonation") conditions.push({ targetType: "admin" });
|
|
23380
24237
|
if (query.from) conditions.push({ createdAt: { $gte: query.from } });
|
|
23381
24238
|
if (query.to) conditions.push({ createdAt: { $lte: query.to } });
|
|
23382
24239
|
return { $and: conditions };
|
|
@@ -24214,7 +25071,7 @@ var require_can_impersonate = __commonJS({
|
|
|
24214
25071
|
return false;
|
|
24215
25072
|
}
|
|
24216
25073
|
};
|
|
24217
|
-
return can("plugin::magic-sessionmanager.impersonate.users") || can("plugin::magic-sessionmanager.impersonate.admins");
|
|
25074
|
+
return can("plugin::magic-sessionmanager.impersonate.users") || can("plugin::magic-sessionmanager.impersonate.admins") || can("plugin::magic-sessionmanager.impersonate.admins.sign-in");
|
|
24218
25075
|
};
|
|
24219
25076
|
}
|
|
24220
25077
|
});
|