@parsrun/auth 0.1.0

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 (54) hide show
  1. package/README.md +133 -0
  2. package/dist/adapters/hono.d.ts +9 -0
  3. package/dist/adapters/hono.js +6 -0
  4. package/dist/adapters/hono.js.map +1 -0
  5. package/dist/adapters/index.d.ts +9 -0
  6. package/dist/adapters/index.js +7 -0
  7. package/dist/adapters/index.js.map +1 -0
  8. package/dist/authorization-By1Xp8Za.d.ts +213 -0
  9. package/dist/base-BKyR8rcE.d.ts +646 -0
  10. package/dist/chunk-42MGHABB.js +263 -0
  11. package/dist/chunk-42MGHABB.js.map +1 -0
  12. package/dist/chunk-7GOBAL4G.js +3 -0
  13. package/dist/chunk-7GOBAL4G.js.map +1 -0
  14. package/dist/chunk-G5I3T73A.js +152 -0
  15. package/dist/chunk-G5I3T73A.js.map +1 -0
  16. package/dist/chunk-IB4WUQDZ.js +410 -0
  17. package/dist/chunk-IB4WUQDZ.js.map +1 -0
  18. package/dist/chunk-MOG4Y6I7.js +415 -0
  19. package/dist/chunk-MOG4Y6I7.js.map +1 -0
  20. package/dist/chunk-NK4TJV2W.js +295 -0
  21. package/dist/chunk-NK4TJV2W.js.map +1 -0
  22. package/dist/chunk-RHNVRCF3.js +838 -0
  23. package/dist/chunk-RHNVRCF3.js.map +1 -0
  24. package/dist/chunk-YTCPXJR5.js +570 -0
  25. package/dist/chunk-YTCPXJR5.js.map +1 -0
  26. package/dist/cloudflare-kv-L64CZKDK.js +105 -0
  27. package/dist/cloudflare-kv-L64CZKDK.js.map +1 -0
  28. package/dist/deno-kv-F55HKKP6.js +111 -0
  29. package/dist/deno-kv-F55HKKP6.js.map +1 -0
  30. package/dist/index-C3kz9XqE.d.ts +226 -0
  31. package/dist/index-DOGcetyD.d.ts +1041 -0
  32. package/dist/index.d.ts +1579 -0
  33. package/dist/index.js +4294 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/jwt-manager-CH8H0kmm.d.ts +182 -0
  36. package/dist/providers/index.d.ts +90 -0
  37. package/dist/providers/index.js +3 -0
  38. package/dist/providers/index.js.map +1 -0
  39. package/dist/providers/otp/index.d.ts +3 -0
  40. package/dist/providers/otp/index.js +4 -0
  41. package/dist/providers/otp/index.js.map +1 -0
  42. package/dist/redis-5TIS6XCA.js +121 -0
  43. package/dist/redis-5TIS6XCA.js.map +1 -0
  44. package/dist/security/index.d.ts +301 -0
  45. package/dist/security/index.js +5 -0
  46. package/dist/security/index.js.map +1 -0
  47. package/dist/session/index.d.ts +117 -0
  48. package/dist/session/index.js +4 -0
  49. package/dist/session/index.js.map +1 -0
  50. package/dist/storage/index.d.ts +97 -0
  51. package/dist/storage/index.js +3 -0
  52. package/dist/storage/index.js.map +1 -0
  53. package/dist/types-DSjafxJ4.d.ts +193 -0
  54. package/package.json +102 -0
package/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # @parsrun/auth
2
+
3
+ Passwordless-first, multi-runtime authentication for Pars framework.
4
+
5
+ ## Features
6
+
7
+ - **Passwordless-First**: OTP (Email/SMS), Magic Links, WebAuthn/Passkeys
8
+ - **OAuth Providers**: Google, Microsoft, GitHub, Apple with PKCE support
9
+ - **Multi-Runtime**: Node.js, Deno, Cloudflare Workers, Bun
10
+ - **Multi-Tenant**: Built-in tenant management and resolution
11
+ - **Security**: Rate limiting, account lockout, CSRF protection
12
+ - **Session Management**: JWT with key rotation, token blocklist
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ pnpm add @parsrun/auth
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import { createAuth } from '@parsrun/auth';
24
+
25
+ const auth = createAuth({
26
+ secret: process.env.AUTH_SECRET,
27
+ adapter: myDatabaseAdapter,
28
+ providers: {
29
+ otp: {
30
+ email: {
31
+ send: async (to, code) => {
32
+ await sendEmail(to, `Your code is: ${code}`);
33
+ },
34
+ },
35
+ },
36
+ },
37
+ });
38
+
39
+ await auth.initialize();
40
+
41
+ // Request OTP
42
+ await auth.requestOTP({ identifier: 'user@example.com', type: 'email' });
43
+
44
+ // Sign in
45
+ const result = await auth.signIn({
46
+ provider: 'otp',
47
+ identifier: 'user@example.com',
48
+ credential: '123456',
49
+ data: { type: 'email' },
50
+ });
51
+ ```
52
+
53
+ ## API Overview
54
+
55
+ ### Core
56
+
57
+ | Export | Description |
58
+ |--------|-------------|
59
+ | `createAuth(config)` | Create auth instance |
60
+ | `ParsAuthEngine` | Main auth engine class |
61
+
62
+ ### Providers
63
+
64
+ | Export | Description |
65
+ |--------|-------------|
66
+ | `OTPProvider` | Email/SMS OTP authentication |
67
+ | `MagicLinkProvider` | Magic link authentication |
68
+ | `GoogleProvider` | Google OAuth |
69
+ | `MicrosoftProvider` | Microsoft OAuth |
70
+ | `GitHubProvider` | GitHub OAuth |
71
+ | `AppleProvider` | Apple Sign In |
72
+ | `TOTPProvider` | 2FA with authenticator apps |
73
+ | `WebAuthnProvider` | Passkeys/WebAuthn |
74
+
75
+ ### Middleware (Hono)
76
+
77
+ ```typescript
78
+ import {
79
+ createAuthMiddleware,
80
+ requireRole,
81
+ requirePermission,
82
+ requireTenant,
83
+ requireAdmin,
84
+ } from '@parsrun/auth';
85
+
86
+ const authMiddleware = createAuthMiddleware({ auth });
87
+
88
+ app.get('/admin', authMiddleware, requireAdmin(), handler);
89
+ app.get('/users', authMiddleware, requireRole('admin', 'manager'), handler);
90
+ app.get('/data', authMiddleware, requirePermission('data:read'), handler);
91
+ ```
92
+
93
+ ### Session Management
94
+
95
+ | Export | Description |
96
+ |--------|-------------|
97
+ | `JwtManager` | JWT token management with rotation |
98
+ | `SessionBlocklist` | Token revocation |
99
+ | `extractBearerToken()` | Extract token from header |
100
+
101
+ ### Storage Adapters
102
+
103
+ | Export | Description |
104
+ |--------|-------------|
105
+ | `createStorage()` | Auto-detect runtime storage |
106
+ | `MemoryStorage` | In-memory (development) |
107
+ | `RedisStorage` | Redis/Upstash |
108
+ | `CloudflareKVStorage` | Cloudflare KV |
109
+ | `DenoKVStorage` | Deno KV |
110
+
111
+ ### Security
112
+
113
+ | Export | Description |
114
+ |--------|-------------|
115
+ | `RateLimiter` | Request rate limiting |
116
+ | `LockoutManager` | Account lockout |
117
+ | `CsrfManager` | CSRF protection |
118
+ | `AuthorizationGuard` | Role/permission checks |
119
+
120
+ ## Exports
121
+
122
+ ```typescript
123
+ import { ... } from '@parsrun/auth'; // Main exports
124
+ import { ... } from '@parsrun/auth/storage'; // Storage adapters
125
+ import { ... } from '@parsrun/auth/session'; // Session management
126
+ import { ... } from '@parsrun/auth/security'; // Security utilities
127
+ import { ... } from '@parsrun/auth/providers'; // Auth providers
128
+ import { ... } from '@parsrun/auth/adapters'; // Framework adapters
129
+ ```
130
+
131
+ ## License
132
+
133
+ MIT
@@ -0,0 +1,9 @@
1
+ import 'hono/types';
2
+ import 'hono';
3
+ export { Q as AuthVariables, W as HonoAdapterConfig, w as createAuthMiddleware, y as createAuthRoutes, z as createHonoAuth, x as createOptionalAuthMiddleware, K as requireAdmin, N as requireAll, O as requireAny, G as requireAnyPermission, L as requireOwnerOrPermission, F as requirePermission, E as requireRole, H as requireTenant, J as requireTenantAccess } from '../index-DOGcetyD.js';
4
+ import '../authorization-By1Xp8Za.js';
5
+ import '../jwt-manager-CH8H0kmm.js';
6
+ import 'jose';
7
+ import '../types-DSjafxJ4.js';
8
+ import '../base-BKyR8rcE.js';
9
+ import '../index-C3kz9XqE.js';
@@ -0,0 +1,6 @@
1
+ export { createAuthMiddleware, createAuthRoutes, createHonoAuth, createOptionalAuthMiddleware, requireAdmin, requireAll, requireAny, requireAnyPermission, requireOwnerOrPermission, requirePermission, requireRole, requireTenant, requireTenantAccess } from '../chunk-RHNVRCF3.js';
2
+ import '../chunk-NK4TJV2W.js';
3
+ import '../chunk-MOG4Y6I7.js';
4
+ import '../chunk-42MGHABB.js';
5
+ //# sourceMappingURL=hono.js.map
6
+ //# sourceMappingURL=hono.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"hono.js"}
@@ -0,0 +1,9 @@
1
+ export { X as AuthContext, Z as AuthResponse, Q as AuthVariables, Y as CookieOptions, W as HonoAdapterConfig, a1 as RefreshBody, _ as RequestOtpBody, a0 as SignInBody, $ as VerifyOtpBody, B as createAuthCookies, w as createAuthMiddleware, y as createAuthRoutes, z as createHonoAuth, D as createLogoutCookies, x as createOptionalAuthMiddleware, K as requireAdmin, N as requireAll, O as requireAny, G as requireAnyPermission, L as requireOwnerOrPermission, F as requirePermission, E as requireRole, H as requireTenant, J as requireTenantAccess } from '../index-DOGcetyD.js';
2
+ import '../jwt-manager-CH8H0kmm.js';
3
+ import 'jose';
4
+ import 'hono/types';
5
+ import 'hono';
6
+ import '../types-DSjafxJ4.js';
7
+ import '../base-BKyR8rcE.js';
8
+ import '../index-C3kz9XqE.js';
9
+ import '../authorization-By1Xp8Za.js';
@@ -0,0 +1,7 @@
1
+ import '../chunk-7GOBAL4G.js';
2
+ export { createAuthCookies, createAuthMiddleware, createAuthRoutes, createHonoAuth, createLogoutCookies, createOptionalAuthMiddleware, requireAdmin, requireAll, requireAny, requireAnyPermission, requireOwnerOrPermission, requirePermission, requireRole, requireTenant, requireTenantAccess } from '../chunk-RHNVRCF3.js';
3
+ import '../chunk-NK4TJV2W.js';
4
+ import '../chunk-MOG4Y6I7.js';
5
+ import '../chunk-42MGHABB.js';
6
+ //# sourceMappingURL=index.js.map
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Authorization & Access Control
3
+ * Guards for tenant, role, and permission-based access control
4
+ */
5
+ /**
6
+ * Authorization context - usually extracted from JWT
7
+ */
8
+ interface AuthorizationContext {
9
+ userId: string;
10
+ tenantId?: string | null;
11
+ roles?: string[];
12
+ permissions?: string[];
13
+ memberships?: TenantMembershipInfo[];
14
+ }
15
+ /**
16
+ * Tenant membership information
17
+ */
18
+ interface TenantMembershipInfo {
19
+ tenantId: string;
20
+ role: string;
21
+ permissions: string[];
22
+ status: 'active' | 'inactive' | 'pending';
23
+ }
24
+ /**
25
+ * Authorization check result
26
+ */
27
+ interface AuthorizationResult {
28
+ allowed: boolean;
29
+ reason?: string;
30
+ missingPermissions?: string[];
31
+ missingRoles?: string[];
32
+ }
33
+ /**
34
+ * Permission pattern for matching
35
+ * Supports wildcards: 'users:*', '*:read', '*'
36
+ */
37
+ type PermissionPattern = string;
38
+ /**
39
+ * Authorization Guard
40
+ * Checks if a user has required permissions/roles for an action
41
+ */
42
+ declare class AuthorizationGuard {
43
+ private context;
44
+ constructor(context: AuthorizationContext);
45
+ /**
46
+ * Check if user is authenticated
47
+ */
48
+ isAuthenticated(): boolean;
49
+ /**
50
+ * Check if user has a tenant context
51
+ */
52
+ hasTenant(): boolean;
53
+ /**
54
+ * Check if user is member of a specific tenant
55
+ */
56
+ isMemberOf(tenantId: string): boolean;
57
+ /**
58
+ * Check if user has a specific role
59
+ */
60
+ hasRole(role: string): boolean;
61
+ /**
62
+ * Check if user has any of the specified roles
63
+ */
64
+ hasAnyRole(roles: string[]): boolean;
65
+ /**
66
+ * Check if user has all of the specified roles
67
+ */
68
+ hasAllRoles(roles: string[]): boolean;
69
+ /**
70
+ * Check if user has a specific permission
71
+ * Supports wildcards: 'users:*', '*:read', '*'
72
+ */
73
+ hasPermission(permission: PermissionPattern): boolean;
74
+ /**
75
+ * Check if user has any of the specified permissions
76
+ */
77
+ hasAnyPermission(permissions: PermissionPattern[]): boolean;
78
+ /**
79
+ * Check if user has all of the specified permissions
80
+ */
81
+ hasAllPermissions(permissions: PermissionPattern[]): boolean;
82
+ /**
83
+ * Check if user has role in specific tenant
84
+ */
85
+ hasRoleInTenant(tenantId: string, role: string): boolean;
86
+ /**
87
+ * Check if user has permission in specific tenant
88
+ */
89
+ hasPermissionInTenant(tenantId: string, permission: PermissionPattern): boolean;
90
+ /**
91
+ * Comprehensive authorization check
92
+ */
93
+ authorize(requirements: AuthorizationRequirements): AuthorizationResult;
94
+ /**
95
+ * Get current tenant ID
96
+ */
97
+ getTenantId(): string | null;
98
+ /**
99
+ * Get current user ID
100
+ */
101
+ getUserId(): string;
102
+ /**
103
+ * Get all user roles
104
+ */
105
+ getRoles(): string[];
106
+ /**
107
+ * Get all user permissions
108
+ */
109
+ getPermissions(): string[];
110
+ /**
111
+ * Get all tenant memberships
112
+ */
113
+ getMemberships(): TenantMembershipInfo[];
114
+ }
115
+ /**
116
+ * Authorization requirements
117
+ */
118
+ interface AuthorizationRequirements {
119
+ /** User must be authenticated */
120
+ authenticated?: boolean;
121
+ /** User must have tenant context */
122
+ tenant?: boolean;
123
+ /** User must be member of specific tenant */
124
+ memberOf?: string;
125
+ /** Required roles */
126
+ roles?: string[];
127
+ /** Role check mode: 'any' (at least one) or 'all' */
128
+ rolesMode?: 'any' | 'all';
129
+ /** Required permissions */
130
+ permissions?: PermissionPattern[];
131
+ /** Permission check mode: 'any' (at least one) or 'all' */
132
+ permissionsMode?: 'any' | 'all';
133
+ /** Custom authorization function */
134
+ custom?: (context: AuthorizationContext) => boolean;
135
+ /** Custom failure reason */
136
+ customReason?: string;
137
+ }
138
+ /**
139
+ * Create an authorization guard
140
+ */
141
+ declare function createAuthorizationGuard(context: AuthorizationContext): AuthorizationGuard;
142
+ /**
143
+ * Quick authorization checks
144
+ */
145
+ declare const authorize: {
146
+ /**
147
+ * Check if authenticated
148
+ */
149
+ isAuthenticated(context: AuthorizationContext): AuthorizationResult;
150
+ /**
151
+ * Check if has tenant
152
+ */
153
+ hasTenant(context: AuthorizationContext): AuthorizationResult;
154
+ /**
155
+ * Check if has specific role
156
+ */
157
+ hasRole(context: AuthorizationContext, role: string): AuthorizationResult;
158
+ /**
159
+ * Check if has specific permission
160
+ */
161
+ hasPermission(context: AuthorizationContext, permission: string): AuthorizationResult;
162
+ /**
163
+ * Check if is member of tenant
164
+ */
165
+ isMemberOf(context: AuthorizationContext, tenantId: string): AuthorizationResult;
166
+ /**
167
+ * Check if is admin (has 'admin' role or '*' permission)
168
+ */
169
+ isAdmin(context: AuthorizationContext): AuthorizationResult;
170
+ /**
171
+ * Check if is owner of resource
172
+ */
173
+ isOwner(context: AuthorizationContext, ownerId: string): AuthorizationResult;
174
+ /**
175
+ * Check if is owner or has permission
176
+ */
177
+ isOwnerOrHasPermission(context: AuthorizationContext, ownerId: string, permission: string): AuthorizationResult;
178
+ };
179
+ /**
180
+ * Common permission patterns
181
+ */
182
+ declare const Permissions: {
183
+ readonly USERS_READ: "users:read";
184
+ readonly USERS_WRITE: "users:write";
185
+ readonly USERS_DELETE: "users:delete";
186
+ readonly USERS_ADMIN: "users:*";
187
+ readonly TENANTS_READ: "tenants:read";
188
+ readonly TENANTS_WRITE: "tenants:write";
189
+ readonly TENANTS_DELETE: "tenants:delete";
190
+ readonly TENANTS_ADMIN: "tenants:*";
191
+ readonly MEMBERS_READ: "members:read";
192
+ readonly MEMBERS_INVITE: "members:invite";
193
+ readonly MEMBERS_REMOVE: "members:remove";
194
+ readonly MEMBERS_ADMIN: "members:*";
195
+ readonly ROLES_READ: "roles:read";
196
+ readonly ROLES_WRITE: "roles:write";
197
+ readonly ROLES_DELETE: "roles:delete";
198
+ readonly ROLES_ADMIN: "roles:*";
199
+ readonly SUPER_ADMIN: "*";
200
+ };
201
+ /**
202
+ * Common roles
203
+ */
204
+ declare const Roles: {
205
+ readonly OWNER: "owner";
206
+ readonly ADMIN: "admin";
207
+ readonly MANAGER: "manager";
208
+ readonly MEMBER: "member";
209
+ readonly VIEWER: "viewer";
210
+ readonly GUEST: "guest";
211
+ };
212
+
213
+ export { AuthorizationGuard as A, Permissions as P, Roles as R, type TenantMembershipInfo as T, authorize as a, type AuthorizationContext as b, createAuthorizationGuard as c, type AuthorizationResult as d, type AuthorizationRequirements as e, type PermissionPattern as f };