onValidate-user-sensitive-fields.js revision 35d8f970e25366e2703f1dacbf8fe3f55b494be1
a4544a5a0e622ef69e38641f87ab1b5685e05911Phill Cunnington/*! @license
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright © 2011-2012 ForgeRock AS. All rights reserved.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * http://forgerock.org/license/CDDLv1.0.html
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at http://forgerock.org/license/CDDLv1.0.html
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @author jdabrowski
c64331fa7c7a38e49ed3b4194ccdffd41af0ff02Peter Major *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This script validates if user is valid.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
6c56bf78246f18c9c14c17ef3ed65065ce178ffdTom Rumseyvar errors = [];
6c56bf78246f18c9c14c17ef3ed65065ce178ffdTom Rumsey
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshottfunction requiredValidator(toValidate, fieldName) {
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott if (!toValidate || toValidate === "") {
bf2a56fd7e5b3bb37378e87e32829a01402d27f0Tom Rumsey errors.push(fieldName + " is required");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott return false;
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott }
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott return true;
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott}
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshottfunction atLeastLengthValidator(toValidate, minLength, fieldName) {
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott if (toValidate.length < minLength) {
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott errors.push(fieldName + " should have at least " + minLength + " characters");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott }
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott}
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Maddenfunction alLeastOneNumberValidator(toValidate, fieldName) {
c64331fa7c7a38e49ed3b4194ccdffd41af0ff02Peter Major var reg = /[(0-9)]+/;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott if (!reg.test(toValidate)) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott errors.push(fieldName + " should have at least one number");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumseyfunction alLeastOneCapitalCharValidator(toValidate, fieldName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster var reg = /[(A-Z)]+/;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!reg.test(toValidate)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster errors.push(fieldName + " should have at least one capital letter");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
1d407e39b7d8f68d9a2b1e178f35fab037d9835aRobert Wapshott}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterfunction requiredOnlyAlfabeticalValidator(toValidate, fieldName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster var exists = requiredValidator(toValidate, fieldName);
c64331fa7c7a38e49ed3b4194ccdffd41af0ff02Peter Major if (!exists) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott return;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott }
26304a2a091af368cfc16c977bcce6d17195360aTom Rumsey
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster var reg = /^([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])+$/;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!reg.test(toValidate)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster errors.push("Only alphabetic characters allowed in " + fieldName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott};
1d407e39b7d8f68d9a2b1e178f35fab037d9835aRobert Wapshott
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumseyfunction numbersAndSpecialCharsValidator(toValidate, fieldName) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey var exists = requiredValidator(toValidate, fieldName);
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey if (!exists) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey return;
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey }
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey var reg = /^\+?([0-9\- \(\)])*$/;
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey if (!reg.test(toValidate)) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey errors.push("Only numbers and special characters allowed in " + fieldName);
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey }
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey}
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumseyfunction requiredEmailValidator(toValidate, fieldName) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey var exists = requiredValidator(toValidate, fieldName);
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey if (!exists) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey return;
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey }
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey if (!reg.test(toValidate)) {
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey errors.push("Not a valid " + fieldName);
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey }
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey}
a19a421277791c670d5a4ebcd6d7af7de159d271Tom Rumsey
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterfunction requiredPasswordValidator(toValidate, fieldName) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott var exists = requiredValidator(toValidate, fieldName);
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott if (!exists) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott return;
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott }
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Madden atLeastLengthValidator(toValidate, 8, fieldName);
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey alLeastOneNumberValidator(toValidate, fieldName);
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey alLeastOneCapitalCharValidator(toValidate, fieldName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshottfunction notEqualValidator(firstVal, secondVal, message) {
7ea769c9edcf1a585b7e3b0b532f790efed64b21David Luna if (firstVal === "" || firstVal === secondVal) {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott errors.push(message);
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott }
786bac66d599daf6355e45e64da84c846a857552Craig McDonnell}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshottfunction isUserValid() {
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott var user = openidm.decrypt(object);
35ab1c5bca11317474fe12bdd8d22c17cdaf2697Robert Wapshott requiredOnlyAlfabeticalValidator(user.givenName, "Given name");
6309b849c2de831a0eaed9c27b5794bed9bd8fd1Neil Madden requiredOnlyAlfabeticalValidator(user.familyName, "Family name");
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey numbersAndSpecialCharsValidator(user.phoneNumber, "Mobile Phone Number");
a90aba9cbcbb8e7fe95e45590d853959efe0d354Tom Rumsey requiredEmailValidator(user.email, "Email");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster requiredEmailValidator(user.userName, "UserName");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster requiredPasswordValidator(user.password, "Password");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott notEqualValidator(user.userName, user.password, "UserName and Password cannot be equal");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott requiredValidator(user.securityQuestion, "Security Question");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott requiredValidator(user.securityAnswer, "Security Answer");
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott if(errors.length > 0) {
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott throw errors;
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott }
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott};
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott//TODO Backend validation defined here supports specific data format required by the default OpenIDM UI. If OpenIDM is used only with default UI this line can be uncommented to ensure proper stored data format.
c6c8bcf74a1e796c167156af1cc1a5d95c67aceaRobert Wapshott//isUserValid();