302 lines
7.8 KiB
JavaScript
302 lines
7.8 KiB
JavaScript
// Browser-only fixtures. A real adapter must obtain safe UI state from its own backend.
|
|
export const identityStates = [
|
|
"authenticated",
|
|
"loading",
|
|
"signed_out",
|
|
"access_denied",
|
|
"factor_required",
|
|
"factor_enrollment_required",
|
|
"rebind_required",
|
|
"unavailable",
|
|
"account_changed",
|
|
"signing_out",
|
|
];
|
|
export const applicationCatalog = [
|
|
{
|
|
id: "identity",
|
|
name: "Anidev Identity",
|
|
code: "ID",
|
|
description: ["Аккаунт и способы входа", "Account and sign-in methods"],
|
|
},
|
|
{
|
|
id: "license",
|
|
name: "License Console",
|
|
code: "LC",
|
|
description: ["Лицензии и активации", "Licenses and activations"],
|
|
},
|
|
{
|
|
id: "monitor",
|
|
name: "Network Monitor",
|
|
code: "NM",
|
|
description: ["Сервисы и состояние сети", "Services and network health"],
|
|
},
|
|
{
|
|
id: "files",
|
|
name: "File Browser",
|
|
code: "FB",
|
|
description: ["Файлы и общие ресурсы", "Files and shared resources"],
|
|
},
|
|
];
|
|
export const systemRoles = ["Master Admin", "SSO Admin", "User"];
|
|
const fixture = () => ({
|
|
status: "authenticated",
|
|
subject: "demo-anna",
|
|
revision: 1,
|
|
accounts: {
|
|
"demo-anna": {
|
|
sub: "demo-anna",
|
|
username: "anna.volkova",
|
|
firstName: "Anna",
|
|
lastName: "Volkova",
|
|
email: "anna@anidev.ru",
|
|
emailVerified: true,
|
|
roles: ["Master Admin", "User"],
|
|
avatar: "",
|
|
totp: true,
|
|
passwordChanged: "2026-11-14",
|
|
passkeys: [{ id: "pk-1", name: "Windows Hello", created: "2026-11-12" }],
|
|
},
|
|
"demo-mikhail": {
|
|
sub: "demo-mikhail",
|
|
username: "mikhail.orlov",
|
|
firstName: "Mikhail",
|
|
lastName: "Orlov",
|
|
email: "",
|
|
emailVerified: false,
|
|
roles: ["User", "Developer"],
|
|
avatar: "",
|
|
totp: false,
|
|
passwordChanged: "2026-10-20",
|
|
passkeys: [],
|
|
},
|
|
},
|
|
sessions: {
|
|
"demo-mikhail": [
|
|
{
|
|
id: "current",
|
|
device: "Linux · Firefox",
|
|
place: "Frankfurt",
|
|
seen: "09:38 UTC",
|
|
current: true,
|
|
},
|
|
],
|
|
"demo-anna": [
|
|
{
|
|
id: "current",
|
|
device: "Windows · Edge",
|
|
place: "Moscow",
|
|
seen: "09:42 UTC",
|
|
current: true,
|
|
},
|
|
{
|
|
id: "s-2",
|
|
device: "macOS · Safari",
|
|
place: "Frankfurt",
|
|
seen: "08:56 UTC",
|
|
current: false,
|
|
},
|
|
{
|
|
id: "s-3",
|
|
device: "iOS · Safari",
|
|
place: "Moscow",
|
|
seen: "26.11 · 19:08 UTC",
|
|
current: false,
|
|
},
|
|
],
|
|
},
|
|
grants: {},
|
|
customRoles: ["Developer", "Auditor"],
|
|
lastApp: null,
|
|
});
|
|
export function createDemoIdentityAdapter(storage = globalThis.sessionStorage) {
|
|
const key = "anidev.identity.demo.v2";
|
|
let data = fixture();
|
|
const listeners = new Set();
|
|
try {
|
|
const saved = JSON.parse(storage?.getItem(key) || "null");
|
|
if (saved?.accounts && identityStates.includes(saved.status)) data = saved;
|
|
} catch {}
|
|
const emit = () => {
|
|
data.revision++;
|
|
try {
|
|
storage?.setItem(key, JSON.stringify(data));
|
|
} catch {}
|
|
listeners.forEach((fn) => fn(snapshot()));
|
|
};
|
|
const snapshot = () => ({
|
|
mode: "demo",
|
|
status: data.status,
|
|
revision: data.revision,
|
|
account:
|
|
data.status === "authenticated"
|
|
? structuredClone(data.accounts[data.subject])
|
|
: null,
|
|
sessions:
|
|
data.status === "authenticated"
|
|
? structuredClone(data.sessions[data.subject])
|
|
: [],
|
|
customRoles: [...data.customRoles],
|
|
});
|
|
const requireAccount = () => {
|
|
if (data.status !== "authenticated") throw Error("not_authenticated");
|
|
return data.accounts[data.subject];
|
|
};
|
|
const grantRows = (userId) => {
|
|
if (!data.grants[userId])
|
|
data.grants[userId] = applicationCatalog.slice(0, 3).map((app) => ({
|
|
application: app.id,
|
|
status: "active",
|
|
added: "2026-11-12",
|
|
by: "Anna Volkova",
|
|
reason: "",
|
|
showReason: false,
|
|
}));
|
|
return structuredClone(data.grants[userId]);
|
|
};
|
|
const adapter = {
|
|
snapshot,
|
|
subscribe(fn) {
|
|
listeners.add(fn);
|
|
return () => listeners.delete(fn);
|
|
},
|
|
setScenario(status) {
|
|
if (!identityStates.includes(status)) throw Error("invalid_state");
|
|
data.status = status;
|
|
emit();
|
|
},
|
|
updateProfile(values) {
|
|
const a = requireAccount();
|
|
for (const k of ["firstName", "lastName", "username"])
|
|
if (typeof values[k] === "string") a[k] = values[k].trim();
|
|
if (values.avatar !== undefined) a.avatar = values.avatar;
|
|
emit();
|
|
},
|
|
verifyEmail(email) {
|
|
const a = requireAccount();
|
|
a.email = email.trim();
|
|
a.emailVerified = true;
|
|
emit();
|
|
},
|
|
signOut() {
|
|
data.status = "signed_out";
|
|
data.lastApp = null;
|
|
emit();
|
|
},
|
|
signIn(email) {
|
|
data.subject = email.toLowerCase().startsWith("mikhail")
|
|
? "demo-mikhail"
|
|
: "demo-anna";
|
|
data.status = "authenticated";
|
|
data.lastApp = null;
|
|
emit();
|
|
},
|
|
revokeSession(id) {
|
|
requireAccount();
|
|
if (id === "current") return adapter.signOut();
|
|
data.sessions[data.subject] = data.sessions[data.subject].filter(
|
|
(s) => s.id !== id,
|
|
);
|
|
emit();
|
|
},
|
|
revokeOtherSessions() {
|
|
requireAccount();
|
|
data.sessions[data.subject] = data.sessions[data.subject].filter(
|
|
(s) => s.current,
|
|
);
|
|
emit();
|
|
},
|
|
addPasskey(name) {
|
|
const a = requireAccount();
|
|
a.passkeys.push({
|
|
id: crypto.randomUUID(),
|
|
name: name.trim(),
|
|
created: "2026-11-28",
|
|
});
|
|
emit();
|
|
},
|
|
removePasskey(id) {
|
|
const a = requireAccount();
|
|
a.passkeys = a.passkeys.filter((k) => k.id !== id);
|
|
emit();
|
|
},
|
|
setTotp(value) {
|
|
requireAccount().totp = value;
|
|
emit();
|
|
},
|
|
getGrants: grantRows,
|
|
addGrant(userId, application) {
|
|
requireAccount();
|
|
if (!applicationCatalog.some((a) => a.id === application))
|
|
throw Error("unknown_application");
|
|
const rows = grantRows(userId);
|
|
if (rows.some((g) => g.application === application))
|
|
throw Error("duplicate_grant");
|
|
rows.push({
|
|
application,
|
|
status: "active",
|
|
added: "2026-11-28",
|
|
by: "Anna Volkova",
|
|
reason: "",
|
|
showReason: false,
|
|
});
|
|
data.grants[userId] = rows;
|
|
emit();
|
|
},
|
|
updateGrants(userId, ids, change) {
|
|
requireAccount();
|
|
data.grants[userId] = grantRows(userId).map((g) =>
|
|
ids.includes(g.application) ? { ...g, ...change } : g,
|
|
);
|
|
emit();
|
|
},
|
|
setUserRoles(userId, roles) {
|
|
requireAccount();
|
|
data.grants["roles:" + userId] = [...new Set(roles)];
|
|
const subject =
|
|
Number(userId) === 1
|
|
? "demo-anna"
|
|
: Number(userId) === 2
|
|
? "demo-mikhail"
|
|
: null;
|
|
if (subject) data.accounts[subject].roles = [...new Set(roles)];
|
|
emit();
|
|
},
|
|
getUserRoles(userId) {
|
|
return (
|
|
data.grants["roles:" + userId] ||
|
|
(Number(userId) === 1
|
|
? [...data.accounts["demo-anna"].roles]
|
|
: Number(userId) === 2
|
|
? [...data.accounts["demo-mikhail"].roles]
|
|
: ["User"])
|
|
);
|
|
},
|
|
addRole(role) {
|
|
requireAccount();
|
|
if (
|
|
role.trim() &&
|
|
!systemRoles.includes(role) &&
|
|
!data.customRoles.includes(role)
|
|
)
|
|
data.customRoles.push(role.trim());
|
|
emit();
|
|
},
|
|
reset() {
|
|
data = fixture();
|
|
emit();
|
|
},
|
|
};
|
|
const onStorage = (e) => {
|
|
if (e.key === key && e.newValue) {
|
|
try {
|
|
data = JSON.parse(e.newValue);
|
|
listeners.forEach((fn) => fn(snapshot()));
|
|
} catch {}
|
|
}
|
|
};
|
|
globalThis.addEventListener?.("storage", onStorage);
|
|
adapter.dispose = () =>
|
|
globalThis.removeEventListener?.("storage", onStorage);
|
|
return adapter;
|
|
}
|