0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-24 00:17:37 +01:00
mongodb/jstests/libs/multitenancy_utils.js
2024-02-08 21:27:13 +00:00

31 lines
1.2 KiB
JavaScript

/**
* Run the database command using the provided security token on behalf of a tenant.
* @param {object} tenantToken The provided security token which indicates the tenant.
* @param {DB} db The database object.
* @param {JSON} command The command body to be executed.
* @returns Return a response document.
*/
export function runCommandWithSecurityToken(tenantToken, db, command) {
const conn = db.getMongo();
const preToken = conn._securityToken;
try {
conn._setSecurityToken(tenantToken);
return db.runCommand(command);
} finally {
conn._setSecurityToken(preToken);
}
}
/**
* Make an unsigned security token if the tenant id is provided.
* @param {ObjectId} tenantId The tenant id to build an unsigned security token for a tenant.
* @param {object} options The options to indicate the value of expectPrefix field in the security
* token.
* @returns Return an unsigned security token.
*/
export function makeUnsignedSecurityToken(tenantId, options) {
options = options || {expectPrefix: false};
const expectPrefix = !!options.expectPrefix;
return _createTenantToken({tenant: tenantId, expectPrefix: expectPrefix});
}