@delmaredigital/payload-better-auth 0.5.6 → 0.6.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.
- package/dist/components/management/ApiKeysManagementClient.d.ts +5 -7
- package/dist/components/management/ApiKeysManagementClient.js +216 -286
- package/dist/components/management/views/ApiKeysView.js +6 -9
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/plugin/index.d.ts +8 -8
- package/dist/plugin/index.js +27 -64
- package/dist/types/apiKey.d.ts +19 -47
- package/dist/types/apiKey.js +10 -5
- package/dist/utils/apiKeyAccess.d.ts +47 -95
- package/dist/utils/apiKeyAccess.js +128 -284
- package/dist/utils/generatePermissions.d.ts +11 -0
- package/dist/utils/generatePermissions.js +30 -0
- package/package.json +1 -1
- package/dist/utils/generateScopes.d.ts +0 -20
- package/dist/utils/generateScopes.js +0 -110
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auto-generate API key scopes from Payload collections.
|
|
3
|
-
*/ /** Default collections to exclude from auto-generated scopes */ const DEFAULT_EXCLUDED_COLLECTIONS = [
|
|
4
|
-
'sessions',
|
|
5
|
-
'verifications',
|
|
6
|
-
'accounts',
|
|
7
|
-
'twoFactors',
|
|
8
|
-
'apiKeys'
|
|
9
|
-
];
|
|
10
|
-
/**
|
|
11
|
-
* Capitalize the first letter of a string.
|
|
12
|
-
*/ function capitalize(str) {
|
|
13
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Convert slug to human-readable label.
|
|
17
|
-
* e.g., 'blog-posts' -> 'Blog Posts'
|
|
18
|
-
*/ function slugToLabel(slug) {
|
|
19
|
-
return slug.split('-').map(capitalize).join(' ');
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Generate scopes from Payload collections.
|
|
23
|
-
* Creates {collection}:read, {collection}:write, {collection}:delete for each collection.
|
|
24
|
-
*/ export function generateScopesFromCollections(collections, excludeCollections = DEFAULT_EXCLUDED_COLLECTIONS) {
|
|
25
|
-
const scopes = {};
|
|
26
|
-
for (const collection of collections){
|
|
27
|
-
if (excludeCollections.includes(collection.slug)) continue;
|
|
28
|
-
const slug = collection.slug;
|
|
29
|
-
const singularLabel = (typeof collection.labels?.singular === 'string' ? collection.labels.singular : null) ?? slugToLabel(slug);
|
|
30
|
-
const pluralLabel = (typeof collection.labels?.plural === 'string' ? collection.labels.plural : null) ?? slugToLabel(slug) + 's';
|
|
31
|
-
scopes[`${slug}:read`] = {
|
|
32
|
-
label: `Read ${pluralLabel}`,
|
|
33
|
-
description: `View ${pluralLabel.toLowerCase()}`,
|
|
34
|
-
permissions: {
|
|
35
|
-
[slug]: [
|
|
36
|
-
'read'
|
|
37
|
-
]
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
scopes[`${slug}:write`] = {
|
|
41
|
-
label: `Write ${pluralLabel}`,
|
|
42
|
-
description: `Create and edit ${pluralLabel.toLowerCase()}`,
|
|
43
|
-
permissions: {
|
|
44
|
-
[slug]: [
|
|
45
|
-
'read',
|
|
46
|
-
'create',
|
|
47
|
-
'update'
|
|
48
|
-
]
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
scopes[`${slug}:delete`] = {
|
|
52
|
-
label: `Delete ${pluralLabel}`,
|
|
53
|
-
description: `Delete ${pluralLabel.toLowerCase()}`,
|
|
54
|
-
permissions: {
|
|
55
|
-
[slug]: [
|
|
56
|
-
'delete'
|
|
57
|
-
]
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return scopes;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Build the final scopes configuration from plugin options and collections.
|
|
65
|
-
* Handles merging custom scopes with auto-generated collection scopes.
|
|
66
|
-
*/ export function buildAvailableScopes(collections, config) {
|
|
67
|
-
const customScopes = config?.scopes ?? {};
|
|
68
|
-
const hasCustomScopes = Object.keys(customScopes).length > 0;
|
|
69
|
-
// Determine if we should include collection scopes
|
|
70
|
-
// Default: true when no custom scopes, false when custom scopes provided
|
|
71
|
-
const includeCollectionScopes = config?.includeCollectionScopes ?? !hasCustomScopes;
|
|
72
|
-
const excludeCollections = config?.excludeCollections ?? DEFAULT_EXCLUDED_COLLECTIONS;
|
|
73
|
-
// Build the combined scopes object
|
|
74
|
-
let allScopes = {};
|
|
75
|
-
// Add collection scopes if enabled
|
|
76
|
-
if (includeCollectionScopes) {
|
|
77
|
-
allScopes = generateScopesFromCollections(collections, excludeCollections);
|
|
78
|
-
}
|
|
79
|
-
// Add custom scopes (they override collection scopes with same ID)
|
|
80
|
-
for (const [id, scope] of Object.entries(customScopes)){
|
|
81
|
-
allScopes[id] = scope;
|
|
82
|
-
}
|
|
83
|
-
// Convert to array format for the client
|
|
84
|
-
return Object.entries(allScopes).map(([id, scope])=>({
|
|
85
|
-
id,
|
|
86
|
-
...scope
|
|
87
|
-
}));
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Convert selected scopes to Better Auth permission format.
|
|
91
|
-
* Used when creating an API key.
|
|
92
|
-
*/ export function scopesToPermissions(selectedScopeIds, availableScopes) {
|
|
93
|
-
const permissions = {};
|
|
94
|
-
for (const scopeId of selectedScopeIds){
|
|
95
|
-
const scope = availableScopes.find((s)=>s.id === scopeId);
|
|
96
|
-
if (!scope) continue;
|
|
97
|
-
for (const [resource, actions] of Object.entries(scope.permissions)){
|
|
98
|
-
if (!permissions[resource]) {
|
|
99
|
-
permissions[resource] = [];
|
|
100
|
-
}
|
|
101
|
-
// Add unique actions
|
|
102
|
-
for (const action of actions){
|
|
103
|
-
if (!permissions[resource].includes(action)) {
|
|
104
|
-
permissions[resource].push(action);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return permissions;
|
|
110
|
-
}
|