@spfn/auth 0.1.0-alpha.1 → 0.1.0-alpha.86

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.
Files changed (124) hide show
  1. package/README.md +250 -0
  2. package/dist/adapters/nextjs/api.d.ts +446 -0
  3. package/dist/adapters/nextjs/api.js +3279 -0
  4. package/dist/adapters/nextjs/api.js.map +1 -0
  5. package/dist/adapters/nextjs/server.d.ts +246 -0
  6. package/dist/adapters/nextjs/server.js +3645 -0
  7. package/dist/adapters/nextjs/server.js.map +1 -0
  8. package/dist/index.d.ts +3 -46
  9. package/dist/index.js +777 -645
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/api/auth-codes-verify.d.ts +37 -0
  12. package/dist/lib/api/auth-codes-verify.js +2949 -0
  13. package/dist/lib/api/auth-codes-verify.js.map +1 -0
  14. package/dist/lib/api/auth-codes.d.ts +37 -0
  15. package/dist/lib/api/auth-codes.js +2949 -0
  16. package/dist/lib/api/auth-codes.js.map +1 -0
  17. package/dist/lib/api/auth-exists.d.ts +38 -0
  18. package/dist/lib/api/auth-exists.js +2949 -0
  19. package/dist/lib/api/auth-exists.js.map +1 -0
  20. package/dist/lib/api/auth-invitations-accept.d.ts +38 -0
  21. package/dist/lib/api/auth-invitations-accept.js +2883 -0
  22. package/dist/lib/api/auth-invitations-accept.js.map +1 -0
  23. package/dist/lib/api/auth-invitations-cancel.d.ts +37 -0
  24. package/dist/lib/api/auth-invitations-cancel.js +2883 -0
  25. package/dist/lib/api/auth-invitations-cancel.js.map +1 -0
  26. package/dist/lib/api/auth-invitations-delete.d.ts +36 -0
  27. package/dist/lib/api/auth-invitations-delete.js +2883 -0
  28. package/dist/lib/api/auth-invitations-delete.js.map +1 -0
  29. package/dist/lib/api/auth-invitations-resend.d.ts +37 -0
  30. package/dist/lib/api/auth-invitations-resend.js +2883 -0
  31. package/dist/lib/api/auth-invitations-resend.js.map +1 -0
  32. package/dist/lib/api/auth-invitations.d.ts +109 -0
  33. package/dist/lib/api/auth-invitations.js +2887 -0
  34. package/dist/lib/api/auth-invitations.js.map +1 -0
  35. package/dist/lib/api/auth-keys-rotate.d.ts +37 -0
  36. package/dist/lib/api/auth-keys-rotate.js +2949 -0
  37. package/dist/lib/api/auth-keys-rotate.js.map +1 -0
  38. package/dist/lib/api/auth-login.d.ts +39 -0
  39. package/dist/lib/api/auth-login.js +2949 -0
  40. package/dist/lib/api/auth-login.js.map +1 -0
  41. package/dist/lib/api/auth-logout.d.ts +36 -0
  42. package/dist/lib/api/auth-logout.js +2949 -0
  43. package/dist/lib/api/auth-logout.js.map +1 -0
  44. package/dist/lib/api/auth-me.d.ts +50 -0
  45. package/dist/lib/api/auth-me.js +2949 -0
  46. package/dist/lib/api/auth-me.js.map +1 -0
  47. package/dist/lib/api/auth-password.d.ts +36 -0
  48. package/dist/lib/api/auth-password.js +2949 -0
  49. package/dist/lib/api/auth-password.js.map +1 -0
  50. package/dist/lib/api/auth-register.d.ts +38 -0
  51. package/dist/lib/api/auth-register.js +2949 -0
  52. package/dist/lib/api/auth-register.js.map +1 -0
  53. package/dist/lib/api/index.d.ts +356 -0
  54. package/dist/lib/api/index.js +3261 -0
  55. package/dist/lib/api/index.js.map +1 -0
  56. package/dist/lib/config.d.ts +70 -0
  57. package/dist/lib/config.js +64 -0
  58. package/dist/lib/config.js.map +1 -0
  59. package/dist/lib/contracts/auth.d.ts +41 -1
  60. package/dist/lib/contracts/auth.js +28 -0
  61. package/dist/lib/contracts/auth.js.map +1 -1
  62. package/dist/lib/contracts/index.d.ts +1 -1
  63. package/dist/lib/contracts/index.js +28 -0
  64. package/dist/lib/contracts/index.js.map +1 -1
  65. package/dist/lib/crypto.d.ts +76 -0
  66. package/dist/lib/crypto.js +127 -0
  67. package/dist/lib/crypto.js.map +1 -0
  68. package/dist/lib/index.d.ts +4 -0
  69. package/dist/lib/index.js +313 -0
  70. package/dist/lib/index.js.map +1 -0
  71. package/dist/lib/session.d.ts +68 -0
  72. package/dist/lib/session.js +126 -0
  73. package/dist/lib/session.js.map +1 -0
  74. package/dist/{api-BcQM4WKb.d.ts → lib/types/api.d.ts} +2 -2
  75. package/dist/lib/types/api.js +1 -0
  76. package/dist/lib/types/api.js.map +1 -0
  77. package/dist/lib/types/index.d.ts +3 -0
  78. package/dist/lib/types/index.js +2647 -0
  79. package/dist/lib/types/index.js.map +1 -0
  80. package/dist/lib/types/schemas.d.ts +45 -0
  81. package/dist/lib/types/schemas.js +2647 -0
  82. package/dist/lib/types/schemas.js.map +1 -0
  83. package/dist/lib.d.ts +2 -0
  84. package/dist/lib.js +1 -0
  85. package/dist/lib.js.map +1 -0
  86. package/dist/plugin.js +777 -645
  87. package/dist/plugin.js.map +1 -1
  88. package/dist/server/entities/index.d.ts +1 -0
  89. package/dist/server/entities/index.js +23 -27
  90. package/dist/server/entities/index.js.map +1 -1
  91. package/dist/server/entities/invitations.js +12 -9
  92. package/dist/server/entities/invitations.js.map +1 -1
  93. package/dist/server/entities/permissions.js +8 -3
  94. package/dist/server/entities/permissions.js.map +1 -1
  95. package/dist/server/entities/role-permissions.js +12 -9
  96. package/dist/server/entities/role-permissions.js.map +1 -1
  97. package/dist/server/entities/roles.js +8 -3
  98. package/dist/server/entities/roles.js.map +1 -1
  99. package/dist/server/entities/schema.d.ts +14 -0
  100. package/dist/server/entities/schema.js +7 -0
  101. package/dist/server/entities/schema.js.map +1 -0
  102. package/dist/server/entities/user-permissions.js +14 -12
  103. package/dist/server/entities/user-permissions.js.map +1 -1
  104. package/dist/server/entities/user-public-keys.js +12 -9
  105. package/dist/server/entities/user-public-keys.js.map +1 -1
  106. package/dist/server/entities/user-social-accounts.js +12 -9
  107. package/dist/server/entities/user-social-accounts.js.map +1 -1
  108. package/dist/server/entities/users.js +10 -6
  109. package/dist/server/entities/users.js.map +1 -1
  110. package/dist/server/entities/verification-codes.js +8 -3
  111. package/dist/server/entities/verification-codes.js.map +1 -1
  112. package/dist/server/routes/auth/index.js +495 -512
  113. package/dist/server/routes/auth/index.js.map +1 -1
  114. package/dist/server/routes/index.js +775 -545
  115. package/dist/server/routes/index.js.map +1 -1
  116. package/dist/server/routes/invitations/index.js +416 -230
  117. package/dist/server/routes/invitations/index.js.map +1 -1
  118. package/dist/server.d.ts +91 -62
  119. package/dist/server.js +320 -327
  120. package/dist/server.js.map +1 -1
  121. package/migrations/{0000_tired_gambit.sql → 0000_complex_swordsman.sql} +2 -0
  122. package/migrations/meta/0000_snapshot.json +4 -2
  123. package/migrations/meta/_journal.json +2 -2
  124. package/package.json +30 -3
package/dist/server.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { User } from './server/entities/users.js';
2
2
  import { Role } from './server/entities/roles.js';
3
3
  import { Invitation, InvitationWithDetails, InvitationStatus } from './server/entities/invitations.js';
4
- import { S as SessionPayload } from './api-BcQM4WKb.js';
4
+ import { SessionPayload } from './lib/types/api.js';
5
5
  import { Context, Next } from 'hono';
6
6
  import 'drizzle-orm/pg-core';
7
7
 
@@ -46,35 +46,39 @@ interface AuthInitOptions {
46
46
  * ```typescript
47
47
  * {
48
48
  * // Extend built-in admin role
49
- * admin: ['post:create', 'post:publish'],
49
+ * admin: ['project:create', 'project:delete'],
50
50
  *
51
51
  * // Define custom role permissions
52
- * 'content-creator': ['post:create', 'post:publish'],
52
+ * 'project-manager': ['project:create', 'task:assign'],
53
53
  * }
54
54
  * ```
55
55
  */
56
56
  rolePermissions?: Record<string, string[]>;
57
- /**
58
- * Use all preset roles and permissions
59
- * Includes: moderator, editor, viewer and related permissions
60
- * @default false
61
- */
62
- usePresets?: boolean;
63
- /**
64
- * Select specific preset roles to include
65
- * Available: MODERATOR, EDITOR, VIEWER
66
- */
67
- presetRoles?: Array<'MODERATOR' | 'EDITOR' | 'VIEWER'>;
68
- /**
69
- * Select specific preset permissions to include
70
- */
71
- presetPermissions?: Array<'CONTENT_READ' | 'CONTENT_WRITE' | 'CONTENT_DELETE' | 'CONTENT_PUBLISH' | 'COMMENT_MODERATE' | 'SYSTEM_CONFIG' | 'ANALYTICS_VIEW'>;
72
57
  /**
73
58
  * Default role name for new users
74
59
  * Must be a valid role name that exists after initialization
75
60
  * @default 'user'
76
61
  */
77
62
  defaultRole?: string;
63
+ /**
64
+ * Default session TTL (Time To Live)
65
+ *
66
+ * Supports:
67
+ * - Number: seconds (e.g., 2592000)
68
+ * - String: duration format ('30d', '12h', '45m', '3600s')
69
+ *
70
+ * Can be overridden at runtime with `remember` parameter.
71
+ *
72
+ * @default '7d' (7 days)
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * {
77
+ * sessionTtl: '30d', // 30 days
78
+ * }
79
+ * ```
80
+ */
81
+ sessionTtl?: string | number;
78
82
  }
79
83
 
80
84
  /**
@@ -102,29 +106,6 @@ declare const BUILTIN_ROLE_PERMISSIONS: Record<string, string[]>;
102
106
  type BuiltinRoleName = keyof typeof BUILTIN_ROLE_PERMISSIONS;
103
107
  type BuiltinPermissionName = typeof BUILTIN_PERMISSIONS[keyof typeof BUILTIN_PERMISSIONS]['name'];
104
108
 
105
- /**
106
- * @spfn/auth - Preset Roles and Permissions
107
- *
108
- * Optional preset roles and permissions for common use cases
109
- * Developers can choose to use these or define their own
110
- */
111
-
112
- /**
113
- * Preset roles (optional)
114
- * Common roles that developers can optionally include
115
- */
116
- declare const PRESET_ROLES: Record<string, RoleConfig>;
117
- /**
118
- * Preset permissions (optional)
119
- * Common permissions for typical application features
120
- */
121
- declare const PRESET_PERMISSIONS: Record<string, PermissionConfig>;
122
- /**
123
- * Preset role-permission mappings
124
- * Recommended permissions for each preset role
125
- */
126
- declare const PRESET_ROLE_PERMISSIONS: Record<string, string[]>;
127
-
128
109
  /**
129
110
  * @spfn/auth - Auth Service
130
111
  *
@@ -303,6 +284,43 @@ declare function updateLastLoginService(userId: number): Promise<void>;
303
284
  */
304
285
  declare function updateUserService(userId: number, updates: Partial<Omit<User, 'id' | 'createdAt'>>): Promise<void>;
305
286
 
287
+ /**
288
+ * @spfn/auth - Me Service
289
+ *
290
+ * Service for retrieving current user information
291
+ */
292
+ interface GetMeResult {
293
+ userId: string;
294
+ email?: string;
295
+ phone?: string;
296
+ role: {
297
+ id: number;
298
+ name: string;
299
+ displayName: string;
300
+ priority: number;
301
+ };
302
+ permissions: Array<{
303
+ id: number;
304
+ name: string;
305
+ displayName: string;
306
+ category?: string;
307
+ }>;
308
+ }
309
+ /**
310
+ * Get current user information including role and permissions
311
+ *
312
+ * @param userId - User ID (string, number, or bigint)
313
+ * @returns User info with role and permissions
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * const userInfo = await getMeService('123');
318
+ * console.log(userInfo.role.name); // 'admin'
319
+ * console.log(userInfo.permissions.length); // 15
320
+ * ```
321
+ */
322
+ declare function getMeService(userId: string | number | bigint): Promise<GetMeResult>;
323
+
306
324
  /**
307
325
  * @spfn/auth - RBAC Initialization Service
308
326
  *
@@ -312,23 +330,29 @@ declare function updateUserService(userId: number, updates: Partial<Omit<User, '
312
330
  /**
313
331
  * Initialize auth package with RBAC system
314
332
  *
315
- * Creates built-in roles, permissions, and optionally presets or custom configurations
333
+ * Creates built-in roles, permissions, and custom configurations
316
334
  *
317
335
  * @param options - Initialization options
318
336
  *
319
337
  * @example
320
338
  * ```typescript
321
- * // Minimal - only built-in roles
339
+ * // Minimal - only built-in roles (user, admin, superadmin)
322
340
  * await initializeAuth();
323
341
  *
324
- * // With presets
325
- * await initializeAuth({ usePresets: true });
326
- *
327
342
  * // Custom roles and permissions
328
343
  * await initializeAuth({
329
- * roles: [{ name: 'editor', displayName: 'Editor', priority: 30 }],
330
- * permissions: [{ name: 'post:create', displayName: 'Create Posts' }],
331
- * rolePermissions: { editor: ['post:create'] },
344
+ * roles: [
345
+ * { name: 'project-manager', displayName: 'Project Manager', priority: 50 },
346
+ * { name: 'developer', displayName: 'Developer', priority: 30 },
347
+ * ],
348
+ * permissions: [
349
+ * { name: 'project:create', displayName: 'Create Project', category: 'project' },
350
+ * { name: 'task:assign', displayName: 'Assign Task', category: 'task' },
351
+ * ],
352
+ * rolePermissions: {
353
+ * 'project-manager': ['project:create', 'task:assign'],
354
+ * 'developer': ['task:complete'],
355
+ * },
332
356
  * });
333
357
  * ```
334
358
  */
@@ -345,7 +369,7 @@ declare function initializeAuth(options?: AuthInitOptions): Promise<void>;
345
369
  * Combines role-based permissions with user-specific overrides
346
370
  * Handles expiration of temporary permissions
347
371
  *
348
- * @param userId - User ID (string or bigint)
372
+ * @param userId - User ID (string, number, or bigint)
349
373
  * @returns Array of permission names
350
374
  *
351
375
  * @example
@@ -354,7 +378,7 @@ declare function initializeAuth(options?: AuthInitOptions): Promise<void>;
354
378
  * // ['auth:self:manage', 'user:read', 'post:create']
355
379
  * ```
356
380
  */
357
- declare function getUserPermissions(userId: string | bigint): Promise<string[]>;
381
+ declare function getUserPermissions(userId: string | number | bigint): Promise<string[]>;
358
382
  /**
359
383
  * Check if user has a specific permission
360
384
  *
@@ -369,7 +393,7 @@ declare function getUserPermissions(userId: string | bigint): Promise<string[]>;
369
393
  * }
370
394
  * ```
371
395
  */
372
- declare function hasPermission(userId: string | bigint, permissionName: string): Promise<boolean>;
396
+ declare function hasPermission(userId: string | number | bigint, permissionName: string): Promise<boolean>;
373
397
  /**
374
398
  * Check if user has any of the specified permissions
375
399
  *
@@ -384,7 +408,7 @@ declare function hasPermission(userId: string | bigint, permissionName: string):
384
408
  * }
385
409
  * ```
386
410
  */
387
- declare function hasAnyPermission(userId: string | bigint, permissionNames: string[]): Promise<boolean>;
411
+ declare function hasAnyPermission(userId: string | number | bigint, permissionNames: string[]): Promise<boolean>;
388
412
  /**
389
413
  * Check if user has all of the specified permissions
390
414
  *
@@ -399,7 +423,7 @@ declare function hasAnyPermission(userId: string | bigint, permissionNames: stri
399
423
  * }
400
424
  * ```
401
425
  */
402
- declare function hasAllPermissions(userId: string | bigint, permissionNames: string[]): Promise<boolean>;
426
+ declare function hasAllPermissions(userId: string | number | bigint, permissionNames: string[]): Promise<boolean>;
403
427
  /**
404
428
  * Check if user has a specific role
405
429
  *
@@ -414,7 +438,7 @@ declare function hasAllPermissions(userId: string | bigint, permissionNames: str
414
438
  * }
415
439
  * ```
416
440
  */
417
- declare function hasRole(userId: string | bigint, roleName: string): Promise<boolean>;
441
+ declare function hasRole(userId: string | number | bigint, roleName: string): Promise<boolean>;
418
442
  /**
419
443
  * Check if user has any of the specified roles
420
444
  *
@@ -422,7 +446,7 @@ declare function hasRole(userId: string | bigint, roleName: string): Promise<boo
422
446
  * @param roleNames - Array of role names
423
447
  * @returns true if user has at least one role
424
448
  */
425
- declare function hasAnyRole(userId: string | bigint, roleNames: string[]): Promise<boolean>;
449
+ declare function hasAnyRole(userId: string | number | bigint, roleNames: string[]): Promise<boolean>;
426
450
 
427
451
  /**
428
452
  * @spfn/auth - Role Service
@@ -812,8 +836,10 @@ declare function validatePasswordStrength(password: string): {
812
836
  interface TokenPayload extends SessionPayload {
813
837
  exp?: number;
814
838
  iat?: number;
839
+ iss?: string;
815
840
  keyId?: string;
816
841
  timestamp?: number;
842
+ [key: string]: any;
817
843
  }
818
844
  /**
819
845
  * Generate a JWT token (legacy server-signed)
@@ -997,11 +1023,14 @@ declare function sendVerificationSMS(phone: string, code: string, purpose: strin
997
1023
  * Verify client-signed JWT token with public key
998
1024
  *
999
1025
  * Flow:
1000
- * 1. Extract Authorization header + X-Key-Id header
1001
- * 2. Fetch public key from database
1002
- * 3. Verify JWT signature with public key
1003
- * 4. Validate user status
1004
- * 5. Attach user to context
1026
+ * 1. Extract Authorization header
1027
+ * 2. Decode JWT to extract keyId
1028
+ * 3. Fetch public key from database
1029
+ * 4. Check key expiration
1030
+ * 5. Verify JWT signature with public key
1031
+ * 6. Validate user status
1032
+ * 7. Update last used timestamp
1033
+ * 8. Attach user to context
1005
1034
  *
1006
1035
  * Security Checks:
1007
1036
  * - Token signature verification
@@ -1240,4 +1269,4 @@ declare function requireRole(...roleNames: string[]): (c: Context, next: Next) =
1240
1269
  */
1241
1270
  declare function ensureAdminExists(): Promise<void>;
1242
1271
 
1243
- export { type AuthContext, type AuthInitOptions, BUILTIN_PERMISSIONS, BUILTIN_ROLES, BUILTIN_ROLE_PERMISSIONS, type BuiltinPermissionName, type BuiltinRoleName, type ChangePasswordParams, type CheckAccountExistsParams, type CheckAccountExistsResult, type LoginParams, type LoginResult, type LogoutParams, PRESET_PERMISSIONS, PRESET_ROLES, PRESET_ROLE_PERMISSIONS, type PermissionConfig, type RegisterParams, type RegisterPublicKeyParams, type RegisterResult, type RevokeKeyParams, type RoleConfig, type RotateKeyParams, type RotateKeyResult, type SendVerificationCodeParams, type SendVerificationCodeResult, type TokenPayload, type VerificationTokenPayload, type VerifyCodeParams, type VerifyCodeResult, acceptInvitation, addPermissionToRole, authenticate, cancelInvitation, changePasswordService, checkAccountExistsService, createInvitation, createRole, createVerificationToken, decodeToken, deleteInvitation, deleteRole, ensureAdminExists, expireOldInvitations, generateToken, generateVerificationCode, getAllRoles, getAuth, getInvitationByToken, getInvitationWithDetails, getKeyId, getRoleByName, getRolePermissions, getUser, getUserByEmailService, getUserByIdService, getUserByPhoneService, getUserId, getUserPermissions, hasAllPermissions, hasAnyPermission, hasAnyRole, hasPermission, hasRole, hashPassword, initializeAuth, listInvitations, loginService, logoutService, markCodeAsUsed, registerPublicKeyService, registerService, removePermissionFromRole, requireAnyPermission, requirePermissions, requireRole, resendInvitation, revokeKeyService, rotateKeyService, sendVerificationCodeService, sendVerificationEmail, sendVerificationSMS, setRolePermissions, storeVerificationCode, updateLastLoginService, updateRole, updateUserService, validateInvitation, validatePasswordStrength, validateVerificationCode, validateVerificationToken, verifyClientToken, verifyCodeService, verifyKeyFingerprint, verifyPassword, verifyToken };
1272
+ export { type AuthContext, type AuthInitOptions, BUILTIN_PERMISSIONS, BUILTIN_ROLES, BUILTIN_ROLE_PERMISSIONS, type BuiltinPermissionName, type BuiltinRoleName, type ChangePasswordParams, type CheckAccountExistsParams, type CheckAccountExistsResult, type GetMeResult, type LoginParams, type LoginResult, type LogoutParams, type PermissionConfig, type RegisterParams, type RegisterPublicKeyParams, type RegisterResult, type RevokeKeyParams, type RoleConfig, type RotateKeyParams, type RotateKeyResult, type SendVerificationCodeParams, type SendVerificationCodeResult, type TokenPayload, type VerificationTokenPayload, type VerifyCodeParams, type VerifyCodeResult, acceptInvitation, addPermissionToRole, authenticate, cancelInvitation, changePasswordService, checkAccountExistsService, createInvitation, createRole, createVerificationToken, decodeToken, deleteInvitation, deleteRole, ensureAdminExists, expireOldInvitations, generateToken, generateVerificationCode, getAllRoles, getAuth, getInvitationByToken, getInvitationWithDetails, getKeyId, getMeService, getRoleByName, getRolePermissions, getUser, getUserByEmailService, getUserByIdService, getUserByPhoneService, getUserId, getUserPermissions, hasAllPermissions, hasAnyPermission, hasAnyRole, hasPermission, hasRole, hashPassword, initializeAuth, listInvitations, loginService, logoutService, markCodeAsUsed, registerPublicKeyService, registerService, removePermissionFromRole, requireAnyPermission, requirePermissions, requireRole, resendInvitation, revokeKeyService, rotateKeyService, sendVerificationCodeService, sendVerificationEmail, sendVerificationSMS, setRolePermissions, storeVerificationCode, updateLastLoginService, updateRole, updateUserService, validateInvitation, validatePasswordStrength, validateVerificationCode, validateVerificationToken, verifyClientToken, verifyCodeService, verifyKeyFingerprint, verifyPassword, verifyToken };