@makolabs/ripple 1.7.2 → 1.7.4
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.
|
@@ -145,7 +145,7 @@ async function verifyApiKeyToken(apiKey) {
|
|
|
145
145
|
};
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
|
-
async function createUserPermissions(
|
|
148
|
+
async function createUserPermissions(email, permissions, clientId = CLIENT_ID) {
|
|
149
149
|
if (permissions.length === 0) {
|
|
150
150
|
return null;
|
|
151
151
|
}
|
|
@@ -153,7 +153,7 @@ async function createUserPermissions(userId, permissions, clientId = CLIENT_ID)
|
|
|
153
153
|
method: 'POST',
|
|
154
154
|
body: JSON.stringify({
|
|
155
155
|
client_id: clientId,
|
|
156
|
-
sub:
|
|
156
|
+
sub: email,
|
|
157
157
|
scopes: permissions
|
|
158
158
|
})
|
|
159
159
|
});
|
|
@@ -242,7 +242,7 @@ export const createUser = command('unchecked', async (userData) => {
|
|
|
242
242
|
}
|
|
243
243
|
if (permissions && permissions.length > 0) {
|
|
244
244
|
try {
|
|
245
|
-
const adminKeyResult = await createUserPermissions(
|
|
245
|
+
const adminKeyResult = await createUserPermissions(emailAddress, permissions);
|
|
246
246
|
const apiKey = adminKeyResult?.data?.key;
|
|
247
247
|
if (adminKeyResult && apiKey) {
|
|
248
248
|
const updatedUser = await makeClerkRequest(`/users/${result.id}`, {
|
|
@@ -333,9 +333,9 @@ export const deleteUsers = command('unchecked', async (userIds) => {
|
|
|
333
333
|
throw new Error(`Failed to delete users: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
334
334
|
}
|
|
335
335
|
});
|
|
336
|
-
async function fetchUserPermissions(
|
|
336
|
+
async function fetchUserPermissions(email) {
|
|
337
337
|
try {
|
|
338
|
-
const userData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${
|
|
338
|
+
const userData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${email}`);
|
|
339
339
|
if (userData?.data?.data && Array.isArray(userData.data.data)) {
|
|
340
340
|
userData.data.data = userData.data.data.filter((key) => key.status === 'active');
|
|
341
341
|
}
|
|
@@ -355,7 +355,7 @@ async function fetchUserPermissions(userId) {
|
|
|
355
355
|
console.error('[fetchUserPermissions] Error fetching user permissions:', error);
|
|
356
356
|
try {
|
|
357
357
|
const allKeysData = await makeAdminRequest('/admin/keys');
|
|
358
|
-
const userKey = allKeysData.data.data.find((key) => key.sub ===
|
|
358
|
+
const userKey = allKeysData.data.data.find((key) => key.sub === email && key.client_id === CLIENT_ID && key.status === 'active');
|
|
359
359
|
if (userKey) {
|
|
360
360
|
const permissions = Array.isArray(userKey.scopes) ? userKey.scopes : [userKey.scopes];
|
|
361
361
|
return permissions;
|
|
@@ -370,7 +370,13 @@ async function fetchUserPermissions(userId) {
|
|
|
370
370
|
}
|
|
371
371
|
export const getUserPermissions = query('unchecked', async (userId) => {
|
|
372
372
|
try {
|
|
373
|
-
|
|
373
|
+
// Fetch user from Clerk to get email
|
|
374
|
+
const user = await makeClerkRequest(`/users/${userId}`);
|
|
375
|
+
const email = user.email_addresses?.[0]?.email_address;
|
|
376
|
+
if (!email) {
|
|
377
|
+
throw new Error('User has no email address');
|
|
378
|
+
}
|
|
379
|
+
const permissions = await fetchUserPermissions(email);
|
|
374
380
|
// Ensure permissions array is serializable
|
|
375
381
|
return JSON.parse(JSON.stringify(permissions));
|
|
376
382
|
}
|
|
@@ -420,20 +426,25 @@ export const updateUserPermissions = command('unchecked', async (options) => {
|
|
|
420
426
|
if (permissions.length === 0) {
|
|
421
427
|
throw new Error('At least one permission scope is required');
|
|
422
428
|
}
|
|
429
|
+
// Fetch user from Clerk to get email
|
|
430
|
+
const user = await makeClerkRequest(`/users/${userId}`);
|
|
431
|
+
const email = user.email_addresses?.[0]?.email_address;
|
|
432
|
+
if (!email) {
|
|
433
|
+
throw new Error('User has no email address');
|
|
434
|
+
}
|
|
423
435
|
// Fetch user's active keys
|
|
424
|
-
const allKeysData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${
|
|
436
|
+
const allKeysData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${email}`);
|
|
425
437
|
const userKeys = (allKeysData?.data?.data || []).filter((key) => key.status === 'active');
|
|
426
438
|
if (userKeys.length === 0) {
|
|
427
439
|
// No active key exists, create new one
|
|
428
|
-
const newKeyResult = await createUserPermissions(
|
|
440
|
+
const newKeyResult = await createUserPermissions(email, permissions);
|
|
429
441
|
const newApiKey = newKeyResult?.data?.key;
|
|
430
442
|
if (newApiKey) {
|
|
431
|
-
const currentUser = await makeClerkRequest(`/users/${userId}`);
|
|
432
443
|
await makeClerkRequest(`/users/${userId}`, {
|
|
433
444
|
method: 'PATCH',
|
|
434
445
|
body: JSON.stringify({
|
|
435
446
|
private_metadata: {
|
|
436
|
-
...(
|
|
447
|
+
...(user.private_metadata || {}),
|
|
437
448
|
mako_api_key: newApiKey
|
|
438
449
|
}
|
|
439
450
|
})
|
|
@@ -496,19 +507,24 @@ export const generateApiKey = command('unchecked', async (options) => {
|
|
|
496
507
|
if (options.permissions.length === 0) {
|
|
497
508
|
throw new Error('At least one permission scope is required');
|
|
498
509
|
}
|
|
510
|
+
// Fetch user from Clerk to get email
|
|
511
|
+
const user = await makeClerkRequest(`/users/${options.userId}`);
|
|
512
|
+
const email = user.email_addresses?.[0]?.email_address;
|
|
513
|
+
if (!email) {
|
|
514
|
+
throw new Error('User has no email address');
|
|
515
|
+
}
|
|
499
516
|
// Check if user has existing active key
|
|
500
|
-
const allKeysData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${
|
|
517
|
+
const allKeysData = await makeAdminRequest(`/admin/keys?client_id=${CLIENT_ID}&sub=${email}`);
|
|
501
518
|
const userKeys = (allKeysData?.data?.data || []).filter((key) => key.status === 'active');
|
|
502
519
|
let newApiKey;
|
|
503
520
|
let wasRotated = false;
|
|
504
521
|
let oldApiKey;
|
|
505
|
-
let currentUser =
|
|
522
|
+
let currentUser = user;
|
|
506
523
|
let verificationWarning;
|
|
507
524
|
if (userKeys.length > 0 && options.revokeOld) {
|
|
508
525
|
// Use rotate endpoint (per Mako Auth API spec)
|
|
509
526
|
const keyId = userKeys[0].id;
|
|
510
527
|
// Get the old API key from Clerk's private_metadata
|
|
511
|
-
currentUser = await makeClerkRequest(`/users/${options.userId}`);
|
|
512
528
|
oldApiKey = currentUser?.private_metadata?.mako_api_key;
|
|
513
529
|
const rotateResult = await makeAdminRequest(`/admin/keys/${keyId}/rotate`, {
|
|
514
530
|
method: 'POST',
|
|
@@ -559,7 +575,7 @@ export const generateApiKey = command('unchecked', async (options) => {
|
|
|
559
575
|
}
|
|
560
576
|
else {
|
|
561
577
|
// Create new key if none exists or revokeOld is false
|
|
562
|
-
const createData = await createUserPermissions(
|
|
578
|
+
const createData = await createUserPermissions(email, options.permissions);
|
|
563
579
|
if (!createData) {
|
|
564
580
|
throw new Error('Failed to create admin key');
|
|
565
581
|
}
|