@tomei/sso 0.40.4 → 0.41.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/package.json
CHANGED
@@ -328,4 +328,104 @@ export class APIKey extends ObjectBase {
|
|
328
328
|
throw error;
|
329
329
|
}
|
330
330
|
}
|
331
|
+
|
332
|
+
public async revoke(
|
333
|
+
apiKey: string,
|
334
|
+
loginUser: LoginUser,
|
335
|
+
dbTransaction: any,
|
336
|
+
reason?: string,
|
337
|
+
) {
|
338
|
+
try {
|
339
|
+
// Part 1: Prepare Required Params
|
340
|
+
// Ensure apiKey, loginUser, and dbTransaction are provided.
|
341
|
+
// Retrieve the existing API key record from the database using the provided apiKey.
|
342
|
+
const apiKeyRecord = await APIKey._Repo.findOne({
|
343
|
+
where: { ApiKey: apiKey },
|
344
|
+
transaction: dbTransaction,
|
345
|
+
});
|
346
|
+
if (!apiKeyRecord) {
|
347
|
+
throw new ClassError(
|
348
|
+
'APIKey',
|
349
|
+
'APIKeyErrMsgO3',
|
350
|
+
'API key not found.',
|
351
|
+
'revoke',
|
352
|
+
);
|
353
|
+
}
|
354
|
+
const EntityValueBefore = {
|
355
|
+
...apiKeyRecord.get({ plain: true }),
|
356
|
+
};
|
357
|
+
|
358
|
+
// Part 2: Revoke API Key
|
359
|
+
// Mark the API key as revoked:
|
360
|
+
// Set the Status to "Revoked".
|
361
|
+
apiKeyRecord.Status = APIKeyStatusEnum.REVOKED;
|
362
|
+
// Set the RevokedAt timestamp to the current date and time.\
|
363
|
+
apiKeyRecord.RevokedAt = new Date();
|
364
|
+
// Set the RevokedById to loginUser.UserId.
|
365
|
+
apiKeyRecord.RevokedById = loginUser.UserId;
|
366
|
+
// Optionally, set the revocation reason:
|
367
|
+
// If the reason parameter is provided, store it in the RevokedReason attribute.
|
368
|
+
if (reason) {
|
369
|
+
apiKeyRecord.RevokedReason = reason;
|
370
|
+
}
|
371
|
+
|
372
|
+
// Part 3: Save API Key to Database
|
373
|
+
// Call APIKey._Repo.update() by passing:
|
374
|
+
// The updated APIKey instance
|
375
|
+
// dbTransaction.
|
376
|
+
await APIKey._Repo.update(
|
377
|
+
{
|
378
|
+
...apiKeyRecord.get({ plain: true }),
|
379
|
+
},
|
380
|
+
{
|
381
|
+
where: { APIKeyId: apiKeyRecord.APIKeyId },
|
382
|
+
transaction: dbTransaction,
|
383
|
+
},
|
384
|
+
);
|
385
|
+
|
386
|
+
// Part 4: Record Update API Key Activity
|
387
|
+
// Initialise EntityValueBefore variable and set to empty object.
|
388
|
+
// Initialise EntityValueAfter variable and set to this APIKey instance.
|
389
|
+
const EntityValueAfter = {
|
390
|
+
...apiKeyRecord.get({ plain: true }),
|
391
|
+
};
|
392
|
+
// Instantiate new activity from Activity class, call createId() method, then set:
|
393
|
+
// Action: ActionEnum.Create
|
394
|
+
// Description: "Revoke API key."
|
395
|
+
// EntityType: "APIKey"
|
396
|
+
// EntityId: <this.APIKeyId>
|
397
|
+
// EntityValueBefore: EntityValueBefore
|
398
|
+
// EntityValueAfter: EntityValueAfter
|
399
|
+
const activity = new Activity();
|
400
|
+
activity.ActivityId = activity.createId();
|
401
|
+
activity.Action = ActionEnum.UPDATE;
|
402
|
+
activity.Description = 'Revoke API key.';
|
403
|
+
activity.EntityType = 'APIKey';
|
404
|
+
activity.EntityId = apiKeyRecord.APIKeyId.toString();
|
405
|
+
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
|
406
|
+
activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
|
407
|
+
// Call new activity create method by passing:
|
408
|
+
// dbTransaction
|
409
|
+
// userId: loginUser.ObjectId
|
410
|
+
await activity.create(loginUser.ObjectId, dbTransaction);
|
411
|
+
|
412
|
+
// Part 5: Returns
|
413
|
+
// Translate the updated APIKey entity into an object and return the following fields:
|
414
|
+
// ApiKey
|
415
|
+
// Status: "Revoked"
|
416
|
+
// RevokedAt
|
417
|
+
// RevokedById
|
418
|
+
// RevokedByName
|
419
|
+
// RevokedReason
|
420
|
+
return {
|
421
|
+
ApiKey: apiKeyRecord.ApiKey,
|
422
|
+
Status: apiKeyRecord.Status,
|
423
|
+
RevokedAt: apiKeyRecord.RevokedAt,
|
424
|
+
RevokedById: apiKeyRecord.RevokedById,
|
425
|
+
RevokedReason: apiKeyRecord.RevokedReason,
|
426
|
+
};
|
427
|
+
} catch (error) {
|
428
|
+
throw error;
|
429
|
+
}
|
430
|
+
}
|
331
431
|
}
|