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.js
CHANGED
|
@@ -2530,7 +2530,11 @@ var require_security_log_model = __commonJS({
|
|
|
2530
2530
|
IMPERSONATION_STARTED: "impersonation.started",
|
|
2531
2531
|
IMPERSONATION_ENDED: "impersonation.ended",
|
|
2532
2532
|
IMPERSONATION_EXPIRED: "impersonation.expired",
|
|
2533
|
-
IMPERSONATION_REFUSED: "impersonation.refused"
|
|
2533
|
+
IMPERSONATION_REFUSED: "impersonation.refused",
|
|
2534
|
+
// 4.23: a change made while signed in as another admin. Actor = the real
|
|
2535
|
+
// admin, target and `meta.impersonating` = the admin signed in as, plus
|
|
2536
|
+
// the method and path (no query, no body).
|
|
2537
|
+
IMPERSONATION_WRITE: "impersonation.write"
|
|
2534
2538
|
});
|
|
2535
2539
|
var KEEP_WITHOUT_ACTOR_EVENTS = Object.freeze([
|
|
2536
2540
|
SECURITY_EVENTS.ADMIN_SESSION_REVOKED,
|
|
@@ -2539,7 +2543,8 @@ var require_security_log_model = __commonJS({
|
|
|
2539
2543
|
SECURITY_EVENTS.IMPERSONATION_STARTED,
|
|
2540
2544
|
SECURITY_EVENTS.IMPERSONATION_ENDED,
|
|
2541
2545
|
SECURITY_EVENTS.IMPERSONATION_EXPIRED,
|
|
2542
|
-
SECURITY_EVENTS.IMPERSONATION_REFUSED
|
|
2546
|
+
SECURITY_EVENTS.IMPERSONATION_REFUSED,
|
|
2547
|
+
SECURITY_EVENTS.IMPERSONATION_WRITE
|
|
2543
2548
|
]);
|
|
2544
2549
|
function dropRankOf(event) {
|
|
2545
2550
|
if (event === SECURITY_EVENTS.ADMIN_SIGN_IN_FAILED || event === SECURITY_EVENTS.ADMIN_LOCKOUT || USER_SIGN_IN_EVENTS.includes(event)) {
|
|
@@ -2579,12 +2584,20 @@ var require_security_log_model = __commonJS({
|
|
|
2579
2584
|
SECURITY_EVENTS.LOG_RETENTION_PURGED,
|
|
2580
2585
|
SECURITY_EVENTS.CONFIG_WEAK_KEY
|
|
2581
2586
|
]);
|
|
2587
|
+
var ADMIN_IMPERSONATION_EVENTS = Object.freeze([
|
|
2588
|
+
SECURITY_EVENTS.IMPERSONATION_STARTED,
|
|
2589
|
+
SECURITY_EVENTS.IMPERSONATION_ENDED,
|
|
2590
|
+
SECURITY_EVENTS.IMPERSONATION_EXPIRED,
|
|
2591
|
+
SECURITY_EVENTS.IMPERSONATION_REFUSED,
|
|
2592
|
+
SECURITY_EVENTS.IMPERSONATION_WRITE
|
|
2593
|
+
]);
|
|
2582
2594
|
var SIGN_IN_FILTERS = Object.freeze({
|
|
2583
2595
|
all: [...ADMIN_SIGN_IN_EVENTS, ...ADMIN_LOG_EVENTS],
|
|
2584
2596
|
succeeded: [SECURITY_EVENTS.ADMIN_SIGN_IN],
|
|
2585
2597
|
failed: [SECURITY_EVENTS.ADMIN_SIGN_IN_FAILED, SECURITY_EVENTS.ADMIN_LOCKOUT],
|
|
2586
2598
|
"signed-out": [SECURITY_EVENTS.ADMIN_SIGN_OUT, SECURITY_EVENTS.ADMIN_SESSION_REVOKED],
|
|
2587
|
-
changes: ADMIN_LOG_EVENTS
|
|
2599
|
+
changes: ADMIN_LOG_EVENTS,
|
|
2600
|
+
impersonation: ADMIN_IMPERSONATION_EVENTS
|
|
2588
2601
|
});
|
|
2589
2602
|
var CONTENT_ACTIONS = Object.freeze([
|
|
2590
2603
|
"view",
|
|
@@ -2786,6 +2799,7 @@ var require_security_log_model = __commonJS({
|
|
|
2786
2799
|
USER_SIGN_IN_EVENTS,
|
|
2787
2800
|
ACTOR_TYPES,
|
|
2788
2801
|
ADMIN_CONTENT_EVENTS,
|
|
2802
|
+
ADMIN_IMPERSONATION_EVENTS,
|
|
2789
2803
|
ADMIN_LOG_EVENTS,
|
|
2790
2804
|
ADMIN_SIGN_IN_EVENTS,
|
|
2791
2805
|
CONTENT_ACTIONS,
|
|
@@ -3258,10 +3272,20 @@ var require_register = __commonJS({
|
|
|
3258
3272
|
// their own.
|
|
3259
3273
|
{
|
|
3260
3274
|
section: "plugins",
|
|
3261
|
-
displayName: "View
|
|
3275
|
+
displayName: "View admin permissions (read-only preview)",
|
|
3262
3276
|
uid: "impersonate.admins",
|
|
3263
3277
|
subCategory: "Impersonation",
|
|
3264
3278
|
pluginName: "magic-sessionmanager"
|
|
3279
|
+
},
|
|
3280
|
+
// "Sign in as this admin" (4.23): a real admin session of another admin
|
|
3281
|
+
// in a separate tab, read-only unless changes are allowed. The same
|
|
3282
|
+
// anti-escalation rules as the preview; only on tested Strapi releases.
|
|
3283
|
+
{
|
|
3284
|
+
section: "plugins",
|
|
3285
|
+
displayName: "Sign in as another admin",
|
|
3286
|
+
uid: "impersonate.admins.sign-in",
|
|
3287
|
+
subCategory: "Impersonation",
|
|
3288
|
+
pluginName: "magic-sessionmanager"
|
|
3265
3289
|
}
|
|
3266
3290
|
]);
|
|
3267
3291
|
disableGraphqlShadowCrud(strapi2, log);
|
|
@@ -5940,6 +5964,237 @@ var require_impersonation_guard = __commonJS({
|
|
|
5940
5964
|
}
|
|
5941
5965
|
});
|
|
5942
5966
|
|
|
5967
|
+
// server/src/utils/admin-sign-in-rules.js
|
|
5968
|
+
var require_admin_sign_in_rules = __commonJS({
|
|
5969
|
+
"server/src/utils/admin-sign-in-rules.js"(exports2, module2) {
|
|
5970
|
+
"use strict";
|
|
5971
|
+
var { REASON_MAX, REASON_MIN, cleanReason, normalizePath } = require_impersonation_rules();
|
|
5972
|
+
var ADMIN_SIGN_IN_ACTION = "plugin::magic-sessionmanager.impersonate.admins.sign-in";
|
|
5973
|
+
var TESTED_STRAPI = Object.freeze({ major: 5, minMinor: 50, maxMinor: 64 });
|
|
5974
|
+
var ADMIN_SIGN_IN_DURATIONS = Object.freeze([15, 30]);
|
|
5975
|
+
var ADMIN_DEVICE_PREFIX = "msm-admin-impersonation-";
|
|
5976
|
+
var LANDING_PATH = "/plugins/magic-sessionmanager/impersonate/land";
|
|
5977
|
+
function adminSignInSupport(version) {
|
|
5978
|
+
const match = typeof version === "string" ? /^(\d+)\.(\d+)\.(\d+)/.exec(version.trim()) : null;
|
|
5979
|
+
if (!match) return { supported: false, version: null, reason: "unknown-version" };
|
|
5980
|
+
const [major, minor] = [Number(match[1]), Number(match[2])];
|
|
5981
|
+
const clean = `${match[1]}.${match[2]}.${match[3]}`;
|
|
5982
|
+
const supported = major === TESTED_STRAPI.major && minor >= TESTED_STRAPI.minMinor && minor <= TESTED_STRAPI.maxMinor;
|
|
5983
|
+
return { supported, version: clean, reason: supported ? null : "untested-version" };
|
|
5984
|
+
}
|
|
5985
|
+
function allowedSignInDurations(accessTokenLifespanSeconds) {
|
|
5986
|
+
const seconds = Number(accessTokenLifespanSeconds);
|
|
5987
|
+
const lifespan = Number.isFinite(seconds) && seconds > 0 ? seconds : 30 * 60;
|
|
5988
|
+
return ADMIN_SIGN_IN_DURATIONS.filter((minutes) => minutes * 60 <= lifespan);
|
|
5989
|
+
}
|
|
5990
|
+
function validateSignInBody(body, durations) {
|
|
5991
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
5992
|
+
return { value: null, errors: [{ path: [], message: "Body must be an object" }] };
|
|
5993
|
+
}
|
|
5994
|
+
const errors = [];
|
|
5995
|
+
const source = (
|
|
5996
|
+
/** @type {Record<string, unknown>} */
|
|
5997
|
+
body
|
|
5998
|
+
);
|
|
5999
|
+
for (const key of Object.keys(source)) {
|
|
6000
|
+
if (!["reason", "durationMinutes", "allowChanges"].includes(key)) {
|
|
6001
|
+
errors.push({ path: [key], message: "Unknown field" });
|
|
6002
|
+
}
|
|
6003
|
+
}
|
|
6004
|
+
const reason = cleanReason(source.reason);
|
|
6005
|
+
if (reason === null) {
|
|
6006
|
+
errors.push({
|
|
6007
|
+
path: ["reason"],
|
|
6008
|
+
message: `reason must be ${REASON_MIN} to ${REASON_MAX} characters`
|
|
6009
|
+
});
|
|
6010
|
+
}
|
|
6011
|
+
const duration = source.durationMinutes;
|
|
6012
|
+
if (typeof duration !== "number" || !durations.includes(duration)) {
|
|
6013
|
+
errors.push({
|
|
6014
|
+
path: ["durationMinutes"],
|
|
6015
|
+
message: `durationMinutes must be one of: ${durations.join(", ")}`
|
|
6016
|
+
});
|
|
6017
|
+
}
|
|
6018
|
+
if (source.allowChanges !== void 0 && typeof source.allowChanges !== "boolean") {
|
|
6019
|
+
errors.push({ path: ["allowChanges"], message: "allowChanges must be a boolean" });
|
|
6020
|
+
}
|
|
6021
|
+
if (errors.length) return { value: null, errors };
|
|
6022
|
+
return {
|
|
6023
|
+
value: {
|
|
6024
|
+
reason: (
|
|
6025
|
+
/** @type {string} */
|
|
6026
|
+
reason
|
|
6027
|
+
),
|
|
6028
|
+
durationMinutes: (
|
|
6029
|
+
/** @type {number} */
|
|
6030
|
+
duration
|
|
6031
|
+
),
|
|
6032
|
+
allowChanges: source.allowChanges === true
|
|
6033
|
+
},
|
|
6034
|
+
errors
|
|
6035
|
+
};
|
|
6036
|
+
}
|
|
6037
|
+
var SAFE_METHODS = Object.freeze(["GET", "HEAD", "OPTIONS"]);
|
|
6038
|
+
var READ_ONLY_POSTS = Object.freeze(["/admin/permissions/check"]);
|
|
6039
|
+
var IDENTITY_PREFIXES = Object.freeze([
|
|
6040
|
+
"/admin/users",
|
|
6041
|
+
"/admin/roles",
|
|
6042
|
+
"/admin/api-tokens",
|
|
6043
|
+
"/admin/transfer",
|
|
6044
|
+
"/admin/providers",
|
|
6045
|
+
"/admin/register",
|
|
6046
|
+
"/admin/register-admin",
|
|
6047
|
+
"/admin/registration-info",
|
|
6048
|
+
"/admin/forgot-password",
|
|
6049
|
+
"/admin/reset-password",
|
|
6050
|
+
"/admin/renew-token",
|
|
6051
|
+
"/admin/login",
|
|
6052
|
+
"/admin/sessions",
|
|
6053
|
+
"/magic-sessionmanager"
|
|
6054
|
+
]);
|
|
6055
|
+
var SELF_PATHS = Object.freeze({
|
|
6056
|
+
state: "/magic-sessionmanager/admin-impersonation/self",
|
|
6057
|
+
end: "/magic-sessionmanager/admin-impersonation/self/end"
|
|
6058
|
+
});
|
|
6059
|
+
function under(path, prefix) {
|
|
6060
|
+
return path === prefix || path.startsWith(`${prefix}/`);
|
|
6061
|
+
}
|
|
6062
|
+
function classifyAdminImpersonationRequest(method, rawPath) {
|
|
6063
|
+
const verb = String(method || "").toUpperCase();
|
|
6064
|
+
const path = normalizePath(rawPath);
|
|
6065
|
+
if (path === SELF_PATHS.state || path === SELF_PATHS.end) return "self";
|
|
6066
|
+
if (verb === "POST" && path === "/admin/logout") return "logout";
|
|
6067
|
+
if (SAFE_METHODS.includes(verb)) return "read";
|
|
6068
|
+
if (verb === "POST" && READ_ONLY_POSTS.includes(path)) return "read";
|
|
6069
|
+
if (IDENTITY_PREFIXES.some((prefix) => under(path, prefix))) return "identity";
|
|
6070
|
+
return "write";
|
|
6071
|
+
}
|
|
6072
|
+
function recordedPath(rawPath) {
|
|
6073
|
+
return String(rawPath || "").split("?")[0].slice(0, 200);
|
|
6074
|
+
}
|
|
6075
|
+
module2.exports = {
|
|
6076
|
+
ADMIN_DEVICE_PREFIX,
|
|
6077
|
+
ADMIN_SIGN_IN_ACTION,
|
|
6078
|
+
ADMIN_SIGN_IN_DURATIONS,
|
|
6079
|
+
IDENTITY_PREFIXES,
|
|
6080
|
+
LANDING_PATH,
|
|
6081
|
+
READ_ONLY_POSTS,
|
|
6082
|
+
SELF_PATHS,
|
|
6083
|
+
TESTED_STRAPI,
|
|
6084
|
+
adminSignInSupport,
|
|
6085
|
+
allowedSignInDurations,
|
|
6086
|
+
classifyAdminImpersonationRequest,
|
|
6087
|
+
recordedPath,
|
|
6088
|
+
validateSignInBody
|
|
6089
|
+
};
|
|
6090
|
+
}
|
|
6091
|
+
});
|
|
6092
|
+
|
|
6093
|
+
// server/src/middlewares/admin-impersonation-guard.js
|
|
6094
|
+
var require_admin_impersonation_guard = __commonJS({
|
|
6095
|
+
"server/src/middlewares/admin-impersonation-guard.js"(exports2, module2) {
|
|
6096
|
+
"use strict";
|
|
6097
|
+
var { extractBearerToken } = require_extract_token();
|
|
6098
|
+
var { IMPERSONATION_HEADER, normalizePath } = require_impersonation_rules();
|
|
6099
|
+
var { classifyAdminImpersonationRequest, recordedPath } = require_admin_sign_in_rules();
|
|
6100
|
+
function reply(ctx, status, name, message, reason) {
|
|
6101
|
+
ctx.status = status;
|
|
6102
|
+
ctx.body = { data: null, error: { status, name, message, details: { reason } } };
|
|
6103
|
+
}
|
|
6104
|
+
module2.exports = (_config, { strapi: strapi2 }) => {
|
|
6105
|
+
const service = () => strapi2.plugin("magic-sessionmanager").service("impersonation");
|
|
6106
|
+
return async function adminImpersonationGuard(ctx, next) {
|
|
6107
|
+
const prefix = normalizePath(strapi2.config.get("api.rest.prefix", "/api") || "/api");
|
|
6108
|
+
const path = normalizePath(ctx.path);
|
|
6109
|
+
const base = prefix === "/" ? "" : prefix;
|
|
6110
|
+
if (base && (path === base || path.startsWith(`${base}/`))) return next();
|
|
6111
|
+
const bearer = extractBearerToken(ctx);
|
|
6112
|
+
if (!bearer) return next();
|
|
6113
|
+
let impersonationId;
|
|
6114
|
+
let impersonation;
|
|
6115
|
+
try {
|
|
6116
|
+
impersonationId = await service().adminImpersonationOfToken(bearer);
|
|
6117
|
+
if (!impersonationId) return next();
|
|
6118
|
+
impersonation = impersonationId === "unknown" ? null : await service().loadAdminForGuard(impersonationId);
|
|
6119
|
+
} catch {
|
|
6120
|
+
return reply(
|
|
6121
|
+
ctx,
|
|
6122
|
+
503,
|
|
6123
|
+
"ServiceUnavailableError",
|
|
6124
|
+
"Session check unavailable",
|
|
6125
|
+
"impersonation-check"
|
|
6126
|
+
);
|
|
6127
|
+
}
|
|
6128
|
+
ctx.set(IMPERSONATION_HEADER, "true");
|
|
6129
|
+
const kind = classifyAdminImpersonationRequest(ctx.method, path);
|
|
6130
|
+
if (!impersonation || impersonation.state !== "active") {
|
|
6131
|
+
if (kind === "logout") {
|
|
6132
|
+
ctx.body = { data: {} };
|
|
6133
|
+
return void 0;
|
|
6134
|
+
}
|
|
6135
|
+
return reply(ctx, 401, "UnauthorizedError", "Impersonation ended", "impersonation-ended");
|
|
6136
|
+
}
|
|
6137
|
+
if (Date.now() >= impersonation.expiresAt) {
|
|
6138
|
+
try {
|
|
6139
|
+
await service().end(impersonation.id, { reason: "expired" });
|
|
6140
|
+
} catch {
|
|
6141
|
+
}
|
|
6142
|
+
if (kind === "logout") {
|
|
6143
|
+
ctx.body = { data: {} };
|
|
6144
|
+
return void 0;
|
|
6145
|
+
}
|
|
6146
|
+
return reply(ctx, 401, "UnauthorizedError", "Impersonation ended", "impersonation-expired");
|
|
6147
|
+
}
|
|
6148
|
+
if (kind === "logout") {
|
|
6149
|
+
await service().end(impersonation.id, { reason: "ended", endedBy: impersonation.actorId });
|
|
6150
|
+
ctx.body = { data: {} };
|
|
6151
|
+
return void 0;
|
|
6152
|
+
}
|
|
6153
|
+
const method = String(ctx.method).toUpperCase();
|
|
6154
|
+
const recorded = recordedPath(ctx.path);
|
|
6155
|
+
if (kind === "identity" || kind === "write" && !impersonation.allowChanges) {
|
|
6156
|
+
const reason = kind === "identity" ? "identity" : "read-only";
|
|
6157
|
+
service().recordRefusedAdminCall(impersonation, reason, { method, path: recorded });
|
|
6158
|
+
return reply(
|
|
6159
|
+
ctx,
|
|
6160
|
+
403,
|
|
6161
|
+
"ForbiddenError",
|
|
6162
|
+
kind === "identity" ? "Not allowed while signed in as another admin" : "Read-only: changes were not allowed for this sign-in",
|
|
6163
|
+
`impersonation-${reason}`
|
|
6164
|
+
);
|
|
6165
|
+
}
|
|
6166
|
+
ctx.state.magicAdminImpersonation = {
|
|
6167
|
+
id: impersonation.id,
|
|
6168
|
+
actorId: impersonation.actorId,
|
|
6169
|
+
targetId: impersonation.targetId,
|
|
6170
|
+
allowChanges: impersonation.allowChanges,
|
|
6171
|
+
expiresAt: impersonation.expiresAt
|
|
6172
|
+
};
|
|
6173
|
+
let failedStatus = 0;
|
|
6174
|
+
try {
|
|
6175
|
+
await next();
|
|
6176
|
+
} catch (err) {
|
|
6177
|
+
failedStatus = Number(
|
|
6178
|
+
/** @type {any} */
|
|
6179
|
+
err?.status
|
|
6180
|
+
) || 500;
|
|
6181
|
+
throw err;
|
|
6182
|
+
} finally {
|
|
6183
|
+
ctx.set(IMPERSONATION_HEADER, "true");
|
|
6184
|
+
if (kind === "write") {
|
|
6185
|
+
service().recordAdminWrite(impersonation, {
|
|
6186
|
+
method,
|
|
6187
|
+
path: recorded,
|
|
6188
|
+
status: failedStatus || Number(ctx.status) || 0
|
|
6189
|
+
});
|
|
6190
|
+
}
|
|
6191
|
+
}
|
|
6192
|
+
return void 0;
|
|
6193
|
+
};
|
|
6194
|
+
};
|
|
6195
|
+
}
|
|
6196
|
+
});
|
|
6197
|
+
|
|
5943
6198
|
// server/src/middlewares/session-rejection-headers.js
|
|
5944
6199
|
var require_session_rejection_headers = __commonJS({
|
|
5945
6200
|
"server/src/middlewares/session-rejection-headers.js"(exports2, module2) {
|
|
@@ -10263,6 +10518,7 @@ var require_bootstrap = __commonJS({
|
|
|
10263
10518
|
);
|
|
10264
10519
|
log.info("[TIME] Dynamic cleanup + retention scheduled");
|
|
10265
10520
|
strapi2.server.use(require_impersonation_guard()({}, { strapi: strapi2 }));
|
|
10521
|
+
strapi2.server.use(require_admin_impersonation_guard()({}, { strapi: strapi2 }));
|
|
10266
10522
|
const impersonationSweep = setInterval(() => {
|
|
10267
10523
|
strapi2.plugin("magic-sessionmanager").service("impersonation").sweep().catch((err) => log.warn("[IMPERSONATION] Sweep failed:", err?.name || "Error"));
|
|
10268
10524
|
}, 30 * 1e3);
|
|
@@ -12477,7 +12733,7 @@ var require_schema4 = __commonJS({
|
|
|
12477
12733
|
singularName: "impersonation",
|
|
12478
12734
|
pluralName: "impersonations",
|
|
12479
12735
|
displayName: "Impersonation",
|
|
12480
|
-
description: "Time-limited impersonations of app users
|
|
12736
|
+
description: "Time-limited impersonations of app users, permission previews of admins and sign-ins as another admin, started by an admin with a reason"
|
|
12481
12737
|
},
|
|
12482
12738
|
options: {
|
|
12483
12739
|
draftAndPublish: false,
|
|
@@ -12540,13 +12796,17 @@ var require_schema4 = __commonJS({
|
|
|
12540
12796
|
},
|
|
12541
12797
|
handoff: {
|
|
12542
12798
|
type: "enumeration",
|
|
12543
|
-
enum: ["frontend", "token", "preview"],
|
|
12799
|
+
enum: ["frontend", "token", "preview", "admin-session"],
|
|
12544
12800
|
required: true
|
|
12545
12801
|
},
|
|
12546
12802
|
notifyUser: {
|
|
12547
12803
|
type: "boolean",
|
|
12548
12804
|
default: false
|
|
12549
12805
|
},
|
|
12806
|
+
allowChanges: {
|
|
12807
|
+
type: "boolean",
|
|
12808
|
+
default: false
|
|
12809
|
+
},
|
|
12550
12810
|
startedAt: {
|
|
12551
12811
|
type: "datetime",
|
|
12552
12812
|
required: true
|
|
@@ -12826,6 +13086,7 @@ var require_admin = __commonJS({
|
|
|
12826
13086
|
var SETTINGS_UPDATE_ACTION = "plugin::magic-sessionmanager.settings.update";
|
|
12827
13087
|
var IMPERSONATE_USERS_ACTION = "plugin::magic-sessionmanager.impersonate.users";
|
|
12828
13088
|
var IMPERSONATE_ADMINS_ACTION = "plugin::magic-sessionmanager.impersonate.admins";
|
|
13089
|
+
var ADMIN_SIGN_IN_ACTION = "plugin::magic-sessionmanager.impersonate.admins.sign-in";
|
|
12829
13090
|
var impersonationListPolicy = () => [
|
|
12830
13091
|
...adminPolicy(),
|
|
12831
13092
|
"plugin::magic-sessionmanager.can-impersonate"
|
|
@@ -13230,6 +13491,59 @@ var require_admin = __commonJS({
|
|
|
13230
13491
|
]
|
|
13231
13492
|
}
|
|
13232
13493
|
},
|
|
13494
|
+
// Sign in as this admin (4.23).
|
|
13495
|
+
{
|
|
13496
|
+
method: "POST",
|
|
13497
|
+
path: "/impersonation/admins/:adminId/sign-in",
|
|
13498
|
+
handler: "impersonation.signInAdmin",
|
|
13499
|
+
config: {
|
|
13500
|
+
policies: adminSessionsPolicy([ADMIN_SIGN_IN_ACTION]),
|
|
13501
|
+
middlewares: [
|
|
13502
|
+
{
|
|
13503
|
+
name: "plugin::magic-sessionmanager.rate-limit",
|
|
13504
|
+
config: { max: 10, window: 10 * 60 * 1e3 }
|
|
13505
|
+
}
|
|
13506
|
+
]
|
|
13507
|
+
}
|
|
13508
|
+
},
|
|
13509
|
+
{
|
|
13510
|
+
method: "POST",
|
|
13511
|
+
path: "/admin-impersonation/exchange",
|
|
13512
|
+
handler: "impersonation.exchangeAdmin",
|
|
13513
|
+
config: {
|
|
13514
|
+
// The code is bound to the admin who started the sign-in, who must
|
|
13515
|
+
// still hold the action.
|
|
13516
|
+
policies: adminSessionsPolicy([ADMIN_SIGN_IN_ACTION]),
|
|
13517
|
+
middlewares: [
|
|
13518
|
+
{
|
|
13519
|
+
name: "plugin::magic-sessionmanager.rate-limit",
|
|
13520
|
+
config: { max: 10, window: 6e4 }
|
|
13521
|
+
}
|
|
13522
|
+
]
|
|
13523
|
+
}
|
|
13524
|
+
},
|
|
13525
|
+
{
|
|
13526
|
+
method: "GET",
|
|
13527
|
+
path: "/admin-impersonation/self",
|
|
13528
|
+
handler: "impersonation.adminSelf",
|
|
13529
|
+
config: {
|
|
13530
|
+
// The impersonation tab holds the target's token, and the target may
|
|
13531
|
+
// not have plugin access. The admin impersonation guard recognises
|
|
13532
|
+
// the session (signed token + session marker) before routing; the
|
|
13533
|
+
// handler answers 401 to everybody else.
|
|
13534
|
+
auth: false,
|
|
13535
|
+
middlewares: adminReadRateLimit()
|
|
13536
|
+
}
|
|
13537
|
+
},
|
|
13538
|
+
{
|
|
13539
|
+
method: "POST",
|
|
13540
|
+
path: "/admin-impersonation/self/end",
|
|
13541
|
+
handler: "impersonation.endAdminSelf",
|
|
13542
|
+
config: {
|
|
13543
|
+
auth: false,
|
|
13544
|
+
middlewares: adminSignOutRateLimit()
|
|
13545
|
+
}
|
|
13546
|
+
},
|
|
13233
13547
|
{
|
|
13234
13548
|
method: "GET",
|
|
13235
13549
|
path: "/impersonation/previews/:id",
|
|
@@ -18450,6 +18764,14 @@ var require_impersonation = __commonJS({
|
|
|
18450
18764
|
resolveFrontendHandoff,
|
|
18451
18765
|
validateStartBody
|
|
18452
18766
|
} = require_impersonation_rules();
|
|
18767
|
+
var {
|
|
18768
|
+
ADMIN_DEVICE_PREFIX,
|
|
18769
|
+
ADMIN_SIGN_IN_ACTION,
|
|
18770
|
+
LANDING_PATH,
|
|
18771
|
+
adminSignInSupport,
|
|
18772
|
+
allowedSignInDurations,
|
|
18773
|
+
validateSignInBody
|
|
18774
|
+
} = require_admin_sign_in_rules();
|
|
18453
18775
|
var UID = "plugin::magic-sessionmanager.impersonation";
|
|
18454
18776
|
var USER_UID = "plugin::users-permissions.user";
|
|
18455
18777
|
var NATIVE_SESSION_UID = "admin::session";
|
|
@@ -18477,7 +18799,8 @@ var require_impersonation = __commonJS({
|
|
|
18477
18799
|
STARTED: "impersonation.started",
|
|
18478
18800
|
ENDED: "impersonation.ended",
|
|
18479
18801
|
EXPIRED: "impersonation.expired",
|
|
18480
|
-
REFUSED: "impersonation.refused"
|
|
18802
|
+
REFUSED: "impersonation.refused",
|
|
18803
|
+
WRITE: "impersonation.write"
|
|
18481
18804
|
});
|
|
18482
18805
|
function remember(map, key, value) {
|
|
18483
18806
|
map.delete(key);
|
|
@@ -18573,9 +18896,27 @@ var require_impersonation = __commonJS({
|
|
|
18573
18896
|
frontendOrigin: frontend ? frontend.origin : null,
|
|
18574
18897
|
durations: [...USER_DURATIONS],
|
|
18575
18898
|
adminDurations: [...ADMIN_DURATIONS],
|
|
18576
|
-
mode: getNativeSessionManager(strapi2) ? "refresh" : "legacy"
|
|
18899
|
+
mode: getNativeSessionManager(strapi2) ? "refresh" : "legacy",
|
|
18900
|
+
adminSignIn: service.adminSignInStatus()
|
|
18577
18901
|
};
|
|
18578
18902
|
},
|
|
18903
|
+
/**
|
|
18904
|
+
* Whether "Sign in as this admin" can run on this Strapi: a tested
|
|
18905
|
+
* release, Strapi's session manager present, and at least one duration
|
|
18906
|
+
* that fits into one admin access token.
|
|
18907
|
+
*
|
|
18908
|
+
* @returns {{supported: boolean, version: string|null, reason: string|null, durations: number[]}}
|
|
18909
|
+
*/
|
|
18910
|
+
adminSignInStatus() {
|
|
18911
|
+
const support = adminSignInSupport(strapi2.config.get("info.strapi"));
|
|
18912
|
+
const durations = allowedSignInDurations(
|
|
18913
|
+
strapi2.config.get("admin.auth.sessions.accessTokenLifespan", 30 * 60)
|
|
18914
|
+
);
|
|
18915
|
+
let reason = support.reason;
|
|
18916
|
+
if (!reason && typeof strapi2.sessionManager !== "function") reason = "no-session-manager";
|
|
18917
|
+
if (!reason && durations.length === 0) reason = "token-lifespan";
|
|
18918
|
+
return { supported: reason === null, version: support.version, reason, durations };
|
|
18919
|
+
},
|
|
18579
18920
|
/**
|
|
18580
18921
|
* Records a refused start.
|
|
18581
18922
|
*
|
|
@@ -18839,6 +19180,416 @@ var require_impersonation = __commonJS({
|
|
|
18839
19180
|
preview
|
|
18840
19181
|
};
|
|
18841
19182
|
},
|
|
19183
|
+
/**
|
|
19184
|
+
* Starts "Sign in as this admin" (4.23): checks every anti-escalation
|
|
19185
|
+
* rule, stores the impersonation with a one-time code (60 seconds,
|
|
19186
|
+
* stored hashed) and returns the landing path for a new tab. The Strapi
|
|
19187
|
+
* admin session is created only when that tab exchanges the code.
|
|
19188
|
+
*
|
|
19189
|
+
* @param {{actor: any, adminId: unknown, body: unknown, canReadAdmins: boolean,
|
|
19190
|
+
* nested?: boolean}} input Caller (with roles), target id, body; `nested` when the
|
|
19191
|
+
* request itself comes from an impersonation session.
|
|
19192
|
+
* @returns {Promise<{impersonation: object, handoff: {type: 'admin-tab', path: string,
|
|
19193
|
+
* code: string, codeExpiresAt: string}}>}
|
|
19194
|
+
* @throws {ImpersonationError} 400 invalid body; 403 off, unsupported Strapi, nested,
|
|
19195
|
+
* self, Super Admin, inactive, not a subset or already active; 404 unknown admin.
|
|
19196
|
+
* @sideeffect Creates an impersonation row; queues `impersonation.started` or `.refused`.
|
|
19197
|
+
*/
|
|
19198
|
+
async startAdminSignIn({ actor, adminId, body, canReadAdmins, nested = false }) {
|
|
19199
|
+
const actorId = String(actor.id);
|
|
19200
|
+
const refuse = (reason, status, message, extra = {}) => {
|
|
19201
|
+
service.recordRefused({
|
|
19202
|
+
actorId,
|
|
19203
|
+
targetType: "admin",
|
|
19204
|
+
targetId: adminId,
|
|
19205
|
+
reason,
|
|
19206
|
+
via: "sign-in"
|
|
19207
|
+
});
|
|
19208
|
+
return new ImpersonationError(status, message, { reason, ...extra });
|
|
19209
|
+
};
|
|
19210
|
+
if (!await service.isEnabled()) {
|
|
19211
|
+
throw refuse("disabled", 403, "Impersonation is turned off");
|
|
19212
|
+
}
|
|
19213
|
+
const support = service.adminSignInStatus();
|
|
19214
|
+
if (!support.supported) {
|
|
19215
|
+
throw refuse("unsupported", 403, "Not available on this Strapi version", {
|
|
19216
|
+
cause: support.reason
|
|
19217
|
+
});
|
|
19218
|
+
}
|
|
19219
|
+
if (nested)
|
|
19220
|
+
throw refuse("nested", 403, "You cannot start an impersonation while impersonating");
|
|
19221
|
+
if (typeof adminId !== "string" || !ADMIN_ID_PATTERN.test(adminId)) {
|
|
19222
|
+
throw new ImpersonationError(404, "Admin not found");
|
|
19223
|
+
}
|
|
19224
|
+
const { value, errors: fieldErrors } = validateSignInBody(body, support.durations);
|
|
19225
|
+
if (!value) {
|
|
19226
|
+
throw new ImpersonationError(400, "Invalid sign-in request", {
|
|
19227
|
+
errors: fieldErrors.map((error) => ({ ...error, name: "ValidationError" }))
|
|
19228
|
+
});
|
|
19229
|
+
}
|
|
19230
|
+
const [caller, target] = await Promise.all([
|
|
19231
|
+
loadAdmin(strapi2, actor.id),
|
|
19232
|
+
loadAdmin(strapi2, Number(adminId))
|
|
19233
|
+
]);
|
|
19234
|
+
if (!target) throw new ImpersonationError(404, "Admin not found");
|
|
19235
|
+
const refusal = await previewRefusal(strapi2, caller || actor, target);
|
|
19236
|
+
if (refusal?.reason === "super-admin" && !canReadAdmins) {
|
|
19237
|
+
service.recordRefused({
|
|
19238
|
+
actorId,
|
|
19239
|
+
targetType: "admin",
|
|
19240
|
+
targetId: adminId,
|
|
19241
|
+
reason: "super-admin",
|
|
19242
|
+
via: "sign-in"
|
|
19243
|
+
});
|
|
19244
|
+
throw new ImpersonationError(404, "Admin not found");
|
|
19245
|
+
}
|
|
19246
|
+
if (refusal) {
|
|
19247
|
+
const messages = {
|
|
19248
|
+
self: "You cannot sign in as yourself",
|
|
19249
|
+
"super-admin": "You cannot sign in as a Super Admin",
|
|
19250
|
+
"not-subset": "This admin has permissions that you do not have"
|
|
19251
|
+
};
|
|
19252
|
+
throw refuse(
|
|
19253
|
+
refusal.reason,
|
|
19254
|
+
403,
|
|
19255
|
+
messages[refusal.reason],
|
|
19256
|
+
refusal.uncovered ? { uncovered: refusal.uncovered } : {}
|
|
19257
|
+
);
|
|
19258
|
+
}
|
|
19259
|
+
if (target.isActive !== true) {
|
|
19260
|
+
throw refuse("inactive", 403, "This admin account is not active");
|
|
19261
|
+
}
|
|
19262
|
+
await service.sweep();
|
|
19263
|
+
const running = await query().findOne({
|
|
19264
|
+
where: {
|
|
19265
|
+
targetType: "admin",
|
|
19266
|
+
targetId: String(target.id),
|
|
19267
|
+
handoff: "admin-session",
|
|
19268
|
+
state: "active"
|
|
19269
|
+
},
|
|
19270
|
+
select: ["id"]
|
|
19271
|
+
});
|
|
19272
|
+
if (running)
|
|
19273
|
+
throw refuse("already-active", 403, "Someone is already signed in as this admin");
|
|
19274
|
+
const now = /* @__PURE__ */ new Date();
|
|
19275
|
+
const expiresAt = new Date(now.getTime() + value.durationMinutes * 60 * 1e3);
|
|
19276
|
+
const code = crypto.randomBytes(32).toString("base64url");
|
|
19277
|
+
const codeExpiresAt = new Date(now.getTime() + CODE_TTL_MS);
|
|
19278
|
+
const row = await query().create({
|
|
19279
|
+
data: {
|
|
19280
|
+
targetType: "admin",
|
|
19281
|
+
targetId: String(target.id),
|
|
19282
|
+
actorId,
|
|
19283
|
+
state: "active",
|
|
19284
|
+
reason: value.reason,
|
|
19285
|
+
durationMinutes: value.durationMinutes,
|
|
19286
|
+
handoff: "admin-session",
|
|
19287
|
+
notifyUser: false,
|
|
19288
|
+
allowChanges: value.allowChanges,
|
|
19289
|
+
startedAt: now,
|
|
19290
|
+
expiresAt,
|
|
19291
|
+
codeHash: hashToken(code),
|
|
19292
|
+
codeExpiresAt
|
|
19293
|
+
}
|
|
19294
|
+
});
|
|
19295
|
+
record({
|
|
19296
|
+
event: EVENTS.STARTED,
|
|
19297
|
+
actorType: "admin",
|
|
19298
|
+
actorId,
|
|
19299
|
+
targetType: "admin",
|
|
19300
|
+
targetId: String(target.id),
|
|
19301
|
+
meta: {
|
|
19302
|
+
reason: value.reason,
|
|
19303
|
+
durationMinutes: value.durationMinutes,
|
|
19304
|
+
handoff: "admin-session",
|
|
19305
|
+
allowChanges: value.allowChanges
|
|
19306
|
+
}
|
|
19307
|
+
});
|
|
19308
|
+
log.info(
|
|
19309
|
+
`[IMPERSONATION] Admin sign-in started (${value.durationMinutes} min, ${value.allowChanges ? "changes allowed" : "read-only"})`
|
|
19310
|
+
);
|
|
19311
|
+
return {
|
|
19312
|
+
impersonation: service.toDto(row, {
|
|
19313
|
+
admins: /* @__PURE__ */ new Map([
|
|
19314
|
+
[String(target.id), canReadAdmins ? target : null],
|
|
19315
|
+
[actorId, caller || actor]
|
|
19316
|
+
]),
|
|
19317
|
+
viewer: actor
|
|
19318
|
+
}),
|
|
19319
|
+
handoff: {
|
|
19320
|
+
type: "admin-tab",
|
|
19321
|
+
path: LANDING_PATH,
|
|
19322
|
+
cookieName: service.accessCookieName(),
|
|
19323
|
+
code,
|
|
19324
|
+
codeExpiresAt: codeExpiresAt.toISOString()
|
|
19325
|
+
}
|
|
19326
|
+
};
|
|
19327
|
+
},
|
|
19328
|
+
/**
|
|
19329
|
+
* Exchanges the one-time code of an admin sign-in for an admin access
|
|
19330
|
+
* token of the target, in the new tab. The request carries the starting
|
|
19331
|
+
* admin's own token, and only that admin can redeem the code. The Strapi
|
|
19332
|
+
* admin session is created here on a dedicated device id, marked in its
|
|
19333
|
+
* metadata, and expires with the impersonation; its refresh token never
|
|
19334
|
+
* leaves the server, so the tab cannot renew it.
|
|
19335
|
+
*
|
|
19336
|
+
* @param {{code: unknown, actor: any, ability: any}} input Code and the calling admin.
|
|
19337
|
+
* @returns {Promise<object|null>} `{ token, expiresAt, cookieName, impersonation }`, or null
|
|
19338
|
+
* for any unusable code.
|
|
19339
|
+
* @sideeffect Consumes the code; creates one Strapi admin session for the target.
|
|
19340
|
+
*/
|
|
19341
|
+
async exchangeAdminCode({ code, actor, ability }) {
|
|
19342
|
+
if (typeof code !== "string" || !CODE_PATTERN.test(code)) return null;
|
|
19343
|
+
if (!await service.isEnabled() || !service.adminSignInStatus().supported) return null;
|
|
19344
|
+
const codeHash = hashToken(code);
|
|
19345
|
+
const now = /* @__PURE__ */ new Date();
|
|
19346
|
+
const row = await query().findOne({
|
|
19347
|
+
where: { codeHash, state: "active", handoff: "admin-session" }
|
|
19348
|
+
});
|
|
19349
|
+
if (!row || String(row.actorId) !== String(actor?.id)) return null;
|
|
19350
|
+
let stillAllowed;
|
|
19351
|
+
try {
|
|
19352
|
+
stillAllowed = ability?.can?.(ADMIN_SIGN_IN_ACTION) === true;
|
|
19353
|
+
} catch {
|
|
19354
|
+
stillAllowed = false;
|
|
19355
|
+
}
|
|
19356
|
+
if (!stillAllowed) return null;
|
|
19357
|
+
const claimed = await query().updateMany({
|
|
19358
|
+
where: {
|
|
19359
|
+
id: row.id,
|
|
19360
|
+
codeHash,
|
|
19361
|
+
state: "active",
|
|
19362
|
+
credentialIssuedAt: { $null: true },
|
|
19363
|
+
codeExpiresAt: { $gt: now },
|
|
19364
|
+
expiresAt: { $gt: now }
|
|
19365
|
+
},
|
|
19366
|
+
data: { codeHash: null, codeExpiresAt: null, credentialIssuedAt: now }
|
|
19367
|
+
});
|
|
19368
|
+
if (Number(claimed?.count) !== 1) return null;
|
|
19369
|
+
const target = await loadAdmin(strapi2, Number(row.targetId));
|
|
19370
|
+
if (!target || target.isActive !== true || await previewRefusal(strapi2, actor, target)) {
|
|
19371
|
+
await service.end(row.documentId, { reason: "failed", silent: true });
|
|
19372
|
+
return null;
|
|
19373
|
+
}
|
|
19374
|
+
try {
|
|
19375
|
+
const token = await service.issueAdminSession(row, target);
|
|
19376
|
+
return {
|
|
19377
|
+
token,
|
|
19378
|
+
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
19379
|
+
cookieName: service.accessCookieName(),
|
|
19380
|
+
impersonation: {
|
|
19381
|
+
id: row.documentId,
|
|
19382
|
+
allowChanges: row.allowChanges === true,
|
|
19383
|
+
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
19384
|
+
targetName: adminLabel(target),
|
|
19385
|
+
actorName: adminLabel(actor)
|
|
19386
|
+
}
|
|
19387
|
+
};
|
|
19388
|
+
} catch (err) {
|
|
19389
|
+
await service.end(row.documentId, { reason: "failed", silent: true });
|
|
19390
|
+
throw err;
|
|
19391
|
+
}
|
|
19392
|
+
},
|
|
19393
|
+
/**
|
|
19394
|
+
* Name of Strapi's admin access cookie (`admin.auth.cookie.name`,
|
|
19395
|
+
* default `jwtToken`), so the impersonation tab can hide it.
|
|
19396
|
+
*
|
|
19397
|
+
* @returns {string}
|
|
19398
|
+
*/
|
|
19399
|
+
accessCookieName() {
|
|
19400
|
+
const configured = strapi2.config.get("admin.auth.cookie.name");
|
|
19401
|
+
return typeof configured === "string" && /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(configured.trim()) ? configured.trim() : "jwtToken";
|
|
19402
|
+
},
|
|
19403
|
+
/**
|
|
19404
|
+
* Creates the Strapi admin session of an admin sign-in and returns its
|
|
19405
|
+
* access token. The device id is recorded before the session exists, so
|
|
19406
|
+
* a failure below still revokes exactly this session.
|
|
19407
|
+
*
|
|
19408
|
+
* @param {any} row Impersonation row.
|
|
19409
|
+
* @param {any} target Target admin.
|
|
19410
|
+
* @returns {Promise<string>} Access token.
|
|
19411
|
+
* @throws {Error} When the session cannot be created.
|
|
19412
|
+
* @sideeffect Creates one `admin::session` row and shortens its expiry.
|
|
19413
|
+
*/
|
|
19414
|
+
async issueAdminSession(row, target) {
|
|
19415
|
+
const expiresAt = new Date(row.expiresAt);
|
|
19416
|
+
if (expiresAt.getTime() - Date.now() < 5e3) {
|
|
19417
|
+
throw new ImpersonationError(403, "Impersonation ended");
|
|
19418
|
+
}
|
|
19419
|
+
const deviceId = `${ADMIN_DEVICE_PREFIX}${crypto.randomBytes(12).toString("hex")}`;
|
|
19420
|
+
await query().update({
|
|
19421
|
+
where: { id: row.id },
|
|
19422
|
+
data: { nativeUserId: String(target.id), nativeDeviceId: deviceId }
|
|
19423
|
+
});
|
|
19424
|
+
const manager = strapi2.sessionManager("admin");
|
|
19425
|
+
const refresh = await manager.generateRefreshToken(String(target.id), deviceId, {
|
|
19426
|
+
type: "session",
|
|
19427
|
+
metadata: {
|
|
19428
|
+
loginAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
19429
|
+
deviceName: "Impersonation",
|
|
19430
|
+
msmImpersonation: row.documentId
|
|
19431
|
+
}
|
|
19432
|
+
});
|
|
19433
|
+
await strapi2.db.query(NATIVE_SESSION_UID).update({
|
|
19434
|
+
where: { sessionId: refresh.sessionId },
|
|
19435
|
+
data: { expiresAt, absoluteExpiresAt: expiresAt }
|
|
19436
|
+
});
|
|
19437
|
+
const access = await manager.generateAccessToken(refresh.token);
|
|
19438
|
+
if (!access || "error" in access) throw new Error("Access token could not be minted");
|
|
19439
|
+
remember(nativeSessionCache, `admin:${refresh.sessionId}`, row.documentId);
|
|
19440
|
+
recordCache.delete(row.documentId);
|
|
19441
|
+
return access.token;
|
|
19442
|
+
},
|
|
19443
|
+
/**
|
|
19444
|
+
* Deletes the Strapi admin session of an admin sign-in, and only that
|
|
19445
|
+
* one: by its user and its own device id. Without a stored device id
|
|
19446
|
+
* nothing is deleted, because Strapi's `invalidateRefreshToken` without
|
|
19447
|
+
* a device id would delete every session of the target.
|
|
19448
|
+
*
|
|
19449
|
+
* @param {any} row Impersonation row.
|
|
19450
|
+
* @returns {Promise<void>}
|
|
19451
|
+
* @sideeffect Deletes `admin::session` rows of the impersonation device.
|
|
19452
|
+
*/
|
|
19453
|
+
async revokeAdminSession(row) {
|
|
19454
|
+
const userId = row.nativeUserId ? String(row.nativeUserId) : "";
|
|
19455
|
+
const deviceId = row.nativeDeviceId ? String(row.nativeDeviceId) : "";
|
|
19456
|
+
if (!userId || !deviceId.startsWith(ADMIN_DEVICE_PREFIX)) return;
|
|
19457
|
+
try {
|
|
19458
|
+
await strapi2.sessionManager("admin").invalidateRefreshToken(userId, deviceId);
|
|
19459
|
+
} catch (err) {
|
|
19460
|
+
log.error("[IMPERSONATION] Admin session revocation failed:", err?.name || "Error");
|
|
19461
|
+
}
|
|
19462
|
+
},
|
|
19463
|
+
/**
|
|
19464
|
+
* The admin sign-in an admin access token belongs to, or null. The
|
|
19465
|
+
* signature is verified with Strapi's session manager; the session row
|
|
19466
|
+
* names the impersonation in its metadata (or by its device id, which
|
|
19467
|
+
* fails closed). Results are cached per session id (immutable).
|
|
19468
|
+
*
|
|
19469
|
+
* @param {string} token Bearer token.
|
|
19470
|
+
* @returns {Promise<string|null>} Impersonation id (`'unknown'` for a marked device
|
|
19471
|
+
* without readable metadata).
|
|
19472
|
+
* @throws {Error} When the session lookup fails.
|
|
19473
|
+
*/
|
|
19474
|
+
async adminImpersonationOfToken(token) {
|
|
19475
|
+
const payload = decodeJwtPayload(token);
|
|
19476
|
+
if (!payload || payload.type !== "access") return null;
|
|
19477
|
+
const sessionId = payload.sessionId;
|
|
19478
|
+
if (typeof sessionId !== "string" || !/^[a-f0-9]{16,64}$/i.test(sessionId)) return null;
|
|
19479
|
+
const key = `admin:${sessionId}`;
|
|
19480
|
+
if (nativeSessionCache.has(key)) {
|
|
19481
|
+
return service.verifyAdminToken(token) ? nativeSessionCache.get(key) : null;
|
|
19482
|
+
}
|
|
19483
|
+
if (!service.verifyAdminToken(token)) return null;
|
|
19484
|
+
const session = await strapi2.db.query(NATIVE_SESSION_UID).findOne({
|
|
19485
|
+
where: { sessionId, origin: "admin" },
|
|
19486
|
+
select: ["metadata", "deviceId"]
|
|
19487
|
+
});
|
|
19488
|
+
let id = null;
|
|
19489
|
+
const mark = session?.metadata?.msmImpersonation;
|
|
19490
|
+
if (typeof mark === "string" && DOCUMENT_ID_PATTERN.test(mark)) id = mark;
|
|
19491
|
+
else if (typeof session?.deviceId === "string" && session.deviceId.startsWith(ADMIN_DEVICE_PREFIX)) {
|
|
19492
|
+
id = "unknown";
|
|
19493
|
+
}
|
|
19494
|
+
if (session) remember(nativeSessionCache, key, id);
|
|
19495
|
+
return id;
|
|
19496
|
+
},
|
|
19497
|
+
/**
|
|
19498
|
+
* Whether a token is a validly signed Strapi admin access token.
|
|
19499
|
+
*
|
|
19500
|
+
* @param {string} token Bearer token.
|
|
19501
|
+
* @returns {boolean}
|
|
19502
|
+
*/
|
|
19503
|
+
verifyAdminToken(token) {
|
|
19504
|
+
try {
|
|
19505
|
+
return strapi2.sessionManager("admin").validateAccessToken(token)?.isValid === true;
|
|
19506
|
+
} catch {
|
|
19507
|
+
return false;
|
|
19508
|
+
}
|
|
19509
|
+
},
|
|
19510
|
+
/**
|
|
19511
|
+
* Loads an admin sign-in for the request guard (active rows are read on
|
|
19512
|
+
* every request, like `loadForGuard`).
|
|
19513
|
+
*
|
|
19514
|
+
* @param {string} documentId Impersonation id.
|
|
19515
|
+
* @returns {Promise<{id: string, actorId: string, targetId: string, state: string,
|
|
19516
|
+
* expiresAt: number, allowChanges: boolean}|null>}
|
|
19517
|
+
*/
|
|
19518
|
+
async loadAdminForGuard(documentId) {
|
|
19519
|
+
const cached = recordCache.get(`admin:${documentId}`);
|
|
19520
|
+
if (cached && Date.now() - cached.at < RECORD_CACHE_MS) return cached.value;
|
|
19521
|
+
const row = await query().findOne({
|
|
19522
|
+
where: { documentId, targetType: "admin", handoff: "admin-session" },
|
|
19523
|
+
select: ["documentId", "actorId", "targetId", "state", "expiresAt", "allowChanges"]
|
|
19524
|
+
});
|
|
19525
|
+
const value = row ? {
|
|
19526
|
+
id: row.documentId,
|
|
19527
|
+
actorId: String(row.actorId),
|
|
19528
|
+
targetId: String(row.targetId),
|
|
19529
|
+
state: row.state,
|
|
19530
|
+
expiresAt: new Date(row.expiresAt).getTime(),
|
|
19531
|
+
allowChanges: row.allowChanges === true
|
|
19532
|
+
} : null;
|
|
19533
|
+
if (!value || value.state !== "active") {
|
|
19534
|
+
remember(recordCache, `admin:${documentId}`, { value, at: Date.now() });
|
|
19535
|
+
}
|
|
19536
|
+
return value;
|
|
19537
|
+
},
|
|
19538
|
+
/**
|
|
19539
|
+
* Records a change made while signed in as another admin: the real
|
|
19540
|
+
* admin is the actor, the target admin in `meta.impersonating`.
|
|
19541
|
+
*
|
|
19542
|
+
* @param {{id: string, actorId: string, targetId: string}} impersonation Impersonation.
|
|
19543
|
+
* @param {{method: string, path: string, status: number}} request What was done.
|
|
19544
|
+
* @returns {void}
|
|
19545
|
+
* @sideeffect Queues one always-on `impersonation.write` event.
|
|
19546
|
+
*/
|
|
19547
|
+
recordAdminWrite(impersonation, { method, path, status }) {
|
|
19548
|
+
record({
|
|
19549
|
+
event: EVENTS.WRITE,
|
|
19550
|
+
actorType: "admin",
|
|
19551
|
+
actorId: impersonation.actorId,
|
|
19552
|
+
targetType: "admin",
|
|
19553
|
+
targetId: impersonation.targetId,
|
|
19554
|
+
meta: {
|
|
19555
|
+
impersonation: impersonation.id,
|
|
19556
|
+
impersonating: impersonation.targetId,
|
|
19557
|
+
method,
|
|
19558
|
+
route: path,
|
|
19559
|
+
status
|
|
19560
|
+
}
|
|
19561
|
+
});
|
|
19562
|
+
},
|
|
19563
|
+
/**
|
|
19564
|
+
* Records a refused admin API call of an admin sign-in (one event per
|
|
19565
|
+
* impersonation and reason per minute).
|
|
19566
|
+
*
|
|
19567
|
+
* @param {{id: string, actorId: string, targetId: string}} impersonation Impersonation.
|
|
19568
|
+
* @param {string} reason `read-only` or `identity`.
|
|
19569
|
+
* @param {{method: string, path: string}} request Refused request.
|
|
19570
|
+
* @returns {void}
|
|
19571
|
+
*/
|
|
19572
|
+
recordRefusedAdminCall(impersonation, reason, { method, path }) {
|
|
19573
|
+
const key = `${impersonation.id}|admin-${reason}`;
|
|
19574
|
+
const now = Date.now();
|
|
19575
|
+
const last = refusals.get(key);
|
|
19576
|
+
if (last !== void 0 && now - last < REFUSAL_DEDUP_MS) return;
|
|
19577
|
+
remember(refusals, key, now);
|
|
19578
|
+
record({
|
|
19579
|
+
event: EVENTS.REFUSED,
|
|
19580
|
+
actorType: "admin",
|
|
19581
|
+
actorId: impersonation.actorId,
|
|
19582
|
+
targetType: "admin",
|
|
19583
|
+
targetId: impersonation.targetId,
|
|
19584
|
+
meta: {
|
|
19585
|
+
refusal: `call-${reason}`,
|
|
19586
|
+
impersonation: impersonation.id,
|
|
19587
|
+
impersonating: impersonation.targetId,
|
|
19588
|
+
method,
|
|
19589
|
+
route: path
|
|
19590
|
+
}
|
|
19591
|
+
});
|
|
19592
|
+
},
|
|
18842
19593
|
/**
|
|
18843
19594
|
* Mints the short-lived credential of an impersonation and links its
|
|
18844
19595
|
* Magic session row.
|
|
@@ -19008,6 +19759,10 @@ var require_impersonation = __commonJS({
|
|
|
19008
19759
|
* @sideeffect Deletes Strapi session rows, writes a revocation record, ends the Magic session.
|
|
19009
19760
|
*/
|
|
19010
19761
|
async revoke(row, sessionReason) {
|
|
19762
|
+
if (row.handoff === "admin-session") {
|
|
19763
|
+
await service.revokeAdminSession(row);
|
|
19764
|
+
return;
|
|
19765
|
+
}
|
|
19011
19766
|
if (row.nativeUserId && row.nativeDeviceId) {
|
|
19012
19767
|
try {
|
|
19013
19768
|
await strapi2.sessionManager(NATIVE_ORIGIN).invalidateRefreshToken(String(row.nativeUserId), String(row.nativeDeviceId));
|
|
@@ -19149,6 +19904,7 @@ var require_impersonation = __commonJS({
|
|
|
19149
19904
|
reason: row.reason,
|
|
19150
19905
|
durationMinutes: row.durationMinutes,
|
|
19151
19906
|
handoff: row.handoff,
|
|
19907
|
+
allowChanges: row.allowChanges === true,
|
|
19152
19908
|
credentialIssued: Boolean(row.credentialIssuedAt),
|
|
19153
19909
|
startedAt: new Date(row.startedAt).toISOString(),
|
|
19154
19910
|
expiresAt: new Date(row.expiresAt).toISOString(),
|
|
@@ -19297,6 +20053,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19297
20053
|
var { sendServerError } = require_http_errors();
|
|
19298
20054
|
var IMPERSONATE_USERS_ACTION = "plugin::magic-sessionmanager.impersonate.users";
|
|
19299
20055
|
var IMPERSONATE_ADMINS_ACTION = "plugin::magic-sessionmanager.impersonate.admins";
|
|
20056
|
+
var { ADMIN_SIGN_IN_ACTION } = require_admin_sign_in_rules();
|
|
19300
20057
|
function callerCan(ctx, action) {
|
|
19301
20058
|
try {
|
|
19302
20059
|
return ctx?.state?.userAbility?.can?.(action) === true;
|
|
@@ -19307,7 +20064,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19307
20064
|
function visibleTargetTypes(ctx) {
|
|
19308
20065
|
return [
|
|
19309
20066
|
...callerCan(ctx, IMPERSONATE_USERS_ACTION) ? ["user"] : [],
|
|
19310
|
-
...callerCan(ctx, IMPERSONATE_ADMINS_ACTION) ? ["admin"] : []
|
|
20067
|
+
...callerCan(ctx, IMPERSONATE_ADMINS_ACTION) || callerCan(ctx, ADMIN_SIGN_IN_ACTION) ? ["admin"] : []
|
|
19311
20068
|
];
|
|
19312
20069
|
}
|
|
19313
20070
|
var REPLY = Object.freeze({ 400: "badRequest", 403: "forbidden", 404: "notFound" });
|
|
@@ -19470,6 +20227,104 @@ var require_impersonation2 = __commonJS({
|
|
|
19470
20227
|
};
|
|
19471
20228
|
});
|
|
19472
20229
|
},
|
|
20230
|
+
/**
|
|
20231
|
+
* Starts "Sign in as this admin" (4.23). Returns the landing path of the
|
|
20232
|
+
* new tab and a one-time code for it; no session exists yet.
|
|
20233
|
+
*
|
|
20234
|
+
* @route POST /magic-sessionmanager/impersonation/admins/:adminId/sign-in
|
|
20235
|
+
* @auth Admin JWT only (admin API tokens and impersonation sessions are refused);
|
|
20236
|
+
* `access` + `admin-sessions.read` + `impersonate.admins.sign-in`.
|
|
20237
|
+
* @param {any} ctx Body `{ reason: string(3..255), durationMinutes: 15|30,
|
|
20238
|
+
* allowChanges?: boolean }`.
|
|
20239
|
+
* @returns {Promise<void>} 201 `{ data: { impersonation, handoff: { type: 'admin-tab', path,
|
|
20240
|
+
* code, codeExpiresAt } } }`. Answers 403 with `details.reason` `disabled`, `unsupported`,
|
|
20241
|
+
* `api-token`, `nested`, `self`, `super-admin`, `not-subset`, `inactive` or
|
|
20242
|
+
* `already-active`, 400 for an invalid body and 404 for an unknown admin.
|
|
20243
|
+
* @sideeffect Creates an impersonation row; logs `impersonation.started` or `.refused`.
|
|
20244
|
+
*/
|
|
20245
|
+
async signInAdmin(ctx) {
|
|
20246
|
+
ctx.set("Cache-Control", "no-store");
|
|
20247
|
+
return respond(ctx, "sign-in", async () => {
|
|
20248
|
+
const { adminId } = ctx.params;
|
|
20249
|
+
requirePerson(ctx, "admin", adminId);
|
|
20250
|
+
const result = await service().startAdminSignIn({
|
|
20251
|
+
actor: ctx.state.user,
|
|
20252
|
+
adminId,
|
|
20253
|
+
body: ctx.request.body,
|
|
20254
|
+
canReadAdmins: callerCan(ctx, "admin::users.read"),
|
|
20255
|
+
nested: Boolean(ctx.state.magicAdminImpersonation)
|
|
20256
|
+
});
|
|
20257
|
+
ctx.status = 201;
|
|
20258
|
+
ctx.body = { data: result };
|
|
20259
|
+
});
|
|
20260
|
+
},
|
|
20261
|
+
/**
|
|
20262
|
+
* Exchanges the one-time code of an admin sign-in for the target's
|
|
20263
|
+
* access token. Called by the new tab with the starting admin's own
|
|
20264
|
+
* token; only that admin can redeem the code. Every unusable code gets
|
|
20265
|
+
* the same 400.
|
|
20266
|
+
*
|
|
20267
|
+
* @route POST /magic-sessionmanager/admin-impersonation/exchange
|
|
20268
|
+
* @auth Admin JWT of the admin who started it (still holding `impersonate.admins.sign-in`).
|
|
20269
|
+
* @param {any} ctx Body `{ code: string }`.
|
|
20270
|
+
* @returns {Promise<void>} `{ data: { token, expiresAt, cookieName, impersonation } }`.
|
|
20271
|
+
* @sideeffect Consumes the code; creates one Strapi admin session for the target.
|
|
20272
|
+
*/
|
|
20273
|
+
async exchangeAdmin(ctx) {
|
|
20274
|
+
ctx.set("Cache-Control", "no-store");
|
|
20275
|
+
const body = ctx.request.body;
|
|
20276
|
+
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;
|
|
20277
|
+
return respond(ctx, "exchange", async () => {
|
|
20278
|
+
const result = valid ? await service().exchangeAdminCode({
|
|
20279
|
+
code: body.code,
|
|
20280
|
+
actor: ctx.state.user,
|
|
20281
|
+
ability: ctx.state.userAbility
|
|
20282
|
+
}) : null;
|
|
20283
|
+
if (!result) throw new ImpersonationError(400, "Invalid or expired code");
|
|
20284
|
+
ctx.body = { data: result };
|
|
20285
|
+
});
|
|
20286
|
+
},
|
|
20287
|
+
/**
|
|
20288
|
+
* State of the admin sign-in of the calling tab, for its banner.
|
|
20289
|
+
*
|
|
20290
|
+
* @route GET /magic-sessionmanager/admin-impersonation/self
|
|
20291
|
+
* @auth The impersonation session's own token (checked by the admin impersonation guard);
|
|
20292
|
+
* 401 for any other caller.
|
|
20293
|
+
* @returns {Promise<void>} `{ data: { id, expiresAt, allowChanges } }`.
|
|
20294
|
+
*/
|
|
20295
|
+
async adminSelf(ctx) {
|
|
20296
|
+
ctx.set("Cache-Control", "no-store");
|
|
20297
|
+
const current = ctx.state.magicAdminImpersonation;
|
|
20298
|
+
if (!current) return ctx.unauthorized("Not an impersonation session");
|
|
20299
|
+
ctx.body = {
|
|
20300
|
+
data: {
|
|
20301
|
+
id: current.id,
|
|
20302
|
+
expiresAt: new Date(current.expiresAt).toISOString(),
|
|
20303
|
+
allowChanges: current.allowChanges === true
|
|
20304
|
+
}
|
|
20305
|
+
};
|
|
20306
|
+
return void 0;
|
|
20307
|
+
},
|
|
20308
|
+
/**
|
|
20309
|
+
* Ends the admin sign-in of the calling tab: deletes its own Strapi
|
|
20310
|
+
* session and nothing else.
|
|
20311
|
+
*
|
|
20312
|
+
* @route POST /magic-sessionmanager/admin-impersonation/self/end
|
|
20313
|
+
* @auth The impersonation session's own token; 401 for any other caller.
|
|
20314
|
+
* @returns {Promise<void>} `{ data: { ended: boolean } }`.
|
|
20315
|
+
* @sideeffect Ends the impersonation; logs `impersonation.ended`.
|
|
20316
|
+
*/
|
|
20317
|
+
async endAdminSelf(ctx) {
|
|
20318
|
+
const current = ctx.state.magicAdminImpersonation;
|
|
20319
|
+
if (!current) return ctx.unauthorized("Not an impersonation session");
|
|
20320
|
+
return respond(ctx, "end", async () => {
|
|
20321
|
+
const ended = await service().end(current.id, {
|
|
20322
|
+
reason: "ended",
|
|
20323
|
+
endedBy: current.actorId
|
|
20324
|
+
});
|
|
20325
|
+
ctx.body = { data: { ended } };
|
|
20326
|
+
});
|
|
20327
|
+
},
|
|
19473
20328
|
/**
|
|
19474
20329
|
* Exchanges a one-time hand-off code for the impersonation credential.
|
|
19475
20330
|
* Public on purpose: the code is the credential (32 random bytes, stored
|
|
@@ -19497,6 +20352,7 @@ var require_impersonation2 = __commonJS({
|
|
|
19497
20352
|
};
|
|
19498
20353
|
module2.exports.IMPERSONATE_USERS_ACTION = IMPERSONATE_USERS_ACTION;
|
|
19499
20354
|
module2.exports.IMPERSONATE_ADMINS_ACTION = IMPERSONATE_ADMINS_ACTION;
|
|
20355
|
+
module2.exports.ADMIN_SIGN_IN_ACTION = ADMIN_SIGN_IN_ACTION;
|
|
19500
20356
|
}
|
|
19501
20357
|
});
|
|
19502
20358
|
|
|
@@ -23371,6 +24227,7 @@ var require_security_log = __commonJS({
|
|
|
23371
24227
|
function signInWhere(query) {
|
|
23372
24228
|
const conditions = [{ event: { $in: [...SIGN_IN_FILTERS[query.event || "all"]] } }];
|
|
23373
24229
|
if (query.adminId) conditions.push({ targetType: "admin", targetId: String(query.adminId) });
|
|
24230
|
+
else if (query.event === "impersonation") conditions.push({ targetType: "admin" });
|
|
23374
24231
|
if (query.from) conditions.push({ createdAt: { $gte: query.from } });
|
|
23375
24232
|
if (query.to) conditions.push({ createdAt: { $lte: query.to } });
|
|
23376
24233
|
return { $and: conditions };
|
|
@@ -24208,7 +25065,7 @@ var require_can_impersonate = __commonJS({
|
|
|
24208
25065
|
return false;
|
|
24209
25066
|
}
|
|
24210
25067
|
};
|
|
24211
|
-
return can("plugin::magic-sessionmanager.impersonate.users") || can("plugin::magic-sessionmanager.impersonate.admins");
|
|
25068
|
+
return can("plugin::magic-sessionmanager.impersonate.users") || can("plugin::magic-sessionmanager.impersonate.admins") || can("plugin::magic-sessionmanager.impersonate.admins.sign-in");
|
|
24212
25069
|
};
|
|
24213
25070
|
}
|
|
24214
25071
|
});
|