@spfn/auth 0.2.0-beta.46 → 0.2.0-beta.48

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/server.js CHANGED
@@ -7884,6 +7884,38 @@ async function getAuthSessionService(userId) {
7884
7884
  };
7885
7885
  }
7886
7886
 
7887
+ // src/server/lib/one-time-token.ts
7888
+ import { SSETokenManager } from "@spfn/core/event/sse";
7889
+ var manager = null;
7890
+ function initOneTimeTokenManager(config) {
7891
+ if (manager) {
7892
+ manager.destroy();
7893
+ }
7894
+ manager = new SSETokenManager({
7895
+ ttl: config?.ttl
7896
+ });
7897
+ }
7898
+ function getOneTimeTokenManager() {
7899
+ if (!manager) {
7900
+ throw new Error(
7901
+ "OneTimeTokenManager not initialized. Ensure createAuthLifecycle() is configured in your server config."
7902
+ );
7903
+ }
7904
+ return manager;
7905
+ }
7906
+
7907
+ // src/server/services/one-time-token.service.ts
7908
+ async function issueOneTimeTokenService(userId) {
7909
+ const manager2 = getOneTimeTokenManager();
7910
+ const token = await manager2.issue(userId);
7911
+ const expiresAt = new Date(Date.now() + 3e4).toISOString();
7912
+ return { token, expiresAt };
7913
+ }
7914
+ async function verifyOneTimeTokenService(token) {
7915
+ const manager2 = getOneTimeTokenManager();
7916
+ return await manager2.verify(token);
7917
+ }
7918
+
7887
7919
  // src/server/services/user-profile.service.ts
7888
7920
  init_repositories();
7889
7921
  async function getUserProfileService(userId) {
@@ -8457,6 +8489,10 @@ var getAuthSession = route.get("/_auth/session").handler(async (c) => {
8457
8489
  const { userId } = getAuth(c);
8458
8490
  return await getAuthSessionService(userId);
8459
8491
  });
8492
+ var issueOneTimeToken = route.post("/_auth/tokens").handler(async (c) => {
8493
+ const { userId } = getAuth(c);
8494
+ return await issueOneTimeTokenService(userId);
8495
+ });
8460
8496
  var authRouter = defineRouter({
8461
8497
  checkAccountExists,
8462
8498
  sendVerificationCode,
@@ -8466,7 +8502,8 @@ var authRouter = defineRouter({
8466
8502
  logout,
8467
8503
  rotateKey,
8468
8504
  changePassword,
8469
- getAuthSession
8505
+ getAuthSession,
8506
+ issueOneTimeToken
8470
8507
  });
8471
8508
 
8472
8509
  // src/server/routes/invitations/index.ts
@@ -8753,6 +8790,47 @@ var roleGuard = defineMiddleware4(
8753
8790
  }
8754
8791
  );
8755
8792
 
8793
+ // src/server/middleware/one-time-token-auth.ts
8794
+ import { defineMiddleware as defineMiddleware5 } from "@spfn/core/route";
8795
+ import { UnauthorizedError as UnauthorizedError2 } from "@spfn/core/errors";
8796
+ import { usersRepository as usersRepository3, userProfilesRepository as userProfilesRepository3 } from "@spfn/auth/server";
8797
+ var oneTimeTokenAuth = defineMiddleware5("oneTimeTokenAuth", async (c, next) => {
8798
+ const token = c.req.query("token") ?? extractOTTHeader(c.req.header("Authorization"));
8799
+ if (!token) {
8800
+ throw new UnauthorizedError2({ message: "One-time token required: ?token=xxx or Authorization: OTT xxx" });
8801
+ }
8802
+ const userId = await verifyOneTimeTokenService(token);
8803
+ if (!userId) {
8804
+ throw new UnauthorizedError2({ message: "Invalid or expired one-time token" });
8805
+ }
8806
+ const [result, locale] = await Promise.all([
8807
+ usersRepository3.findByIdWithRole(Number(userId)),
8808
+ userProfilesRepository3.findLocaleByUserId(Number(userId))
8809
+ ]);
8810
+ if (!result) {
8811
+ throw new UnauthorizedError2({ message: "User not found" });
8812
+ }
8813
+ const { user, role } = result;
8814
+ if (user.status !== "active") {
8815
+ throw new UnauthorizedError2({ message: "Account is not active" });
8816
+ }
8817
+ c.set("auth", {
8818
+ user,
8819
+ userId: String(user.id),
8820
+ keyId: "",
8821
+ // No key involved in OTT auth
8822
+ role: role?.name ?? null,
8823
+ locale
8824
+ });
8825
+ await next();
8826
+ }, { skips: ["auth"] });
8827
+ function extractOTTHeader(header) {
8828
+ if (!header || !header.startsWith("OTT ")) {
8829
+ return null;
8830
+ }
8831
+ return header.substring(4);
8832
+ }
8833
+
8756
8834
  // src/server/routes/invitations/index.ts
8757
8835
  init_types();
8758
8836
  init_esm();
@@ -9242,6 +9320,8 @@ var mainAuthRouter = defineRouter5({
9242
9320
  rotateKey,
9243
9321
  changePassword,
9244
9322
  getAuthSession,
9323
+ // One-Time Token routes
9324
+ issueOneTimeToken,
9245
9325
  // OAuth routes
9246
9326
  oauthGoogleStart,
9247
9327
  oauthGoogleCallback,
@@ -9571,10 +9651,12 @@ function createAuthLifecycle(options = {}) {
9571
9651
  * Performs:
9572
9652
  * 1. Ensures admin account exists (creates if missing)
9573
9653
  * 2. Initializes RBAC system with built-in + custom roles/permissions
9654
+ * 3. Initializes one-time token manager
9574
9655
  */
9575
9656
  afterInfrastructure: async () => {
9576
9657
  await initializeAuth(options);
9577
9658
  await ensureAdminExists();
9659
+ initOneTimeTokenManager(options.oneTimeToken);
9578
9660
  }
9579
9661
  };
9580
9662
  }
@@ -9647,6 +9729,7 @@ export {
9647
9729
  getKeyId,
9648
9730
  getKeySize,
9649
9731
  getLocale,
9732
+ getOneTimeTokenManager,
9650
9733
  getOptionalAuth,
9651
9734
  getRole,
9652
9735
  getRoleByName,
@@ -9667,18 +9750,21 @@ export {
9667
9750
  hasPermission,
9668
9751
  hasRole,
9669
9752
  hashPassword,
9753
+ initOneTimeTokenManager,
9670
9754
  initializeAuth,
9671
9755
  invitationAcceptedEvent,
9672
9756
  invitationCreatedEvent,
9673
9757
  invitationsRepository,
9674
9758
  isGoogleOAuthEnabled,
9675
9759
  isOAuthProviderEnabled,
9760
+ issueOneTimeTokenService,
9676
9761
  keysRepository,
9677
9762
  listInvitations,
9678
9763
  loginService,
9679
9764
  logoutService,
9680
9765
  oauthCallbackService,
9681
9766
  oauthStartService,
9767
+ oneTimeTokenAuth,
9682
9768
  optionalAuth,
9683
9769
  parseDuration,
9684
9770
  permissions,
@@ -9728,6 +9814,7 @@ export {
9728
9814
  verifyCodeService,
9729
9815
  verifyKeyFingerprint,
9730
9816
  verifyOAuthState,
9817
+ verifyOneTimeTokenService,
9731
9818
  verifyPassword,
9732
9819
  verifyToken
9733
9820
  };