@sap-cloud-sdk/core
Version:
SAP Cloud SDK for JavaScript core
58 lines • 2.25 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isIdenticalTenant = exports.tenantFromJwt = exports.tenantName = exports.tenantId = exports.mappingTenantFields = void 0;
var jwt_1 = require("./jwt");
/**
* Mapping between key name in the Tenant and key name in decoded JWT.
*/
exports.mappingTenantFields = {
id: { keyInJwt: 'zid', extractorFunction: tenantId },
name: { keyInJwt: 'zdn', extractorFunction: tenantName }
};
/**
* Get the tenant id of a decoded JWT.
* @param jwtPayload - Token payload to read the tenant id from.
* @returns The tenant id, if available.
*/
function tenantId(jwtPayload) {
return (0, jwt_1.readPropertyWithWarn)(jwtPayload, exports.mappingTenantFields.id.keyInJwt);
}
exports.tenantId = tenantId;
/**
* Get the tenant name of a decoded JWT.
* @param jwtPayload - Token payload to read the tenant name from.
* @returns The tenant name, if available.
*/
function tenantName(jwtPayload) {
var extAttr = (0, jwt_1.readPropertyWithWarn)(jwtPayload, 'ext_attr');
if (extAttr) {
return (0, jwt_1.readPropertyWithWarn)(extAttr, 'zdn');
}
}
exports.tenantName = tenantName;
/**
* Creates a tenant object from the JWT payload.
* Throws an error if the property `id` is not present in the payload.
* @param jwtPayload - Token payload to get the tenant information from.
* @returns Representation of the tenant.
*/
function tenantFromJwt(jwtPayload) {
(0, jwt_1.checkMandatoryValue)('id', exports.mappingTenantFields, jwtPayload);
return {
id: tenantId(jwtPayload),
name: tenantName(jwtPayload)
};
}
exports.tenantFromJwt = tenantFromJwt;
/**
* Compare two decoded JWTs based on their `tenantId`s.
* @param userTokenPayload - User JWT payload.
* @param providerTokenPayload - Provider JWT payload.
* @returns Whether the tenant is identical.
*/
function isIdenticalTenant(userTokenPayload, providerTokenPayload) {
return ((0, jwt_1.readPropertyWithWarn)(userTokenPayload, exports.mappingTenantFields.id.keyInJwt) ===
(0, jwt_1.readPropertyWithWarn)(providerTokenPayload, exports.mappingTenantFields.id.keyInJwt));
}
exports.isIdenticalTenant = isIdenticalTenant;
//# sourceMappingURL=tenant.js.map
;