@absolutejs/auth 0.76.3 → 0.77.1

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/index.js CHANGED
@@ -3372,8 +3372,8 @@ var failureResponse = (config, failure, requiredScopes) => new Response(JSON.str
3372
3372
  var agentAuthContextPlugin = (config) => new Elysia2({
3373
3373
  name: "@absolutejs/auth/agent-context",
3374
3374
  seed: pluginDependencySeed(config)
3375
- }).derive(({ request }) => ({
3376
- protectAgent: async (requiredScopes, handleAuth, handleAuthFail) => {
3375
+ }).derive(({ request }) => {
3376
+ const protectAgent = async (requiredScopes, handleAuth, handleAuthFail) => {
3377
3377
  if (config === undefined) {
3378
3378
  const failure = {
3379
3379
  code: "Unauthorized",
@@ -3399,8 +3399,9 @@ var agentAuthContextPlugin = (config) => new Elysia2({
3399
3399
  return await handleAuthFail?.(failure) ?? failureResponse(config, failure, requiredScopes);
3400
3400
  }
3401
3401
  return handleAuth(principal);
3402
- }
3403
- })).as("global");
3402
+ };
3403
+ return { protectAgent };
3404
+ }).as("global");
3404
3405
 
3405
3406
  // src/agents/oauthGuide.ts
3406
3407
  var DEFAULT_GUIDE_ROUTE = "/auth.md";
@@ -4611,17 +4612,17 @@ var protectPermissionPlugin = ({
4611
4612
  }).use(sessionStore()).guard({
4612
4613
  cookie: t3.Cookie({ user_session_id: userSessionIdTypebox }),
4613
4614
  schema: "merge"
4614
- }).derive(({ store: { session }, cookie: { user_session_id }, status }) => ({
4615
- protectPermission: (check, handleAuth, handleAuthFail) => getStatusFromSource({
4615
+ }).derive(({ store: { session }, cookie: { user_session_id }, status }) => {
4616
+ const protectPermission = (check, handleAuth, handleAuthFail) => getStatusFromSource({
4616
4617
  authSessionStore,
4617
4618
  session,
4618
4619
  user_session_id
4619
4620
  }).then(async ({ user, error }) => {
4620
4621
  if (error) {
4621
- return handleAuthFail?.(error) ?? status(error.code, error.message);
4622
+ return await handleAuthFail?.(error) ?? status(error.code, error.message);
4622
4623
  }
4623
4624
  if (!user) {
4624
- return handleAuthFail?.({
4625
+ return await handleAuthFail?.({
4625
4626
  code: "Unauthorized",
4626
4627
  message: "User is not authenticated"
4627
4628
  }) ?? status("Unauthorized", "User is not authenticated");
@@ -4638,14 +4639,15 @@ var protectPermissionPlugin = ({
4638
4639
  organizationId: check.organizationId,
4639
4640
  type: "authorization_denied"
4640
4641
  });
4641
- return handleAuthFail?.({
4642
+ return await handleAuthFail?.({
4642
4643
  code: "Forbidden",
4643
4644
  message: "Insufficient permissions"
4644
4645
  }) ?? status("Forbidden", "Insufficient permissions");
4645
4646
  }
4646
4647
  return handleAuth(user);
4647
- })
4648
- })).as("global");
4648
+ });
4649
+ return { protectPermission };
4650
+ }).as("global");
4649
4651
 
4650
4652
  // src/routes/protectRoute.ts
4651
4653
  import { Elysia as Elysia6, t as t4 } from "elysia";
@@ -4709,8 +4711,7 @@ var protectRoutePlugin = ({
4709
4711
  }).derive(async ({
4710
4712
  store: { session },
4711
4713
  cookie: { user_session_id },
4712
- headers,
4713
- status
4714
+ headers
4714
4715
  }) => {
4715
4716
  const sessionStatus = await getStatusFromSource({
4716
4717
  authSessionStore,
@@ -4729,25 +4730,30 @@ var protectRoutePlugin = ({
4729
4730
  authorization: headers.authorization,
4730
4731
  config: accessTokens
4731
4732
  });
4732
- return {
4733
- authPrincipal,
4734
- protectRoute: (handleAuth, handleAuthFail) => {
4735
- const user = authPrincipal?.user;
4736
- const { error } = sessionStatus;
4737
- if (error) {
4738
- if (user)
4739
- return handleAuth(user);
4740
- return handleAuthFail?.(error) ?? status(error.code, error.message);
4741
- }
4742
- if (!user) {
4743
- return handleAuthFail?.({
4744
- code: "Unauthorized",
4745
- message: "User is not authenticated"
4746
- }) ?? status("Unauthorized", "User is not authenticated");
4747
- }
4748
- return handleAuth(user);
4733
+ return { absoluteAuthStatus: sessionStatus, authPrincipal };
4734
+ }).derive(({ absoluteAuthStatus, authPrincipal, status }) => {
4735
+ const protectRoute = async (handleAuth, handleAuthFail) => {
4736
+ if (!absoluteAuthStatus)
4737
+ return await handleAuthFail?.({
4738
+ code: "Unauthorized",
4739
+ message: "User is not authenticated"
4740
+ }) ?? status("Unauthorized", "User is not authenticated");
4741
+ const user = authPrincipal?.user;
4742
+ const { error } = absoluteAuthStatus;
4743
+ if (error) {
4744
+ if (user)
4745
+ return handleAuth(user);
4746
+ return await handleAuthFail?.(error) ?? status(error.code, error.message);
4749
4747
  }
4748
+ if (!user) {
4749
+ return await handleAuthFail?.({
4750
+ code: "Unauthorized",
4751
+ message: "User is not authenticated"
4752
+ }) ?? status("Unauthorized", "User is not authenticated");
4753
+ }
4754
+ return handleAuth(user);
4750
4755
  };
4756
+ return { protectRoute };
4751
4757
  }).as("global");
4752
4758
 
4753
4759
  // src/routes/stepUp.ts
@@ -4760,23 +4766,24 @@ var stepUpPlugin = ({
4760
4766
  }).use(sessionStore()).guard({
4761
4767
  cookie: t5.Cookie({ user_session_id: userSessionIdTypebox }),
4762
4768
  schema: "merge"
4763
- }).derive(({ store: { session }, cookie: { user_session_id }, status }) => ({
4764
- requireRecentAuth: (maxAgeMs, handleAuth, handleAuthFail) => loadSessionFromSource({
4769
+ }).derive(({ store: { session }, cookie: { user_session_id }, status }) => {
4770
+ const requireRecentAuth = (maxAgeMs, handleAuth, handleAuthFail) => loadSessionFromSource({
4765
4771
  authSessionStore,
4766
4772
  session,
4767
4773
  userSessionId: user_session_id.value
4768
- }).then((userSession) => {
4774
+ }).then(async (userSession) => {
4769
4775
  const authenticatedAt = userSession?.authenticatedAt;
4770
4776
  const isRecent = authenticatedAt !== undefined && Date.now() - authenticatedAt <= maxAgeMs;
4771
4777
  if (!userSession || !isRecent) {
4772
- return handleAuthFail?.({
4778
+ return await handleAuthFail?.({
4773
4779
  code: "Unauthorized",
4774
4780
  message: "Recent authentication required"
4775
4781
  }) ?? status("Unauthorized", "Recent authentication required");
4776
4782
  }
4777
4783
  return handleAuth(userSession.user);
4778
- })
4779
- })).as("global");
4784
+ });
4785
+ return { requireRecentAuth };
4786
+ }).as("global");
4780
4787
 
4781
4788
  // src/oidc/socketTickets.ts
4782
4789
  init_crypto();
@@ -7214,7 +7221,10 @@ var mfaTotpRoutes = ({
7214
7221
  verified: false
7215
7222
  };
7216
7223
  const factors = getMfaFactors(base).filter((existingFactor) => existingFactor.type !== "totp" || existingFactor.verified);
7217
- await mfaStore.saveEnrollment(withMfaFactors({ ...base, updatedAt: now }, [...factors, factor]));
7224
+ await mfaStore.saveEnrollment(withMfaFactors({ ...base, updatedAt: now }, [
7225
+ ...factors,
7226
+ factor
7227
+ ]));
7218
7228
  return status("OK", {
7219
7229
  factorId: factor.id,
7220
7230
  secret,
@@ -34022,17 +34032,17 @@ var resolveBindingFailureStatus = (binding, report) => {
34022
34032
  }
34023
34033
  return binding.status;
34024
34034
  };
34025
- var buildNextGrant = (grant, report, currentTime) => ({
34035
+ var buildNextGrant = (grant, report, currentTime, failurePolicy) => ({
34026
34036
  ...grant,
34027
34037
  lastRefreshError: report.message ?? report.code,
34028
34038
  metadata: annotateFailureMetadata(grant.metadata, report, currentTime),
34029
- status: resolveGrantFailureStatus(grant, report),
34039
+ status: failurePolicy === "record" ? grant.status : resolveGrantFailureStatus(grant, report),
34030
34040
  updatedAt: currentTime
34031
34041
  });
34032
- var buildNextBinding = (binding, report, currentTime) => ({
34042
+ var buildNextBinding = (binding, report, currentTime, failurePolicy) => ({
34033
34043
  ...binding,
34034
34044
  metadata: annotateFailureMetadata(binding.metadata, report, currentTime),
34035
- status: resolveBindingFailureStatus(binding, report),
34045
+ status: failurePolicy === "record" ? binding.status : resolveBindingFailureStatus(binding, report),
34036
34046
  updatedAt: currentTime
34037
34047
  });
34038
34048
  var resolveBindingCredential = async (grantStore, binding, input) => {
@@ -34049,6 +34059,7 @@ var resolveBindingCredential = async (grantStore, binding, input) => {
34049
34059
  var createLinkedProviderCredentialResolver = ({
34050
34060
  grantStore,
34051
34061
  bindingStore,
34062
+ failurePolicy = "latch",
34052
34063
  loadAccessTokenLease,
34053
34064
  refreshAccessTokenLease,
34054
34065
  now = () => Date.now(),
@@ -34094,10 +34105,10 @@ var createLinkedProviderCredentialResolver = ({
34094
34105
  const grant = await grantStore.getGrant(credential.grantId);
34095
34106
  const binding = await bindingStore.getBinding(credential.bindingId);
34096
34107
  if (grant) {
34097
- await grantStore.saveGrant(buildNextGrant(grant, report, currentTime));
34108
+ await grantStore.saveGrant(buildNextGrant(grant, report, currentTime, failurePolicy));
34098
34109
  }
34099
34110
  if (binding) {
34100
- await bindingStore.saveBinding(buildNextBinding(binding, report, currentTime));
34111
+ await bindingStore.saveBinding(buildNextBinding(binding, report, currentTime, failurePolicy));
34101
34112
  }
34102
34113
  await onReportFailure?.({
34103
34114
  binding: binding ?? undefined,
@@ -34396,7 +34407,7 @@ var toBinding2 = (row) => ({
34396
34407
  updatedAt: row.updated_at.getTime(),
34397
34408
  username: row.username ?? undefined
34398
34409
  });
34399
- var createNeonLinkedProviderBindingStore = (db) => ({
34410
+ var createLinkedProviderBindingStore = (db) => ({
34400
34411
  getBinding: async (id2) => {
34401
34412
  const [row] = await db.select().from(linkedProviderBindingsTable).where(eq(linkedProviderBindingsTable.id, id2)).limit(1);
34402
34413
  return row ? toBinding2(row) : undefined;
@@ -34447,7 +34458,7 @@ var createNeonLinkedProviderBindingStore = (db) => ({
34447
34458
  });
34448
34459
  }
34449
34460
  });
34450
- var createNeonLinkedProviderGrantStore = (db) => ({
34461
+ var createLinkedProviderGrantStore = (db) => ({
34451
34462
  getGrant: async (id2) => {
34452
34463
  const [row] = await db.select().from(linkedProviderGrantsTable).where(eq(linkedProviderGrantsTable.id, id2)).limit(1);
34453
34464
  return row ? toGrant2(row) : undefined;
@@ -34503,9 +34514,9 @@ var createNeonLinkedProviderStores = (databaseUrl) => {
34503
34514
  const sql2 = as(databaseUrl);
34504
34515
  const db = drizzle({ client: sql2 });
34505
34516
  return {
34506
- bindingStore: createNeonLinkedProviderBindingStore(db),
34517
+ bindingStore: createLinkedProviderBindingStore(db),
34507
34518
  db,
34508
- grantStore: createNeonLinkedProviderGrantStore(db)
34519
+ grantStore: createLinkedProviderGrantStore(db)
34509
34520
  };
34510
34521
  };
34511
34522
  var createNeonOAuthLinkedProviderCredentialResolver = async ({
@@ -39793,7 +39804,12 @@ var renderChunk = (chunk) => {
39793
39804
  return "";
39794
39805
  };
39795
39806
  var formatSqlTemplate = (value) => value.queryChunks.map(renderChunk).join("");
39796
- var formatDefault = (value) => {
39807
+ var columnSqlType = (column) => {
39808
+ const dimensions = Reflect.get(column, "dimensions");
39809
+ const elementType = column.getSQLType();
39810
+ return typeof dimensions === "number" && dimensions > 0 ? `${elementType}${"[]".repeat(dimensions)}` : elementType;
39811
+ };
39812
+ var formatScalarDefault = (value) => {
39797
39813
  if (value === null || value === undefined)
39798
39814
  return "NULL";
39799
39815
  if (is(value, SQL))
@@ -39804,15 +39820,17 @@ var formatDefault = (value) => {
39804
39820
  return value ? "true" : "false";
39805
39821
  if (typeof value === "number")
39806
39822
  return String(value);
39807
- if (Array.isArray(value) || typeof value === "object") {
39823
+ if (typeof value === "object") {
39808
39824
  return `'${JSON.stringify(value)}'::jsonb`;
39809
39825
  }
39810
39826
  return String(value);
39811
39827
  };
39812
- var columnSqlType = (column) => {
39828
+ var formatArrayDefault = (values) => `ARRAY[${values.map((value) => Array.isArray(value) ? formatArrayDefault(value) : formatScalarDefault(value)).join(", ")}]`;
39829
+ var formatDefault = (column, value) => {
39813
39830
  const dimensions = Reflect.get(column, "dimensions");
39814
- const elementType = column.getSQLType();
39815
- return typeof dimensions === "number" && dimensions > 0 ? `${elementType}${"[]".repeat(dimensions)}` : elementType;
39831
+ if (Array.isArray(value) && typeof dimensions === "number" && dimensions > 0)
39832
+ return `${formatArrayDefault(value)}::${columnSqlType(column)}`;
39833
+ return formatScalarDefault(value);
39816
39834
  };
39817
39835
  var columnSql = (column) => {
39818
39836
  const parts = [`"${column.name}"`, columnSqlType(column)];
@@ -39821,7 +39839,7 @@ var columnSql = (column) => {
39821
39839
  if (column.notNull && !column.primary)
39822
39840
  parts.push("NOT NULL");
39823
39841
  if (column.hasDefault && column.default !== undefined) {
39824
- parts.push(`DEFAULT ${formatDefault(column.default)}`);
39842
+ parts.push(`DEFAULT ${formatDefault(column, column.default)}`);
39825
39843
  }
39826
39844
  if (column.isUnique)
39827
39845
  parts.push("UNIQUE");
@@ -41057,7 +41075,9 @@ export {
41057
41075
  createInMemoryWarrantStore,
41058
41076
  createInMemoryWebAuthnCredentialStore,
41059
41077
  createInMemoryWebhookDeliveryStore,
41078
+ createLinkedProviderBindingStore,
41060
41079
  createLinkedProviderCredentialResolver,
41080
+ createLinkedProviderGrantStore,
41061
41081
  createLockoutGuard,
41062
41082
  createMembershipPermissionResolver,
41063
41083
  createMfaGate,
@@ -41257,6 +41277,8 @@ export {
41257
41277
  issueTokenSet,
41258
41278
  jwkThumbprint,
41259
41279
  knownDevicesTable,
41280
+ linkedProviderBindingsTable,
41281
+ linkedProviderGrantsTable,
41260
41282
  listObjects,
41261
41283
  listRingSessions,
41262
41284
  listSubjects,
@@ -41423,5 +41445,5 @@ export {
41423
41445
  writeWarrant
41424
41446
  };
41425
41447
 
41426
- //# debugId=B86263B79140DD5464756E2164756E21
41448
+ //# debugId=DEC787FA4F1E8D6464756E2164756E21
41427
41449
  //# sourceMappingURL=index.js.map