@x12i/keyrox-store 0.2.1 → 0.2.3

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/dist/memory.js CHANGED
@@ -184,7 +184,7 @@ export function createMemoryStore() {
184
184
  };
185
185
  const user = (id) => tables.users.get(id) ?? fail("conflict", "User is missing.");
186
186
  const writeEmailChallenge = (row) => {
187
- if (row.purpose !== "verify" && row.purpose !== "reset")
187
+ if (row.purpose !== "verify" && row.purpose !== "reset" && row.purpose !== "password-change")
188
188
  fail("conflict", "Invalid challenge.");
189
189
  if ([...tables.challenges.values()].some((item) => item.tokenHash === row.tokenHash))
190
190
  fail("duplicate", "Duplicate key.");
@@ -408,6 +408,14 @@ export function createMemoryStore() {
408
408
  return true;
409
409
  },
410
410
  async hostedHumanSession(id) { return one(tables.hostedHumanSessions, id); },
411
+ async hostedHumanSessionByAccessJti(jti) {
412
+ return rows(tables.hostedHumanSessions, (row) => row.accessJti === jti)[0] ?? null;
413
+ },
414
+ async setHostedHumanAccessJti(id, jti) {
415
+ const session = tables.hostedHumanSessions.get(id);
416
+ if (session)
417
+ session.accessJti = jti;
418
+ },
411
419
  async listHostedHumanSessions(environmentId, subjectId) {
412
420
  return rows(tables.hostedHumanSessions, (row) => row.environmentId === environmentId && row.subjectId === subjectId)
413
421
  .sort((left, right) => right.createdAt.localeCompare(left.createdAt) || right.id.localeCompare(left.id));
@@ -606,6 +614,7 @@ export function createMemoryStore() {
606
614
  if (!account || account.disabledAt)
607
615
  fail("challenge_invalid", "Reset link is no longer valid.");
608
616
  account.passwordHash = input.passwordHash;
617
+ account.passwordChangeRequired = false;
609
618
  if (!account.emailVerifiedAt)
610
619
  account.emailVerifiedAt = stamp;
611
620
  for (const pending of tables.challenges.values()) {
@@ -619,6 +628,31 @@ export function createMemoryStore() {
619
628
  return clone(account);
620
629
  });
621
630
  },
631
+ async prepareAccountPasswordChange(tokenHash, at) {
632
+ const stamp = asTime(at);
633
+ const challenge = [...tables.challenges.values()].find((row) => row.tokenHash === tokenHash && row.purpose === "password-change");
634
+ if (!challenge || challenge.usedAt || isExpired(challenge.expiresAt, stamp))
635
+ return null;
636
+ const account = tables.users.get(challenge.userId);
637
+ if (!account || account.disabledAt || !account.passwordHash)
638
+ return null;
639
+ return clone(account);
640
+ },
641
+ async commitAccountPasswordChange(input) {
642
+ return tx(() => {
643
+ const stamp = asTime(input.at);
644
+ const challenge = [...tables.challenges.values()].find((row) => row.tokenHash === input.tokenHash && row.purpose === "password-change");
645
+ if (!challenge || challenge.usedAt || isExpired(challenge.expiresAt, stamp))
646
+ return null;
647
+ const account = tables.users.get(challenge.userId);
648
+ if (!account || account.disabledAt || !account.passwordHash)
649
+ return null;
650
+ account.passwordHash = input.passwordHash;
651
+ account.passwordChangeRequired = false;
652
+ challenge.usedAt = stamp;
653
+ return clone(account);
654
+ });
655
+ },
622
656
  async insertInvite(row, delivery) {
623
657
  tx(() => {
624
658
  org(row.orgId);
@@ -1225,6 +1259,18 @@ export function createMemoryStore() {
1225
1259
  return rows(tables.environments, (row) => row.appId === appId)
1226
1260
  .sort((left, right) => left.createdAt.localeCompare(right.createdAt) || left.name.localeCompare(right.name));
1227
1261
  },
1262
+ async saveEnvironmentJwtProfile(environmentId, appId, orgId, profile, updatedAt) {
1263
+ tx(() => {
1264
+ const current = tables.environments.get(environmentId);
1265
+ if (!current || !activeApplication(current.appId) || current.appId !== appId || current.orgId !== orgId)
1266
+ fail("not_found", "Environment not found.");
1267
+ if (profile)
1268
+ current.jwtProfile = clone(profile);
1269
+ else
1270
+ delete current.jwtProfile;
1271
+ current.updatedAt = asTime(updatedAt);
1272
+ });
1273
+ },
1228
1274
  async updateEnvironment(row) {
1229
1275
  tx(() => {
1230
1276
  const current = tables.environments.get(row.id);
@@ -1238,7 +1284,10 @@ export function createMemoryStore() {
1238
1284
  fail("conflict", "Environment key changed.");
1239
1285
  if (row.status !== "active" && row.status !== "disabled")
1240
1286
  fail("conflict", "Invalid environment status.");
1241
- replace(tables.environments, stampEnvironment(row));
1287
+ const next = stampEnvironment(row);
1288
+ if (row.jwtProfile === undefined)
1289
+ next.jwtProfile = current.jwtProfile;
1290
+ replace(tables.environments, next);
1242
1291
  });
1243
1292
  },
1244
1293
  async getNotificationSettings(environmentId) {
@@ -1792,9 +1841,14 @@ export function createMemoryStore() {
1792
1841
  org(row.orgId);
1793
1842
  if (row.appId && activeApplication(row.appId)?.orgId !== row.orgId)
1794
1843
  fail("conflict", "Choose an application from this tenant.");
1844
+ if (row.environmentId) {
1845
+ const environment = tables.environments.get(row.environmentId);
1846
+ if (!environment || environment.orgId !== row.orgId || (row.appId && environment.appId !== row.appId))
1847
+ fail("conflict", "Choose an environment from this tenant.");
1848
+ }
1795
1849
  if ([...tables.audiences.values()].some((item) => item.orgId === row.orgId && item.identifier === row.identifier))
1796
1850
  fail("duplicate", "Duplicate key.");
1797
- put(tables.audiences, { ...row, createdAt: asTime(row.createdAt) });
1851
+ put(tables.audiences, { ...row, environmentId: row.environmentId ?? null, createdAt: asTime(row.createdAt) });
1798
1852
  });
1799
1853
  },
1800
1854
  async listAudiences(orgId) {
@@ -1804,6 +1858,31 @@ export function createMemoryStore() {
1804
1858
  return rows(tables.audiences, (row) => row.orgId === orgId && row.identifier === identifier
1805
1859
  && (!row.appId || activeApplication(row.appId)?.orgId === orgId))[0] ?? null;
1806
1860
  },
1861
+ async findAudiencesForJwt(audience) {
1862
+ return rows(tables.audiences, (row) => {
1863
+ if (row.appId && activeApplication(row.appId)?.orgId !== row.orgId)
1864
+ return false;
1865
+ if (row.identifier === audience)
1866
+ return true;
1867
+ const environment = row.environmentId ? tables.environments.get(row.environmentId) : undefined;
1868
+ return environment?.jwtProfile?.audience === audience;
1869
+ });
1870
+ },
1871
+ async updateAudienceEnvironment(id, orgId, environmentId) {
1872
+ return tx(() => {
1873
+ const current = tables.audiences.get(id);
1874
+ if (!current || current.orgId !== orgId || (current.appId && activeApplication(current.appId)?.orgId !== orgId))
1875
+ return null;
1876
+ if (environmentId) {
1877
+ const environment = tables.environments.get(environmentId);
1878
+ if (!environment || environment.orgId !== orgId || (current.appId && environment.appId !== current.appId))
1879
+ fail("conflict", "Choose an environment from this tenant.");
1880
+ }
1881
+ const next = { ...current, environmentId };
1882
+ replace(tables.audiences, next);
1883
+ return clone(next);
1884
+ });
1885
+ },
1807
1886
  async insertRole(row) {
1808
1887
  tx(() => { org(row.orgId); put(tables.roles, { ...row, actions: [...row.actions], createdAt: asTime(row.createdAt) }); });
1809
1888
  },