@cloud-copilot/iam-simulate
Version:
Simulate evaluation of AWS IAM policies
57 lines • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDate = parseDate;
exports.checkIfDate = checkIfDate;
const util_js_1 = require("../../util.js");
/**
* Parse a string to an epoch date value
*
* @param value the string to parse
* @returns the number or undefined if it cannot be parsed
*/
function parseDate(value) {
// An Integer such as 2024 will be interpreted as a unix epoch date
// A unix epoch date
const epochDate = parseInt(value, 10);
if ((0, util_js_1.isDefined)(epochDate) && !isNaN(epochDate) && epochDate.toString() === value) {
return epochDate;
}
// Date Values can be a date string: https://www.w3.org/TR/NOTE-datetime
const dateNumber = Date.parse(value);
if ((0, util_js_1.isDefined)(dateNumber) && !isNaN(dateNumber)) {
return dateNumber;
}
return undefined;
}
/**
* Test two values to see if they are dates or date epochs, if they are, run the check function
*
* @param policyValue
* @param testValue
* @param check
* @returns
*/
function checkIfDate(policyValue, testValue, check) {
const policyDate = parseDate(policyValue);
const testDate = parseDate(testValue);
if ((0, util_js_1.isNotDefined)(policyDate)) {
return {
value: policyValue,
matches: false,
errors: [`${policyValue} is not a date`]
};
}
if ((0, util_js_1.isNotDefined)(testDate)) {
return {
value: policyValue,
matches: false,
errors: [`request value '${testValue}' is not a date`]
};
}
const matches = check(policyDate, testDate);
return {
value: policyValue,
matches
};
}
//# sourceMappingURL=date.js.map