onResponse-set-newPassword-for-userName-and-security-answer.js revision a51d95a87538f829bbcaa442e43df70b5e30edc9
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl/*! @license
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl *
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * Copyright © 2011-2012 ForgeRock AS. All rights reserved.
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl *
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * The contents of this file are subject to the terms
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * of the Common Development and Distribution License
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * (the License). You may not use this file except in
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * compliance with the License.
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl *
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * You can obtain a copy of the License at
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * http://forgerock.org/license/CDDLv1.0.html
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * See the License for the specific language governing
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * permission and limitations under the License.
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl *
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * When distributing Covered Code, include this CDDL
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * Header Notice in each file and include the License file
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * at http://forgerock.org/license/CDDLv1.0.html
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * If applicable, add the following below the CDDL Header,
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * with the fields enclosed by brackets [] replaced by
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * your own identifying information:
4b3769ce483ece06f60f983193712492b920144fJake Feasel * "Portions Copyrighted [year] [name of copyright owner]"
4b3769ce483ece06f60f983193712492b920144fJake Feasel */
4b3769ce483ece06f60f983193712492b920144fJake Feasel
4b3769ce483ece06f60f983193712492b920144fJake Feasel/**
4b3769ce483ece06f60f983193712492b920144fJake Feasel * @author jdabrowski
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl *
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * This script changes user password. It is run as a response to
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl * set-newPassword-for-userName query.
4b3769ce483ece06f60f983193712492b920144fJake Feasel */
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristluser = openidm.read("managed/user/" + response.result[0]._id);
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias TristlsecurityAnswer = user.securityAnswer;
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias TristlrequestedUserNameMatchesReturnedUserName = (response.result[0].userName == request.params['username']);
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristlif (securityAnswer && requestedUserNameMatchesReturnedUserName) {
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl isRequestedSecurityEqualToReturned = (openidm.decrypt(user.securityAnswer) === request.params['securityAnswer']);
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl if (isRequestedSecurityEqualToReturned) {
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl logger.info("Setting new password for {}", request.params['username']);
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl user.password = request.params['newpassword'];
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl user.securityAnswer = request.params['securityAnswer'];
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl openidm.update("managed/user/" + response.result[0]._id, user._rev, user);
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl response.result = "correct";
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl }
b0663f520f7ec2df9447b5cc08c44a1029642a8dMatthias Tristl}