@delmaredigital/payload-better-auth 0.5.6 → 0.6.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.
@@ -1,46 +1,26 @@
1
1
  /**
2
- * API Key Scope Enforcement Utilities
2
+ * API Key Permission Enforcement Utilities
3
3
  *
4
- * These utilities help enforce API key scopes in Payload access control.
5
- * They extract the API key from requests, validate scopes, and provide
6
- * type-safe access control functions.
4
+ * Thin wrappers around Better Auth's verifyApiKey() for use in
5
+ * Payload access control. Uses BA's native permission format.
7
6
  *
8
7
  * @example
9
8
  * ```ts
10
- * import { requireScope, requireAnyScope } from '@delmaredigital/payload-better-auth'
9
+ * import { requirePermission, allowSessionOrPermission } from '@delmaredigital/payload-better-auth'
11
10
  *
12
11
  * export const Posts: CollectionConfig = {
13
12
  * slug: 'posts',
14
13
  * access: {
15
- * read: requireAnyScope(['posts:read', 'content:read']),
16
- * create: requireScope('posts:write'),
17
- * update: requireScope('posts:write'),
18
- * delete: requireScope('posts:delete'),
14
+ * read: requirePermission('posts', 'read'),
15
+ * create: requirePermission('posts', 'write'),
16
+ * update: requirePermission('posts', 'write'),
17
+ * delete: requirePermission('posts', 'write'),
19
18
  * },
20
19
  * }
21
20
  * ```
22
21
  */
23
22
  import type { Access, PayloadRequest } from 'payload';
24
- export type ApiKeyInfo = {
25
- /** The API key ID */
26
- id: string;
27
- /** Reference ID (user or organization) who owns this key */
28
- referenceId: string;
29
- /** Reference type - whether key is owned by a user or organization */
30
- referenceType: 'user' | 'organization';
31
- /** Array of granted scope strings */
32
- scopes: string[];
33
- /** The raw key (only first/last chars visible) */
34
- keyPrefix?: string;
35
- /** Optional metadata */
36
- metadata?: Record<string, unknown>;
37
- };
38
- export type ApiKeyAccessConfig = {
39
- /**
40
- * API keys collection slug.
41
- * @default 'apiKeys' or 'api-keys' (auto-detected)
42
- */
43
- apiKeysCollection?: string;
23
+ export type ApiKeyPermissionConfig = {
44
24
  /**
45
25
  * Allow access if user is authenticated (non-API key session).
46
26
  * Useful for allowing both API keys and regular sessions.
@@ -53,121 +33,93 @@ export type ApiKeyAccessConfig = {
53
33
  */
54
34
  extractApiKey?: (req: PayloadRequest) => string | null;
55
35
  };
36
+ /** A single permission check: resource + action */
37
+ export type PermissionCheck = {
38
+ resource: string;
39
+ action: string;
40
+ };
56
41
  /**
57
42
  * Extract API key from request headers.
58
43
  * Supports Bearer token format: Authorization: Bearer <api-key>
59
44
  */
60
45
  export declare function extractApiKeyFromRequest(req: PayloadRequest): string | null;
61
46
  /**
62
- * Look up API key info from the database.
63
- * Returns null if key not found or disabled.
64
- */
65
- export declare function getApiKeyInfo(req: PayloadRequest, apiKey: string, apiKeysCollection?: string): Promise<ApiKeyInfo | null>;
66
- /**
67
- * Check if an API key has a specific scope.
68
- * Supports wildcard patterns like 'posts:*' matching 'posts:read', 'posts:write', etc.
69
- */
70
- export declare function hasScope(keyScopes: string[], requiredScope: string): boolean;
71
- /**
72
- * Check if an API key has any of the specified scopes.
73
- */
74
- export declare function hasAnyScope(keyScopes: string[], requiredScopes: string[]): boolean;
75
- /**
76
- * Check if an API key has all of the specified scopes.
77
- */
78
- export declare function hasAllScopes(keyScopes: string[], requiredScopes: string[]): boolean;
79
- /**
80
- * Create an access control function that requires a specific scope.
47
+ * Require a specific permission on an API key.
81
48
  *
82
- * @param scope - The required scope string (e.g., 'posts:read')
83
- * @param config - Configuration options
49
+ * @param resource - Collection slug (e.g., 'posts')
50
+ * @param action - Permission action: 'read' or 'write'
51
+ * @param config - Optional configuration
84
52
  * @returns Payload access function
85
53
  *
86
54
  * @example
87
55
  * ```ts
88
56
  * access: {
89
- * read: requireScope('posts:read'),
90
- * create: requireScope('posts:write'),
57
+ * read: requirePermission('posts', 'read'),
58
+ * create: requirePermission('posts', 'write'),
91
59
  * }
92
60
  * ```
93
61
  */
94
- export declare function requireScope(scope: string, config?: ApiKeyAccessConfig): Access;
62
+ export declare function requirePermission(resource: string, action: string, config?: ApiKeyPermissionConfig): Access;
95
63
  /**
96
- * Create an access control function that requires any of the specified scopes.
64
+ * Require any one of the specified permissions.
97
65
  *
98
- * @param scopes - Array of acceptable scopes (at least one must match)
99
- * @param config - Configuration options
66
+ * @param permissions - Array of {resource, action} pairs (at least one must match)
67
+ * @param config - Optional configuration
100
68
  * @returns Payload access function
101
69
  *
102
70
  * @example
103
71
  * ```ts
104
72
  * access: {
105
- * read: requireAnyScope(['posts:read', 'content:read', 'admin:*']),
73
+ * read: requireAnyPermission([
74
+ * { resource: 'posts', action: 'read' },
75
+ * { resource: 'pages', action: 'read' },
76
+ * ]),
106
77
  * }
107
78
  * ```
108
79
  */
109
- export declare function requireAnyScope(scopes: string[], config?: ApiKeyAccessConfig): Access;
80
+ export declare function requireAnyPermission(permissions: PermissionCheck[], config?: ApiKeyPermissionConfig): Access;
110
81
  /**
111
- * Create an access control function that requires all specified scopes.
82
+ * Require all of the specified permissions.
112
83
  *
113
- * @param scopes - Array of required scopes (all must be present)
114
- * @param config - Configuration options
84
+ * @param permissions - Array of {resource, action} pairs (all must match)
85
+ * @param config - Optional configuration
115
86
  * @returns Payload access function
116
87
  *
117
88
  * @example
118
89
  * ```ts
119
90
  * access: {
120
- * delete: requireAllScopes(['posts:delete', 'admin:write']),
91
+ * delete: requireAllPermissions([
92
+ * { resource: 'posts', action: 'write' },
93
+ * { resource: 'admin', action: 'write' },
94
+ * ]),
121
95
  * }
122
96
  * ```
123
97
  */
124
- export declare function requireAllScopes(scopes: string[], config?: ApiKeyAccessConfig): Access;
98
+ export declare function requireAllPermissions(permissions: PermissionCheck[], config?: ApiKeyPermissionConfig): Access;
125
99
  /**
126
- * Create an access control function that allows either:
127
- * 1. Authenticated users (via session)
128
- * 2. API key with required scope
129
- *
130
- * This is useful for endpoints that should work with both auth methods.
131
- *
132
- * @param scope - The required scope for API key access
133
- * @param config - Configuration options
134
- * @returns Payload access function
100
+ * Allow either authenticated session OR API key with permission.
135
101
  *
136
102
  * @example
137
103
  * ```ts
138
104
  * access: {
139
- * read: allowSessionOrScope('posts:read'),
105
+ * read: allowSessionOrPermission('posts', 'read'),
140
106
  * }
141
107
  * ```
142
108
  */
143
- export declare function allowSessionOrScope(scope: string, config?: Omit<ApiKeyAccessConfig, 'allowAuthenticatedUsers'>): Access;
109
+ export declare function allowSessionOrPermission(resource: string, action: string, config?: Omit<ApiKeyPermissionConfig, 'allowAuthenticatedUsers'>): Access;
144
110
  /**
145
- * Create an access control function that allows either:
146
- * 1. Authenticated users (via session)
147
- * 2. API key with any of the required scopes
148
- *
149
- * @param scopes - Array of acceptable scopes for API key access
150
- * @param config - Configuration options
151
- * @returns Payload access function
111
+ * Allow either authenticated session OR API key with any of the permissions.
152
112
  */
153
- export declare function allowSessionOrAnyScope(scopes: string[], config?: Omit<ApiKeyAccessConfig, 'allowAuthenticatedUsers'>): Access;
113
+ export declare function allowSessionOrAnyPermission(permissions: PermissionCheck[], config?: Omit<ApiKeyPermissionConfig, 'allowAuthenticatedUsers'>): Access;
154
114
  /**
155
- * Validate an API key and get its info.
156
- *
157
- * This performs a database lookup to validate the key and retrieve
158
- * its associated scopes and user.
159
- *
160
- * @param req - Payload request
161
- * @param apiKeysCollection - The API keys collection slug
162
- * @returns API key info if valid, null otherwise
115
+ * Require a valid API key (no specific permissions checked).
116
+ * Useful for apps that use role-based access and just need to verify the key exists.
163
117
  *
164
118
  * @example
165
119
  * ```ts
166
- * const keyInfo = await validateApiKey(req)
167
- * if (keyInfo) {
168
- * console.log('Valid API key for:', keyInfo.referenceId, keyInfo.referenceType)
169
- * console.log('Scopes:', keyInfo.scopes)
120
+ * access: {
121
+ * read: requireApiKey(),
170
122
  * }
171
123
  * ```
172
124
  */
173
- export declare function validateApiKey(req: PayloadRequest, apiKeysCollection?: string): Promise<ApiKeyInfo | null>;
125
+ export declare function requireApiKey(config?: ApiKeyPermissionConfig): Access;