@jskit-ai/auth-provider-local-core 0.2.46 → 0.2.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-core",
3
- "version": "0.2.46",
3
+ "version": "0.2.47",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -684,7 +684,9 @@ function createLocalFileBackend({ storeDir }) {
684
684
 
685
685
  return Object.freeze({
686
686
  storeDir: resolvedStoreDir,
687
- withTransaction
687
+ withTransaction,
688
+ // The files are read and written under the store's lock either way.
689
+ withRecords: withTransaction
688
690
  });
689
691
  }
690
692
 
@@ -209,9 +209,11 @@ function createLocalSessions({ appProfileProjection, backend, config, deliveries
209
209
  await tx.sessions.touch(session.id, { lastSeenAt: new Date(now).toISOString(), ...requestClient(request) });
210
210
  }
211
211
 
212
+ // A request's session and user are read with no transaction around them: each read sees the latest rows, so a
213
+ // session revoked or an account disabled is refused from the next request on.
212
214
  async function resolveValidSessionByRefreshToken(refreshToken, request) {
213
215
  const tokenHash = sha256Base64url(refreshToken);
214
- return backend.withTransaction(async (tx) => {
216
+ return backend.withRecords(async (tx) => {
215
217
  const session = await tx.sessions.findByTokenHash(tokenHash);
216
218
  if (!session || session.revokedAt || isExpiredIso(session.expiresAt)) {
217
219
  return null;
@@ -318,7 +320,7 @@ function createLocalSessions({ appProfileProjection, backend, config, deliveries
318
320
  if (accessToken) {
319
321
  const payload = verifySignedToken(accessToken, config.sessionSecret);
320
322
  if (payload?.sid && payload?.sub) {
321
- const resolved = await backend.withTransaction(async (tx) => {
323
+ const resolved = await backend.withRecords(async (tx) => {
322
324
  const session = await tx.sessions.findById(String(payload.sid));
323
325
  if (!session || session.revokedAt || isExpiredIso(session.expiresAt)) {
324
326
  return null;
@@ -216,6 +216,9 @@
216
216
  * @typedef {object} AuthLocalBackend
217
217
  * @property {<T>(callback: (tx: AuthLocalBackendTransaction) => Promise<T>) => Promise<T>} withTransaction
218
218
  * Runs the callback in one transaction and answers what it answered.
219
+ * @property {<T>(callback: (records: AuthLocalBackendTransaction) => Promise<T>) => Promise<T>} withRecords
220
+ * Runs the callback on the same records outside a transaction, each call on its own, and answers what it answered:
221
+ * a request's authentication reads its session and its user this way, without a transaction's BEGIN and COMMIT.
219
222
  */
220
223
 
221
224
  /**
@@ -21,15 +21,17 @@ async function startService(t) {
21
21
  }
22
22
  return tx.sessions[method](...args);
23
23
  }
24
+ const counted = (tx) => ({
25
+ ...tx,
26
+ sessions: Object.create(tx.sessions, {
27
+ findById: { value: (...args) => readSession(tx, "findById", args) },
28
+ findByTokenHash: { value: (...args) => readSession(tx, "findByTokenHash", args) }
29
+ })
30
+ });
24
31
  const backend = {
25
32
  ...files,
26
- withTransaction: (work) => files.withTransaction((tx) => work({
27
- ...tx,
28
- sessions: Object.create(tx.sessions, {
29
- findById: { value: (...args) => readSession(tx, "findById", args) },
30
- findByTokenHash: { value: (...args) => readSession(tx, "findByTokenHash", args) }
31
- })
32
- }))
33
+ withTransaction: (work) => files.withTransaction((tx) => work(counted(tx))),
34
+ withRecords: (work) => files.withRecords((tx) => work(counted(tx)))
33
35
  };
34
36
  const service = createLocalAuthService({ ...localServiceOptions(), backend });
35
37
  await service.register({ email: "ada@example.com", password: "correct horse" });