policy.js revision 3032add8d51a0dcb46e076c4dc6105e78a7c9150
/*! @license
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2012 ForgeRock AS. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*/
//var params;
//var value;
//var fullObject;
var returnObject = {};
var failedPolicies = new Array();
var policyConfig = {
"policies" : [
{ "policyId" : "required",
"policyExec" : "required",
"clientValidation": true,
"policyRequirements" : ["REQUIRED"]
},
{ "policyId" : "not-empty",
"policyExec" : "notEmpty",
"clientValidation": true,
"policyRequirements" : ["REQUIRED"]
},
{ "policyId" : "unique",
"policyExec" : "unique",
"policyRequirements" : ["UNIQUE"]
},
{
"policyId" : "valid-email-address-format",
"policyExec" : "validEmailAddressFormat",
"clientValidation": true,
"policyRequirements": ["VALID_EMAIL_ADDRESS_FORMAT"]
},
{
"policyId" : "valid-name-format",
"policyExec" : "validNameFormat",
"clientValidation": true,
"policyRequirements": ["VALID_NAME_FORMAT"]
},
{
"policyId" : "valid-phone-format",
"policyExec" : "validPhoneFormat",
"clientValidation": true,
"policyRequirements": ["VALID_PHONE_FORMAT"]
},
{ "policyId" : "at-least-X-capitals",
"clientValidation": true,
"policyExec" : "atLeastXCapitalLetters",
"policyRequirements" : ["AT_LEAST_X_CAPITAL_LETTERS"]
},
{ "policyId" : "at-least-X-numbers",
"clientValidation": true,
"policyExec" : "atLeastXNumbers",
"policyRequirements" : ["AT_LEAST_X_NUMBERS"]
},
{ "policyId" : "minimum-length",
"clientValidation": true,
"policyExec" : "propertyMinLength",
"policyRequirements" : ["MIN_LENGTH"]
},
{ "policyId" : "cannot-contain-others",
"clientValidation": true,
"policyExec" : "cannotContainOthers",
"policyRequirements" : ["CANNOT_CONTAIN_OTHERS"]
},
{
"policyId" : "required-if-configured",
"policyExec": "requiredIfConfigured",
"policyRequirements" : ["REQUIRED"]
}
]
};
return [{"policyRequirement": "CANNOT_CONTAIN_OTHERS", params: {"disallowedFields": fieldArray[i]}}];
}
}
return [];
}
return [ {"policyRequirement": "VALID_PHONE_FORMAT"}];
else
return [];
}
var namePattern = /^([A-Za'-\u0105\u0107\u0119\u0142\u00F3\u015B\u017C\u017A\u0104\u0106\u0118\u0141\u00D3\u015A\u017B\u0179\u00C0\u00C8\u00CC\u00D2\u00D9\u00E0\u00E8\u00EC\u00F2\u00F9\u00C1\u00C9\u00CD\u00D3\u00DA\u00DD\u00E1\u00E9\u00ED\u00F3\u00FA\u00FD\u00C2\u00CA\u00CE\u00D4\u00DB\u00E2\u00EA\u00EE\u00F4\u00FB\u00C3\u00D1\u00D5\u00E3\u00F1\u00F5\u00C4\u00CB\u00CF\u00D6\u00DC\u0178\u00E4\u00EB\u00EF\u00F6\u00FC\u0178\u00A1\u00BF\u00E7\u00C7\u0152\u0153\u00DF\u00D8\u00F8\u00C5\u00E5\u00C6\u00E6\u00DE\u00FE\u00D0\u00F0\-\s])+$/;
if (value && value.length && !namePattern.test(value))
return [ {"policyRequirement": "VALID_NAME_FORMAT"}];
else
return [];
}
function validEmailAddressFormat(fullObject, value, params, property) {
var emailPattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if (value && value.length && !emailPattern.test(value))
return [ {"policyRequirement": "VALID_EMAIL_ADDRESS_FORMAT"}];
else
return [];
}
function required(fullObject, value, params, propName) {
if (value === undefined) {
return [ { "policyRequirement" : "REQUIRED" } ];
}
return [];
}
function notEmpty(fullObject, value, params, property) {
if (!value || !value.length)
return [ {"policyRequirement": "REQUIRED"}];
else
return [];
}
function requiredIfConfigured(fullObject, value, params, property) {
var currentValue = openidm.read("config/" + params.configBase),
baseKeyArray = params.baseKey.split(".");
for (var i in baseKeyArray)
currentValue = currentValue[baseKeyArray[i]];
if (currentValue && (!value || !value.length))
return [ {"policyRequirement": "REQUIRED"}];
else
return [];
}
function unique(fullObject, value, params, property) {
var queryParams = {
"_query-id": "get-by-field-value",
"field": property,
"value": value
};
if (value && value.length)
{
var existing = openidm.query(request.id, queryParams);
if (existing.result.length != 0 && (!fullObject["_id"] || existing.result[0]["_id"] != fullObject["_id"])) {
return [{"policyRequirement": "UNIQUE"}];
}
}
return [];
}
function propertyMinLength(fullObject, value, params, property) {
var minLength = params.minLength;
if (typeof value !== "string" || value.length < minLength) {
return [ { "policyRequirement" : "MIN_LENGTH", "params" : {"minLength":minLength} } ];
}
return [];
}
function atLeastXCapitalLetters(fullObject, value, params, property) {
var reg = /[(A-Z)]/g;
if (typeof value !== "string" || !value.length || value.match(reg) === null || value.match(reg).length < params.numCaps) {
return [ { "policyRequirement" : "AT_LEAST_X_CAPITAL_LETTERS", "params" : {"numCaps": params.numCaps} } ];
}
return [];
}
function atLeastXNumbers(fullObject, value, params, property) {
var reg = /\d/g;
if (typeof value !== "string" || !value.length || value.match(reg) === null || value.match(reg).length < params.numNums) {
return [ { "policyRequirement" : "AT_LEAST_X_NUMBERS", "params" : {"numNums": params.numNums} } ];
}
return [];
}
function getPolicy(policyId) {
for (var i = 0; i < policyConfig.policies.length; i++) {
if (policyConfig.policies[i].policyId == policyId) {
return policyConfig.policies[i];
}
}
return null;
}
function getPropertyValue(requestObject, propName) {
var propAddress = propName.split("/");
var tmpObject = requestObject;
for (var i = 0; i < propAddress.length; i++) {
tmpObject = tmpObject[propAddress[i]];
if (tmpObject === undefined || tmpObject === null) {
return tmpObject;
}
}
return tmpObject;
}
function getPropertyConfig(resource, propName) {
var props = resource.properties;
for (var i = 0; i < props.length; i++) {
prop = props[i];
if (prop.name == propName) {
return prop;
}
}
return null;
}
function getResource(resources, resourceName) {
if (resources != null) {
for (var i = 0; i < resources.length; i++) {
var resource = resources[i];
if (resourceMatches(resource.resource, resourceName)) {
return resource;
}
}
}
return null;
}
function resourceMatches(resource1, resource2) {
rsrc1 = resource1.split("/");
rsrc2 = resource2.split("/");
if (rsrc1.length == rsrc2.length) {
for (var i = 0; i < rsrc1.length; i++) {
if (rsrc1[i] != rsrc2[i] &&
rsrc1[i] != "*" &&
rsrc2[i] != "*") {
return false;
}
}
return true;
}
return false;
}
function getResourceWithPolicyRequirements(resource) {
var compProps = resource.properties;
// Loop through the properties for this resource
for (var i = 0; i < compProps.length; i++) {
var propPolicyReqs = new Array();
var reqs;
var prop = compProps[i];
// loop through the policies of each property
for (var j = 0; j < prop.policies.length; j++) {
var policy = getPolicy(prop.policies[j].policyId);
// Check if client validation is enabled, if so add source
if ((policy.clientValidation !== undefined) && policy.clientValidation) {
prop.policies[j].policyFunction = eval(policy.policyExec).toString();
}
prop.policies[j].policyRequirements = policy.policyRequirements;
reqs = policy.policyRequirements;
// loop through the requirements for each policy
for (var x = 0; x < reqs.length; x++) {
}
}
}
// Add the requirements array to the property object
}
// Return all property configs for this resource
return resource;
}
var retObj = {};
var policyRequirements = new Array();
if (policy == null) {
}
}
}
}
}
}
var found = false;
var newPolicy = newPolicies[i];
var policy = returnPolicies[j];
// update old policy with new config
returnPolicies[j] = newPolicy;
found = true;
}
}
if (!found) {
var p = {};
p.params = new Array();
var param = {};
}
// add new policy
}
found = false;
}
return returnPolicies;
}
var found = false;
found = true;
}
}
if (!found) {
}
found = false;
}
}
function getAdditionalPolicies(id) {
var returnArray = new Array();
var resource;
var object;
if (index != -1) {
} else {
}
if (props != null) {
if (policies != null) {
var property = {};
}
}
}
}
}
}
}
}
return returnArray;
}
function processRequest() {
var resource;
// Get the policy configuration for the specified resource
if (resource == null ) {
resource = {};
resource.properties = new Array();
}
// Update the policy configuration with any resource specific
}
if (method == "read") {
var compArray = new Array();
}
returnObject = {};
} else {
}
} else if (method == "action") {
var failedPolicyRequirements = new Array();
returnObject = {};
throw "No resource specified";
}
if (resource == null) {
// There is no configured policies for this resource (nothing to verify)
returnObject.result = true;
} else {
if (fullObject === undefined) {
}
// Perform the validation
if (action == "validateObject") {
// Validate
}
} else if (action == "validateProperty") {
// Validate
}
} else {
throw "Unsupported action: " + action;
}
// Set the result to true if no failedPolicyRequirements (failures), false otherwise
// Set the return failedPolicyRequirements
}
} else {
throw "Unsupported method: " + method;
}
}
//Load additional policy scripts if configured
if (typeof additionalPolicies != 'undefined') {
try {
eval(additionalPolicies[i]);
} catch (error) {
}
}
}